bom.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2020 Floiran Henry <florian.henry@scopen.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/product/stats/mo.php
  22. * \ingroup product mo
  23. * \brief Page of MO referring product
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('mrp', 'products', 'companies'));
  32. $id = GETPOST('id', 'int');
  33. $ref = GETPOST('ref', 'alpha');
  34. // Security check
  35. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  36. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  37. if ($user->socid) {
  38. $socid = $user->socid;
  39. }
  40. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  41. $hookmanager->initHooks(array('productstatsbom'));
  42. $mesg = '';
  43. $option = '';
  44. // Load variable for pagination
  45. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  46. $sortfield = GETPOST('sortfield', 'aZ09comma');
  47. $sortorder = GETPOST('sortorder', 'aZ09comma');
  48. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  49. if (empty($page) || $page == -1) {
  50. $page = 0;
  51. } // If $page is not defined, or '' or -1
  52. $offset = $limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. if (!$sortorder) {
  56. $sortorder = "DESC";
  57. }
  58. if (!$sortfield) {
  59. $sortfield = "b.date_valid";
  60. }
  61. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  62. /*
  63. * View
  64. */
  65. $form = new Form($db);
  66. if ($id > 0 || !empty($ref)) {
  67. $product = new Product($db);
  68. $result = $product->fetch($id, $ref);
  69. $object = $product;
  70. $parameters = array('id'=>$id);
  71. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  72. if ($reshook < 0) {
  73. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  74. }
  75. llxHeader("", "", $langs->trans("CardProduct".$product->type));
  76. if ($result > 0) {
  77. $head = product_prepare_head($product);
  78. $titre = $langs->trans("CardProduct".$product->type);
  79. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  80. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  81. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  82. print $hookmanager->resPrint;
  83. if ($reshook < 0) {
  84. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  85. }
  86. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  87. $shownav = 1;
  88. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  89. $shownav = 0;
  90. }
  91. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  92. print '<div class="fichecenter">';
  93. print '<div class="underbanner clearboth"></div>';
  94. print '<table class="border tableforfield" width="100%">';
  95. $nboflines = show_stats_for_company($product, $socid);
  96. print "</table>";
  97. print '</div>';
  98. print '<div style="clear:both"></div>';
  99. print dol_get_fiche_end();
  100. $now = dol_now();
  101. //Calcul total qty and amount for global if full scan list
  102. $total_qty_toconsume = 0;
  103. $total_qty_toproduce = 0;
  104. $product_cache=array();
  105. $bom_data_result = array();
  106. //Qauntity to produce
  107. $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
  108. $sql .= " b.qty as qty_toproduce";
  109. $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
  110. $sql .= " WHERE ";
  111. $sql .= " b.entity IN (".getEntity('bom').")";
  112. $sql .= " AND b.fk_product = ".((int) $product->id);
  113. $sql .= $db->order($sortfield, $sortorder);
  114. // Count total nb of records
  115. $totalofrecords = '';
  116. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  117. $result = $db->query($sql);
  118. if ($result) {
  119. $totalofrecords = $db->num_rows($result);
  120. while ($objp = $db->fetch_object($result)) {
  121. $total_qty_toproduce += $objp->qty_toproduce;
  122. }
  123. } else {
  124. dol_print_error($db);
  125. }
  126. }
  127. $sql .= $db->plimit($limit + 1, $offset);
  128. $result = $db->query($sql);
  129. if ($result) {
  130. $bomtmp = new BOM($db);
  131. $num = $db->num_rows($result);
  132. $i = 0;
  133. if ($num > 0) {
  134. while ($i < min($num, $limit)) {
  135. $objp = $db->fetch_object($result);
  136. $bomtmp->id = $objp->rowid;
  137. $bomtmp->ref = $objp->ref;
  138. $product = new Product($db);
  139. if (!empty($objp->fk_product)) {
  140. if (!array_key_exists($product->id, $product_cache)) {
  141. $resultFetch = $product->fetch($objp->fk_product);
  142. if ($resultFetch < 0) {
  143. setEventMessages($product->error, $product->errors, 'errors');
  144. } else {
  145. $product_cache[$product->id] = $product;
  146. }
  147. }
  148. }
  149. $bomtmp->fk_product = $objp->fk_product;
  150. $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
  151. $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache)? $product_cache[$objp->fk_product]->getNomUrl(1): '');
  152. $bom_data_result[$objp->rowid]['qty_toproduce'] += ($objp->qty_toproduce > 0 ? $objp->qty_toproduce : 0);
  153. $bom_data_result[$objp->rowid]['qty_toconsume'] = 0;
  154. $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
  155. $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
  156. $i++;
  157. }
  158. }
  159. } else {
  160. dol_print_error($db);
  161. }
  162. $db->free($result);
  163. //Qauntity to consume
  164. $sql = "SELECT b.rowid as rowid, b.ref, b.status, b.date_valid, b.fk_product,";
  165. $sql .= " SUM(bl.qty) as qty_toconsume";
  166. $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom as b";
  167. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."bom_bomline as bl ON bl.fk_bom=b.rowid";
  168. $sql .= " WHERE b.entity IN (".getEntity('bom').")";
  169. $sql .= " AND bl.fk_product = ".((int) $product->id);
  170. $sql .= " GROUP BY b.rowid, b.ref, b.status, b.date_valid, b.fk_product";
  171. $sql .= $db->order($sortfield, $sortorder);
  172. // Count total nb of records
  173. $totalofrecords = '';
  174. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  175. $result = $db->query($sql);
  176. if ($result) {
  177. $totalofrecords = $db->num_rows($result);
  178. while ($objp = $db->fetch_object($result)) {
  179. $total_qty_toconsume += $objp->qty_toconsume;
  180. }
  181. } else {
  182. dol_print_error($db);
  183. }
  184. }
  185. $sql .= $db->plimit($limit + 1, $offset);
  186. $result = $db->query($sql);
  187. if ($result) {
  188. $bomtmp = new BOM($db);
  189. $num = $db->num_rows($result);
  190. $i = 0;
  191. if ($num > 0) {
  192. while ($i < min($num, $limit)) {
  193. $objp = $db->fetch_object($result);
  194. $bomtmp->id = $objp->rowid;
  195. $bomtmp->ref = $objp->ref;
  196. $product = new Product($db);
  197. if (!empty($objp->fk_product)) {
  198. if (!array_key_exists($product->id, $product_cache)) {
  199. $resultFetch = $product->fetch($objp->fk_product);
  200. if ($resultFetch < 0) {
  201. setEventMessages($product->error, $product->errors, 'errors');
  202. } else {
  203. $product_cache[$product->id] = $product;
  204. }
  205. }
  206. }
  207. $bomtmp->fk_product = $objp->fk_product;
  208. if (!array_key_exists($objp->rowid, $bom_data_result)) {
  209. $bom_data_result[$objp->rowid]['link'] = $bomtmp->getNomUrl(1, 'production');
  210. $bom_data_result[$objp->rowid]['product'] = (array_key_exists($objp->fk_product, $product_cache)? $product_cache[$objp->fk_product]->getNomUrl(1): '');
  211. $bom_data_result[$objp->rowid]['qty_toproduce'] = 0;
  212. $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
  213. $bom_data_result[$objp->rowid]['date_valid'] = dol_print_date($db->jdate($objp->date_valid), 'dayhour');
  214. $bom_data_result[$objp->rowid]['status'] = $bomtmp->LibStatut($objp->status, 5);
  215. } else {
  216. $bom_data_result[$objp->rowid]['qty_toconsume'] += ($objp->qty_toconsume > 0 ? $objp->qty_toconsume : 0);
  217. }
  218. $i++;
  219. }
  220. }
  221. } else {
  222. dol_print_error($db);
  223. }
  224. $db->free($result);
  225. $option .= '&id='.$product->id;
  226. if ($limit > 0 && $limit != $conf->liste_limit) {
  227. $option .= '&limit='.urlencode($limit);
  228. }
  229. if (!empty($search_month)) {
  230. $option .= '&search_month='.urlencode($search_month);
  231. }
  232. if (!empty($search_year)) {
  233. $option .= '&search_year='.urlencode($search_year);
  234. }
  235. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  236. print '<input type="hidden" name="token" value="'.newToken().'">';
  237. if (!empty($sortfield)) {
  238. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  239. }
  240. if (!empty($sortorder)) {
  241. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  242. }
  243. print_barre_liste($langs->trans("BOMs"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', count($bom_data_result), count($bom_data_result), '', 0, '', '', $limit, 0, 0, 1);
  244. if (!empty($page)) {
  245. $option .= '&page='.urlencode($page);
  246. }
  247. print '<div class="div-table-responsive">';
  248. print '<table class="tagtable liste listwithfilterbefore centpercent">';
  249. print '<tr class="liste_titre">';
  250. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "b.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  251. print_liste_field_titre("Product", $_SERVER["PHP_SELF"], "b.fk_product", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  252. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "b.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
  253. print_liste_field_titre("RowMaterial", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  254. print_liste_field_titre("Finished", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  255. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'right ');
  256. print "</tr>\n";
  257. if (!empty($bom_data_result)) {
  258. foreach ($bom_data_result as $data) {
  259. print '<tr class="oddeven">';
  260. print '<td>';
  261. print $data['link'];
  262. print "</td>\n";
  263. print '<td>';
  264. print $data['product'];
  265. print "</td>\n";
  266. print "<td align=\"center\">";
  267. print $data['date_valid']."</td>";
  268. print '<td class="center">'.$data['qty_toconsume'].'</td>';
  269. print '<td class="center">'.$data['qty_toproduce'].'</td>';
  270. print '<td class="right">'.$data['status'].'</td>';
  271. print "</tr>\n";
  272. }
  273. print '</table>';
  274. print '</div>';
  275. print '</form>';
  276. }
  277. }
  278. } else {
  279. dol_print_error();
  280. }
  281. // End of page
  282. llxFooter();
  283. $db->close();