dolibarr 21.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 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 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 foreach ($this->fields as $key => $val) {
217 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
218 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
219 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
220 }
221 }
222 }
223 }
224
232 public function create(User $user, $notrigger = 0)
233 {
234 if ($this->efficiency < 0 || $this->efficiency > 1) {
235 $this->efficiency = 1;
236 }
237
238 return $this->createCommon($user, $notrigger);
239 }
240
248 public function fetch($id, $ref = null)
249 {
250 $result = $this->fetchCommon($id, $ref);
251 //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
252 return $result;
253 }
254
267 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
268 {
269 dol_syslog(__METHOD__, LOG_DEBUG);
270
271 $records = array();
272
273 $sql = 'SELECT ';
274 $sql .= $this->getFieldList();
275 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
276 if ($this->ismultientitymanaged) {
277 $sql .= ' WHERE t.entity IN ('.getEntity($this->element).')';
278 } else {
279 $sql .= ' WHERE 1 = 1';
280 }
281
282 // Manage filter
283 $errormessage = '';
284 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
285 if ($errormessage) {
286 $this->errors[] = $errormessage;
287 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
288 return -1;
289 }
290
291 if (!empty($sortfield)) {
292 $sql .= $this->db->order($sortfield, $sortorder);
293 }
294 if (!empty($limit)) {
295 $sql .= $this->db->plimit($limit, $offset);
296 }
297
298 $resql = $this->db->query($sql);
299 if ($resql) {
300 $num = $this->db->num_rows($resql);
301
302 while ($obj = $this->db->fetch_object($resql)) {
303 $record = new self($this->db);
304 $record->setVarsFromFetchObj($obj);
305 $record->fetch_optionals();
306
307 $records[$record->id] = $record;
308 }
309 $this->db->free($resql);
310
311 return $records;
312 } else {
313 $this->errors[] = 'Error '.$this->db->lasterror();
314 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
315
316 return -1;
317 }
318 }
319
327 public function update(User $user, $notrigger = 0)
328 {
329 if ($this->efficiency < 0 || $this->efficiency > 1) {
330 $this->efficiency = 1;
331 }
332
333 return $this->updateCommon($user, $notrigger);
334 }
335
343 public function delete(User $user, $notrigger = 0)
344 {
345 return $this->deleteCommon($user, $notrigger);
346 //return $this->deleteCommon($user, $notrigger, 1);
347 }
348
359 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
360 {
361 global $db, $conf, $langs, $hookmanager;
362
363 if (!empty($conf->dol_no_mouse_hover)) {
364 $notooltip = 1; // Force disable tooltips
365 }
366
367 $result = '';
368
369 $label = '<u>'.$langs->trans("BillOfMaterialsLine").'</u>';
370 $label .= '<br>';
371 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
372
373 $url = DOL_URL_ROOT.'/bom/bomline_card.php?id='.$this->id;
374
375 if ($option != 'nolink') {
376 // Add param to save lastsearch_values or not
377 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
378 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
379 $add_save_lastsearch_values = 1;
380 }
381 if ($add_save_lastsearch_values) {
382 $url .= '&save_lastsearch_values=1';
383 }
384 }
385
386 $linkclose = '';
387 if (empty($notooltip)) {
388 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
389 $label = $langs->trans("ShowBillOfMaterialsLine");
390 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
391 }
392 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
393 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
394 } else {
395 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
396 }
397
398 $linkstart = '<a href="'.$url.'"';
399 $linkstart .= $linkclose.'>';
400 $linkend = '</a>';
401
402 $result .= $linkstart;
403 if ($withpicto) {
404 $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);
405 }
406 if ($withpicto != 2) {
407 $result .= $this->ref;
408 }
409 $result .= $linkend;
410 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
411
412 global $action, $hookmanager;
413 $hookmanager->initHooks(array('bomlinedao'));
414 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
415 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
416 if ($reshook > 0) {
417 $result = $hookmanager->resPrint;
418 } else {
419 $result .= $hookmanager->resPrint;
420 }
421
422 return $result;
423 }
424
431 public function getLibStatut($mode = 0)
432 {
433 return $this->LibStatut($this->status, $mode);
434 }
435
436 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
444 public function LibStatut($status, $mode = 0)
445 {
446 // phpcs:enable
447 return '';
448 }
449
456 public function info($id)
457 {
458 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
459 $sql .= ' fk_user_creat, fk_user_modif';
460 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
461 $sql .= ' WHERE t.rowid = '.((int) $id);
462 $result = $this->db->query($sql);
463 if ($result) {
464 if ($this->db->num_rows($result)) {
465 $obj = $this->db->fetch_object($result);
466
467 $this->id = $obj->rowid;
468
469 $this->user_creation_id = $obj->fk_user_creat;
470 $this->user_modification_id = $obj->fk_user_modif;
471 $this->date_creation = $this->db->jdate($obj->datec);
472 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
473 }
474 $this->db->free($result);
475 } else {
476 dol_print_error($this->db);
477 }
478 }
479
486 public function initAsSpecimen()
487 {
488 return $this->initAsSpecimenCommon();
489 }
490}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:634
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.