26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/workstation/class/workstation.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
41 public $element =
'bom';
46 public $table_element =
'bom_bom';
51 public $ismultientitymanaged = 1;
56 public $isextrafieldmanaged = 1;
61 public $picto =
'bom';
64 const STATUS_DRAFT = 0;
65 const STATUS_VALIDATED = 1;
66 const STATUS_CANCELED = 9;
99 public $fields = array(
100 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
101 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'notnull'=> 1,
'default'=>1,
'index'=>1,
'position'=>5),
102 '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'),
103 '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'),
104 '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'),
106 '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'),
107 'description' => array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'notnull'=>-1,),
108 'qty' => array(
'type'=>
'real',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'default'=>1,
'position'=>55,
'notnull'=>1,
'isameasure'=>
'1',
'css'=>
'maxwidth50imp right'),
110 'duration' => array(
'type'=>
'duration',
'label'=>
'EstimatedDuration',
'enabled'=>1,
'visible'=>-1,
'position'=>101,
'notnull'=>-1,
'css'=>
'maxwidth50imp',
'help'=>
'EstimatedDurationDesc'),
111 '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'),
112 'note_public' => array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>-2,
'position'=>161,
'notnull'=>-1,),
113 'note_private' => array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>-2,
'position'=>162,
'notnull'=>-1,),
114 'date_creation' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>300,
'notnull'=>1,),
115 'tms' => array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'position'=>501,
'notnull'=>1,),
116 'date_valid' => array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-2,
'position'=>502,
'notnull'=>0,),
117 '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'),
118 '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'),
119 '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'),
120 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
121 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>1010),
122 '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')),
153 public $date_creation;
161 public $fk_user_creat;
166 public $fk_user_modif;
192 public $table_element_line =
'bom_bomline';
197 public $fk_element =
'fk_bom';
202 public $class_element_line =
'BOMLine';
212 protected $childtablesoncascade = array(
'bom_bomline');
217 public $lines = array();
222 public $total_cost = 0;
227 public $unit_cost = 0;
238 global $conf, $langs;
242 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
243 $this->fields[
'rowid'][
'visible'] = 0;
245 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
246 $this->fields[
'entity'][
'enabled'] = 0;
250 foreach ($this->fields as $key => $val) {
251 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
252 unset($this->fields[$key]);
257 foreach ($this->fields as $key => $val) {
258 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
259 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
260 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
275 if ($this->efficiency <= 0 || $this->efficiency > 1) {
276 $this->efficiency = 1;
291 global $langs, $hookmanager, $extrafields;
296 $object =
new self($this->db);
301 $result = $object->fetchCommon($fromid);
302 if ($result > 0 && !empty($object->table_element_line)) {
303 $object->fetchLines();
312 unset($object->fk_user_creat);
313 unset($object->import_key);
316 $object->ref = empty($this->fields[
'ref'][
'default']) ? $langs->trans(
"copy_of_").$object->ref : $this->fields[
'ref'][
'default'];
317 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
318 $object->status = self::STATUS_DRAFT;
321 if (is_array($object->array_options) && count($object->array_options) > 0) {
322 $extrafields->fetch_name_optionals_label($object->table_element);
323 foreach ($object->array_options as $key => $option) {
324 $shortkey = preg_replace(
'/options_/',
'', $key);
325 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
327 unset($object->array_options[$key]);
333 $object->context[
'createfromclone'] =
'createfromclone';
334 $result = $object->createCommon($user);
337 $this->error = $object->error;
338 $this->errors = $object->errors;
350 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
361 unset($object->context[
'createfromclone']);
368 $this->
db->rollback();
380 public function fetch($id, $ref =
null)
384 if ($result > 0 && !empty($this->table_element_line)) {
399 $this->lines = array();
414 $this->lines = array();
416 $objectlineclassname = get_class($this).
'Line';
417 if (!class_exists($objectlineclassname)) {
418 $this->error =
'Error, class '.$objectlineclassname.
' not found during call of fetchLinesCommon';
422 $objectline =
new $objectlineclassname($this->
db);
424 $sql =
"SELECT ".$objectline->getFieldList(
'l');
425 $sql .=
" FROM ".$this->db->prefix().$objectline->table_element.
" as l";
426 $sql .=
" LEFT JOIN ".$this->db->prefix().
"product as p ON p.rowid = l.fk_product";
427 $sql .=
" WHERE l.fk_".$this->db->escape($this->element).
" = ".((int) $this->
id);
428 $sql .=
" AND p.fk_product_type = ". ((int) $typeproduct);
429 if (isset($objectline->fields[
'position'])) {
430 $sql .= $this->
db->order(
'position',
'ASC');
433 $resql = $this->
db->query(
$sql);
435 $num_rows = $this->
db->num_rows($resql);
437 while ($i < $num_rows) {
438 $obj = $this->
db->fetch_object($resql);
440 $newline =
new $objectlineclassname($this->
db);
441 $newline->setVarsFromFetchObj($obj);
443 $this->lines[] = $newline;
450 $this->error = $this->
db->lasterror();
451 $this->errors[] = $this->error;
468 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
478 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
479 if ($this->ismultientitymanaged) {
480 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
482 $sql .=
' WHERE 1 = 1';
486 if (count($filter) > 0) {
487 foreach ($filter as $key => $value) {
488 if ($key ==
't.rowid') {
489 $sqlwhere[] = $key.
" = ".((int) $value);
490 } elseif (strpos($key,
'date') !==
false) {
491 $sqlwhere[] = $key.
" = '".$this->
db->idate($value).
"'";
492 } elseif ($key ==
'customsql') {
493 $sqlwhere[] = $value;
495 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
499 if (count($sqlwhere) > 0) {
500 $sql .=
" AND (".implode(
" ".$filtermode.
" ", $sqlwhere).
")";
503 if (!empty($sortfield)) {
504 $sql .= $this->
db->order($sortfield, $sortorder);
506 if (!empty($limit)) {
507 $sql .= $this->
db->plimit($limit, $offset);
510 $resql = $this->
db->query(
$sql);
512 $num = $this->
db->num_rows($resql);
514 while ($obj = $this->
db->fetch_object($resql)) {
515 $record =
new self($this->db);
516 $record->setVarsFromFetchObj($obj);
518 $records[$record->id] = $record;
520 $this->
db->free($resql);
524 $this->errors[] =
'Error '.$this->db->lasterror();
525 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
540 if ($this->efficiency <= 0 || $this->efficiency > 1) {
541 $this->efficiency = 1;
554 public function delete(
User $user, $notrigger =
false)
575 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)
577 global $mysoc, $conf, $langs, $user;
579 $logtext =
"::addLine bomid=$this->id, qty=$qty, fk_product=$fk_product, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency";
580 $logtext .=
", fk_bom_child=$fk_bom_child, import_key=$import_key";
581 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
583 if ($this->statut == self::STATUS_DRAFT) {
584 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
590 if (empty($qty_frozen)) {
593 if (empty($disable_stock_change)) {
594 $disable_stock_change = 0;
596 if (empty($efficiency)) {
599 if (empty($fk_bom_child)) {
600 $fk_bom_child =
null;
602 if (empty($import_key)) {
605 if (empty($position)) {
617 $rankToUse = $position;
618 if ($rankToUse <= 0 or $rankToUse > $rangMax) {
619 $rankToUse = $rangMax + 1;
621 foreach ($this->lines as $bl) {
622 if ($bl->position >= $rankToUse) {
632 $this->line->context = $this->context;
634 $this->line->fk_bom = $this->id;
635 $this->line->fk_product = $fk_product;
636 $this->line->qty = $qty;
637 $this->line->qty_frozen = $qty_frozen;
638 $this->line->disable_stock_change = $disable_stock_change;
639 $this->line->efficiency = $efficiency;
640 $this->line->fk_bom_child = $fk_bom_child;
641 $this->line->import_key = $import_key;
642 $this->line->position = $rankToUse;
643 $this->line->fk_unit = $fk_unit;
645 if (is_array($array_options) && count($array_options) > 0) {
646 $this->line->array_options = $array_options;
649 $result = $this->line->create($user);
656 $this->error = $this->line->error;
657 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
658 $this->
db->rollback();
662 dol_syslog(get_class($this).
"::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR);
681 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)
683 global $mysoc, $conf, $langs, $user;
685 $logtext =
"::updateLine bomid=$this->id, qty=$qty, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency";
686 $logtext .=
", import_key=$import_key";
687 dol_syslog(get_class($this).$logtext, LOG_DEBUG);
689 if ($this->statut == self::STATUS_DRAFT) {
690 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
696 if (empty($qty_frozen)) {
699 if (empty($disable_stock_change)) {
700 $disable_stock_change = 0;
702 if (empty($efficiency)) {
705 if (empty($import_key)) {
708 if (empty($position)) {
720 $line->fetch($rowid);
721 $line->fetch_optionals();
723 $staticLine = clone $line;
724 $line->oldcopy = $staticLine;
726 $this->line->context = $this->context;
729 $rankToUse = (int) $position;
730 if ($rankToUse != $line->oldcopy->position) {
731 foreach ($this->lines as $bl) {
732 if ($bl->position >= $rankToUse AND $bl->position < ($line->oldcopy->position + 1)) {
736 if ($bl->position <= $rankToUse AND $bl->position > ($line->oldcopy->position)) {
744 $this->line->fk_bom = $this->id;
745 $this->line->qty = $qty;
746 $this->line->qty_frozen = $qty_frozen;
747 $this->line->disable_stock_change = $disable_stock_change;
748 $this->line->efficiency = $efficiency;
749 $this->line->import_key = $import_key;
750 $this->line->position = $rankToUse;
751 if (!empty($fk_unit)) {
752 $this->line->fk_unit = $fk_unit;
755 if (is_array($array_options) && count($array_options) > 0) {
757 foreach ($array_options as $key => $value) {
758 $this->line->array_options[$key] = $array_options[$key];
762 $result = $this->line->update($user);
769 $this->error = $this->line->error;
770 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
771 $this->
db->rollback();
775 dol_syslog(get_class($this).
"::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR);
790 if ($this->status < 0) {
791 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
799 $line->fetch($idline);
800 $line->fetch_optionals();
802 $staticLine = clone $line;
803 $line->oldcopy = $staticLine;
805 $this->line->context = $this->context;
807 $result = $this->line->delete($user, $notrigger);
810 foreach ($this->lines as $bl) {
811 if ($bl->position > ($line->oldcopy->position)) {
822 $this->error = $this->line->error;
823 dol_syslog(get_class($this).
"::addLine error=".$this->error, LOG_ERR);
824 $this->
db->rollback();
838 global $langs, $conf;
841 if (!empty($conf->global->BOM_ADDON)) {
844 $file = $conf->global->BOM_ADDON.
".php";
845 $classname = $conf->global->BOM_ADDON;
848 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
849 foreach ($dirmodels as $reldir) {
853 $mybool |= @include_once $dir.$file;
856 if ($mybool ===
false) {
861 $obj =
new $classname();
862 $numref = $obj->getNextValue($prod, $this);
867 $this->error = $obj->error;
872 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_BOM_ADDON_NotDefined");
886 global $conf, $langs;
888 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
893 if ($this->status == self::STATUS_VALIDATED) {
894 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
911 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
920 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
921 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
922 $sql .=
" status = ".self::STATUS_VALIDATED.
",";
923 $sql .=
" date_valid='".$this->db->idate($now).
"',";
924 $sql .=
" fk_user_valid = ".((int) $user->id);
925 $sql .=
" WHERE rowid = ".((int) $this->
id);
927 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
928 $resql = $this->
db->query(
$sql);
931 $this->error = $this->
db->lasterror();
935 if (!$error && !$notrigger) {
945 $this->oldref = $this->ref;
948 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
950 $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).
"'";
951 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'bom/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
952 $resql = $this->
db->query(
$sql);
954 $error++; $this->error = $this->
db->lasterror();
960 $dirsource = $conf->bom->dir_output.
'/'.$oldref;
961 $dirdest = $conf->bom->dir_output.
'/'.$newref;
962 if (!$error && file_exists($dirsource)) {
963 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
965 if (@rename($dirsource, $dirdest)) {
968 $listoffiles =
dol_dir_list($conf->bom->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
969 foreach ($listoffiles as $fileentry) {
970 $dirsource = $fileentry[
'name'];
971 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
972 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
973 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
974 @rename($dirsource, $dirdest);
984 $this->status = self::STATUS_VALIDATED;
991 $this->
db->rollback();
1006 if ($this->status <= self::STATUS_DRAFT) {
1017 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'BOM_UNVALIDATE');
1027 public function cancel($user, $notrigger = 0)
1030 if ($this->status != self::STATUS_VALIDATED) {
1041 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'BOM_CLOSE');
1051 public function reopen($user, $notrigger = 0)
1054 if ($this->status != self::STATUS_CANCELED) {
1065 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'BOM_REOPEN');
1076 global $conf, $langs, $user;
1078 $langs->loadLangs([
'product',
'mrp']);
1082 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1083 return [
'optimize' => $langs->trans(
"ShowBillOfMaterials")];
1085 $picto =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"BillOfMaterials").
'</u>';
1086 if (isset($this->status)) {
1087 $picto .=
' '.$this->getLibStatut(5);
1089 $datas[
'picto'] = $picto;
1090 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1091 if (isset($this->label)) {
1092 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
1094 if (!empty($this->fk_product) && $this->fk_product > 0) {
1095 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1097 $resultFetch = $product->fetch($this->fk_product);
1098 if ($resultFetch > 0) {
1099 $datas[
'product'] =
"<br><b>".$langs->trans(
"Product").
'</b>: '.$product->ref.
' - '.$product->label;
1116 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1118 global $db, $conf, $langs, $hookmanager;
1120 if (!empty($conf->dol_no_mouse_hover)) {
1127 'objecttype' => $this->element,
1128 'option' => $option,
1130 $classfortooltip =
'classfortooltip';
1133 $classfortooltip =
'classforajaxtooltip';
1134 $dataparams =
' data-params='.json_encode($params);
1141 $url = DOL_URL_ROOT.
'/bom/bom_card.php?id='.$this->id;
1143 if ($option !=
'nolink') {
1145 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1146 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1147 $add_save_lastsearch_values = 1;
1149 if ($add_save_lastsearch_values) {
1150 $url .=
'&save_lastsearch_values=1';
1155 if (empty($notooltip)) {
1156 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1157 $label = $langs->trans(
"ShowBillOfMaterials");
1158 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1160 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1161 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1163 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1166 $linkstart =
'<a href="'.$url.
'"';
1167 $linkstart .= $linkclose.
'>';
1170 $result .= $linkstart;
1172 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
1174 if ($withpicto != 2) {
1175 $result .= $this->ref;
1177 $result .= $linkend;
1180 global $action, $hookmanager;
1181 $hookmanager->initHooks(array(
'bomdao'));
1182 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1183 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1185 $result = $hookmanager->resPrint;
1187 $result .= $hookmanager->resPrint;
1201 return $this->
LibStatut($this->status, $mode);
1215 if (empty($this->labelStatus)) {
1218 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1219 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Enabled');
1220 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Disabled');
1223 $statusType =
'status'.$status;
1224 if ($status == self::STATUS_VALIDATED) {
1225 $statusType =
'status4';
1227 if ($status == self::STATUS_CANCELED) {
1228 $statusType =
'status6';
1231 return dolGetStatus($this->labelStatus[$status], $this->labelStatus[$status],
'', $statusType, $mode);
1242 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
1243 $sql .=
' fk_user_creat, fk_user_modif';
1244 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1245 $sql .=
' WHERE t.rowid = '.((int) $id);
1246 $result = $this->
db->query(
$sql);
1248 if ($this->
db->num_rows($result)) {
1249 $obj = $this->
db->fetch_object($result);
1250 $this->
id = $obj->rowid;
1252 $this->user_creation_id = $obj->fk_user_creat;
1253 $this->user_modification_id = $obj->fk_user_modif;
1254 $this->date_creation = $this->
db->jdate($obj->datec);
1255 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
1258 $this->
db->free($result);
1271 $this->lines = array();
1274 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, array(
'customsql'=>
'fk_bom = '.((
int) $this->
id)));
1276 if (is_numeric($result)) {
1277 $this->error = $objectline->error;
1278 $this->errors = $objectline->errors;
1281 $this->lines = $result;
1282 return $this->lines;
1297 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1299 global $conf, $langs;
1301 $langs->load(
"mrp");
1302 $outputlangs->load(
"products");
1305 $modele =
'standard';
1307 if ($this->model_pdf) {
1308 $modele = $this->model_pdf;
1309 } elseif (!empty($conf->global->BOM_ADDON_PDF)) {
1310 $modele = $conf->global->BOM_ADDON_PDF;
1314 $modelpath =
"core/modules/bom/doc/";
1316 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1328 $this->
ref =
'BOM-123';
1329 $this->date = $this->date_creation;
1341 global $conf, $langs;
1357 $this->
db->commit();
1370 global $conf, $hookmanager;
1372 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1373 $this->unit_cost = 0;
1374 $this->total_cost = 0;
1376 $parameters=array();
1377 $reshook = $hookmanager->executeHooks(
'calculateCostsBom', $parameters, $this);
1380 return $hookmanager->resPrint;
1383 if (is_array($this->lines) && count($this->lines)) {
1384 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1388 foreach ($this->lines as &$line) {
1389 $tmpproduct->cost_price = 0;
1390 $tmpproduct->pmp = 0;
1391 $result = $tmpproduct->fetch($line->fk_product,
'',
'',
'', 0, 1, 1);
1393 if ($tmpproduct->type == $tmpproduct::TYPE_PRODUCT) {
1394 if (empty($line->fk_bom_child)) {
1396 $this->error = $tmpproduct->error;
1399 $line->unit_cost =
price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp);
1400 if (empty($line->unit_cost)) {
1401 if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) {
1402 if ($productFournisseur->fourn_remise_percent !=
"0") {
1403 $line->unit_cost = $productFournisseur->fourn_unitprice_with_discount;
1405 $line->unit_cost = $productFournisseur->fourn_unitprice;
1410 $line->total_cost =
price2num($line->qty * $line->unit_cost,
'MT');
1412 $this->total_cost += $line->total_cost;
1414 $bom_child =
new BOM($this->
db);
1415 $res = $bom_child->fetch($line->fk_bom_child);
1417 $bom_child->calculateCosts();
1418 $line->childBom[] = $bom_child;
1419 $this->total_cost +=
price2num($bom_child->total_cost * $line->qty,
'MT');
1420 $this->total_cost += $line->total_cost;
1422 $this->error = $bom_child->error;
1431 if (
isModEnabled(
'workstation') && !empty($tmpproduct->fk_default_workstation)) {
1433 $res = $workstation->fetch($tmpproduct->fk_default_workstation);
1435 if ($res > 0) $line->total_cost =
price2num($qty * ($workstation->thm_operator_estimated + $workstation->thm_machine_estimated),
'MT');
1437 $this->error = $workstation->error;
1441 $line->total_cost =
price2num($qty * $tmpproduct->cost_price,
'MT');
1444 $this->total_cost += $line->total_cost;
1448 $this->total_cost =
price2num($this->total_cost,
'MT');
1450 if ($this->qty > 0) {
1451 $this->unit_cost =
price2num($this->total_cost / $this->qty,
'MU');
1452 } elseif ($this->qty < 0) {
1453 $this->unit_cost =
price2num($this->total_cost * $this->qty,
'MU');
1486 if (!empty($this->lines)) {
1487 foreach ($this->lines as $line) {
1488 if (!empty($line->childBom)) {
1489 foreach ($line->childBom as $childBom) $childBom->getNetNeeds($TNetNeeds, $line->qty*$qty);
1491 if (empty($TNetNeeds[$line->fk_product])) {
1492 $TNetNeeds[$line->fk_product] = 0;
1494 $TNetNeeds[$line->fk_product] += $line->qty*$qty;
1510 if (!empty($this->lines)) {
1511 foreach ($this->lines as $line) {
1512 if (!empty($line->childBom)) {
1513 foreach ($line->childBom as $childBom) {
1514 $TNetNeeds[$childBom->id][
'bom'] = $childBom;
1515 $TNetNeeds[$childBom->id][
'parentid'] = $this->id;
1516 $TNetNeeds[$childBom->id][
'qty'] = $line->qty*$qty;
1517 $TNetNeeds[$childBom->id][
'level'] = $level;
1518 $childBom->getNetNeedsTree($TNetNeeds, $line->qty*$qty, $level+1);
1521 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'qty'] += $line->qty * $qty;
1522 $TNetNeeds[$this->id][
'product'][$line->fk_product][
'level'] = $level;
1540 if ($level > 1000) {
1544 if (empty($bom_id)) $bom_id=$this->id;
1546 $sql =
'SELECT l.fk_bom, b.label
1547 FROM '.MAIN_DB_PREFIX.
'bom_bomline l
1548 INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.
' b ON b.rowid = l.fk_bom
1549 WHERE fk_bom_child = '.((int) $bom_id);
1551 $resql = $this->
db->query(
$sql);
1552 if (!empty($resql)) {
1553 while ($res = $this->
db->fetch_object($resql)) {
1554 $TParentBom[$res->fk_bom] = $res->fk_bom;
1571 $prod->fetch($this->fk_product);
1573 $return =
'<div class="box-flex-item box-flex-grow-zero">';
1574 $return .=
'<div class="info-box info-box-sm">';
1575 $return .=
'<span class="info-box-icon bg-infobox-action">';
1577 $return .=
'</span>';
1578 $return .=
'<div class="info-box-content">';
1579 $return .=
'<span class="info-box-ref">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() :
'').
'</span>';
1580 if (property_exists($this,
'fields') && !empty($this->fields[
'bomtype'][
'arrayofkeyval'])) {
1581 $return .=
'<br><span class="info-box-label opacitymedium">'.$langs->trans(
"Type").
' : </span>';
1582 if ($this->bomtype == 0) {
1583 $return .=
'<span class="info-box-label">'.$this->fields[
'bomtype'][
'arrayofkeyval'][0].
'</span>';
1585 $return .=
'<span class="info-box-label">'.$this->fields[
'bomtype'][
'arrayofkeyval'][1].
'</span>';
1588 if (property_exists($this,
'fk_product') && !is_null($this->fk_product)) {
1589 $return .=
'<br><span class="info-box-label">'.$prod->getNomUrl(1).
'</span>';
1591 if (method_exists($this,
'getLibStatut')) {
1592 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(5).
'</div>';
1595 $return .=
'</div>';
1596 $return .=
'</div>';
1597 $return .=
'</div>';
1611 public $element =
'bomline';
1616 public $table_element =
'bom_bomline';
1621 public $ismultientitymanaged = 0;
1626 public $isextrafieldmanaged = 1;
1631 public $picto =
'bomline';
1657 public $fields = array(
1658 'rowid' => array(
'type'=>
'integer',
'label'=>
'LineID',
'enabled'=>1,
'visible'=>-1,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
1659 'fk_bom' => array(
'type'=>
'integer:BillOfMaterials:societe/class/bom.class.php',
'label'=>
'BillOfMaterials',
'enabled'=>1,
'visible'=>1,
'position'=>10,
'notnull'=>1,
'index'=>1,),
1660 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php',
'label'=>
'Product',
'enabled'=>1,
'visible'=>1,
'position'=>20,
'notnull'=>1,
'index'=>1,),
1661 'fk_bom_child' => array(
'type'=>
'integer:BOM:bom/class/bom.class.php',
'label'=>
'BillOfMaterials',
'enabled'=>1,
'visible'=>-1,
'position'=>40,
'notnull'=>-1,),
1662 'description' => array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'notnull'=>-1,),
1663 'qty' => array(
'type'=>
'double(24,8)',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'position'=>100,
'notnull'=>1,
'isameasure'=>
'1',),
1664 'qty_frozen' => array(
'type'=>
'smallint',
'label'=>
'QuantityFrozen',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>105,
'css'=>
'maxwidth50imp',
'help'=>
'QuantityConsumedInvariable'),
1665 'disable_stock_change' => array(
'type'=>
'smallint',
'label'=>
'DisableStockChange',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>108,
'css'=>
'maxwidth50imp',
'help'=>
'DisableStockChangeHelp'),
1666 'efficiency' => array(
'type'=>
'double(24,8)',
'label'=>
'ManufacturingEfficiency',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'position'=>110,
'notnull'=>1,
'css'=>
'maxwidth50imp',
'help'=>
'ValueOfEfficiencyConsumedMeans'),
1667 'fk_unit' => array(
'type'=>
'integer',
'label'=>
'Unit',
'enabled'=>1,
'visible'=>1,
'position'=>120,
'notnull'=>-1,),
1668 'position' => array(
'type'=>
'integer',
'label'=>
'Rank',
'enabled'=>1,
'visible'=>0,
'default'=>0,
'position'=>200,
'notnull'=>1,),
1669 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
1690 public $fk_bom_child;
1695 public $description;
1702 public $disable_stock_change;
1719 public $total_cost = 0;
1724 public $unit_cost = 0;
1730 public $childBom = array();
1740 global $conf, $langs;
1744 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
1745 $this->fields[
'rowid'][
'visible'] = 0;
1747 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
1748 $this->fields[
'entity'][
'enabled'] = 0;
1752 foreach ($this->fields as $key => $val) {
1753 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
1754 unset($this->fields[$key]);
1759 foreach ($this->fields as $key => $val) {
1760 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
1761 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
1762 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
1777 if ($this->efficiency < 0 || $this->efficiency > 1) {
1778 $this->efficiency = 1;
1809 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
1819 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1820 if ($this->ismultientitymanaged) {
1821 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
1823 $sql .=
' WHERE 1 = 1';
1826 $sqlwhere = array();
1827 if (count($filter) > 0) {
1828 foreach ($filter as $key => $value) {
1829 if ($key ==
't.rowid') {
1830 $sqlwhere[] = $key.
" = ".((int) $value);
1831 } elseif (strpos($key,
'date') !==
false) {
1832 $sqlwhere[] = $key.
" = '".$this->
db->idate($value).
"'";
1833 } elseif ($key ==
'customsql') {
1834 $sqlwhere[] = $value;
1836 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
1840 if (count($sqlwhere) > 0) {
1841 $sql .=
' AND ('.implode(
' '.$this->
db->escape($filtermode).
' ', $sqlwhere).
')';
1844 if (!empty($sortfield)) {
1845 $sql .= $this->
db->order($sortfield, $sortorder);
1847 if (!empty($limit)) {
1848 $sql .= $this->
db->plimit($limit, $offset);
1851 $resql = $this->
db->query(
$sql);
1853 $num = $this->
db->num_rows($resql);
1855 while ($obj = $this->
db->fetch_object($resql)) {
1856 $record =
new self($this->db);
1857 $record->setVarsFromFetchObj($obj);
1859 $records[$record->id] = $record;
1861 $this->
db->free($resql);
1865 $this->errors[] =
'Error '.$this->db->lasterror();
1866 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1881 if ($this->efficiency < 0 || $this->efficiency > 1) {
1882 $this->efficiency = 1;
1895 public function delete(
User $user, $notrigger =
false)
1911 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1913 global $db, $conf, $langs, $hookmanager;
1915 if (!empty($conf->dol_no_mouse_hover)) {
1921 $label =
'<u>'.$langs->trans(
"BillOfMaterialsLine").
'</u>';
1923 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1925 $url = DOL_URL_ROOT.
'/bom/bomline_card.php?id='.$this->id;
1927 if ($option !=
'nolink') {
1929 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1930 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1931 $add_save_lastsearch_values = 1;
1933 if ($add_save_lastsearch_values) {
1934 $url .=
'&save_lastsearch_values=1';
1939 if (empty($notooltip)) {
1940 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1941 $label = $langs->trans(
"ShowBillOfMaterialsLine");
1942 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1944 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1945 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1947 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1950 $linkstart =
'<a href="'.$url.
'"';
1951 $linkstart .= $linkclose.
'>';
1954 $result .= $linkstart;
1956 $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);
1958 if ($withpicto != 2) {
1959 $result .= $this->ref;
1961 $result .= $linkend;
1964 global $action, $hookmanager;
1965 $hookmanager->initHooks(array(
'bomlinedao'));
1966 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1967 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1969 $result = $hookmanager->resPrint;
1971 $result .= $hookmanager->resPrint;
1985 return $this->
LibStatut($this->status, $mode);
2010 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
2011 $sql .=
' fk_user_creat, fk_user_modif';
2012 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
2013 $sql .=
' WHERE t.rowid = '.((int) $id);
2014 $result = $this->
db->query(
$sql);
2016 if ($this->
db->num_rows($result)) {
2017 $obj = $this->
db->fetch_object($result);
2018 $this->
id = $obj->rowid;
2019 $this->user_creation_id = $obj->fk_user_creat;
2020 $this->user_modification_id = $obj->fk_user_modif;
2021 $this->date_creation = $this->
db->jdate($obj->datec);
2022 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
2024 $this->
db->free($result);