27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobjectline.class.php";
29 require_once DOL_DOCUMENT_ROOT.
'/workstation/class/workstation.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
44 public $module =
'bom';
49 public $element =
'bom';
54 public $table_element =
'bom_bom';
59 public $ismultientitymanaged = 1;
64 public $isextrafieldmanaged = 1;
69 public $picto =
'bom';
72 const STATUS_DRAFT = 0;
73 const STATUS_VALIDATED = 1;
74 const STATUS_CANCELED = 9;
107 public $fields = array(
108 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
109 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'notnull'=> 1,
'default'=>1,
'index'=>1,
'position'=>5),
110 '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'),
111 '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'),
112 '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'),
114 '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'),
115 'description' => array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'notnull'=>-1,),
116 'qty' => array(
'type'=>
'real',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'default'=>1,
'position'=>55,
'notnull'=>1,
'isameasure'=>
'1',
'css'=>
'maxwidth50imp right'),
118 'duration' => array(
'type'=>
'duration',
'label'=>
'EstimatedDuration',
'enabled'=>1,
'visible'=>-1,
'position'=>101,
'notnull'=>-1,
'css'=>
'maxwidth50imp',
'help'=>
'EstimatedDurationDesc'),
119 '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'),
120 'note_public' => array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>-2,
'position'=>161,
'notnull'=>-1,),
121 'note_private' => array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>-2,
'position'=>162,
'notnull'=>-1,),
122 'date_creation' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>300,
'notnull'=>1,),
123 'tms' => array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'position'=>501,
'notnull'=>1,),
124 'date_valid' => array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-2,
'position'=>502,
'notnull'=>0,),
125 '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'),
126 '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'),
127 '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'),
128 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
129 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>1010),
130 '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')),
161 public $date_creation;
173 public $fk_user_creat;
178 public $fk_user_modif;
183 public $fk_user_valid;
188 public $fk_warehouse;
215 public $table_element_line =
'bom_bomline';
220 public $fk_element =
'fk_bom';
225 public $class_element_line =
'BOMLine';
235 protected $childtablesoncascade = array(
'bom_bomline');
240 public $lines = array();
245 public $total_cost = 0;
250 public $unit_cost = 0;
260 global $conf, $langs;
264 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
265 $this->fields[
'rowid'][
'visible'] = 0;
267 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
268 $this->fields[
'entity'][
'enabled'] = 0;
272 foreach ($this->fields as $key => $val) {
273 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
274 unset($this->fields[$key]);
279 foreach ($this->fields as $key => $val) {
280 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
281 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
282 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
297 if ($this->efficiency <= 0 || $this->efficiency > 1) {
298 $this->efficiency = 1;
313 global $langs, $hookmanager, $extrafields;
318 $object =
new self($this->db);
323 $result = $object->fetchCommon($fromid);
324 if ($result > 0 && !empty($object->table_element_line)) {
325 $object->fetchLines();
334 unset($object->fk_user_creat);
335 unset($object->import_key);
338 $object->ref = empty($this->fields[
'ref'][
'default']) ? $langs->trans(
"copy_of_").$object->ref : $this->fields[
'ref'][
'default'];
339 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
340 $object->status = self::STATUS_DRAFT;
343 if (is_array($object->array_options) && count($object->array_options) > 0) {
344 $extrafields->fetch_name_optionals_label($object->table_element);
345 foreach ($object->array_options as $key => $option) {
346 $shortkey = preg_replace(
'/options_/',
'', $key);
347 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
349 unset($object->array_options[$key]);
355 $object->context[
'createfromclone'] =
'createfromclone';
356 $result = $object->createCommon($user);
359 $this->error = $object->error;
360 $this->errors = $object->errors;
372 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
383 unset($object->context[
'createfromclone']);
390 $this->db->rollback();
402 public function fetch($id, $ref =
null)
406 if ($result > 0 && !empty($this->table_element_line)) {
421 $this->lines = array();
436 $this->lines = array();
438 $objectlineclassname = get_class($this).
'Line';
439 if (!class_exists($objectlineclassname)) {
440 $this->error =
'Error, class '.$objectlineclassname.
' not found during call of fetchLinesCommon';
444 $objectline =
new $objectlineclassname($this->db);
446 $sql =
"SELECT ".$objectline->getFieldList(
'l');
447 $sql .=
" FROM ".$this->db->prefix().$objectline->table_element.
" as l";
448 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = l.fk_product";
449 $sql .=
" WHERE l.fk_".$this->db->escape($this->element).
" = ".((int) $this->
id);
450 $sql .=
" AND p.fk_product_type = ". ((int) $typeproduct);
451 if (isset($objectline->fields[
'position'])) {
452 $sql .= $this->db->order(
'position',
'ASC');
455 $resql = $this->db->query(
$sql);
457 $num_rows = $this->db->num_rows($resql);
459 while ($i < $num_rows) {
460 $obj = $this->db->fetch_object($resql);
462 $newline =
new $objectlineclassname($this->db);
463 $newline->setVarsFromFetchObj($obj);
465 $this->lines[] = $newline;
472 $this->error = $this->db->lasterror();
473 $this->errors[] = $this->error;
490 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
500 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
501 if ($this->ismultientitymanaged) {
502 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
504 $sql .=
' WHERE 1 = 1';
508 if (count($filter) > 0) {
509 foreach ($filter as $key => $value) {
510 if ($key ==
't.rowid') {
511 $sqlwhere[] = $key.
" = ".((int) $value);
512 } elseif (strpos($key,
'date') !==
false) {
513 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
514 } elseif ($key ==
'customsql') {
515 $sqlwhere[] = $value;
517 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
521 if (count($sqlwhere) > 0) {
522 $sql .=
" AND (".implode(
" ".$filtermode.
" ", $sqlwhere).
")";
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__.
' '.join(
',', $this->errors), LOG_ERR);
562 if ($this->efficiency <= 0 || $this->efficiency > 1) {
563 $this->efficiency = 1;
576 public function delete(
User $user, $notrigger =
false)
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 =
'', $array_options = 0, $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)) {
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);
705 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 = 0)
707 global $mysoc, $conf, $langs, $user;
709 $logtext =
"::updateLine bomid=$this->id, qty=$qty, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency";
710 $logtext .=
", import_key=$import_key";
711 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
713 if ($this->statut == self::STATUS_DRAFT) {
714 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
720 if (empty($qty_frozen)) {
723 if (empty($disable_stock_change)) {
724 $disable_stock_change = 0;
726 if (empty($efficiency)) {
729 if (empty($import_key)) {
732 if (empty($position)) {
743 $line =
new BOMLine($this->db);
744 $line->fetch($rowid);
745 $line->fetch_optionals();
747 $staticLine = clone $line;
748 $line->oldcopy = $staticLine;
749 $line->context = $this->context;
752 $rankToUse = (int) $position;
753 if ($rankToUse != $line->oldcopy->position) {
754 foreach ($this->lines as $bl) {
755 if ($bl->position >= $rankToUse AND $bl->position < ($line->oldcopy->position + 1)) {
759 if ($bl->position <= $rankToUse AND $bl->position > ($line->oldcopy->position)) {
767 $line->fk_bom = $this->id;
769 $line->qty_frozen = $qty_frozen;
770 $line->disable_stock_change = $disable_stock_change;
771 $line->efficiency = $efficiency;
772 $line->import_key = $import_key;
773 $line->position = $rankToUse;
774 if (!empty($fk_unit)) {
775 $line->fk_unit = $fk_unit;
778 if (is_array($array_options) && count($array_options) > 0) {
780 foreach ($array_options as $key => $value) {
781 $line->array_options[$key] = $array_options[$key];
785 $result = $line->update($user);
793 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
794 $this->db->rollback();
798 dol_syslog(get_class($this).
"::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR);
813 if ($this->status < 0) {
814 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
821 $line =
new BOMLine($this->db);
822 $line->fetch($idline);
823 $line->fetch_optionals();
825 $staticLine = clone $line;
826 $line->oldcopy = $staticLine;
827 $line->context = $this->context;
829 $result = $line->delete($user, $notrigger);
832 foreach ($this->lines as $bl) {
833 if ($bl->position > ($line->oldcopy->position)) {
845 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
846 $this->db->rollback();
860 global $langs, $conf;
863 if (!empty($conf->global->BOM_ADDON)) {
866 $file = $conf->global->BOM_ADDON.
".php";
867 $classname = $conf->global->BOM_ADDON;
870 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
871 foreach ($dirmodels as $reldir) {
875 $mybool |= @include_once $dir.$file;
878 if ($mybool ===
false) {
883 $obj =
new $classname();
884 $numref = $obj->getNextValue($prod, $this);
889 $this->error = $obj->error;
894 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_BOM_ADDON_NotDefined");
908 global $conf, $langs;
910 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
915 if ($this->status == self::STATUS_VALIDATED) {
916 dol_syslog(get_class($this).
"::validate action abandonned: 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);
976 $error++; $this->error = $this->db->lasterror();
982 $dirsource = $conf->bom->dir_output.
'/'.$oldref;
983 $dirdest = $conf->bom->dir_output.
'/'.$newref;
984 if (!$error && file_exists($dirsource)) {
985 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
987 if (@rename($dirsource, $dirdest)) {
990 $listoffiles =
dol_dir_list($conf->bom->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
991 foreach ($listoffiles as $fileentry) {
992 $dirsource = $fileentry[
'name'];
993 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
994 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
995 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
996 @rename($dirsource, $dirdest);
1006 $this->status = self::STATUS_VALIDATED;
1010 $this->db->commit();
1013 $this->db->rollback();
1028 if ($this->status <= self::STATUS_DRAFT) {
1039 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'BOM_UNVALIDATE');
1049 public function cancel($user, $notrigger = 0)
1052 if ($this->status != self::STATUS_VALIDATED) {
1063 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'BOM_CLOSE');
1073 public function reopen($user, $notrigger = 0)
1076 if ($this->status != self::STATUS_CANCELED) {
1087 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'BOM_REOPEN');
1098 global $conf, $langs, $user;
1100 $langs->loadLangs([
'product',
'mrp']);
1104 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1105 return [
'optimize' => $langs->trans(
"ShowBillOfMaterials")];
1107 $picto =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"BillOfMaterials").
'</u>';
1108 if (isset($this->status)) {
1109 $picto .=
' '.$this->getLibStatut(5);
1111 $datas[
'picto'] = $picto;
1112 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1113 if (isset($this->label)) {
1114 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
1116 if (!empty($this->fk_product) && $this->fk_product > 0) {
1117 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1118 $product =
new Product($this->db);
1119 $resultFetch = $product->fetch($this->fk_product);
1120 if ($resultFetch > 0) {
1121 $datas[
'product'] =
"<br><b>".$langs->trans(
"Product").
'</b>: '.$product->ref.
' - '.$product->label;
1138 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1140 global $db, $conf, $langs, $hookmanager;
1142 if (!empty($conf->dol_no_mouse_hover)) {
1149 'objecttype' => $this->element,
1150 'option' => $option,
1152 $classfortooltip =
'classfortooltip';
1155 $classfortooltip =
'classforajaxtooltip';
1156 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1162 $url = DOL_URL_ROOT.
'/bom/bom_card.php?id='.$this->id;
1164 if ($option !=
'nolink') {
1166 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1167 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1168 $add_save_lastsearch_values = 1;
1170 if ($add_save_lastsearch_values) {
1171 $url .=
'&save_lastsearch_values=1';
1176 if (empty($notooltip)) {
1177 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1178 $label = $langs->trans(
"ShowBillOfMaterials");
1179 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1181 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1182 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1184 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1187 $linkstart =
'<a href="'.$url.
'"';
1188 $linkstart .= $linkclose.
'>';
1191 $result .= $linkstart;
1193 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1195 if ($withpicto != 2) {
1196 $result .= $this->ref;
1198 $result .= $linkend;
1201 global $action, $hookmanager;
1202 $hookmanager->initHooks(array(
'bomdao'));
1203 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1204 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1206 $result = $hookmanager->resPrint;
1208 $result .= $hookmanager->resPrint;
1222 return $this->
LibStatut($this->status, $mode);
1236 if (empty($this->labelStatus)) {
1239 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1240 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Enabled');
1241 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Disabled');
1244 $statusType =
'status'.$status;
1245 if ($status == self::STATUS_VALIDATED) {
1246 $statusType =
'status4';
1248 if ($status == self::STATUS_CANCELED) {
1249 $statusType =
'status6';
1252 return dolGetStatus($this->labelStatus[$status], $this->labelStatus[$status],
'', $statusType, $mode);
1263 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
1264 $sql .=
' fk_user_creat, fk_user_modif';
1265 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1266 $sql .=
' WHERE t.rowid = '.((int) $id);
1267 $result = $this->db->query(
$sql);
1269 if ($this->db->num_rows($result)) {
1270 $obj = $this->db->fetch_object($result);
1271 $this->
id = $obj->rowid;
1273 $this->user_creation_id = $obj->fk_user_creat;
1274 $this->user_modification_id = $obj->fk_user_modif;
1275 $this->date_creation = $this->db->jdate($obj->datec);
1276 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
1279 $this->db->free($result);
1292 $this->lines = array();
1294 $objectline =
new BOMLine($this->db);
1295 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, array(
'customsql'=>
'fk_bom = '.((
int) $this->
id)));
1297 if (is_numeric($result)) {
1298 $this->error = $objectline->error;
1299 $this->errors = $objectline->errors;
1302 $this->lines = $result;
1303 return $this->lines;
1318 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1320 global $conf, $langs;
1322 $langs->load(
"mrp");
1323 $outputlangs->load(
"products");
1328 if ($this->model_pdf) {
1329 $modele = $this->model_pdf;
1330 } elseif (!empty($conf->global->BOM_ADDON_PDF)) {
1331 $modele = $conf->global->BOM_ADDON_PDF;
1335 $modelpath =
"core/modules/bom/doc/";
1336 if (!empty($modele)) {
1337 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1352 $this->
ref =
'BOM-123';
1353 $this->date = $this->date_creation;
1365 global $conf, $langs;
1381 $this->db->commit();
1394 global $conf, $hookmanager;
1396 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1397 $this->unit_cost = 0;
1398 $this->total_cost = 0;
1400 $parameters=array();
1401 $reshook = $hookmanager->executeHooks(
'calculateCostsBom', $parameters, $this);
1404 return $hookmanager->resPrint;
1407 if (is_array($this->lines) && count($this->lines)) {
1408 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1410 $tmpproduct =
new Product($this->db);
1412 foreach ($this->lines as &$line) {
1413 $tmpproduct->cost_price = 0;
1414 $tmpproduct->pmp = 0;
1415 $result = $tmpproduct->fetch($line->fk_product,
'',
'',
'', 0, 1, 1);
1417 if ($tmpproduct->type == $tmpproduct::TYPE_PRODUCT) {
1418 if (empty($line->fk_bom_child)) {
1420 $this->error = $tmpproduct->error;
1423 $line->unit_cost =
price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp);
1424 if (empty($line->unit_cost)) {
1425 if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) {
1426 if ($productFournisseur->fourn_remise_percent !=
"0") {
1427 $line->unit_cost = $productFournisseur->fourn_unitprice_with_discount;
1429 $line->unit_cost = $productFournisseur->fourn_unitprice;
1434 $line->total_cost =
price2num($line->qty * $line->unit_cost,
'MT');
1436 $this->total_cost += $line->total_cost;
1438 $bom_child =
new BOM($this->db);
1439 $res = $bom_child->fetch($line->fk_bom_child);
1441 $bom_child->calculateCosts();
1442 $line->childBom[] = $bom_child;
1443 $this->total_cost +=
price2num($bom_child->total_cost * $line->qty,
'MT');
1444 $this->total_cost += $line->total_cost;
1446 $this->error = $bom_child->error;
1455 if (
isModEnabled(
'workstation') && !empty($tmpproduct->fk_default_workstation)) {
1457 $res = $workstation->fetch($tmpproduct->fk_default_workstation);
1459 if ($res > 0) $line->total_cost =
price2num($qtyhourforline * ($workstation->thm_operator_estimated + $workstation->thm_machine_estimated),
'MT');
1461 $this->error = $workstation->error;
1465 $defaultdurationofservice = $tmpproduct->duration;
1467 $qtyhourservice = 0;
1468 if (preg_match(
'/^(\d+)([a-z]+)$/', $defaultdurationofservice, $reg)) {
1472 if ($qtyhourservice) {
1473 $line->total_cost =
price2num($qtyhourforline / $qtyhourservice * $tmpproduct->cost_price,
'MT');
1475 $line->total_cost =
price2num($line->qty * $tmpproduct->cost_price,
'MT');
1479 $this->total_cost += $line->total_cost;
1483 $this->total_cost =
price2num($this->total_cost,
'MT');
1485 if ($this->qty > 0) {
1486 $this->unit_cost =
price2num($this->total_cost / $this->qty,
'MU');
1487 } elseif ($this->qty < 0) {
1488 $this->unit_cost =
price2num($this->total_cost * $this->qty,
'MU');
1521 if (!empty($this->lines)) {
1522 foreach ($this->lines as $line) {
1523 if (!empty($line->childBom)) {
1524 foreach ($line->childBom as $childBom) $childBom->getNetNeeds($TNetNeeds, $line->qty*$qty);
1526 if (empty($TNetNeeds[$line->fk_product])) {
1527 $TNetNeeds[$line->fk_product] = 0;
1529 $TNetNeeds[$line->fk_product] += $line->qty*$qty;
1545 if (!empty($this->lines)) {
1546 foreach ($this->lines as $line) {
1547 if (!empty($line->childBom)) {
1548 foreach ($line->childBom as $childBom) {
1549 $TNetNeeds[$childBom->id][
'bom'] = $childBom;
1550 $TNetNeeds[$childBom->id][
'parentid'] = $this->id;
1551 $TNetNeeds[$childBom->id][
'qty'] = $line->qty*$qty;
1552 $TNetNeeds[$childBom->id][
'level'] = $level;
1553 $childBom->getNetNeedsTree($TNetNeeds, $line->qty*$qty, $level+1);
1556 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'qty'] += $line->qty * $qty;
1557 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'level'] = $level;
1575 if ($level > 1000) {
1579 if (empty($bom_id)) $bom_id=$this->id;
1581 $sql =
'SELECT l.fk_bom, b.label
1582 FROM '.MAIN_DB_PREFIX.
'bom_bomline l
1583 INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.
' b ON b.rowid = l.fk_bom
1584 WHERE fk_bom_child = '.((int) $bom_id);
1586 $resql = $this->db->query(
$sql);
1587 if (!empty($resql)) {
1588 while ($res = $this->db->fetch_object($resql)) {
1589 $TParentBom[$res->fk_bom] = $res->fk_bom;
1606 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
1609 $prod->fetch($this->fk_product);
1611 $return =
'<div class="box-flex-item box-flex-grow-zero">';
1612 $return .=
'<div class="info-box info-box-sm">';
1613 $return .=
'<span class="info-box-icon bg-infobox-action">';
1615 $return .=
'</span>';
1616 $return .=
'<div class="info-box-content">';
1617 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() :
'').
'</span>';
1618 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
1619 if (property_exists($this,
'fields') && !empty($this->fields[
'bomtype'][
'arrayofkeyval'])) {
1620 $return .=
'<br><span class="info-box-label opacitymedium">'.$langs->trans(
"Type").
' : </span>';
1621 if ($this->bomtype == 0) {
1622 $return .=
'<span class="info-box-label">'.$this->fields[
'bomtype'][
'arrayofkeyval'][0].
'</span>';
1624 $return .=
'<span class="info-box-label">'.$this->fields[
'bomtype'][
'arrayofkeyval'][1].
'</span>';
1627 if (property_exists($this,
'fk_product') && !is_null($this->fk_product)) {
1628 $return .=
'<br><span class="info-box-label">'.$prod->getNomUrl(1).
'</span>';
1630 if (method_exists($this,
'getLibStatut')) {
1631 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).
'</div>';
1634 $return .=
'</div>';
1635 $return .=
'</div>';
1636 $return .=
'</div>';
1650 public $element =
'bomline';
1655 public $table_element =
'bom_bomline';
1660 public $ismultientitymanaged = 0;
1665 public $isextrafieldmanaged = 1;
1670 public $picto =
'bomline';
1696 public $fields = array(
1697 'rowid' => array(
'type'=>
'integer',
'label'=>
'LineID',
'enabled'=>1,
'visible'=>-1,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
1698 'fk_bom' => array(
'type'=>
'integer:BillOfMaterials:societe/class/bom.class.php',
'label'=>
'BillOfMaterials',
'enabled'=>1,
'visible'=>1,
'position'=>10,
'notnull'=>1,
'index'=>1,),
1699 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php',
'label'=>
'Product',
'enabled'=>1,
'visible'=>1,
'position'=>20,
'notnull'=>1,
'index'=>1,),
1700 'fk_bom_child' => array(
'type'=>
'integer:BOM:bom/class/bom.class.php',
'label'=>
'BillOfMaterials',
'enabled'=>1,
'visible'=>-1,
'position'=>40,
'notnull'=>-1,),
1701 'description' => array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'notnull'=>-1,),
1702 'qty' => array(
'type'=>
'double(24,8)',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'position'=>100,
'notnull'=>1,
'isameasure'=>
'1',),
1703 'qty_frozen' => array(
'type'=>
'smallint',
'label'=>
'QuantityFrozen',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>105,
'css'=>
'maxwidth50imp',
'help'=>
'QuantityConsumedInvariable'),
1704 'disable_stock_change' => array(
'type'=>
'smallint',
'label'=>
'DisableStockChange',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>108,
'css'=>
'maxwidth50imp',
'help'=>
'DisableStockChangeHelp'),
1705 'efficiency' => array(
'type'=>
'double(24,8)',
'label'=>
'ManufacturingEfficiency',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'position'=>110,
'notnull'=>1,
'css'=>
'maxwidth50imp',
'help'=>
'ValueOfEfficiencyConsumedMeans'),
1706 'fk_unit' => array(
'type'=>
'integer',
'label'=>
'Unit',
'enabled'=>1,
'visible'=>1,
'position'=>120,
'notnull'=>-1,),
1707 'position' => array(
'type'=>
'integer',
'label'=>
'Rank',
'enabled'=>1,
'visible'=>0,
'default'=>0,
'position'=>200,
'notnull'=>1,),
1708 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
1709 'fk_default_workstation' =>array(
'type'=>
'integer',
'label'=>
'DefaultWorkstation',
'enabled'=>1,
'visible'=>1,
'notnull'=>0,
'position'=>1050)
1730 public $fk_bom_child;
1735 public $description;
1742 public $disable_stock_change;
1759 public $total_cost = 0;
1764 public $unit_cost = 0;
1770 public $childBom = array();
1775 public $fk_default_workstation;
1786 global $conf, $langs;
1790 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
1791 $this->fields[
'rowid'][
'visible'] = 0;
1793 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
1794 $this->fields[
'entity'][
'enabled'] = 0;
1798 foreach ($this->fields as $key => $val) {
1799 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
1800 unset($this->fields[$key]);
1805 foreach ($this->fields as $key => $val) {
1806 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
1807 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
1808 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
1823 if ($this->efficiency < 0 || $this->efficiency > 1) {
1824 $this->efficiency = 1;
1855 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
1865 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1866 if ($this->ismultientitymanaged) {
1867 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
1869 $sql .=
' WHERE 1 = 1';
1872 $sqlwhere = array();
1873 if (count($filter) > 0) {
1874 foreach ($filter as $key => $value) {
1875 if ($key ==
't.rowid') {
1876 $sqlwhere[] = $key.
" = ".((int) $value);
1877 } elseif (strpos($key,
'date') !==
false) {
1878 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
1879 } elseif ($key ==
'customsql') {
1880 $sqlwhere[] = $value;
1882 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
1886 if (count($sqlwhere) > 0) {
1887 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
1890 if (!empty($sortfield)) {
1891 $sql .= $this->db->order($sortfield, $sortorder);
1893 if (!empty($limit)) {
1894 $sql .= $this->db->plimit($limit, $offset);
1897 $resql = $this->db->query(
$sql);
1899 $num = $this->db->num_rows($resql);
1901 while ($obj = $this->db->fetch_object($resql)) {
1902 $record =
new self($this->db);
1903 $record->setVarsFromFetchObj($obj);
1905 $records[$record->id] = $record;
1907 $this->db->free($resql);
1911 $this->errors[] =
'Error '.$this->db->lasterror();
1912 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1927 if ($this->efficiency < 0 || $this->efficiency > 1) {
1928 $this->efficiency = 1;
1941 public function delete(
User $user, $notrigger =
false)
1957 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1959 global $db, $conf, $langs, $hookmanager;
1961 if (!empty($conf->dol_no_mouse_hover)) {
1967 $label =
'<u>'.$langs->trans(
"BillOfMaterialsLine").
'</u>';
1969 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1971 $url = DOL_URL_ROOT.
'/bom/bomline_card.php?id='.$this->id;
1973 if ($option !=
'nolink') {
1975 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1976 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1977 $add_save_lastsearch_values = 1;
1979 if ($add_save_lastsearch_values) {
1980 $url .=
'&save_lastsearch_values=1';
1985 if (empty($notooltip)) {
1986 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1987 $label = $langs->trans(
"ShowBillOfMaterialsLine");
1988 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1990 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1991 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1993 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1996 $linkstart =
'<a href="'.$url.
'"';
1997 $linkstart .= $linkclose.
'>';
2000 $result .= $linkstart;
2002 $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);
2004 if ($withpicto != 2) {
2005 $result .= $this->ref;
2007 $result .= $linkend;
2010 global $action, $hookmanager;
2011 $hookmanager->initHooks(array(
'bomlinedao'));
2012 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2013 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2015 $result = $hookmanager->resPrint;
2017 $result .= $hookmanager->resPrint;
2031 return $this->
LibStatut($this->status, $mode);
2056 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
2057 $sql .=
' fk_user_creat, fk_user_modif';
2058 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
2059 $sql .=
' WHERE t.rowid = '.((int) $id);
2060 $result = $this->db->query(
$sql);
2062 if ($this->db->num_rows($result)) {
2063 $obj = $this->db->fetch_object($result);
2064 $this->
id = $obj->rowid;
2065 $this->user_creation_id = $obj->fk_user_creat;
2066 $this->user_modification_id = $obj->fk_user_modif;
2067 $this->date_creation = $this->db->jdate($obj->datec);
2068 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
2070 $this->db->free($result);