29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
31require_once DOL_DOCUMENT_ROOT.
'/bom/class/bomline.class.php';
33if (isModEnabled(
'workstation')) {
34 require_once DOL_DOCUMENT_ROOT.
'/workstation/class/workstation.class.php';
46 public $module =
'bom';
51 public $element =
'bom';
56 public $table_element =
'bom_bom';
61 public $picto =
'bom';
68 const STATUS_DRAFT = 0;
69 const STATUS_VALIDATED = 1;
70 const STATUS_CANCELED = 9;
103 public $fields = array(
104 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -2,
'position' => 1,
'notnull' => 1,
'index' => 1,
'comment' =>
"Id",),
105 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'enabled' => 1,
'visible' => 0,
'notnull' => 1,
'default' =>
'1',
'index' => 1,
'position' => 5),
106 'ref' => array(
'type' =>
'varchar(128)',
'label' =>
'Ref',
'enabled' => 1,
'noteditable' => 1,
'visible' => 4,
'position' => 10,
'notnull' => 1,
'default' =>
'(PROV)',
'index' => 1,
'searchall' => 1,
'comment' =>
"Reference of BOM",
'showoncombobox' => 1,
'csslist' =>
'nowraponall'),
107 'label' => array(
'type' =>
'varchar(255)',
'label' =>
'Label',
'enabled' => 1,
'visible' => 1,
'position' => 30,
'notnull' => 1,
'searchall' => 1,
'showoncombobox' => 2,
'autofocusoncreate' => 1,
'css' =>
'minwidth300 maxwidth400',
'csslist' =>
'tdoverflowmax200'),
108 'bomtype' => array(
'type' =>
'integer',
'label' =>
'Type',
'enabled' => 1,
'visible' => 1,
'position' => 33,
'notnull' => 1,
'default' =>
'0',
'arrayofkeyval' => array(0 =>
'Manufacturing', 1 =>
'Disassemble'),
'css' =>
'minwidth175',
'csslist' =>
'minwidth175 center'),
110 'fk_product' => array(
'type' =>
'integer:Product:product/class/product.class.php:1:((finished:is:null) or (finished:!=:0))',
'label' =>
'Product',
'picto' =>
'product',
'enabled' => 1,
'visible' => 1,
'position' => 35,
'notnull' => 1,
'index' => 1,
'help' =>
'ProductBOMHelp',
'css' =>
'maxwidth500',
'csslist' =>
'tdoverflowmax100'),
111 'description' => array(
'type' =>
'text',
'label' =>
'Description',
'enabled' => 1,
'visible' => -1,
'position' => 60,
'notnull' => -1,),
112 'qty' => array(
'type' =>
'real',
'label' =>
'Quantity',
'enabled' => 1,
'visible' => 1,
'default' =>
'1',
'position' => 55,
'notnull' => 1,
'isameasure' => 1,
'css' =>
'maxwidth50imp right'),
114 'duration' => array(
'type' =>
'duration',
'label' =>
'EstimatedDuration',
'enabled' => 1,
'visible' => -1,
'position' => 101,
'notnull' => -1,
'css' =>
'maxwidth50imp',
'help' =>
'EstimatedDurationDesc'),
115 'fk_warehouse' => array(
'type' =>
'integer:Entrepot:product/stock/class/entrepot.class.php:0',
'label' =>
'WarehouseForProduction',
'picto' =>
'stock',
'enabled' => 1,
'visible' => -1,
'position' => 102,
'css' =>
'maxwidth500',
'csslist' =>
'tdoverflowmax100'),
116 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => -2,
'position' => 161,
'notnull' => -1,),
117 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => -2,
'position' => 162,
'notnull' => -1,),
118 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -2,
'position' => 300,
'notnull' => 1,),
119 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -2,
'position' => 501,
'notnull' => 1,),
120 'date_valid' => array(
'type' =>
'datetime',
'label' =>
'DateValidation',
'enabled' => 1,
'visible' => -2,
'position' => 502,
'notnull' => 0,),
121 'fk_user_creat' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserCreation',
'picto' =>
'user',
'enabled' => 1,
'visible' => -2,
'position' => 510,
'notnull' => 1,
'foreignkey' =>
'user.rowid',
'csslist' =>
'tdoverflowmax100'),
122 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'picto' =>
'user',
'enabled' => 1,
'visible' => -2,
'position' => 511,
'notnull' => -1,
'csslist' =>
'tdoverflowmax100'),
123 'fk_user_valid' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserValidation',
'picto' =>
'user',
'enabled' => 1,
'visible' => -2,
'position' => 512,
'notnull' => 0,
'csslist' =>
'tdoverflowmax100'),
124 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'enabled' => 1,
'visible' => -2,
'position' => 1000,
'notnull' => -1,),
125 'model_pdf' => array(
'type' =>
'varchar(255)',
'label' =>
'Model pdf',
'enabled' => 1,
'visible' => 0,
'position' => 1010),
126 'status' => array(
'type' =>
'integer',
'label' =>
'Status',
'enabled' => 1,
'visible' => 2,
'position' => 1000,
'notnull' => 1,
'default' =>
'0',
'index' => 1,
'arrayofkeyval' => array(0 =>
'Draft', 1 =>
'Enabled', 9 =>
'Disabled')),
162 public $fk_user_creat;
167 public $fk_user_modif;
172 public $fk_user_valid;
177 public $fk_warehouse;
213 public $table_element_line =
'bom_bomline';
218 public $fk_element =
'fk_bom';
223 public $class_element_line =
'BOMLine';
233 protected $childtablesoncascade = array(
'bom_bomline');
238 public $lines = array();
243 public $total_cost = 0;
248 public $unit_cost = 0;
258 global
$conf, $langs;
262 $this->ismultientitymanaged = 1;
263 $this->isextrafieldmanaged = 1;
266 $this->fields[
'rowid'][
'visible'] = 0;
268 if (!isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
269 $this->fields[
'entity'][
'enabled'] = 0;
273 foreach ($this->fields as $key => $val) {
274 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
275 unset($this->fields[$key]);
280 foreach ($this->fields as $key => $val) {
281 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
282 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
283 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
298 if ($this->efficiency <= 0 || $this->efficiency > 1) {
299 $this->efficiency = 1;
314 global $langs, $hookmanager, $extrafields;
324 $result =
$object->fetchCommon($fromid);
325 if ($result > 0 && !empty(
$object->table_element_line)) {
339 $default_ref = $this->fields[
'ref'][
'default'] ??
null;
340 $object->ref = empty($default_ref) ? $langs->trans(
"copy_of_").$object->ref : $default_ref;
342 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".
$object->label : $this->fields[
'label'][
'default'];
343 $object->status = self::STATUS_DRAFT;
346 if (is_array(
$object->array_options) && count(
$object->array_options) > 0) {
347 $extrafields->fetch_name_optionals_label(
$object->table_element);
348 foreach (
$object->array_options as $key => $option) {
349 $shortkey = preg_replace(
'/options_/',
'', $key);
350 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
352 unset(
$object->array_options[$key]);
358 $object->context[
'createfromclone'] =
'createfromclone';
359 $result =
$object->createCommon($user);
363 $this->errors =
$object->errors;
376 if (property_exists($this,
'socid') && $this->socid ==
$object->socid) {
387 unset(
$object->context[
'createfromclone']);
394 $this->db->rollback();
406 public function fetch($id, $ref =
null)
410 if ($result > 0 && !empty($this->table_element_line)) {
425 $this->lines = array();
439 $this->lines = array();
441 $objectlineclassname = get_class($this).
'Line';
442 if (!class_exists($objectlineclassname)) {
443 $this->error =
'Error, class '.$objectlineclassname.
' not found during call of fetchLinesCommon';
447 $objectline =
new $objectlineclassname($this->db);
449 '@phan-var-force BOMLine $objectline';
451 $sql =
"SELECT ".$objectline->getFieldList(
'l');
452 $sql .=
" FROM ".$this->db->prefix().$objectline->table_element.
" as l";
453 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = l.fk_product";
454 $sql .=
" WHERE l.fk_".$this->db->escape($this->element).
" = ".((int) $this->
id);
455 $sql .=
" AND p.fk_product_type = ". ((int) $typeproduct);
456 if (isset($objectline->fields[
'position'])) {
457 $sql .= $this->db->order(
'position',
'ASC');
460 $resql = $this->db->query($sql);
462 $num_rows = $this->db->num_rows($resql);
464 while ($i < $num_rows) {
465 $obj = $this->db->fetch_object($resql);
467 $newline =
new $objectlineclassname($this->db);
468 '@phan-var-force BOMLine $newline';
469 $newline->setVarsFromFetchObj($obj);
473 $newline->fetch_optionals();
476 $this->lines[] = $newline;
483 $this->error = $this->db->lasterror();
484 $this->errors[] = $this->error;
501 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
509 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
510 if ($this->ismultientitymanaged) {
511 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
513 $sql .=
' WHERE 1 = 1';
520 $this->errors[] = $errormessage;
521 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
525 if (!empty($sortfield)) {
526 $sql .= $this->db->order($sortfield, $sortorder);
528 if (!empty($limit)) {
529 $sql .= $this->db->plimit($limit, $offset);
532 $resql = $this->db->query($sql);
534 $num = $this->db->num_rows($resql);
536 while ($obj = $this->db->fetch_object($resql)) {
537 $record =
new self($this->db);
538 $record->setVarsFromFetchObj($obj);
540 $records[$record->id] = $record;
542 $this->db->free($resql);
546 $this->errors[] =
'Error '.$this->db->lasterror();
547 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
562 if ($this->efficiency <= 0 || $this->efficiency > 1) {
563 $this->efficiency = 1;
576 public function delete(
User $user, $notrigger = 1)
598 public function addLine($fk_product, $qty, $qty_frozen = 0, $disable_stock_change = 0, $efficiency = 1.0, $position = -1, $fk_bom_child =
null, $import_key =
null, $fk_unit = 0, $array_options = array(), $fk_default_workstation =
null)
600 global $mysoc,
$conf, $langs, $user;
602 $logtext =
"::addLine bomid=$this->id, qty=$qty, fk_product=$fk_product, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency";
603 $logtext .=
", fk_bom_child=$fk_bom_child, import_key=$import_key";
604 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
606 if ($this->statut == self::STATUS_DRAFT) {
607 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
613 if (empty($qty_frozen)) {
616 if (empty($disable_stock_change)) {
617 $disable_stock_change = 0;
619 if (empty($efficiency)) {
622 if (empty($fk_bom_child)) {
623 $fk_bom_child =
null;
625 if (empty($import_key)) {
628 if (empty($position)) {
633 $efficiency = (float)
price2num($efficiency);
634 $position = (float)
price2num($position);
640 $rankToUse = $position;
641 if ($rankToUse <= 0 or $rankToUse > $rangMax) {
642 $rankToUse = $rangMax + 1;
644 foreach ($this->lines as $bl) {
645 if ($bl->position >= $rankToUse) {
653 $line =
new BOMLine($this->db);
655 $line->context = $this->context;
657 $line->fk_bom = $this->id;
658 $line->fk_product = $fk_product;
660 $line->qty_frozen = $qty_frozen;
661 $line->disable_stock_change = $disable_stock_change;
662 $line->efficiency = $efficiency;
663 $line->fk_bom_child = $fk_bom_child;
664 $line->import_key = $import_key;
665 $line->position = $rankToUse;
666 $line->fk_unit = $fk_unit;
667 $line->fk_default_workstation = $fk_default_workstation;
669 if (is_array($array_options) && count($array_options) > 0) {
670 $line->array_options = $array_options;
673 $result = $line->create($user);
681 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
682 $this->db->rollback();
686 dol_syslog(get_class($this).
"::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR);
706 public function updateLine($rowid, $qty, $qty_frozen = 0, $disable_stock_change = 0, $efficiency = 1.0, $position = -1, $import_key =
null, $fk_unit = 0, $array_options = array(), $fk_default_workstation =
null)
710 $logtext =
"::updateLine bomid=$this->id, qty=$qty, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency";
711 $logtext .=
", import_key=$import_key";
712 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
714 if ($this->statut == self::STATUS_DRAFT) {
715 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
721 if (empty($qty_frozen)) {
724 if (empty($disable_stock_change)) {
725 $disable_stock_change = 0;
727 if (empty($efficiency)) {
730 if (empty($import_key)) {
733 if (empty($position)) {
738 $efficiency = (float)
price2num($efficiency);
739 $position = (float)
price2num($position);
744 $line =
new BOMLine($this->db);
745 $line->fetch($rowid);
746 $line->fetch_optionals();
748 $staticLine = clone $line;
749 $line->oldcopy = $staticLine;
750 $line->context = $this->context;
753 $rankToUse = (int) $position;
754 if ($rankToUse != $line->oldcopy->position) {
755 foreach ($this->lines as $bl) {
756 if ($bl->position >= $rankToUse and $bl->position < ($line->oldcopy->position + 1)) {
760 if ($bl->position <= $rankToUse and $bl->position > ($line->oldcopy->position)) {
768 $line->fk_bom = $this->id;
770 $line->qty_frozen = $qty_frozen;
771 $line->disable_stock_change = $disable_stock_change;
772 $line->efficiency = $efficiency;
773 $line->import_key = $import_key;
774 $line->position = $rankToUse;
777 if (!empty($fk_unit)) {
778 $line->fk_unit = $fk_unit;
782 if (is_array($array_options) && count($array_options) > 0) {
784 foreach ($array_options as $key => $value) {
785 $line->array_options[$key] = $array_options[$key];
788 if ($line->fk_default_workstation != $fk_default_workstation) {
789 $line->fk_default_workstation = ($fk_default_workstation > 0 ? $fk_default_workstation : 0);
792 $result = $line->update($user);
800 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
801 $this->db->rollback();
805 dol_syslog(get_class($this).
"::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR);
821 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
828 $line =
new BOMLine($this->db);
829 $line->fetch($idline);
830 $line->fetch_optionals();
832 $staticLine = clone $line;
833 $line->oldcopy = $staticLine;
834 $line->context = $this->context;
836 $result = $line->delete($user, $notrigger);
839 foreach ($this->lines as $bl) {
840 if ($bl->position > ($line->oldcopy->position)) {
852 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
853 $this->db->rollback();
867 global $langs,
$conf;
877 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
878 foreach ($dirmodels as $reldir) {
882 $mybool = ((bool) @include_once $dir.$file) || $mybool;
890 $obj =
new $classname();
891 '@phan-var-force ModeleNumRefBoms $obj';
892 $numref = $obj->getNextValue($prod, $this);
897 $this->error = $obj->error;
902 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_BOM_ADDON_NotDefined");
918 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
923 if ($this->
status == self::STATUS_VALIDATED) {
924 dol_syslog(get_class($this).
"::validate action abandoned: already validated", LOG_WARNING);
933 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
942 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
943 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
944 $sql .=
" status = ".self::STATUS_VALIDATED.
",";
945 $sql .=
" date_valid='".$this->db->idate($now).
"',";
946 $sql .=
" fk_user_valid = ".((int) $user->id);
947 $sql .=
" WHERE rowid = ".((int) $this->
id);
949 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
950 $resql = $this->db->query($sql);
953 $this->error = $this->db->lasterror();
957 if (!$error && !$notrigger) {
967 $this->oldref = $this->ref;
970 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
972 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'bom/".$this->db->escape($this->newref).
"'";
973 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'bom/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
974 $resql = $this->db->query($sql);
977 $this->error = $this->db->lasterror();
979 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filepath = 'bom/".$this->db->escape($this->newref).
"'";
980 $sql .=
" WHERE filepath = 'bom/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
981 $resql = $this->db->query($sql);
984 $this->error = $this->db->lasterror();
990 $dirsource =
$conf->bom->dir_output.
'/'.$oldref;
991 $dirdest =
$conf->bom->dir_output.
'/'.$newref;
992 if (!$error && file_exists($dirsource)) {
993 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
995 if (@rename($dirsource, $dirdest)) {
998 $listoffiles =
dol_dir_list(
$conf->bom->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
999 foreach ($listoffiles as $fileentry) {
1000 $dirsource = $fileentry[
'name'];
1001 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
1002 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
1003 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
1004 @rename($dirsource, $dirdest);
1014 $this->
status = self::STATUS_VALIDATED;
1018 $this->db->commit();
1021 $this->db->rollback();
1036 if ($this->
status <= self::STATUS_DRAFT) {
1040 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'BOM_UNVALIDATE');
1050 public function cancel($user, $notrigger = 0)
1053 if ($this->
status != self::STATUS_VALIDATED) {
1057 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'BOM_CLOSE');
1067 public function reopen($user, $notrigger = 0)
1070 if ($this->
status != self::STATUS_CANCELED) {
1074 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'BOM_REOPEN');
1085 global
$conf, $langs, $user;
1087 $langs->loadLangs([
'product',
'mrp']);
1092 return [
'optimize' => $langs->trans(
"ShowBillOfMaterials")];
1094 $picto =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"BillOfMaterials").
'</u>';
1095 if (isset($this->
status)) {
1096 $picto .=
' '.$this->getLibStatut(5);
1098 $datas[
'picto'] = $picto;
1099 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1100 if (isset($this->label)) {
1101 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
1103 if (!empty($this->fk_product) && $this->fk_product > 0) {
1104 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1105 $product =
new Product($this->db);
1106 $resultFetch = $product->fetch($this->fk_product);
1107 if ($resultFetch > 0) {
1108 $datas[
'product'] =
"<br><b>".$langs->trans(
"Product").
'</b>: '.$product->ref.
' - '.$product->label;
1125 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1127 global $db,
$conf, $langs, $hookmanager;
1129 if (!empty(
$conf->dol_no_mouse_hover)) {
1136 'objecttype' => $this->element,
1137 'option' => $option,
1139 $classfortooltip =
'classfortooltip';
1142 $classfortooltip =
'classforajaxtooltip';
1143 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1149 $url = DOL_URL_ROOT.
'/bom/bom_card.php?id='.$this->id;
1151 if ($option !=
'nolink') {
1153 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1154 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1155 $add_save_lastsearch_values = 1;
1157 if ($add_save_lastsearch_values) {
1158 $url .=
'&save_lastsearch_values=1';
1163 if (empty($notooltip)) {
1165 $label = $langs->trans(
"ShowBillOfMaterials");
1166 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
1168 $linkclose .= ($label ?
' title="'.dolPrintHTMLForAttribute($label).
'"' :
' title="tocomplete"');
1169 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1171 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1174 $linkstart =
'<a href="'.$url.
'"';
1175 $linkstart .= $linkclose.
'>';
1178 $result .= $linkstart;
1180 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1182 if ($withpicto != 2) {
1183 $result .= $this->ref;
1185 $result .= $linkend;
1188 global $action, $hookmanager;
1189 $hookmanager->initHooks(array(
'bomdao'));
1190 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1191 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1193 $result = $hookmanager->resPrint;
1195 $result .= $hookmanager->resPrint;
1223 if (empty($this->labelStatus)) {
1226 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1227 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Enabled');
1228 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Disabled');
1231 $statusType =
'status'.$status;
1232 if ($status == self::STATUS_VALIDATED) {
1233 $statusType =
'status4';
1235 if ($status == self::STATUS_CANCELED) {
1236 $statusType =
'status6';
1239 return dolGetStatus($this->labelStatus[$status], $this->labelStatus[$status],
'', $statusType, $mode);
1250 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
1251 $sql .=
' fk_user_creat, fk_user_modif';
1252 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1253 $sql .=
' WHERE t.rowid = '.((int) $id);
1254 $result = $this->db->query($sql);
1256 if ($this->db->num_rows($result)) {
1257 $obj = $this->db->fetch_object($result);
1259 $this->
id = $obj->rowid;
1261 $this->user_creation_id = $obj->fk_user_creat;
1262 $this->user_modification_id = $obj->fk_user_modif;
1263 $this->date_creation = $this->db->jdate($obj->datec);
1264 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
1267 $this->db->free($result);
1280 $this->lines = array();
1282 $objectline =
new BOMLine($this->db);
1283 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0,
'(fk_bom:=:'.((
int) $this->
id).
')');
1285 if (is_numeric($result)) {
1286 $this->error = $objectline->error;
1287 $this->errors = $objectline->errors;
1290 $this->lines = $result;
1291 return $this->lines;
1306 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1308 global
$conf, $langs;
1310 $langs->load(
"mrp");
1311 $outputlangs->load(
"products");
1316 if ($this->model_pdf) {
1317 $modele = $this->model_pdf;
1323 $modelpath =
"core/modules/bom/doc/";
1324 if (!empty($modele)) {
1325 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1341 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1342 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
1344 $sdir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'bom');
1347 if (file_exists($dir_osencoded)) {
1348 $handle = opendir($dir_osencoded);
1349 if (is_resource($handle)) {
1350 while (($file = readdir($handle)) !==
false) {
1352 $file = mb_convert_encoding($file,
'UTF-8',
'ISO-8859-1');
1372 $this->
ref =
'BOM-123';
1373 $this->date_creation =
dol_now() - 20000;
1387 global
$conf, $langs;
1403 $this->db->commit();
1416 global $hookmanager;
1418 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1419 $this->unit_cost = 0;
1420 $this->total_cost = 0;
1422 $parameters = array();
1423 $reshook = $hookmanager->executeHooks(
'calculateCostsBom', $parameters, $this);
1426 return $hookmanager->resPrint;
1429 if (is_array($this->lines) && count($this->lines)) {
1430 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1432 $tmpproduct =
new Product($this->db);
1434 foreach ($this->lines as &$line) {
1435 $tmpproduct->cost_price = 0;
1436 $tmpproduct->pmp = 0;
1437 $result = $tmpproduct->fetch($line->fk_product,
'',
'',
'', 0, 1, 1);
1439 if ($tmpproduct->type == $tmpproduct::TYPE_PRODUCT) {
1440 if (empty($line->fk_bom_child)) {
1442 $this->error = $tmpproduct->error;
1446 $unit_cost = (float) (is_null($tmpproduct->cost_price) ? $tmpproduct->pmp : $tmpproduct->cost_price);
1447 if (empty($unit_cost)) {
1448 if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) {
1449 if ($productFournisseur->fourn_remise_percent !=
"0") {
1450 $line->unit_cost = $productFournisseur->fourn_unitprice_with_discount;
1452 $line->unit_cost = $productFournisseur->fourn_unitprice;
1455 $line->unit_cost = 0;
1458 $line->unit_cost = (float) $unit_cost;
1461 $line->total_cost = (float) $line->qty * $line->unit_cost;
1463 $this->total_cost += $line->total_cost;
1465 $bom_child =
new BOM($this->db);
1466 $res = $bom_child->fetch((
int) $line->fk_bom_child);
1468 $bom_child->calculateCosts();
1469 $line->childBom[] = $bom_child;
1470 $this->total_cost += (float) $bom_child->total_cost * $line->qty;
1471 $this->total_cost += $line->total_cost;
1473 $this->error = $bom_child->error;
1479 require_once DOL_DOCUMENT_ROOT.
'/core/class/cunits.class.php';
1480 $measuringUnits =
new CUnits($this->db);
1481 $measuringUnits->fetch($line->fk_unit);
1484 $qtyhourforline = $line->qty * (int) $measuringUnits->scale / 3600;
1486 if (isModEnabled(
'workstation') && !empty($line->fk_default_workstation)) {
1488 $res = $workstation->fetch($line->fk_default_workstation);
1491 $line->total_cost = (float) $qtyhourforline * ($workstation->thm_operator_estimated + $workstation->thm_machine_estimated);
1493 $this->error = $workstation->error;
1497 $defaultdurationofservice = $tmpproduct->duration;
1499 $qtyhourservice = 0;
1500 if (preg_match(
'/^(\d+)([a-z]+)$/', $defaultdurationofservice, $reg)) {
1504 if ($qtyhourservice) {
1505 $line->total_cost = (float) $qtyhourforline / $qtyhourservice * $tmpproduct->cost_price;
1507 $line->total_cost = (float) $line->qty * $tmpproduct->cost_price;
1511 $this->total_cost += $line->total_cost;
1515 $this->total_cost = (float)
price2num($this->total_cost,
'MT');
1517 if ($this->qty > 0) {
1518 $this->unit_cost = (float)
price2num($this->total_cost / $this->qty,
'MU');
1519 } elseif ($this->qty < 0) {
1520 $this->unit_cost = (float)
price2num($this->total_cost * $this->qty,
'MU');
1553 if (!empty($this->lines)) {
1554 foreach ($this->lines as $line) {
1555 if (!empty($line->childBom)) {
1556 foreach ($line->childBom as $childBom) {
1557 $childBom->getNetNeeds($TNetNeeds, $line->qty * $qty);
1560 if (empty($TNetNeeds[$line->fk_product][
'qty'])) {
1561 $TNetNeeds[$line->fk_product][
'qty'] = 0.0;
1565 $TNetNeeds[$line->fk_product][
'fk_unit'] = $line->fk_unit;
1566 $TNetNeeds[$line->fk_product][
'qty'] += $line->qty * $qty;
1582 if (!empty($this->lines)) {
1583 foreach ($this->lines as $line) {
1584 if (!empty($line->childBom)) {
1585 foreach ($line->childBom as $childBom) {
1586 $TNetNeeds[$childBom->id][
'bom'] = $childBom;
1587 $TNetNeeds[$childBom->id][
'parentid'] = $this->id;
1591 $TNetNeeds[$childBom->id][
'qty'] = $line->qty * $qty;
1592 $TNetNeeds[$childBom->id][
'level'] = $level;
1593 $childBom->getNetNeedsTree($TNetNeeds, $line->qty * $qty, $level + 1);
1598 if (!isset($TNetNeeds[$this->
id][
'product'])) {
1599 $TNetNeeds[$this->id][
'product'] = array();
1601 if (!isset($TNetNeeds[$this->
id][
'product'][$line->fk_product])) {
1602 $TNetNeeds[$this->id][
'product'][$line->fk_product] = array();
1604 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'fk_unit'] = $line->fk_unit;
1605 if (!isset($TNetNeeds[$this->
id][
'product'][$line->fk_product][
'qty'])) {
1606 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'qty'] = 0.0;
1608 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'qty'] += $line->qty * $qty;
1609 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'level'] = $level;
1627 if ($level > 1000) {
1631 if (empty($bom_id)) {
1632 $bom_id = $this->id;
1635 $sql =
'SELECT l.fk_bom, b.label
1636 FROM '.MAIN_DB_PREFIX.
'bom_bomline l
1637 INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.
' b ON b.rowid = l.fk_bom
1638 WHERE fk_bom_child = '.((int) $bom_id);
1640 $resql = $this->db->query($sql);
1641 if (!empty($resql)) {
1642 while ($res = $this->db->fetch_object($resql)) {
1643 $TParentBom[$res->fk_bom] = $res->fk_bom;
1660 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
1662 $return =
'<div class="box-flex-item box-flex-grow-zero">';
1663 $return .=
'<div class="info-box info-box-sm">';
1664 $return .=
'<span class="info-box-icon bg-infobox-action">';
1666 $return .=
'</span>';
1667 $return .=
'<div class="info-box-content">';
1668 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() :
'').
'</span>';
1669 if ($selected >= 0) {
1670 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
1672 $arrayofkeyval = $this->fields[
'bomtype'][
'arrayofkeyval'] ??
null;
1673 if (!empty($arrayofkeyval)) {
1674 $return .=
'<br><span class="info-box-label opacitymedium">'.$langs->trans(
"Type").
' : </span>';
1675 if ($this->bomtype == 0) {
1676 $return .=
'<span class="info-box-label">'.$arrayofkeyval[0].
'</span>';
1678 $return .=
'<span class="info-box-label">'.$arrayofkeyval[1].
'</span>';
1681 if (!empty($arraydata[
'prod'])) {
1682 $prod = $arraydata[
'prod'];
1683 $return .=
'<br><span class="info-box-label">'.$prod->getNomUrl(1).
'</span>';
1685 if (method_exists($this,
'getLibStatut')) {
1686 $return .=
'<br><div class="info-box-status">'.$this->getLibStatut(3).
'</div>';
1689 $return .=
'</div>';
1690 $return .=
'</div>';
1691 $return .=
'</div>';
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
fetchLines()
Load object lines in memory from the database.
is_photo_available($sdir)
Return if at least one photo is available.
__construct(DoliDB $db)
Constructor.
calculateCosts()
BOM costs calculation based on cost_price or pmp of each BOM line.
info($id)
Load the info information in the object.
getLibStatut($mode=0)
Return label of the status.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
create(User $user, $notrigger=0)
Create object into database.
validate($user, $notrigger=0)
Validate bom.
reopen($user, $notrigger=0)
Reopen if canceled.
cancel($user, $notrigger=0)
Set cancel status.
LibStatut($status, $mode=0)
Return the status.
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
getParentBomTreeRecursive(&$TParentBom, $bom_id=0, $level=1)
Recursively retrieves all parent bom in the tree that leads to the $bom_id bom.
getTooltipContentArray($params)
getTooltipContentArray
fetchLinesbytypeproduct($typeproduct=0)
Load object lines in memory from the database by type of product.
deleteLine(User $user, $idline, $notrigger=0)
Delete a line of object in database.
createFromClone(User $user, $fromid)
Clone an object into another one.
fetch($id, $ref=null)
Load object 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)
updateLine($rowid, $qty, $qty_frozen=0, $disable_stock_change=0, $efficiency=1.0, $position=-1, $import_key=null, $fk_unit=0, $array_options=array(), $fk_default_workstation=null)
Update an BOM line into database.
getNetNeeds(&$TNetNeeds=array(), $qty=0)
Get Net needs by product.
update(User $user, $notrigger=0)
Update object into database.
getNextNumRef($prod)
Returns the reference to the following non used BOM depending on the active numbering module defined ...
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
getLinesArray()
Create an array of lines.
setDraft($user, $notrigger=0)
Set draft status.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
addLine($fk_product, $qty, $qty_frozen=0, $disable_stock_change=0, $efficiency=1.0, $position=-1, $fk_bom_child=null, $import_key=null, $fk_unit=0, $array_options=array(), $fk_default_workstation=null)
Add an BOM line into database (linked to BOM)
getNetNeedsTree(&$TNetNeeds=array(), $qty=0, $level=0)
Get/add Net needs Tree by product or bom.
Class of dictionary type of thirdparty (used by imports)
Parent class of all other business classes (invoices, contracts, proposals, orders,...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
setErrorsFromObject($object)
setErrorsFromObject
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.
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
fetch_product()
Load the product with id $this->fk_product into this->product.
fetchLinesCommon($morewhere='', $noextrafields=0)
Load object in memory from the database.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage predefined suppliers products.
Class to manage products or services.
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...
convertDurationtoHour($duration_value, $duration_unit)
Convert duration to hour.
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
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)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...