bom_net_needs.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. $diroutputmassaction = $conf->bom->dir_output.'/temp/massgeneration/'.$user->id;
  44. $hookmanager->initHooks(array('bomnetneeds')); // Note that conf->hooks_modules contains array
  45. // Fetch optionals attributes and labels
  46. $extrafields->fetch_name_optionals_label($object->table_element);
  47. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  48. // Initialize array of search criterias
  49. $search_all = GETPOST("search_all", 'alpha');
  50. $search = array();
  51. foreach ($object->fields as $key => $val) {
  52. if (GETPOST('search_'.$key, 'alpha')) {
  53. $search[$key] = GETPOST('search_'.$key, 'alpha');
  54. }
  55. }
  56. if (empty($action) && empty($id) && empty($ref)) {
  57. $action = 'view';
  58. }
  59. // Load object
  60. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  61. if ($object->id > 0) {
  62. $object->calculateCosts();
  63. }
  64. // Security check - Protection if external user
  65. //if ($user->socid > 0) accessforbidden();
  66. //if ($user->socid > 0) $socid = $user->socid;
  67. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  68. $result = restrictedArea($user, 'bom', $object->id, 'bom_bom', '', '', 'rowid', $isdraft);
  69. $permissionnote = $user->rights->bom->write; // Used by the include of actions_setnotes.inc.php
  70. $permissiondellink = $user->rights->bom->write; // Used by the include of actions_dellink.inc.php
  71. $permissiontoadd = $user->rights->bom->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  72. $permissiontodelete = $user->rights->bom->delete || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
  73. $upload_dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
  74. /*
  75. * Actions
  76. */
  77. $parameters = array();
  78. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  79. if ($reshook < 0) {
  80. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  81. }
  82. if (empty($reshook)) {
  83. $error = 0;
  84. $backurlforlist = DOL_URL_ROOT.'/bom/bom_list.php';
  85. if (empty($backtopage) || ($cancel && empty($id))) {
  86. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  87. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  88. $backtopage = $backurlforlist;
  89. } else {
  90. $backtopage = DOL_URL_ROOT.'/bom/bom_net_needs.php?id='.($id > 0 ? $id : '__ID__');
  91. }
  92. }
  93. }
  94. if ($action == 'treeview') $object->getNetNeedsTree($TChildBom, 1);
  95. else $object->getNetNeeds($TChildBom, 1);
  96. }
  97. /*
  98. * View
  99. */
  100. $form = new Form($db);
  101. $formfile = new FormFile($db);
  102. $title = $langs->trans('BOM');
  103. $help_url ='EN:Module_BOM';
  104. llxHeader('', $title, $help_url);
  105. // Part to show record
  106. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  107. $head = bomPrepareHead($object);
  108. print dol_get_fiche_head($head, 'net_needs', $langs->trans("BillOfMaterials"), -1, 'bom');
  109. $formconfirm = '';
  110. // Call Hook formConfirm
  111. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  112. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  113. if (empty($reshook)) {
  114. $formconfirm .= $hookmanager->resPrint;
  115. } elseif ($reshook > 0) {
  116. $formconfirm = $hookmanager->resPrint;
  117. }
  118. // Print form confirm
  119. print $formconfirm;
  120. // Object card
  121. // ------------------------------------------------------------
  122. $linkback = '<a href="'.DOL_URL_ROOT.'/bom/bom_list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  123. $morehtmlref = '<div class="refidno">';
  124. $morehtmlref .= '</div>';
  125. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  126. print '<div class="fichecenter">';
  127. print '<div class="fichehalfleft">';
  128. print '<div class="underbanner clearboth"></div>';
  129. print '<table class="border centpercent tableforfield">'."\n";
  130. // Common attributes
  131. $keyforbreak = 'duration';
  132. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  133. print '<tr><td>'.$form->textwithpicto($langs->trans("TotalCost"), $langs->trans("BOMTotalCost")).'</td><td><span class="amount">'.price($object->total_cost).'</span></td></tr>';
  134. print '<tr><td>'.$langs->trans("UnitCost").'</td><td>'.price($object->unit_cost).'</td></tr>';
  135. // Other attributes
  136. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  137. print '</table>';
  138. print '</div>';
  139. print '</div>';
  140. print '<div class="clearboth"></div>';
  141. print dol_get_fiche_end();
  142. $viewlink = dolGetButtonTitle($langs->trans('GroupByProduct'), '', 'fa fa-bars imgforviewmode', $_SERVER['PHP_SELF'].'?id='.$object->id.'&token='.newToken(), '', 1, array('morecss' => 'reposition '.($action !== 'treeview' ? 'btnTitleSelected':'')));
  143. $viewlink .= dolGetButtonTitle($langs->trans('TreeStructure'), '', 'fa fa-stream imgforviewmode', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=treeview&token='.newToken(), '', 1, array('morecss' => 'reposition marginleftonly '.($action == 'treeview' ? 'btnTitleSelected':'')));
  144. print load_fiche_titre($langs->trans("BOMNetNeeds"), $viewlink, '');
  145. /*
  146. * Lines
  147. */
  148. $text_stock_options = $langs->trans("RealStockDesc").'<br>';
  149. $text_stock_options .= $langs->trans("RealStockWillAutomaticallyWhen").'<br>';
  150. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || ! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
  151. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
  152. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_BILL) ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
  153. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
  154. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
  155. $text_stock_options .= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
  156. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE) ? '- '.$langs->trans("StockOnReception").'<br>' : '');
  157. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  158. print "<thead>\n";
  159. print '<tr class="liste_titre nodrag nodrop">';
  160. print '<td class="linecoldescription">'.$langs->trans('Product');
  161. if (! empty($conf->global->BOM_SUB_BOM) && $action == 'treeview') {
  162. print ' &nbsp; <a id="show_all" href="#">'.img_picto('', 'folder-open', 'class="paddingright"').$langs->trans("ExpandAll").'</a>&nbsp;&nbsp;';
  163. print '<a id="hide_all" href="#">'.img_picto('', 'folder', 'class="paddingright"').$langs->trans("UndoExpandAll").'</a>&nbsp;';
  164. }
  165. print '</td>';
  166. print '<td class="linecolqty right">'.$langs->trans('Quantity').'</td>';
  167. print '<td class="linecolstock right">'.$form->textwithpicto($langs->trans("PhysicalStock"), $text_stock_options, 1).'</td>';
  168. print '<td class="linecoltheoricalstock right">'.$form->textwithpicto($langs->trans("VirtualStock"), $langs->trans("VirtualStockDesc")).'</td>';
  169. print '</tr>';
  170. if (! empty($TChildBom)) {
  171. if ($action == 'treeview') {
  172. foreach ($TChildBom as $fk_bom => $TProduct) {
  173. $repeatChar = '&emsp;';
  174. if (! empty($TProduct['bom'])) {
  175. if ($TProduct['parentid'] != $object->id) print '<tr class="sub_bom_lines oddeven" parentid="'.$TProduct['parentid'].'">';
  176. else print '<tr class="oddeven">';
  177. print '<td class="linecoldescription">'.str_repeat($repeatChar, $TProduct['level']).$TProduct['bom']->getNomUrl(1);
  178. print ' <a class="collapse_bom" id="collapse-'.$fk_bom.'" href="#">';
  179. print img_picto('', 'folder-open');
  180. print '</a>';
  181. print '</td>';
  182. print '<td class="linecolqty right">'.$TProduct['qty'].'</td>';
  183. print '<td class="linecolstock right"></td>';
  184. print '<td class="linecoltheoricalstock right"></td>';
  185. print '</tr>';
  186. }
  187. if (! empty($TProduct['product'])) {
  188. foreach ($TProduct['product'] as $fk_product => $TInfos) {
  189. $prod = new Product($db);
  190. $prod->fetch($fk_product);
  191. $prod->load_virtual_stock();
  192. if (empty($prod->stock_reel)) $prod->stock_reel = 0;
  193. if ($fk_bom != $object->id) print '<tr class="sub_bom_lines oddeven" parentid="'.$fk_bom.'">';
  194. else print '<tr class="oddeven">';
  195. print '<td class="linecoldescription">'.str_repeat($repeatChar, $TInfos['level']).$prod->getNomUrl(1).'</td>';
  196. print '<td class="linecolqty right">'.$TInfos['qty'].'</td>';
  197. print '<td class="linecolstock right">'.price2num($prod->stock_reel, 'MS').'</td>';
  198. print '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
  199. print '</tr>';
  200. }
  201. }
  202. }
  203. } else {
  204. foreach ($TChildBom as $fk_product => $qty) {
  205. $prod = new Product($db);
  206. $prod->fetch($fk_product);
  207. $prod->load_virtual_stock();
  208. if (empty($prod->stock_reel)) $prod->stock_reel = 0;
  209. print '<tr class="oddeven">';
  210. print '<td class="linecoldescription">'.$prod->getNomUrl(1).'</td>';
  211. print '<td class="linecolqty right">'.$qty.'</td>';
  212. print '<td class="linecolstock right">'.price2num($prod->stock_reel, 'MS').'</td>';
  213. print '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
  214. print '</tr>';
  215. }
  216. }
  217. }
  218. print '</thead>';
  219. print '</table>';
  220. /*
  221. * ButAction
  222. */
  223. print '<div class="tabsAction">'."\n";
  224. $parameters = array();
  225. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  226. if ($reshook < 0) {
  227. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  228. }
  229. print '</div>';
  230. ?>
  231. <script type="text/javascript" language="javascript">
  232. $(document).ready(function() {
  233. // When clicking on collapse
  234. $(".collapse_bom").click(function() {
  235. console.log("We click on collapse");
  236. var id_bom_line = $(this).attr('id').replace('collapse-', '');
  237. console.log($(this).html().indexOf('folder-open'));
  238. if($(this).html().indexOf('folder-open') <= 0) {
  239. $('[parentid="'+ id_bom_line +'"]').show();
  240. $(this).html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
  241. }
  242. else {
  243. $('[parentid="'+ id_bom_line +'"]').hide();
  244. $(this).html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
  245. }
  246. return false;
  247. });
  248. // To Show all the sub bom lines
  249. $("#show_all").click(function() {
  250. console.log("We click on show all");
  251. $("[class^=sub_bom_lines]").show();
  252. $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
  253. return false;
  254. });
  255. // To Hide all the sub bom lines
  256. $("#hide_all").click(function() {
  257. console.log("We click on hide all");
  258. $("[class^=sub_bom_lines]").hide();
  259. $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
  260. return false;
  261. });
  262. });
  263. </script>
  264. <?php
  265. }
  266. // End of page
  267. llxFooter();
  268. $db->close();