bom_net_needs.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /* Copyright (C) 2017-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/bom/bom_net_needs.php
  20. * \ingroup bom
  21. * \brief Page to create/edit/view bom
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("mrp", "other", "stocks"));
  31. // Get parameters
  32. $id = GETPOST('id', 'int');
  33. $lineid = GETPOST('lineid', 'int');
  34. $ref = GETPOST('ref', 'alpha');
  35. $action = GETPOST('action', 'aZ09');
  36. $confirm = GETPOST('confirm', 'alpha');
  37. $cancel = GETPOST('cancel', 'aZ09');
  38. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'bomnet_needs'; // To manage different context of search
  39. $backtopage = GETPOST('backtopage', 'alpha');
  40. // Initialize technical objects
  41. $object = new BOM($db);
  42. $extrafields = new ExtraFields($db);
  43. // Initialize technical objects for hooks
  44. $hookmanager->initHooks(array('bomnetneeds')); // Note that conf->hooks_modules contains array
  45. // Massaction
  46. $diroutputmassaction = $conf->bom->dir_output.'/temp/massgeneration/'.$user->id;
  47. // Fetch optionals attributes and labels
  48. $extrafields->fetch_name_optionals_label($object->table_element);
  49. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  50. // Initialize array of search criterias
  51. $search_all = GETPOST("search_all", 'alpha');
  52. $search = array();
  53. foreach ($object->fields as $key => $val) {
  54. if (GETPOST('search_'.$key, 'alpha')) {
  55. $search[$key] = GETPOST('search_'.$key, 'alpha');
  56. }
  57. }
  58. if (empty($action) && empty($id) && empty($ref)) {
  59. $action = 'view';
  60. }
  61. // Load object
  62. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  63. if ($object->id > 0) {
  64. $object->calculateCosts();
  65. }
  66. // Security check - Protection if external user
  67. //if ($user->socid > 0) accessforbidden();
  68. //if ($user->socid > 0) $socid = $user->socid;
  69. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  70. $result = restrictedArea($user, 'bom', $object->id, $object->table_element, '', '', 'rowid', $isdraft);
  71. // Permissions
  72. $permissionnote = $user->hasRight('bom', 'write'); // Used by the include of actions_setnotes.inc.php
  73. $permissiondellink = $user->hasRight('bom', 'write'); // Used by the include of actions_dellink.inc.php
  74. $permissiontoadd = $user->hasRight('bom', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  75. $permissiontodelete = $user->hasRight('bom', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
  76. $upload_dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
  77. /*
  78. * Actions
  79. */
  80. $parameters = array();
  81. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  82. if ($reshook < 0) {
  83. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  84. }
  85. if (empty($reshook)) {
  86. $error = 0;
  87. $backurlforlist = DOL_URL_ROOT.'/bom/bom_list.php';
  88. if (empty($backtopage) || ($cancel && empty($id))) {
  89. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  90. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  91. $backtopage = $backurlforlist;
  92. } else {
  93. $backtopage = DOL_URL_ROOT.'/bom/bom_net_needs.php?id='.($id > 0 ? $id : '__ID__');
  94. }
  95. }
  96. }
  97. if ($action == 'treeview') $object->getNetNeedsTree($TChildBom, 1);
  98. else $object->getNetNeeds($TChildBom, 1);
  99. }
  100. /*
  101. * View
  102. */
  103. $form = new Form($db);
  104. $formfile = new FormFile($db);
  105. $title = $langs->trans('BOM');
  106. $help_url ='EN:Module_BOM';
  107. llxHeader('', $title, $help_url);
  108. // Part to show record
  109. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  110. $head = bomPrepareHead($object);
  111. print dol_get_fiche_head($head, 'net_needs', $langs->trans("BillOfMaterials"), -1, 'bom');
  112. $formconfirm = '';
  113. // Call Hook formConfirm
  114. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  115. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  116. if (empty($reshook)) {
  117. $formconfirm .= $hookmanager->resPrint;
  118. } elseif ($reshook > 0) {
  119. $formconfirm = $hookmanager->resPrint;
  120. }
  121. // Print form confirm
  122. print $formconfirm;
  123. // Object card
  124. // ------------------------------------------------------------
  125. $linkback = '<a href="'.DOL_URL_ROOT.'/bom/bom_list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  126. $morehtmlref = '<div class="refidno">';
  127. $morehtmlref .= '</div>';
  128. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  129. print '<div class="fichecenter">';
  130. print '<div class="fichehalfleft">';
  131. print '<div class="underbanner clearboth"></div>';
  132. print '<table class="border centpercent tableforfield">'."\n";
  133. // Common attributes
  134. $keyforbreak = 'duration';
  135. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  136. print '<tr><td>'.$form->textwithpicto($langs->trans("TotalCost"), $langs->trans("BOMTotalCost")).'</td><td><span class="amount">'.price($object->total_cost).'</span></td></tr>';
  137. print '<tr><td>'.$langs->trans("UnitCost").'</td><td>'.price($object->unit_cost).'</td></tr>';
  138. // Other attributes
  139. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  140. print '</table>';
  141. print '</div>';
  142. print '</div>';
  143. print '<div class="clearboth"></div>';
  144. print dol_get_fiche_end();
  145. $viewlink = dolGetButtonTitle($langs->trans('GroupByX', $langs->transnoentitiesnoconv("Products")), '', 'fa fa-bars imgforviewmode', $_SERVER['PHP_SELF'].'?id='.$object->id.'&token='.newToken(), '', 1, array('morecss' => 'reposition '.($action !== 'treeview' ? 'btnTitleSelected':'')));
  146. $viewlink .= dolGetButtonTitle($langs->trans('TreeView'), '', 'fa fa-stream imgforviewmode', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=treeview&token='.newToken(), '', 1, array('morecss' => 'reposition marginleftonly '.($action == 'treeview' ? 'btnTitleSelected':'')));
  147. print load_fiche_titre($langs->trans("BOMNetNeeds"), $viewlink, 'product');
  148. /*
  149. * Lines
  150. */
  151. $text_stock_options = $langs->trans("RealStockDesc").'<br>';
  152. $text_stock_options .= $langs->trans("RealStockWillAutomaticallyWhen").'<br>';
  153. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
  154. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
  155. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_BILL) ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
  156. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
  157. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
  158. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
  159. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE) ? '- '.$langs->trans("StockOnReception").'<br>' : '');
  160. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  161. print "<thead>\n";
  162. print '<tr class="liste_titre nodrag nodrop">';
  163. print '<td class="linecoldescription">'.$langs->trans('Product');
  164. if (!empty($conf->global->BOM_SUB_BOM) && $action == 'treeview') {
  165. print ' &nbsp; <a id="show_all" href="#">'.img_picto('', 'folder-open', 'class="paddingright"').$langs->trans("ExpandAll").'</a>&nbsp;&nbsp;';
  166. print '<a id="hide_all" href="#">'.img_picto('', 'folder', 'class="paddingright"').$langs->trans("UndoExpandAll").'</a>&nbsp;';
  167. }
  168. print '</td>';
  169. if ($action == 'treeview') print '<td class="left">'.$langs->trans('ProducedBy').'</td>';
  170. print '<td class="linecolqty right">'.$langs->trans('Quantity').'</td>';
  171. print '<td class="linecolstock right">'.$form->textwithpicto($langs->trans("PhysicalStock"), $text_stock_options, 1).'</td>';
  172. print '<td class="linecoltheoricalstock right">'.$form->textwithpicto($langs->trans("VirtualStock"), $langs->trans("VirtualStockDesc")).'</td>';
  173. print '</tr>';
  174. print '</thead>';
  175. print '<tbody>';
  176. if (!empty($TChildBom)) {
  177. if ($action == 'treeview') {
  178. foreach ($TChildBom as $fk_bom => $TProduct) {
  179. $repeatChar = '&emsp;';
  180. if (!empty($TProduct['bom'])) {
  181. $prod = new Product($db);
  182. $prod->fetch($TProduct['bom']->fk_product);
  183. if ($TProduct['parentid'] != $object->id) print '<tr class="sub_bom_lines oddeven" parentid="'.$TProduct['parentid'].'">';
  184. else print '<tr class="oddeven">';
  185. if ($action == 'treeview') print '<td class="linecoldescription">'.str_repeat($repeatChar, $TProduct['level']).$prod->getNomUrl(1);
  186. else print '<td class="linecoldescription">'.str_repeat($repeatChar, $TProduct['level']).$TProduct['bom']->getNomUrl(1);
  187. print ' <a class="collapse_bom" id="collapse-'.$fk_bom.'" href="#">';
  188. print img_picto('', 'folder-open');
  189. print '</a>';
  190. print '</td>';
  191. if ($action == 'treeview') print '<td class="left">'.$TProduct['bom']->getNomUrl(1).'</td>';
  192. print '<td class="linecolqty right">'.$TProduct['qty'].'</td>';
  193. print '<td class="linecolstock right"></td>';
  194. print '<td class="linecoltheoricalstock right"></td>';
  195. print '</tr>';
  196. }
  197. if (!empty($TProduct['product'])) {
  198. foreach ($TProduct['product'] as $fk_product => $TInfos) {
  199. $prod = new Product($db);
  200. $prod->fetch($fk_product);
  201. $prod->load_virtual_stock();
  202. if (empty($prod->stock_reel)) $prod->stock_reel = 0;
  203. if ($fk_bom != $object->id) print '<tr class="sub_bom_lines oddeven" parentid="'.$fk_bom.'">';
  204. else print '<tr class="oddeven">';
  205. print '<td class="linecoldescription">'.str_repeat($repeatChar, $TInfos['level']).$prod->getNomUrl(1).'</td>';
  206. if ($action == 'treeview') print '<td></td>';
  207. print '<td class="linecolqty right">'.$TInfos['qty'].'</td>';
  208. print '<td class="linecolstock right">'.price2num($prod->stock_reel, 'MS').'</td>';
  209. print '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
  210. print '</tr>';
  211. }
  212. }
  213. }
  214. } else {
  215. foreach ($TChildBom as $fk_product => $qty) {
  216. $prod = new Product($db);
  217. $prod->fetch($fk_product);
  218. $prod->load_virtual_stock();
  219. if (empty($prod->stock_reel)) $prod->stock_reel = 0;
  220. print '<tr class="oddeven">';
  221. print '<td class="linecoldescription">'.$prod->getNomUrl(1).'</td>';
  222. print '<td class="linecolqty right">'.$qty.'</td>';
  223. print '<td class="linecolstock right">'.price2num($prod->stock_reel, 'MS').'</td>';
  224. print '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
  225. print '</tr>';
  226. }
  227. }
  228. }
  229. print '</tbody>';
  230. print '</table>';
  231. /*
  232. * ButAction
  233. */
  234. print '<div class="tabsAction">'."\n";
  235. $parameters = array();
  236. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  237. if ($reshook < 0) {
  238. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  239. }
  240. print '</div>';
  241. ?>
  242. <script type="text/javascript" language="javascript">
  243. $(document).ready(function() {
  244. function folderManage(element) {
  245. var id_bom_line = element.attr('id').replace('collapse-', '');
  246. let TSubLines = $('[parentid="'+ id_bom_line +'"]');
  247. if(element.html().indexOf('folder-open') <= 0) {
  248. $('[parentid="'+ id_bom_line +'"]').show();
  249. element.html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
  250. }
  251. else {
  252. for (let i = 0; i < TSubLines.length; i++) {
  253. let subBomFolder = $(TSubLines[i]).children('.linecoldescription').children('.collapse_bom');
  254. if (subBomFolder.length > 0) {
  255. folderManage(subBomFolder);
  256. }
  257. }
  258. TSubLines.hide();
  259. element.html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
  260. }
  261. }
  262. // When clicking on collapse
  263. $(".collapse_bom").click(function() {
  264. folderManage($(this));
  265. return false;
  266. });
  267. // To Show all the sub bom lines
  268. $("#show_all").click(function() {
  269. console.log("We click on show all");
  270. $("[class^=sub_bom_lines]").show();
  271. $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
  272. return false;
  273. });
  274. // To Hide all the sub bom lines
  275. $("#hide_all").click(function() {
  276. console.log("We click on hide all");
  277. $("[class^=sub_bom_lines]").hide();
  278. $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
  279. return false;
  280. });
  281. });
  282. </script>
  283. <?php
  284. }
  285. // End of page
  286. llxFooter();
  287. $db->close();