27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
38 public $element =
'mo';
43 public $table_element =
'mrp_mo';
48 public $ismultientitymanaged = 1;
53 public $isextrafieldmanaged = 1;
58 public $picto =
'mrp';
61 const STATUS_DRAFT = 0;
62 const STATUS_VALIDATED = 1;
63 const STATUS_INPROGRESS = 2;
64 const STATUS_PRODUCED = 3;
65 const STATUS_CANCELED = 9;
97 public $fields = array(
98 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
99 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'position'=>5,
'notnull'=>1,
'default'=>
'1',
'index'=>1),
100 'ref' => array(
'type'=>
'varchar(128)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>4,
'position'=>10,
'notnull'=>1,
'default'=>
'(PROV)',
'index'=>1,
'searchall'=>1,
'comment'=>
"Reference of object",
'showoncombobox'=>
'1',
'noteditable'=>1),
101 'fk_bom' => array(
'type'=>
'integer:Bom:bom/class/bom.class.php:0:(t.status:=:1)',
'filter'=>
'active=1',
'label'=>
'BOM',
'enabled'=>
'$conf->bom->enabled',
'visible'=>1,
'position'=>33,
'notnull'=>-1,
'index'=>1,
'comment'=>
"Original BOM",
'css'=>
'minwidth100 maxwidth500',
'csslist'=>
'tdoverflowmax150',
'picto'=>
'bom'),
102 'mrptype' => array(
'type'=>
'integer',
'label'=>
'Type',
'enabled'=>1,
'visible'=>1,
'position'=>34,
'notnull'=>1,
'default'=>
'0',
'arrayofkeyval'=>array(0=>
'Manufacturing', 1=>
'Disassemble'),
'css'=>
'minwidth150',
'csslist'=>
'minwidth150 center'),
103 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php:0',
'label'=>
'Product',
'enabled'=>
'isModEnabled("product")',
'visible'=>1,
'position'=>35,
'notnull'=>1,
'index'=>1,
'comment'=>
"Product to produce",
'css'=>
'maxwidth300',
'csslist'=>
'tdoverflowmax100',
'picto'=>
'product'),
104 'qty' => array(
'type'=>
'real',
'label'=>
'QtyToProduce',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'notnull'=>1,
'comment'=>
"Qty to produce",
'css'=>
'width75',
'default'=>1,
'isameasure'=>1),
105 'label' => array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'position'=>42,
'notnull'=>-1,
'searchall'=>1,
'showoncombobox'=>
'2',
'css'=>
'maxwidth300',
'csslist'=>
'tdoverflowmax200',
'alwayseditable'=>1),
106 'fk_soc' => array(
'type'=>
'integer:Societe:societe/class/societe.class.php:1',
'label'=>
'ThirdParty',
'picto'=>
'company',
'enabled'=>
'isModEnabled("societe")',
'visible'=>-1,
'position'=>50,
'notnull'=>-1,
'index'=>1,
'css'=>
'maxwidth400',
'csslist'=>
'tdoverflowmax150'),
107 'fk_project' => array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'picto'=>
'project',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>51,
'notnull'=>-1,
'index'=>1,
'css'=>
'minwidth200 maxwidth400',
'csslist'=>
'tdoverflowmax100'),
108 'fk_warehouse' => array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php:0',
'label'=>
'WarehouseForProduction',
'picto'=>
'stock',
'enabled'=>
'$conf->stock->enabled',
'visible'=>1,
'position'=>52,
'css'=>
'maxwidth400',
'csslist'=>
'tdoverflowmax200'),
109 'note_public' => array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61,
'notnull'=>-1,),
110 'note_private' => array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62,
'notnull'=>-1,),
111 'date_creation' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>500,
'notnull'=>1,),
112 'tms' => array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'position'=>501,
'notnull'=>1,),
113 'date_valid' => array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-2,
'position'=>502,),
114 'fk_user_creat' => array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'position'=>510,
'notnull'=>1,
'foreignkey'=>
'user.rowid',
'csslist'=>
'tdoverflowmax100'),
115 'fk_user_modif' => array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'position'=>511,
'notnull'=>-1,
'csslist'=>
'tdoverflowmax100'),
116 'date_start_planned' => array(
'type'=>
'datetime',
'label'=>
'DateStartPlannedMo',
'enabled'=>1,
'visible'=>1,
'position'=>55,
'notnull'=>-1,
'index'=>1,
'help'=>
'KeepEmptyForAsap',
'alwayseditable'=>1),
117 'date_end_planned' => array(
'type'=>
'datetime',
'label'=>
'DateEndPlannedMo',
'enabled'=>1,
'visible'=>1,
'position'=>56,
'notnull'=>-1,
'index'=>1,
'alwayseditable'=>1),
118 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
119 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>1010),
120 'status' => array(
'type'=>
'integer',
'label'=>
'Status',
'enabled'=>1,
'visible'=>2,
'position'=>1000,
'default'=>0,
'notnull'=>1,
'index'=>1,
'arrayofkeyval'=>array(
'0'=>
'Draft',
'1'=>
'Validated',
'2'=>
'InProgress',
'3'=>
'StatusMOProduced',
'9'=>
'Canceled')),
121 'fk_parent_line' => array(
'type'=>
'integer:MoLine:mrp/class/mo.class.php',
'label'=>
'ParentMo',
'enabled'=>1,
'visible'=>0,
'position'=>1020,
'default'=>0,
'notnull'=>0,
'index'=>1,
'showoncombobox'=>0),
133 public $fk_warehouse;
145 public $note_private;
150 public $date_creation;
154 public $fk_user_creat;
155 public $fk_user_modif;
172 public $date_start_planned;
177 public $date_end_planned;
200 public $table_element_line =
'mrp_production';
205 public $fk_element =
'fk_mo';
210 public $class_element_line =
'MoLine';
215 protected $childtables = array();
220 protected $childtablesoncascade = array(
'mrp_production');
225 public $lines = array();
230 public $line = array();
235 public $fk_parent_line;
240 public $tpl = array();
249 global $conf, $langs;
253 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
254 $this->fields[
'rowid'][
'visible'] = 0;
256 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
257 $this->fields[
'entity'][
'enabled'] = 0;
261 foreach ($this->fields as $key => $val) {
262 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
263 unset($this->fields[$key]);
268 foreach ($this->fields as $key => $val) {
269 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
270 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
271 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
291 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
292 $tmpproduct =
new Product($this->db);
293 $tmpproduct->fetch($this->fk_product);
294 if ($tmpproduct->hasFatherOrChild(1) > 0) {
295 $this->error =
'ErrorAVirtualProductCantBeUsedIntoABomOrMo';
296 $this->errors[] = $this->error;
303 if ($this->fk_bom > 0) {
305 include_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
306 $tmpbom =
new BOM($this->db);
307 $tmpbom->fetch($this->fk_bom);
309 $this->mrptype = $tmpbom->bomtype;
314 if ($idcreated <= 0) {
329 $this->db->rollback();
344 global $langs, $extrafields;
349 $object =
new self($this->db);
354 $result = $object->fetchCommon($fromid);
355 if ($result > 0 && !empty($object->table_element_line)) {
356 $object->fetchLines();
365 unset($object->fk_user_creat);
366 unset($object->import_key);
369 $object->ref = empty($this->fields[
'ref'][
'default']) ?
"copy_of_".$object->ref : $this->fields[
'ref'][
'default'];
370 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
371 $object->status = self::STATUS_DRAFT;
374 if (is_array($object->array_options) && count($object->array_options) > 0) {
375 $extrafields->fetch_name_optionals_label($this->table_element);
376 foreach ($object->array_options as $key => $option) {
377 $shortkey = preg_replace(
'/options_/',
'', $key);
378 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
381 unset($object->array_options[$key]);
387 $object->context[
'createfromclone'] =
'createfromclone';
388 $result = $object->createCommon($user);
391 $this->error = $object->error;
392 $this->errors = $object->errors;
404 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
411 unset($object->context[
'createfromclone']);
418 $this->db->rollback();
430 public function fetch($id, $ref =
null)
433 if ($result > 0 && !empty($this->table_element_line)) {
437 $this->socid = $this->fk_soc;
449 $this->lines = array();
467 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
477 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
478 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
479 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
481 $sql .=
' WHERE 1 = 1';
485 if (count($filter) > 0) {
486 foreach ($filter as $key => $value) {
487 if ($key ==
't.rowid') {
488 $sqlwhere[] = $key.
" = ".((int) $value);
489 } elseif (strpos($key,
'date') !==
false) {
490 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
491 } elseif ($key ==
'customsql') {
492 $sqlwhere[] = $value;
494 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
498 if (count($sqlwhere) > 0) {
499 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
502 if (!empty($sortfield)) {
503 $sql .= $this->db->order($sortfield, $sortorder);
505 if (!empty($limit)) {
506 $sql .= $this->db->plimit($limit, $offset);
509 $resql = $this->db->query(
$sql);
511 $num = $this->db->num_rows($resql);
513 while ($i < min($limit, $num)) {
514 $obj = $this->db->fetch_object($resql);
516 $record =
new self($this->db);
517 $record->setVarsFromFetchObj($obj);
519 $records[$record->id] = $record;
523 $this->db->free($resql);
527 $this->errors[] =
'Error '.$this->db->lasterror();
528 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
544 $mostatic =
new MoLine($this->db);
547 $sql .= $mostatic->getFieldList();
548 $sql .=
' FROM '.MAIN_DB_PREFIX.$mostatic->table_element.
' as t';
549 $sql .=
" WHERE t.role = '".$this->db->escape($role).
"'";
551 $sql .=
' AND t.fk_mrp_production = '.((int) $lineid);
553 $sql .=
'AND t.fk_mo = '.((int) $this->
id);
556 $resql = $this->db->query(
$sql);
558 $num = $this->db->num_rows($resql);
562 $obj = $this->db->fetch_object($resql);
565 'rowid'=> $obj->rowid,
566 'date'=> $this->db->jdate($obj->date_creation),
568 'role' => $obj->role,
569 'fk_product' => $obj->fk_product,
570 'fk_warehouse' => $obj->fk_warehouse,
571 'batch' => $obj->batch,
572 'fk_stock_movement' => $obj->fk_stock_movement
581 $this->error = $this->db->lasterror();
596 $sql =
'SELECT COUNT(rowid) as nb FROM '.MAIN_DB_PREFIX.
'stock_mouvement as sm';
597 $sql .=
" WHERE sm.origintype = 'mo' and sm.fk_origin = ".((int) $this->
id);
599 $resql = $this->db->query(
$sql);
601 $num = $this->db->num_rows($resql);
605 $obj = $this->db->fetch_object($resql);
613 $this->error = $this->db->lasterror();
650 $this->db->rollback();
668 if ($this->status != self::STATUS_DRAFT) {
676 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'mrp_production WHERE fk_mo = '.((int) $this->
id);
677 $this->db->query(
$sql);
679 $moline =
new MoLine($this->db);
682 $moline->fk_mo = $this->id;
683 $moline->qty = $this->qty;
684 $moline->fk_product = $this->fk_product;
685 $moline->position = 1;
687 if ($this->fk_bom > 0) {
688 include_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
689 $bom =
new Bom($this->db);
690 $bom->fetch($this->fk_bom);
691 if ($bom->bomtype == 1) {
693 $moline->role =
'toconsume';
696 $moline->role =
'toproduce';
699 if ($this->mrptype == 1) {
700 $moline->role =
'toconsume';
702 $moline->role =
'toproduce';
706 $resultline = $moline->create($user,
false);
707 if ($resultline <= 0) {
709 $this->error = $moline->error;
710 $this->errors = $moline->errors;
714 if ($this->fk_bom > 0) {
718 foreach ($bom->lines as $line) {
719 $moline =
new MoLine($this->db);
721 $moline->fk_mo = $this->id;
722 $moline->origin_id = $line->id;
723 $moline->origin_type =
'bomline';
724 if ($line->qty_frozen) {
725 $moline->qty = $line->qty;
727 $moline->qty =
price2num(($line->qty / ( !empty($bom->qty) ? $bom->qty : 1 ) ) * $this->qty / ( !empty($line->efficiency) ? $line->efficiency : 1 ),
'MS');
729 if ($moline->qty <= 0) {
731 $this->error =
"BadValueForquantityToConsume";
734 $moline->fk_product = $line->fk_product;
735 $moline->role = $role;
736 $moline->position = $line->position;
737 $moline->qty_frozen = $line->qty_frozen;
738 $moline->disable_stock_change = $line->disable_stock_change;
739 if (!empty($line->fk_default_workstation)) $moline->fk_default_workstation = $line->fk_default_workstation;
741 $resultline = $moline->create($user,
false);
742 if ($resultline <= 0) {
744 $this->error = $moline->error;
745 $this->errors = $moline->errors;
760 $this->db->rollback();
776 if ($this->status != self::STATUS_DRAFT)
return 1;
780 $oldQty = $this->oldQty;
781 $newQty = $this->qty;
782 if ($newQty != $oldQty && !empty($this->oldQty)) {
783 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"mrp_production WHERE fk_mo = " . (int) $this->
id;
784 $resql = $this->db->query(
$sql);
786 while ($obj = $this->db->fetch_object($resql)) {
787 $moLine =
new MoLine($this->db);
788 $res = $moLine->fetch($obj->rowid);
791 if ($moLine->role ==
'toconsume' || $moLine->role ==
'toproduce') {
792 if (empty($moLine->qty_frozen)) {
793 $qty = $newQty * $moLine->qty / $oldQty;
794 $moLine->qty =
price2num($qty * (!empty($line->efficiency) ? $line->efficiency : 1 ),
'MS');
795 $res = $moLine->update($user);
807 $this->db->rollback();
820 public function delete(
User $user, $notrigger =
false)
837 $langs->load(
'stocks');
839 if ($this->status < 0) {
840 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
844 $productstatic =
new Product($this->db);
845 $fk_movement =
GETPOST(
'fk_movement',
'int');
848 if (!empty($arrayoflines)) {
852 $stockmove->setOrigin($this->element, $this->
id);
854 if (!empty($fk_movement)) {
855 $moline =
new MoLine($this->db);
856 $TArrayMoLine = $moline->fetchAll(
'',
'', 1, 0, array(
'customsql' =>
'fk_stock_movement ='.$fk_movement));
857 $moline = array_shift($TArrayMoLine);
860 $movement->fetch($fk_movement);
861 $productstatic->fetch($movement->product_id);
862 $qtytoprocess = $movement->qty;
865 $labelmovementCancel = $langs->trans(
"CancelProductionForRef", $productstatic->ref);
866 $codemovementCancel = $langs->trans(
"StockIncrease");
868 if (($qtytoprocess >= 0)) {
869 $idstockmove = $stockmove->reception($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel,
'',
'', $movement->batch,
dol_now(), 0, $codemovementCancel);
871 $idstockmove = $stockmove->livraison($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel,
dol_now(),
'',
'', $movement->batch, 0, $codemovementCancel);
873 if ($idstockmove < 0) {
875 $this->db->rollback();
880 return $moline->delete($user, $notrigger);
882 foreach ($arrayoflines as $key => $arrayofline) {
883 $lineDetails = $arrayoflines[$key];
884 $productstatic->fetch($lineDetails[
'fk_product']);
885 $qtytoprocess = $lineDetails[
'qty'];
888 $labelmovementCancel = $langs->trans(
"CancelProductionForRef", $productstatic->ref);
889 $codemovementCancel = $langs->trans(
"StockIncrease");
891 if ($qtytoprocess >= 0) {
892 $idstockmove = $stockmove->reception($user, $lineDetails[
'fk_product'], $lineDetails[
'fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel,
'',
'', $lineDetails[
'batch'],
dol_now(), 0, $codemovementCancel);
894 $idstockmove = $stockmove->livraison($user, $lineDetails[
'fk_product'], $lineDetails[
'fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel,
dol_now(),
'',
'', $lineDetails[
'batch'], 0, $codemovementCancel);
896 if ($idstockmove < 0) {
898 $this->db->rollback();
921 global $langs, $conf;
924 if (!empty($conf->global->MRP_MO_ADDON)) {
927 $file = $conf->global->MRP_MO_ADDON.
".php";
928 $classname = $conf->global->MRP_MO_ADDON;
931 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
932 foreach ($dirmodels as $reldir) {
936 $mybool |= @include_once $dir.$file;
939 if ($mybool ===
false) {
944 $obj =
new $classname();
945 $numref = $obj->getNextValue($prod, $this);
950 $this->error = $obj->error;
955 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_MRP_MO_ADDON_NotDefined");
969 global $conf, $langs;
971 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
976 if ($this->status == self::STATUS_VALIDATED) {
977 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
994 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
1000 $this->newref = $num;
1003 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1004 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
1005 $sql .=
" status = ".self::STATUS_VALIDATED.
",";
1006 $sql .=
" date_valid='".$this->db->idate($now).
"',";
1007 $sql .=
" fk_user_valid = ".$user->id;
1008 $sql .=
" WHERE rowid = ".((int) $this->
id);
1010 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
1011 $resql = $this->db->query(
$sql);
1014 $this->error = $this->db->lasterror();
1018 if (!$error && !$notrigger) {
1020 $result = $this->
call_trigger(
'MRP_MO_VALIDATE', $user);
1028 $this->oldref = $this->ref;
1031 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1033 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'mrp/".$this->db->escape($this->newref).
"'";
1034 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'mrp/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
1035 $resql = $this->db->query(
$sql);
1037 $error++; $this->error = $this->db->lasterror();
1043 $dirsource = $conf->mrp->dir_output.
'/'.$oldref;
1044 $dirdest = $conf->mrp->dir_output.
'/'.$newref;
1045 if (!$error && file_exists($dirsource)) {
1046 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
1048 if (@rename($dirsource, $dirdest)) {
1051 $listoffiles =
dol_dir_list($conf->mrp->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
1052 foreach ($listoffiles as $fileentry) {
1053 $dirsource = $fileentry[
'name'];
1054 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
1055 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
1056 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
1057 @rename($dirsource, $dirdest);
1067 $this->status = self::STATUS_VALIDATED;
1071 $this->db->commit();
1074 $this->db->rollback();
1089 if ($this->status <= self::STATUS_DRAFT) {
1100 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'MRP_MO_UNVALIDATE');
1110 public function cancel($user, $notrigger = 0)
1113 if ($this->status != self::STATUS_VALIDATED && $this->status != self::STATUS_INPROGRESS) {
1124 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'MRP_MO_CANCEL');
1134 public function reopen($user, $notrigger = 0)
1137 if ($this->status != self::STATUS_PRODUCED && $this->status != self::STATUS_CANCELED) {
1148 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'MRP_MO_REOPEN');
1160 global $conf, $langs;
1162 $langs->loadLangs([
'mrp',
'products']);
1163 $nofetch = isset($params[
'nofetch']) ? true :
false;
1167 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ManufacturingOrder").
'</u>';
1168 if (isset($this->status)) {
1169 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
1171 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1172 if (isset($this->label)) {
1173 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
1175 if (isset($this->mrptype)) {
1176 $datas[
'type'] =
'<br><b>'.$langs->trans(
'Type').
':</b> '.$this->fields[
'mrptype'][
'arrayofkeyval'][$this->mrptype];
1178 if (isset($this->qty)) {
1179 $datas[
'qty'] =
'<br><b>'.$langs->trans(
'QtyToProduce').
':</b> '.$this->qty;
1181 if (!$nofetch && isset($this->fk_product)) {
1182 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
1183 $product =
new Product($this->db);
1184 $product->fetch($this->fk_product);
1185 $datas[
'product'] =
'<br><b>'.$langs->trans(
'Product').
':</b> '.$product->getNomUrl(1,
'', 0, -1, 1);
1187 if (!$nofetch && isset($this->fk_warehouse)) {
1188 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
1189 $warehouse =
new Entrepot($this->db);
1190 $warehouse->fetch($this->fk_warehouse);
1191 $datas[
'warehouse'] =
'<br><b>'.$langs->trans(
'WarehouseForProduction').
':</b> '.$warehouse->getNomUrl(1,
'', 0, 1);
1207 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1209 global $conf, $langs, $hookmanager;
1211 if (!empty($conf->dol_no_mouse_hover)) {
1218 'objecttype' => $this->element,
1219 'option' => $option,
1222 $classfortooltip =
'classfortooltip';
1225 $classfortooltip =
'classforajaxtooltip';
1226 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1232 $url = DOL_URL_ROOT.
'/mrp/mo_card.php?id='.$this->id;
1233 if ($option ==
'production') {
1234 $url = DOL_URL_ROOT.
'/mrp/mo_production.php?id='.$this->id;
1237 if ($option !=
'nolink') {
1239 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1240 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1241 $add_save_lastsearch_values = 1;
1243 if ($add_save_lastsearch_values) {
1244 $url .=
'&save_lastsearch_values=1';
1249 if (empty($notooltip)) {
1250 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1251 $label = $langs->trans(
"ShowMo");
1252 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1254 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1255 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1257 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1260 $linkstart =
'<a href="'.$url.
'"';
1261 $linkstart .= $linkclose.
'>';
1264 $result .= $linkstart;
1266 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1268 if ($withpicto != 2) {
1269 $result .= $this->ref;
1271 $result .= $linkend;
1274 global $action, $hookmanager;
1275 $hookmanager->initHooks(array(
'modao'));
1276 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1277 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1279 $result = $hookmanager->resPrint;
1281 $result .= $hookmanager->resPrint;
1295 return $this->
LibStatut($this->status, $mode);
1309 if (empty($this->labelStatus)) {
1312 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1313 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ValidatedToProduce');
1314 $this->labelStatus[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv(
'InProgress');
1315 $this->labelStatus[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv(
'StatusMOProduced');
1316 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Canceled');
1318 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1319 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Validated');
1320 $this->labelStatusShort[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv(
'InProgress');
1321 $this->labelStatusShort[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv(
'StatusMOProduced');
1322 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Canceled');
1325 $statusType =
'status'.$status;
1326 if ($status == self::STATUS_VALIDATED) {
1327 $statusType =
'status1';
1329 if ($status == self::STATUS_INPROGRESS) {
1330 $statusType =
'status4';
1332 if ($status == self::STATUS_PRODUCED) {
1333 $statusType =
'status6';
1335 if ($status == self::STATUS_CANCELED) {
1336 $statusType =
'status9';
1339 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1350 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
1351 $sql .=
' fk_user_creat, fk_user_modif';
1352 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1353 $sql .=
' WHERE t.rowid = '.((int) $id);
1354 $result = $this->db->query(
$sql);
1356 if ($this->db->num_rows($result)) {
1357 $obj = $this->db->fetch_object($result);
1358 $this->
id = $obj->rowid;
1360 $this->user_creation_id = $obj->fk_user_creat;
1361 $this->user_modification_id = $obj->fk_user_modif;
1362 $this->date_creation = $this->db->jdate($obj->datec);
1363 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
1366 $this->db->free($result);
1382 $this->lines = array();
1393 $this->lines = array();
1395 $objectline =
new MoLine($this->db);
1397 $TFilters = array(
'customsql'=>
'fk_mo = '.((
int) $this->
id));
1398 if (!empty($rolefilter)) $TFilters[
'role'] = $rolefilter;
1399 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, $TFilters);
1401 if (is_numeric($result)) {
1402 $this->error = $objectline->error;
1403 $this->errors = $objectline->errors;
1406 $this->lines = $result;
1407 return $this->lines;
1422 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1424 global $conf, $langs;
1426 $langs->load(
"mrp");
1432 if ($this->model_pdf) {
1433 $modele = $this->model_pdf;
1434 } elseif (!empty($conf->global->MO_ADDON_PDF)) {
1435 $modele = $conf->global->MO_ADDON_PDF;
1439 $modelpath =
"core/modules/mrp/doc/";
1441 if (empty($modele)) {
1445 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1457 global $conf, $langs;
1473 $this->db->commit();
1490 global $langs, $hookmanager, $conf,
$form;
1492 $langs->load(
'stocks');
1493 $text_stock_options = $langs->trans(
"RealStockDesc").
'<br>';
1494 $text_stock_options .= $langs->trans(
"RealStockWillAutomaticallyWhen").
'<br>';
1495 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ?
'- '.$langs->trans(
"DeStockOnShipment").
'<br>' :
'');
1496 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ?
'- '.$langs->trans(
"DeStockOnValidateOrder").
'<br>' :
'');
1497 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_BILL) ?
'- '.$langs->trans(
"DeStockOnBill").
'<br>' :
'');
1498 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ?
'- '.$langs->trans(
"ReStockOnBill").
'<br>' :
'');
1499 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ?
'- '.$langs->trans(
"ReStockOnValidateOrder").
'<br>' :
'');
1500 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ?
'- '.$langs->trans(
"ReStockOnDispatchOrder").
'<br>' :
'');
1501 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE) ?
'- '.$langs->trans(
"StockOnReception").
'<br>' :
'');
1503 print
'<tr class="liste_titre">';
1505 print
'<td class="linecoldescription">'.$langs->trans(
'Ref');
1506 if (!empty($conf->global->BOM_SUB_BOM)) {
1507 print
' <a id="show_all" href="#">'.img_picto(
'',
'folder-open',
'class="paddingright"').$langs->trans(
"ExpandAll").
'</a> ';
1508 print
'<a id="hide_all" href="#">'.img_picto(
'',
'folder',
'class="paddingright"').$langs->trans(
"UndoExpandAll").
'</a> ';
1512 print
'<td class="right">'.$langs->trans(
'Qty');
1513 if ($this->bom->bomtype == 0) {
1514 print
' <span class="opacitymedium">('.$langs->trans(
"ForAQuantityOf", $this->bom->qty).
')</span>';
1516 print
' <span class="opacitymedium">('.$langs->trans(
"ForAQuantityToConsumeOf", $this->bom->qty).
')</span>';
1519 print
'<td class="center">'.$form->textwithpicto($langs->trans(
"PhysicalStock"), $text_stock_options, 1).
'</td>';
1520 print
'<td class="center">'.$form->textwithpicto($langs->trans(
"VirtualStock"), $langs->trans(
"VirtualStockDesc")).
'</td>';
1521 print
'<td class="center">'.$langs->trans(
'QtyFrozen').
'</td>';
1522 print
'<td class="center">'.$langs->trans(
'DisableStockChange').
'</td>';
1523 print
'<td class="center">'.$langs->trans(
'MoChildGenerate').
'</td>';
1529 if (!empty($this->lines)) {
1530 foreach ($this->lines as $line) {
1532 if (is_object($hookmanager)) {
1533 $parameters = array(
'line'=>$line,
'i'=>$i,
'restrictlist'=>$restrictlist,
'selectedLines'=> $selectedLines);
1534 if (!empty($line->fk_parent_line)) { $parameters[
'fk_parent_line'] = $line->fk_parent_line; }
1535 $reshook = $hookmanager->executeHooks(
'printOriginObjectLine', $parameters, $this, $action);
1537 if (empty($reshook)) {
1538 $this->
printOriginLine($line,
'', $restrictlist,
'/core/tpl', $selectedLines);
1560 public function printOriginLine($line, $var, $restrictlist =
'', $defaulttpldir =
'/core/tpl', $selectedLines = array())
1562 global $langs, $conf;
1564 $this->tpl[
'id'] = $line->id;
1566 $this->tpl[
'label'] =
'';
1567 if (!empty($line->fk_product)) {
1568 $productstatic =
new Product($this->db);
1569 $productstatic->fetch($line->fk_product);
1570 $productstatic->load_virtual_stock();
1571 $this->tpl[
'label'] .= $productstatic->getNomUrl(1);
1578 $this->tpl[
'qty_bom'] = 1;
1579 if (is_object($this->bom) && $this->bom->qty > 1) {
1580 $this->tpl[
'qty_bom'] = $this->bom->qty;
1583 $this->tpl[
'stock'] = $productstatic->stock_reel;
1584 $this->tpl[
'seuil_stock_alerte'] = $productstatic->seuil_stock_alerte;
1585 $this->tpl[
'virtual_stock'] = $productstatic->stock_theorique;
1586 $this->tpl[
'qty'] = $line->qty;
1587 $this->tpl[
'qty_frozen'] = $line->qty_frozen;
1588 $this->tpl[
'disable_stock_change'] = $line->disable_stock_change;
1589 $this->tpl[
'efficiency'] = $line->efficiency;
1591 $tpl = DOL_DOCUMENT_ROOT.
'/mrp/tpl/originproductline.tpl.php';
1592 $res = include $tpl;
1605 $tables = array(
'mrp_mo');
1619 $TMoChilds = array();
1622 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"mrp_mo as mo_child";
1623 $sql.=
" WHERE fk_parent_line IN ";
1624 $sql.=
" (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mrp_production as line_parent";
1625 $sql.=
" WHERE fk_mo=".((int) $this->
id).
")";
1627 $resql = $this->db->query(
$sql);
1630 if ($this->db->num_rows($resql) > 0) {
1631 while ($obj = $this->db->fetch_object($resql)) {
1632 $MoChild =
new Mo($this->db);
1633 $res = $MoChild->fetch($obj->rowid);
1634 if ($res > 0) $TMoChilds[$MoChild->id] = $MoChild;
1656 $MoParent =
new Mo($this->db);
1659 $sql =
"SELECT lineparent.fk_mo as id_moparent FROM ".MAIN_DB_PREFIX.
"mrp_mo as mo";
1660 $sql.=
" LEFT JOIN ".MAIN_DB_PREFIX.
"mrp_production lineparent ON mo.fk_parent_line = lineparent.rowid";
1661 $sql.=
" WHERE mo.rowid = ".((int) $this->
id);
1663 $resql = $this->db->query(
$sql);
1666 if ($this->db->num_rows($resql) > 0) {
1667 $obj = $this->db->fetch_object($resql);
1668 $res = $MoParent->fetch($obj->id_moparent);
1669 if ($res < 0) $error++;
1695 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
1697 $return =
'<div class="box-flex-item box-flex-grow-zero">';
1698 $return .=
'<div class="info-box info-box-sm">';
1699 $return .=
'<span class="info-box-icon bg-infobox-action">';
1702 $return .=
'</span>';
1703 $return .=
'<div class="info-box-content">';
1704 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
1705 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
1706 if (property_exists($this,
'fk_bom')) {
1707 $return .=
'<br><span class="info-box-label opacitymedium">'.$this->fk_bom.
'</span>';
1709 if (property_exists($this,
'qty')) {
1710 $return .=
'<br><span class="info-box-label">'.$langs->trans(
'Quantity').
' : '.$this->qty.
'</span>';
1712 if (method_exists($this,
'getLibStatut')) {
1713 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).
'</div>';
1715 $return .=
'</div>';
1716 $return .=
'</div>';
1717 $return .=
'</div>';
1730 public $element =
'mrp_production';
1735 public $table_element =
'mrp_production';
1740 public $ismultientitymanaged = 0;
1745 public $isextrafieldmanaged = 0;
1747 public $fields = array(
1748 'rowid' =>array(
'type'=>
'integer',
'label'=>
'ID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
1749 'fk_mo' =>array(
'type'=>
'integer',
'label'=>
'Mo',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>15),
1750 'origin_id' =>array(
'type'=>
'integer',
'label'=>
'Origin',
'enabled'=>1,
'visible'=>-1,
'notnull'=>0,
'position'=>17),
1751 'origin_type' =>array(
'type'=>
'varchar(10)',
'label'=>
'Origin type',
'enabled'=>1,
'visible'=>-1,
'notnull'=>0,
'position'=>18),
1752 'position' =>array(
'type'=>
'integer',
'label'=>
'Position',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>20),
1753 'fk_product' =>array(
'type'=>
'integer',
'label'=>
'Product',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>25),
1754 'fk_warehouse' =>array(
'type'=>
'integer',
'label'=>
'Warehouse',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
1755 'qty' =>array(
'type'=>
'real',
'label'=>
'Qty',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
1756 'qty_frozen' => array(
'type'=>
'smallint',
'label'=>
'QuantityFrozen',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>105,
'css'=>
'maxwidth50imp',
'help'=>
'QuantityConsumedInvariable'),
1757 'disable_stock_change' => array(
'type'=>
'smallint',
'label'=>
'DisableStockChange',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>108,
'css'=>
'maxwidth50imp',
'help'=>
'DisableStockChangeHelp'),
1758 'batch' =>array(
'type'=>
'varchar(30)',
'label'=>
'Batch',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
1759 'role' =>array(
'type'=>
'varchar(10)',
'label'=>
'Role',
'enabled'=>1,
'visible'=>-1,
'position'=>145),
1760 'fk_mrp_production' =>array(
'type'=>
'integer',
'label'=>
'Fk mrp production',
'enabled'=>1,
'visible'=>-1,
'position'=>150),
1761 'fk_stock_movement' =>array(
'type'=>
'integer',
'label'=>
'StockMovement',
'enabled'=>1,
'visible'=>-1,
'position'=>155),
1762 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>160),
1763 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'Tms',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>165),
1764 'fk_user_creat' =>array(
'type'=>
'integer',
'label'=>
'UserCreation',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>170),
1765 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModification',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
1766 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
1767 'fk_default_workstation' =>array(
'type'=>
'integer',
'label'=>
'DefaultWorkstation',
'enabled'=>1,
'visible'=>1,
'notnull'=>0,
'position'=>185)
1773 public $origin_type;
1776 public $fk_warehouse;
1779 public $disable_stock_change;
1783 public $fk_mrp_production;
1784 public $fk_stock_movement;
1785 public $date_creation;
1787 public $fk_user_creat;
1788 public $fk_user_modif;
1790 public $fk_parent_line;
1795 public $fk_default_workstation;
1804 global $conf, $langs;
1808 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
1809 $this->fields[
'rowid'][
'visible'] = 0;
1811 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
1812 $this->fields[
'entity'][
'enabled'] = 0;
1816 foreach ($this->fields as $key => $val) {
1817 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
1818 unset($this->fields[$key]);
1823 if (is_object($langs)) {
1824 foreach ($this->fields as $key => $val) {
1825 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
1826 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
1827 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
1843 if (empty($this->qty)) {
1844 $this->error =
'BadValueForQty';
1875 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
1885 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1886 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
1887 $sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
1889 $sql .=
' WHERE 1 = 1';
1892 $sqlwhere = array();
1893 if (count($filter) > 0) {
1894 foreach ($filter as $key => $value) {
1895 if ($key ==
't.rowid') {
1896 $sqlwhere[] = $key.
" = ".((int) $value);
1897 } elseif (strpos($key,
'date') !==
false) {
1898 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
1899 } elseif ($key ==
'customsql') {
1900 $sqlwhere[] = $value;
1902 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
1906 if (count($sqlwhere) > 0) {
1907 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
1910 if (!empty($sortfield)) {
1911 $sql .= $this->db->order($sortfield, $sortorder);
1913 if (!empty($limit)) {
1914 $sql .= $this->db->plimit($limit, $offset);
1917 $resql = $this->db->query(
$sql);
1919 $num = $this->db->num_rows($resql);
1921 while ($i < ($limit ? min($limit, $num) : $num)) {
1922 $obj = $this->db->fetch_object($resql);
1924 $record =
new self($this->db);
1925 $record->setVarsFromFetchObj($obj);
1927 $records[$record->id] = $record;
1931 $this->db->free($resql);
1935 $this->errors[] =
'Error '.$this->db->lasterror();
1936 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1961 public function delete(
User $user, $notrigger =
false)