mo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.'/core/class/html.formother.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('mrp', 'products', 'companies'));
  33. $id = GETPOST('id', 'int');
  34. $ref = GETPOST('ref', 'alpha');
  35. // Security check
  36. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  37. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  38. if ($user->socid) {
  39. $socid = $user->socid;
  40. }
  41. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  42. $hookmanager->initHooks(array('productstatsmo'));
  43. // Load variable for pagination
  44. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  45. $sortfield = GETPOST('sortfield', 'aZ09comma');
  46. $sortorder = GETPOST('sortorder', 'aZ09comma');
  47. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  48. if (empty($page) || $page == -1) {
  49. $page = 0;
  50. } // If $page is not defined, or '' or -1
  51. $offset = $limit * $page;
  52. $pageprev = $page - 1;
  53. $pagenext = $page + 1;
  54. if (!$sortorder) {
  55. $sortorder = "DESC";
  56. }
  57. if (!$sortfield) {
  58. $sortfield = "c.date_valid";
  59. }
  60. $search_month = GETPOST('search_month', 'int');
  61. $search_year = GETPOST('search_year', 'int');
  62. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  63. $search_month = '';
  64. $search_year = '';
  65. }
  66. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  67. /*
  68. * View
  69. */
  70. $staticmo = new Mo($db);
  71. $staticmoligne = new MoLine($db);
  72. $form = new Form($db);
  73. $formother = new FormOther($db);
  74. if ($id > 0 || !empty($ref)) {
  75. $product = new Product($db);
  76. $result = $product->fetch($id, $ref);
  77. $object = $product;
  78. $parameters = array('id'=>$id);
  79. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  80. if ($reshook < 0) {
  81. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  82. }
  83. llxHeader("", "", $langs->trans("CardProduct".$product->type));
  84. if ($result > 0) {
  85. $head = product_prepare_head($product);
  86. $titre = $langs->trans("CardProduct".$product->type);
  87. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  88. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  89. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  90. print $hookmanager->resPrint;
  91. if ($reshook < 0) {
  92. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  93. }
  94. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  95. $shownav = 1;
  96. if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
  97. $shownav = 0;
  98. }
  99. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  100. print '<div class="fichecenter">';
  101. print '<div class="underbanner clearboth"></div>';
  102. print '<table class="border tableforfield" width="100%">';
  103. $nboflines = show_stats_for_company($product, $socid);
  104. print "</table>";
  105. print '</div>';
  106. print '<div class="clearboth"></div>';
  107. print dol_get_fiche_end();
  108. $now = dol_now();
  109. $sql = "SELECT";
  110. $sql .= " sum(".$db->ifsql("cd.role='toconsume'", "cd.qty", 0).') as nb_toconsume,';
  111. $sql .= " sum(".$db->ifsql("cd.role='consumed'", "cd.qty", 0).') as nb_consumed,';
  112. $sql .= " sum(".$db->ifsql("cd.role='toproduce'", "cd.qty", 0).') as nb_toproduce,';
  113. $sql .= " sum(".$db->ifsql("cd.role='produced'", "cd.qty", 0).') as nb_produced,';
  114. $sql .= " c.rowid as rowid, c.ref, c.date_valid, c.status";
  115. //$sql .= " s.nom as name, s.rowid as socid, s.code_client";
  116. $sql .= " FROM ".MAIN_DB_PREFIX."mrp_mo as c";
  117. $sql .= ", ".MAIN_DB_PREFIX."mrp_production as cd";
  118. $sql .= " WHERE c.rowid = cd.fk_mo";
  119. $sql .= " AND c.entity IN (".getEntity('mo').")";
  120. $sql .= " AND cd.fk_product = ".((int) $product->id);
  121. if (!empty($search_month)) {
  122. $sql .= ' AND MONTH(c.date_valid) IN ('.$db->sanitize($search_month).')';
  123. }
  124. if (!empty($search_year)) {
  125. $sql .= ' AND YEAR(c.date_valid) IN ('.$db->sanitize($search_year).')';
  126. }
  127. if ($socid) {
  128. $sql .= " AND s.rowid = ".((int) $socid);
  129. }
  130. $sql .= " GROUP BY c.rowid, c.ref, c.date_valid, c.status";
  131. //$sql .= ", s.nom, s.rowid, s.code_client";
  132. $sql .= $db->order($sortfield, $sortorder);
  133. //Calcul total qty and amount for global if full scan list
  134. $total_ht = 0;
  135. $total_qty = 0;
  136. // Count total nb of records
  137. $totalofrecords = '';
  138. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  139. $result = $db->query($sql);
  140. $totalofrecords = $db->num_rows($result);
  141. }
  142. $sql .= $db->plimit($limit + 1, $offset);
  143. $result = $db->query($sql);
  144. if ($result) {
  145. $num = $db->num_rows($result);
  146. $option = '&id='.$product->id;
  147. if ($limit > 0 && $limit != $conf->liste_limit) {
  148. $option .= '&limit='.((int) $limit);
  149. }
  150. if (!empty($search_month)) {
  151. $option .= '&search_month='.urlencode($search_month);
  152. }
  153. if (!empty($search_year)) {
  154. $option .= '&search_year='.urlencode($search_year);
  155. }
  156. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  157. print '<input type="hidden" name="token" value="'.newToken().'">';
  158. if (!empty($sortfield)) {
  159. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  160. }
  161. if (!empty($sortorder)) {
  162. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  163. }
  164. print_barre_liste($langs->trans("MOs"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  165. if (!empty($page)) {
  166. $option .= '&page='.urlencode($page);
  167. }
  168. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  169. print '<div class="divsearchfield">';
  170. print $langs->trans('Period').' ('.$langs->trans("DateCreation").') - ';
  171. print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
  172. print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
  173. print '<div style="vertical-align: middle; display: inline-block">';
  174. 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")).'">';
  175. 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")).'">';
  176. print '</div>';
  177. print '</div>';
  178. print '</div>';
  179. $i = 0;
  180. print '<div class="div-table-responsive">';
  181. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  182. print '<tr class="liste_titre">';
  183. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "c.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  184. //print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  185. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "c.date_valid", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
  186. //print_liste_field_titre("AmountHT"),$_SERVER["PHP_SELF"],"c.amount","","&amp;id=".$product->id,'align="right"',$sortfield,$sortorder);
  187. print_liste_field_titre("ToConsume", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  188. print_liste_field_titre("QtyAlreadyConsumed", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  189. print_liste_field_titre("QtyToProduce", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  190. print_liste_field_titre("QtyAlreadyProduced", $_SERVER["PHP_SELF"], "", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'center ');
  191. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "b.status", "", "&amp;id=".$product->id, '', $sortfield, $sortorder, 'right ');
  192. print "</tr>\n";
  193. $motmp = new Mo($db);
  194. if ($num > 0) {
  195. while ($i < min($num, $limit)) {
  196. $objp = $db->fetch_object($result);
  197. $motmp->id = $objp->rowid;
  198. $motmp->ref = $objp->ref;
  199. $motmp->status = $objp->status;
  200. print '<tr class="oddeven">';
  201. print '<td>';
  202. print $motmp->getNomUrl(1, 'production');
  203. print "</td>\n";
  204. print "<td align=\"center\">";
  205. print dol_print_date($db->jdate($objp->date_valid), 'dayhour')."</td>";
  206. //print "<td align=\"right\">".price($objp->total_ht)."</td>\n";
  207. //print '<td align="right">';
  208. print '<td class="center">'.($objp->nb_toconsume > 0 ? $objp->nb_toconsume : '').'</td>';
  209. print '<td class="center">'.($objp->nb_consumed > 0 ? $objp->nb_consumed : '').'</td>';
  210. print '<td class="center">'.($objp->nb_toproduce > 0 ? $objp->nb_toproduce : '').'</td>';
  211. print '<td class="center">'.($objp->nb_produced > 0 ? $objp->nb_produced : '').'</td>';
  212. //$mostatic->LibStatut($objp->statut,5).'</td>';
  213. print '<td class="right">'.$motmp->getLibStatut(2).'</td>';
  214. print "</tr>\n";
  215. $i++;
  216. }
  217. }
  218. print '</table>';
  219. print '</div>';
  220. print '</form>';
  221. } else {
  222. dol_print_error($db);
  223. }
  224. $db->free($result);
  225. }
  226. } else {
  227. dol_print_error();
  228. }
  229. // End of page
  230. llxFooter();
  231. $db->close();