mo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.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.'/mrp/class/mo.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('mrp', 'products', 'companies', 'productbatch'));
  34. $id = GETPOST('id', 'int');
  35. $ref = GETPOST('ref', 'alpha');
  36. // Security check
  37. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  38. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  39. if ($user->socid) {
  40. $socid = $user->socid;
  41. }
  42. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  43. $hookmanager->initHooks(array('batchproductstatsmo'));
  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 = "c.date_valid";
  60. }
  61. $search_month = GETPOST('search_month', 'int');
  62. $search_year = GETPOST('search_year', 'int');
  63. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  64. $search_month = '';
  65. $search_year = '';
  66. }
  67. if (!$user->hasRight('produit', 'lire')) {
  68. accessforbidden();
  69. }
  70. /*
  71. * View
  72. */
  73. $staticmo = new Mo($db);
  74. $staticmoligne = new MoLine($db);
  75. $form = new Form($db);
  76. $formother = new FormOther($db);
  77. if ($id > 0 || !empty($ref)) {
  78. $object = new Productlot($db);
  79. if ($ref) {
  80. $tmp = explode('_', $ref);
  81. $objectid = $tmp[0];
  82. $batch = $tmp[1];
  83. }
  84. $result = $object->fetch($id, $objectid, $batch);
  85. $parameters = array('id'=>$id);
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  87. if ($reshook < 0) {
  88. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  89. }
  90. $helpurl = '';
  91. $shortlabel = dol_trunc($object->batch, 16);
  92. $title = $langs->trans('Batch')." ".$shortlabel." - ".$langs->trans('Referers');
  93. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  94. llxHeader('', $title, $helpurl);
  95. if ($result > 0) {
  96. $head = productlot_prepare_head($object);
  97. $titre = $langs->trans("CardProduct".$object->type);
  98. $picto = 'lot';
  99. print dol_get_fiche_head($head, 'referers', $langs->trans("Batch"), -1, $object->picto);
  100. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  101. print $hookmanager->resPrint;
  102. if ($reshook < 0) {
  103. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  104. }
  105. $linkback = '<a href="'.DOL_URL_ROOT.'/product/stock/productlot_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  106. $shownav = 1;
  107. if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
  108. $shownav = 0;
  109. }
  110. dol_banner_tab($object, 'id', $linkback, $shownav, 'rowid', 'batch', $morehtmlref);
  111. print '<div class="fichecenter">';
  112. print '<div class="underbanner clearboth"></div>';
  113. print '<table class="border tableforfield" width="100%">';
  114. // Product
  115. print '<tr><td class="titlefield">'.$langs->trans("Product").'</td><td>';
  116. $producttmp = new Product($db);
  117. $producttmp->fetch($object->fk_product);
  118. print $producttmp->getNomUrl(1, 'stock')." - ".$producttmp->label;
  119. print '</td></tr>';
  120. print "</table>";
  121. echo '<br>';
  122. print '<table class="border centpercent tableforfield" width="100%">';
  123. $nboflines = show_stats_for_batch($object, $socid);
  124. print "</table>";
  125. print '</div>';
  126. print '<div class="clearboth"></div>';
  127. print dol_get_fiche_end();
  128. $now = dol_now();
  129. $sql = "SELECT";
  130. // $sql .= " sum(".$db->ifsql("cd.role='toconsume'", "cd.qty", 0).') as nb_toconsume,';
  131. $sql .= " sum(".$db->ifsql("cd.role='consumed'", "cd.qty", 0).') as nb_consumed,';
  132. // $sql .= " sum(".$db->ifsql("cd.role='toproduce'", "cd.qty", 0).') as nb_toproduce,';
  133. $sql .= " sum(".$db->ifsql("cd.role='produced'", "cd.qty", 0).') as nb_produced,';
  134. $sql .= " c.rowid as rowid, c.ref, c.date_valid, c.status";
  135. //$sql .= " s.nom as name, s.rowid as socid, s.code_client";
  136. $sql .= " FROM ".MAIN_DB_PREFIX."mrp_mo as c";
  137. $sql .= ", ".MAIN_DB_PREFIX."mrp_production as cd";
  138. $sql .= " WHERE c.rowid = cd.fk_mo";
  139. $sql .= " AND c.entity IN (".getEntity('mo').")";
  140. $sql .= " AND cd.batch = '".($db->escape($object->batch))."'";
  141. if (!empty($search_month)) {
  142. $sql .= ' AND MONTH(c.date_valid) IN ('.$db->sanitize($search_month).')';
  143. }
  144. if (!empty($search_year)) {
  145. $sql .= ' AND YEAR(c.date_valid) IN ('.$db->sanitize($search_year).')';
  146. }
  147. if ($socid) {
  148. $sql .= " AND s.rowid = ".((int) $socid);
  149. }
  150. $sql .= " GROUP BY c.rowid, c.ref, c.date_valid, c.status";
  151. //$sql .= ", s.nom, s.rowid, s.code_client";
  152. $sql .= $db->order($sortfield, $sortorder);
  153. //Calcul total qty and amount for global if full scan list
  154. $total_ht = 0;
  155. $total_qty = 0;
  156. // Count total nb of records
  157. $totalofrecords = '';
  158. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  159. $result = $db->query($sql);
  160. $totalofrecords = $db->num_rows($result);
  161. }
  162. $sql .= $db->plimit($limit + 1, $offset);
  163. $result = $db->query($sql);
  164. if ($result) {
  165. $num = $db->num_rows($result);
  166. $option = '&id='.$object->id;
  167. if ($limit > 0 && $limit != $conf->liste_limit) {
  168. $option .= '&limit='.((int) $limit);
  169. }
  170. if (!empty($search_month)) {
  171. $option .= '&search_month='.urlencode($search_month);
  172. }
  173. if (!empty($search_year)) {
  174. $option .= '&search_year='.urlencode($search_year);
  175. }
  176. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" name="search_form">'."\n";
  177. print '<input type="hidden" name="token" value="'.newToken().'">';
  178. if (!empty($sortfield)) {
  179. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  180. }
  181. if (!empty($sortorder)) {
  182. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  183. }
  184. print_barre_liste($langs->trans("MOs"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  185. if (!empty($page)) {
  186. $option .= '&page='.urlencode($page);
  187. }
  188. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  189. print '<div class="divsearchfield">';
  190. print $langs->trans('Period').' ('.$langs->trans("DateCreation").') - ';
  191. print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
  192. print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
  193. print '<div style="vertical-align: middle; display: inline-block">';
  194. print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"), 'search.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  195. print '<input type="image" class="liste_titre" name="button_removefilter" src="'.img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
  196. print '</div>';
  197. print '</div>';
  198. print '</div>';
  199. $i = 0;
  200. print '<div class="div-table-responsive">';
  201. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  202. print '<tr class="liste_titre">';
  203. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "c.rowid", "", $option."&amp;id=".$object->id, '', $sortfield, $sortorder);
  204. //print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&amp;id=".$object->id, '', $sortfield, $sortorder);
  205. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "c.date_valid", "", $option."&amp;id=".$object->id, 'align="center"', $sortfield, $sortorder);
  206. //print_liste_field_titre("AmountHT"),$_SERVER["PHP_SELF"],"c.amount","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  207. // print_liste_field_titre("ToConsume", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$object->id, '', $sortfield, $sortorder, 'center ');
  208. print_liste_field_titre("QtyAlreadyConsumed", $_SERVER["PHP_SELF"], "", "", $option."&amp;id=".$object->id, '', $sortfield, $sortorder, 'center ');
  209. // print_liste_field_titre("QtyToProduce", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$object->id, '', $sortfield, $sortorder, 'center ');
  210. print_liste_field_titre("QtyAlreadyProduced", $_SERVER["PHP_SELF"], "", "", $option."&amp;id=".$object->id, '', $sortfield, $sortorder, 'center ');
  211. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "c.status", "", $option."&amp;id=".$object->id, '', $sortfield, $sortorder, 'right ');
  212. print "</tr>\n";
  213. $motmp = new Mo($db);
  214. $total_consumed=$total_produced=0;
  215. if ($num > 0) {
  216. while ($i < min($num, $limit)) {
  217. $objp = $db->fetch_object($result);
  218. $motmp->id = $objp->rowid;
  219. $motmp->ref = $objp->ref;
  220. $motmp->status = $objp->status;
  221. $total_consumed+=$objp->nb_consumed;
  222. $total_produced+=$objp->nb_produced;
  223. print '<tr class="oddeven">';
  224. print '<td>';
  225. print $motmp->getNomUrl(1, 'production');
  226. print "</td>\n";
  227. print "<td align=\"center\">";
  228. print dol_print_date($db->jdate($objp->date_valid), 'dayhour')."</td>";
  229. //print "<td align=\"right\">".price($objp->total_ht)."</td>\n";
  230. //print '<td align="right">';
  231. // print '<td class="center">'.($objp->nb_toconsume > 0 ? $objp->nb_toconsume : '').'</td>';
  232. print '<td class="center">'.($objp->nb_consumed > 0 ? $objp->nb_consumed : '').'</td>';
  233. // print '<td class="center">'.($objp->nb_toproduce > 0 ? $objp->nb_toproduce : '').'</td>';
  234. print '<td class="center">'.($objp->nb_produced > 0 ? $objp->nb_produced : '').'</td>';
  235. //$mostatic->LibStatut($objp->statut,5).'</td>';
  236. print '<td class="right">'.$motmp->getLibStatut(2).'</td>';
  237. print "</tr>\n";
  238. $i++;
  239. }
  240. }
  241. print '<tr class="liste_total">';
  242. if ($num < $limit) {
  243. print '<td class="left">'.$langs->trans("Total").'</td>';
  244. } else {
  245. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  246. }
  247. print '<td></td>';
  248. print '<td class="center">'.$total_consumed.'</td>';
  249. print '<td class="center">'.$total_produced.'</td>';
  250. print '<td></td>';
  251. print "</table>";
  252. print '</div>';
  253. print '</form>';
  254. print '</table>';
  255. print '</div>';
  256. print '</form>';
  257. } else {
  258. dol_print_error($db);
  259. }
  260. $db->free($result);
  261. }
  262. } else {
  263. dol_print_error();
  264. }
  265. // End of page
  266. llxFooter();
  267. $db->close();