dolibarr 24.0.0-beta
bom_net_needs.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2019-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
37require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
39require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
40require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
41
42// Load translation files required by the page
43$langs->loadLangs(array("mrp", "other", "stocks"));
44
45// Get parameters
46$id = GETPOSTINT('id');
47$lineid = GETPOSTINT('lineid');
48$ref = GETPOST('ref', 'alpha');
49$action = GETPOST('action', 'aZ09');
50$confirm = GETPOST('confirm', 'alpha');
51$cancel = GETPOST('cancel', 'alpha');
52$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'bomnet_needs'; // To manage different context of search
53$backtopage = GETPOST('backtopage', 'alpha');
54
55
56// Initialize a technical objects
57$object = new BOM($db);
58
59// Initialize a technical objects for hooks
60$hookmanager->initHooks(array('bomnetneeds')); // Note that conf->hooks_modules contains array
61
62// Massaction
63$diroutputmassaction = getMultidirOutput($object) . '/temp/massgeneration/'.$user->id;
64
65// Fetch optionals attributes and labels
66$extrafields->fetch_name_optionals_label($object->table_element);
67$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
68
69// Initialize array of search criteria
70$search_all = GETPOST("search_all", 'alpha');
71$search = array();
72foreach ($object->fields as $key => $val) {
73 if (GETPOST('search_'.$key, 'alpha')) {
74 $search[$key] = GETPOST('search_'.$key, 'alpha');
75 }
76}
77
78if (empty($action) && empty($id) && empty($ref)) {
79 $action = 'view';
80}
81
82// Load object
83include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be 'include', not 'include_once'.
84if ($object->id > 0) {
85 $object->calculateCosts();
86}
87
88
89// Security check - Protection if external user
90//if ($user->socid > 0) accessforbidden();
91//if ($user->socid > 0) $socid = $user->socid;
92$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
93restrictedArea($user, 'bom', $object->id, $object->table_element, '', '', 'rowid', $isdraft);
94
95// Permissions
96$permissionnote = $user->hasRight('bom', 'write'); // Used by the include of actions_setnotes.inc.php
97$permissiondellink = $user->hasRight('bom', 'write'); // Used by the include of actions_dellink.inc.php
98$permissiontoadd = $user->hasRight('bom', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
99$permissiontodelete = $user->hasRight('bom', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
100$upload_dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
101
102
103/*
104 * Actions
105 */
106
107$parameters = array();
108$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
109if ($reshook < 0) {
110 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
111}
112
113if (empty($reshook)) {
114 $error = 0;
115
116 $backurlforlist = DOL_URL_ROOT.'/bom/bom_list.php';
117
118 if (empty($backtopage) || ($cancel && empty($id))) {
119 if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
120 if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
121 $backtopage = $backurlforlist;
122 } else {
123 $backtopage = DOL_URL_ROOT.'/bom/bom_net_needs.php?id='.($id > 0 ? $id : '__ID__');
124 }
125 }
126 }
127}
128
129
130/*
131 * View
132 */
133
134$form = new Form($db);
135$formfile = new FormFile($db);
136
137$title = $langs->trans('BOM');
138$help_url = 'EN:Module_BOM';
139
140llxHeader('', $title, $help_url, '', 0, 0, '', '', '', 'mod-bom page-net_needs');
141
142
143$TChildBom = array();
144if ($action == 'treeview') {
145 $object->getNetNeedsTree($TChildBom, 1);
146} else {
147 $object->getNetNeeds($TChildBom, 1);
148}
149
150
151// Part to show record
152if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
153 $head = bomPrepareHead($object);
154 print dol_get_fiche_head($head, 'net_needs', $langs->trans("BillOfMaterials"), -1, $object->picto);
155
156 $formconfirm = '';
157
158 // Call Hook formConfirm
159 $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
160 $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
161 if (empty($reshook)) {
162 $formconfirm .= $hookmanager->resPrint;
163 } elseif ($reshook > 0) {
164 $formconfirm = $hookmanager->resPrint;
165 }
166
167 // Print form confirm
168 print $formconfirm;
169
170
171 // Object card
172 // ------------------------------------------------------------
173 $linkback = '<a href="'.DOL_URL_ROOT.'/bom/bom_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
174
175 $morehtmlref = '<div class="refidno">';
176
177 $morehtmlref .= '</div>';
178
179
180 dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
181
182
183 print '<div class="fichecenter">';
184 print '<div class="fichehalfleft">';
185 print '<div class="underbanner clearboth"></div>';
186 print '<table class="border centpercent tableforfield">'."\n";
187
188 // Common attributes
189 $keyforbreak = 'duration'; // used into tpl
190 include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
191
192 // Manufacturing cost
193 print '<tr><td>'.$form->textwithpicto($langs->trans("ManufacturingCost"), $langs->trans("BOMTotalCost")).'</td><td><span class="amount">';
194 print price($object->total_cost);
195 print '</span>';
196 if ($object->total_cost != $object->unit_cost) {
197 print '&nbsp; &nbsp; <span class="opacitymedium">('.$form->textwithpicto(price($object->unit_cost), $langs->trans("ManufacturingUnitCost"), 1, 'help', '').')</span>';
198 }
199 print '</td></tr>';
200
201 // Find sell price of generated product. We suppose we sell it to a company like ours (same country...).
202 $res = $object->fetch_product();
203 $manufacturedvalued = '';
204 if ($res && is_object($object->product) && !$object->product->isEmpty()) {
205 global $mysoc;
206 $tmparray = $object->product->getSellPrice($mysoc, $mysoc);
207 $manufacturedvalued = $tmparray['pu_ht'] * $object->qty;
208 }
209 print '<tr><td>'.$langs->trans("ManufacturingGeneratedValue").'</td><td>'.price($manufacturedvalued).'</td></tr>';
210
211 // Other attributes
212 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
213
214 print '</table>';
215 print '</div>';
216 print '</div>';
217
218 print '<div class="clearboth"></div>';
219
220 print dol_get_fiche_end();
221
222 $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' : '')));
223 $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' : '')));
224
225 print load_fiche_titre($langs->trans("BOMNetNeeds"), $viewlink, 'product');
226
227 /*
228 * Lines
229 */
230 $text_stock_options = $langs->trans("RealStockDesc").'<br>';
231 $text_stock_options .= $langs->trans("RealStockWillAutomaticallyWhen").'<br>';
232 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT') || getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE') ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
233 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_VALIDATE_ORDER') ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
234 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_BILL') ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
235 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
236 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER') ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
237 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER') ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
238 $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_RECEPTION') || getDolGlobalString('STOCK_CALCULATE_ON_RECEPTION_CLOSE') ? '- '.$langs->trans("StockOnReception").'<br>' : '');
239
240 print '<table id="tablelines" class="noborder noshadow" width="100%">';
241 print "<thead>\n";
242 print '<tr class="liste_titre nodrag nodrop">';
243 print '<td class="linecoldescription">'.$langs->trans('Product');
244 if (getDolGlobalString('BOM_SUB_BOM') && $action == 'treeview') {
245 print ' &nbsp; <a id="show_all" href="#">'.img_picto('', 'folder-open', 'class="paddingright"').$langs->trans("ExpandAll").'</a>&nbsp;&nbsp;';
246 print '<a id="hide_all" href="#">'.img_picto('', 'folder', 'class="paddingright"').$langs->trans("UndoExpandAll").'</a>&nbsp;';
247 }
248 print '</td>';
249 if ($action == 'treeview') {
250 print '<td class="left">'.$langs->trans('ProducedBy').'</td>';
251 }
252 print '<td class="linecolqty right">'.$langs->trans('Quantity').'</td>';
253 print '<td></td>'; // For unit
254 print '<td class="linecolstock right">'.$form->textwithpicto($langs->trans("PhysicalStock"), $text_stock_options, 1).'</td>';
255 print '<td class="linecoltheoricalstock right">'.$form->textwithpicto($langs->trans("VirtualStock"), $langs->trans("VirtualStockDesc")).'</td>';
256 print '</tr>';
257
258 print '</thead>';
259 print '<tbody>';
260 $tablerows = array();
261 $position = '';
262 $levelposition = '';
263 $lineposition = 0;
264 if (count($TChildBom) > 0) {
265 if ($action == 'treeview') {
266 foreach ($TChildBom as $fk_bom => $TProduct) {
267 $repeatChar = '&emsp;';
268 if (!empty($TProduct['bom'])) {
269 // we make position string in format 'lineposition.bomlevel.childposition'
270 if (!empty($TProduct['position']) && $TProduct['parentid'] == $object->id) {
271 // define lineposition
272 $lineposition = $TProduct['position'];
273 }
274 // define lineposition.bomlevel
275 $position = sprintf('%d.%d', $lineposition, $TProduct['level']);
276 // memorize level position for products in bom
277 $levelposition = $position;
278 if (!empty($TProduct['parentid']) && $TProduct['parentid'] != $object->id && empty($TProduct['product'])) {
279 // define lineposition.bomlevel.childposition
280 $position = sprintf('%s.%d', $position, $TProduct['position']);
281 }
282 $prod = new Product($db);
283 $prod->fetch($TProduct['bom']->fk_product);
284 if ($TProduct['parentid'] != $object->id) {
285 $tablerows[$position] = '<tr class="sub_bom_lines oddeven" parentid="'.$TProduct['parentid'].'">';
286 } else {
287 $tablerows[$position] = '<tr class="oddeven">';
288 }
289 $tablerows[$position] .= '<td class="linecoldescription">'.str_repeat($repeatChar, $TProduct['level']).$prod->getNomUrl(1);
290 $tablerows[$position] .= ' <a class="collapse_bom" id="collapse-'.$fk_bom.'" href="#">';
291 $tablerows[$position] .= img_picto('', 'folder-open');
292 $tablerows[$position] .= '</a>';
293 $tablerows[$position] .= '</td>';
294 $tablerows[$position] .= '<td class="left">'.$TProduct['bom']->getNomUrl(1).'</td>';
295 $tablerows[$position] .= '<td class="linecolqty right">'.price(price2num($TProduct['qty'], 'MS')).'</td>';
296 $tablerows[$position] .= '<td>';
297 $tablerows[$position] .= '</td>';
298 $tablerows[$position] .= '<td class="linecolstock right"></td>';
299 $tablerows[$position] .= '<td class="linecoltheoricalstock right"></td>';
300 $tablerows[$position] .= '</tr>';
301 }
302 if (!empty($TProduct['product'])) {
303 foreach ($TProduct['product'] as $fk_product => $TInfos) {
304 if (!empty($TInfos['position']) && $fk_bom == $object->id) {
305 // top level position
306 $position = $TInfos['position'];
307 } else {
308 // sublevel position
309 $position = sprintf('%s.%d', $levelposition, $TInfos['position']);
310 }
311 $prod = new Product($db);
312 $prod->fetch($fk_product);
313 $prod->load_virtual_stock();
314 if (empty($prod->stock_reel)) {
315 $prod->stock_reel = 0;
316 }
317 if ($fk_bom != $object->id) {
318 $tablerows[$position] = '<tr class="sub_bom_lines oddeven" parentid="'.$fk_bom.'">'; // sub bom on same position
319 } else {
320 $tablerows[$position] = '<tr class="oddeven">';
321 }
322 $tablerows[$position] .= '<td class="linecoldescription">'.str_repeat($repeatChar, (int) $TInfos['level']).$prod->getNomUrl(1).'</td>';
323 $tablerows[$position] .= '<td></td>';
324 $tablerows[$position] .= '<td class="linecolqty right">'.price(price2num((float) $TInfos['qty'], 'MS')).'</td>';
325 $tablerows[$position] .= '<td>';
326 $tablerows[$position] .= '</td>';
327 $tablerows[$position] .= '<td class="linecolstock right">'.price2num($prod->stock_reel, 'MS').'</td>';
328 $tablerows[$position] .= '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
329 $tablerows[$position] .= '</tr>';
330 }
331 }
332 }
333 // Sort the rows numeric by position string
334 ksort($tablerows, SORT_NUMERIC);
335 // Print the rows
336 foreach ($tablerows as $position => $row) {
337 print $row;
338 }
339 } else {
340 foreach ($TChildBom as $fk_product => $elem) {
341 $prod = new Product($db);
342 $prod->fetch($fk_product);
343 $prod->load_virtual_stock();
344 if (empty($prod->stock_reel)) {
345 $prod->stock_reel = 0;
346 }
347 print '<tr class="oddeven">';
348 print '<td class="linecoldescription">'.$prod->getNomUrl(1).'</td>';
349 print '<td class="linecolqty right">'.price(price2num($elem['qty'], 'MS')).'</td>';
350 print '<td>';
351 $useunit = (($prod->type == Product::TYPE_PRODUCT && getDolGlobalInt('PRODUCT_USE_UNITS')) || (($prod->type == Product::TYPE_SERVICE) && ($elem['fk_unit'])));
352 if ($useunit) {
353 require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php';
354 $unit = new CUnits($db);
355 $unit->fetch((int) $elem['fk_unit']);
356 print(isset($unit->label) ? "&nbsp;".$langs->trans(ucwords((string) $unit->label))."&nbsp;" : '');
357 }
358 print '</td>';
359 print '<td class="linecolstock right">'.price(price2num($prod->stock_reel, 'MS')).'</td>';
360 print '<td class="linecoltheoricalstock right">'.$prod->stock_theorique.'</td>';
361 print '</tr>';
362 }
363 }
364 }
365 print '</tbody>';
366 print '</table>';
367
368
369
370 /*
371 * ButAction
372 */
373 print '<div class="tabsAction">'."\n";
374 $parameters = array();
375 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
376 if ($reshook < 0) {
377 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
378 }
379 print '</div>'; ?>
380
381 <script type="text/javascript" language="javascript">
382 $(document).ready(function() {
383
384 function folderManage(element) {
385 var id_bom_line = element.attr('id').replace('collapse-', '');
386 let TSubLines = $('[parentid="'+ id_bom_line +'"]');
387
388 if(element.html().indexOf('folder-open') <= 0) {
389 $('[parentid="'+ id_bom_line +'"]').show();
390 element.html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
391 }
392 else {
393 for (let i = 0; i < TSubLines.length; i++) {
394 let subBomFolder = $(TSubLines[i]).children('.linecoldescription').children('.collapse_bom');
395 if (subBomFolder.length > 0) {
396 folderManage(subBomFolder);
397 }
398 }
399 TSubLines.hide();
400 element.html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
401 }
402 }
403
404 // When clicking on collapse
405 $(".collapse_bom").click(function() {
406 folderManage($(this));
407 return false;
408 });
409
410 // To Show all the sub bom lines
411 $("#show_all").click(function() {
412 console.log("We click on show all");
413 $("[class^=sub_bom_lines]").show();
414 $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder-open')); ?>');
415 return false;
416 });
417
418 // To Hide all the sub bom lines
419 $("#hide_all").click(function() {
420 console.log("We click on hide all");
421 $("[class^=sub_bom_lines]").hide();
422 $("[class^=collapse_bom]").html('<?php echo dol_escape_js(img_picto('', 'folder')); ?>');
423 return false;
424 });
425
426 });
427 </script>
428
429 <?php
430}
431
432// End of page
433llxFooter();
434$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
bomPrepareHead($object)
Prepare array of tabs for BillOfMaterials.
Definition bom.lib.php:90
Class for BOM.
Definition bom.class.php:42
Class of dictionary type of thirdparty (used by imports)
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.