dolibarr 25.0.0-alpha
bomline.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2023 Benjamin Falière <benjamin.faliere@altairis.fr>
4 * Copyright (C) 2023 Charlene Benke <charlene@patas-monkey.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
30
35{
39 public $element = 'bomline';
40
44 public $table_element = 'bom_bomline';
45
49 public $parent_element = 'bom';
50
54 public $fk_parent_attribute = 'fk_bom';
55
59 public $picto = 'bomline';
60
61
81 // BEGIN MODULEBUILDER PROPERTIES
85 public $fields = array(
86 'rowid' => array('type' => 'integer', 'label' => 'LineID', 'enabled' => 1, 'visible' => -1, 'position' => 1, 'notnull' => 1, 'index' => 1, 'comment' => "Id",),
87 'fk_bom' => array('type' => 'integer:BillOfMaterials:societe/class/bom.class.php', 'label' => 'BillOfMaterials', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => 1, 'index' => 1,),
88 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php', 'label' => 'Product', 'enabled' => 1, 'visible' => 1, 'position' => 20, 'notnull' => 1, 'index' => 1,),
89 'fk_bom_child' => array('type' => 'integer:BOM:bom/class/bom.class.php', 'label' => 'BillOfMaterials', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'notnull' => -1,),
90 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'notnull' => -1,),
91 'qty' => array('type' => 'double(24,8)', 'label' => 'Quantity', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'notnull' => 1, 'isameasure' => 1,),
92 'qty_frozen' => array('type' => 'smallint', 'label' => 'QuantityFrozen', 'enabled' => 1, 'visible' => 1, 'default' => '0', 'position' => 105, 'css' => 'maxwidth50imp', 'help' => 'QuantityConsumedInvariable'),
93 'disable_stock_change' => array('type' => 'smallint', 'label' => 'DisableStockChange', 'enabled' => 1, 'visible' => 1, 'default' => '0', 'position' => 108, 'css' => 'maxwidth50imp', 'help' => 'DisableStockChangeHelp'),
94 'efficiency' => array('type' => 'double(24,8)', 'label' => 'ManufacturingEfficiency', 'enabled' => 1, 'visible' => 0, 'default' => '1', 'position' => 110, 'notnull' => 1, 'css' => 'maxwidth50imp', 'help' => 'ValueOfEfficiencyConsumedMeans'),
95 'fk_unit' => array('type' => 'integer', 'label' => 'Unit', 'enabled' => 1, 'visible' => 1, 'position' => 120, 'notnull' => -1,),
96 'position' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'default' => '0', 'position' => 200, 'notnull' => 1,),
97 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 1000, 'notnull' => -1,),
98 'fk_default_workstation' => array('type' => 'integer', 'label' => 'DefaultWorkstation', 'enabled' => 1, 'visible' => 1, 'notnull' => 0, 'position' => 1050)
99 );
100
104 public $rowid;
105
109 public $fk_bom;
110
114 public $fk_product;
115
119 public $fk_bom_child;
120
124 public $description;
125
129 public $qty;
130
134 public $qty_frozen;
135
139 public $disable_stock_change;
140
144 public $efficiency;
145
151 public $fk_unit;
152
156 public $fk_default_workstation;
157
161 public $position;
162
166 public $import_key;
167 // END MODULEBUILDER PROPERTIES
168
172 public $total_cost = 0;
173
177 public $unit_cost = 0;
178
182 public $childBom = array();
183
184
185
191 public function __construct(DoliDB $db)
192 {
193 global $langs;
194
195 $this->db = $db;
196
197 $this->ismultientitymanaged = 0;
198
199 $this->isextrafieldmanaged = 1;
200
201 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
202 $this->fields['rowid']['visible'] = 0;
203 }
204 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
205 $this->fields['entity']['enabled'] = 0;
206 }
207
208 // Unset fields that are disabled
209 foreach ($this->fields as $key => $val) {
210 if (isset($val['enabled']) && empty($val['enabled'])) {
211 unset($this->fields[$key]);
212 }
213 }
214
215 // Translate some data of arrayofkeyval
216 if (is_object($langs)) {
217 foreach ($this->fields as $key => $val) {
218 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
219 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
220 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
221 }
222 }
223 }
224 }
225 }
226
234 public function create(User $user, $notrigger = 0)
235 {
236 if ($this->efficiency < 0 || $this->efficiency > 1) {
237 $this->efficiency = 1;
238 }
239
240 return $this->createCommon($user, $notrigger);
241 }
242
250 public function fetch($id, $ref = null)
251 {
252 $result = $this->fetchCommon($id, $ref);
253 //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
254 return $result;
255 }
256
269 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
270 {
271 dol_syslog(__METHOD__, LOG_DEBUG);
272
273 $records = array();
274
275 $sql = 'SELECT ';
276 $sql .= $this->getFieldList();
277 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
278 if ($this->ismultientitymanaged) {
279 $sql .= ' WHERE t.entity IN ('.getEntity($this->element).')';
280 } else {
281 $sql .= ' WHERE 1 = 1';
282 }
283
284 // Manage filter
285 $errormessage = '';
286 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
287 if ($errormessage) {
288 $this->errors[] = $errormessage;
289 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
290 return -1;
291 }
292
293 if (!empty($sortfield)) {
294 $sql .= $this->db->order($sortfield, $sortorder);
295 }
296 if (!empty($limit)) {
297 $sql .= $this->db->plimit($limit, $offset);
298 }
299
300 $resql = $this->db->query($sql);
301 if ($resql) {
302 $num = $this->db->num_rows($resql);
303
304 while ($obj = $this->db->fetch_object($resql)) {
305 $record = new self($this->db);
306 $record->setVarsFromFetchObj($obj);
307 $record->fetch_optionals();
308
309 $records[$record->id] = $record;
310 }
311 $this->db->free($resql);
312
313 return $records;
314 } else {
315 $this->errors[] = 'Error '.$this->db->lasterror();
316 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
317
318 return -1;
319 }
320 }
321
329 public function update(User $user, $notrigger = 0)
330 {
331 if ($this->efficiency < 0 || $this->efficiency > 1) {
332 $this->efficiency = 1;
333 }
334
335 return $this->updateCommon($user, $notrigger);
336 }
337
345 public function delete(User $user, $notrigger = 0)
346 {
347 return $this->deleteCommon($user, $notrigger);
348 //return $this->deleteCommon($user, $notrigger, 1);
349 }
350
361 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
362 {
363 global $db, $conf, $langs, $hookmanager;
364
365 if (!empty($conf->dol_no_mouse_hover)) {
366 $notooltip = 1; // Force disable tooltips
367 }
368
369 $result = '';
370
371 $label = '<u>'.$langs->trans("BillOfMaterialsLine").'</u>';
372 $label .= '<br>';
373 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
374
375 $url = DOL_URL_ROOT.'/bom/bomline_card.php?id='.$this->id;
376
377 if ($option != 'nolink') {
378 // Add param to save lastsearch_values or not
379 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
380 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
381 $add_save_lastsearch_values = 1;
382 }
383 if ($add_save_lastsearch_values) {
384 $url .= '&save_lastsearch_values=1';
385 }
386 }
387
388 $linkclose = '';
389 if (empty($notooltip)) {
390 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
391 $label = $langs->trans("ShowBillOfMaterialsLine");
392 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
393 }
394 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
395 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
396 } else {
397 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
398 }
399
400 $linkstart = '<a href="'.$url.'"';
401 $linkstart .= $linkclose.'>';
402 $linkend = '</a>';
403
404 $result .= $linkstart;
405 if ($withpicto) {
406 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
407 }
408 if ($withpicto != 2) {
409 $result .= $this->ref;
410 }
411 $result .= $linkend;
412 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
413
414 global $action, $hookmanager;
415 $hookmanager->initHooks(array('bomlinedao'));
416 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
417 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
418 if ($reshook > 0) {
419 $result = $hookmanager->resPrint;
420 } else {
421 $result .= $hookmanager->resPrint;
422 }
423
424 return $result;
425 }
426
433 public function getLibStatut($mode = 0)
434 {
435 return $this->LibStatut($this->status, $mode);
436 }
437
438 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
446 public function LibStatut($status, $mode = 0)
447 {
448 // phpcs:enable
449 return '';
450 }
451
458 public function info($id)
459 {
460 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
461 $sql .= ' fk_user_creat, fk_user_modif';
462 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
463 $sql .= ' WHERE t.rowid = '.((int) $id);
464 $result = $this->db->query($sql);
465 if ($result) {
466 if ($this->db->num_rows($result)) {
467 $obj = $this->db->fetch_object($result);
468
469 $this->id = $obj->rowid;
470
471 $this->user_creation_id = $obj->fk_user_creat;
472 $this->user_modification_id = $obj->fk_user_modif;
473 $this->date_creation = $this->db->jdate($obj->datec);
474 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
475 }
476 $this->db->free($result);
477 } else {
478 dol_print_error($this->db);
479 }
480 }
481
488 public function initAsSpecimen()
489 {
490 return $this->initAsSpecimenCommon();
491 }
492}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class for BOMLine.
fetch($id, $ref=null)
Load object in memory from the database.
getLibStatut($mode=0)
Return label of the status.
__construct(DoliDB $db)
Constructor.
update(User $user, $notrigger=0)
Update object into database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
info($id)
Load the info information in the object.
LibStatut($status, $mode=0)
Return the status.
create(User $user, $notrigger=0)
Create object into database.
createCommon(User $user, $notrigger=0)
Create object in the database.
getFieldList($alias='', $excludefields=array())
Function to concat keys of fields.
updateCommon(User $user, $notrigger=0)
Update object into database.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.