27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
40 public $element =
'mo';
45 public $table_element =
'mrp_mo';
50 public $ismultientitymanaged = 1;
55 public $isextrafieldmanaged = 1;
60 public $picto =
'mrp';
63 const STATUS_DRAFT = 0;
64 const STATUS_VALIDATED = 1;
65 const STATUS_INPROGRESS = 2;
66 const STATUS_PRODUCED = 3;
67 const STATUS_CANCELED = 9;
100 public $fields = array(
101 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'position'=>1,
'notnull'=>1,
'index'=>1,
'comment'=>
"Id",),
102 'entity' => array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'position'=>5,
'notnull'=>1,
'default'=>
'1',
'index'=>1),
103 '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),
104 '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 maxwidth300',
'csslist'=>
'nowraponall',
'picto'=>
'bom'),
105 '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'),
106 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php:0',
'label'=>
'Product',
'enabled'=>
'$conf->product->enabled',
'visible'=>1,
'position'=>35,
'notnull'=>1,
'index'=>1,
'comment'=>
"Product to produce",
'css'=>
'maxwidth300',
'csslist'=>
'tdoverflowmax100',
'picto'=>
'product'),
107 'qty' => array(
'type'=>
'real',
'label'=>
'QtyToProduce',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'notnull'=>1,
'comment'=>
"Qty to produce",
'css'=>
'width75',
'default'=>1,
'isameasure'=>1),
108 'label' => array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'position'=>42,
'notnull'=>-1,
'searchall'=>1,
'showoncombobox'=>
'2',
'css'=>
'maxwidth300',
'csslist'=>
'tdoverflowmax200'),
109 'fk_soc' => array(
'type'=>
'integer:Societe:societe/class/societe.class.php:1',
'label'=>
'ThirdParty',
'picto'=>
'company',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'position'=>50,
'notnull'=>-1,
'index'=>1,
'css'=>
'maxwidth400',
'csslist'=>
'tdoverflowmax150'),
110 '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'),
111 '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'),
112 'note_public' => array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61,
'notnull'=>-1,),
113 'note_private' => array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62,
'notnull'=>-1,),
114 'date_creation' => array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>500,
'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,),
117 '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'),
118 'fk_user_modif' => array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'position'=>511,
'notnull'=>-1,
'csslist'=>
'tdoverflowmax100'),
119 'date_start_planned' => array(
'type'=>
'datetime',
'label'=>
'DateStartPlannedMo',
'enabled'=>1,
'visible'=>1,
'position'=>55,
'notnull'=>-1,
'index'=>1,
'help'=>
'KeepEmptyForAsap'),
120 'date_end_planned' => array(
'type'=>
'datetime',
'label'=>
'DateEndPlannedMo',
'enabled'=>1,
'visible'=>1,
'position'=>56,
'notnull'=>-1,
'index'=>1,),
121 'import_key' => array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>1000,
'notnull'=>-1,),
122 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>1010),
123 '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')),
124 '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),
132 public $fk_warehouse;
143 public $note_private;
148 public $date_creation;
152 public $fk_user_creat;
153 public $fk_user_modif;
161 public $date_start_planned;
166 public $date_end_planned;
179 public $table_element_line =
'mrp_production';
184 public $fk_element =
'fk_mo';
189 public $class_element_line =
'MoLine';
194 protected $childtables = array();
199 protected $childtablesoncascade = array(
'mrp_production');
204 public $lines = array();
210 public $fk_parent_line;
220 global $conf, $langs;
224 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
225 $this->fields[
'rowid'][
'visible'] = 0;
227 if (empty($conf->multicompany->enabled) && isset($this->fields[
'entity'])) {
228 $this->fields[
'entity'][
'enabled'] = 0;
232 foreach ($this->fields as $key => $val) {
233 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
234 unset($this->fields[$key]);
239 foreach ($this->fields as $key => $val) {
240 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
241 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
242 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
265 if (empty($conf->global->ALLOW_USE_KITS_INTO_BOM_AND_MO) && $this->fk_product > 0) {
266 include_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
268 $tmpproduct->fetch($this->fk_product);
269 if ($tmpproduct->hasFatherOrChild(1) > 0) {
270 $this->error =
'ErrorAVirtualProductCantBeUsedIntoABomOrMo';
271 $this->errors[] = $this->error;
272 $this->
db->rollback();
277 if ($this->fk_bom > 0) {
279 $tmpbom =
new BOM($this->
db);
280 $tmpbom->fetch($this->fk_bom);
282 $this->mrptype = $tmpbom->bomtype;
287 if ($idcreated <= 0) {
302 $this->
db->rollback();
317 global $langs, $extrafields;
322 $object =
new self($this->db);
327 $result = $object->fetchCommon($fromid);
328 if ($result > 0 && !empty($object->table_element_line)) {
329 $object->fetchLines();
338 unset($object->fk_user_creat);
339 unset($object->import_key);
342 $object->ref = empty($this->fields[
'ref'][
'default']) ?
"copy_of_".$object->ref : $this->fields[
'ref'][
'default'];
343 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
344 $object->status = self::STATUS_DRAFT;
347 if (is_array($object->array_options) && count($object->array_options) > 0) {
348 $extrafields->fetch_name_optionals_label($this->table_element);
349 foreach ($object->array_options as $key => $option) {
350 $shortkey = preg_replace(
'/options_/',
'', $key);
351 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
354 unset($object->array_options[$key]);
360 $object->context[
'createfromclone'] =
'createfromclone';
361 $result = $object->createCommon($user);
364 $this->error = $object->error;
365 $this->errors = $object->errors;
377 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
384 unset($object->context[
'createfromclone']);
391 $this->
db->rollback();
403 public function fetch($id, $ref =
null)
406 if ($result > 0 && !empty($this->table_element_line)) {
419 $this->lines = array();
437 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
447 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
448 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
449 $sql .=
' WHERE t.entity IN ('.getEntity($this->table_element).
')';
451 $sql .=
' WHERE 1 = 1';
455 if (count($filter) > 0) {
456 foreach ($filter as $key => $value) {
457 if ($key ==
't.rowid') {
458 $sqlwhere[] = $key.
" = ".((int) $value);
459 } elseif (strpos($key,
'date') !==
false) {
460 $sqlwhere[] = $key.
" = '".$this->
db->idate($value).
"'";
461 } elseif ($key ==
'customsql') {
462 $sqlwhere[] = $value;
464 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
468 if (count($sqlwhere) > 0) {
469 $sql .=
' AND ('.implode(
' '.$this->
db->escape($filtermode).
' ', $sqlwhere).
')';
472 if (!empty($sortfield)) {
473 $sql .= $this->
db->order($sortfield, $sortorder);
475 if (!empty($limit)) {
476 $sql .= $this->
db->plimit($limit, $offset);
483 while ($i < min($limit, $num)) {
484 $obj = $this->
db->fetch_object(
$resql);
486 $record =
new self($this->db);
487 $record->setVarsFromFetchObj($obj);
489 $records[$record->id] = $record;
497 $this->errors[] =
'Error '.$this->db->lasterror();
498 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
517 $sql .= $mostatic->getFieldList();
518 $sql .=
' FROM '.MAIN_DB_PREFIX.$mostatic->table_element.
' as t';
519 $sql .=
" WHERE t.role = '".$this->db->escape($role).
"'";
521 $sql .=
' AND t.fk_mrp_production = '.((int) $lineid);
523 $sql .=
'AND t.fk_mo = '.((int) $this->
id);
532 $obj = $this->
db->fetch_object(
$resql);
535 'rowid'=> $obj->rowid,
536 'date'=> $this->db->jdate($obj->date_creation),
538 'role' => $obj->role,
539 'fk_product' => $obj->fk_product,
540 'fk_warehouse' => $obj->fk_warehouse,
541 'batch' => $obj->batch,
542 'fk_stock_movement' => $obj->fk_stock_movement
551 $this->error = $this->
db->lasterror();
566 $sql =
'SELECT COUNT(rowid) as nb FROM '.MAIN_DB_PREFIX.
'stock_mouvement as sm';
567 $sql .=
" WHERE sm.origintype = 'mo' and sm.fk_origin = ".((int) $this->
id);
575 $obj = $this->
db->fetch_object(
$resql);
583 $this->error = $this->
db->lasterror();
616 setEventMessages($langs->trans(
"RecordModifiedSuccessfully"),
null,
'mesgs');
621 $this->
db->rollback();
638 if ($this->status != self::STATUS_DRAFT) {
639 $this->error =
'BadStatus';
649 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'mrp_production WHERE fk_mo = '.((int) $this->
id);
650 $this->
db->query($sql);
655 $moline->fk_mo = $this->id;
656 $moline->qty = $this->qty;
657 $moline->fk_product = $this->fk_product;
658 $moline->position = 1;
660 if ($this->fk_bom > 0) {
661 include_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
662 $bom =
new Bom($this->
db);
663 $bom->fetch($this->fk_bom);
664 if ($bom->bomtype == 1) {
666 $moline->role =
'toconsume';
669 $moline->role =
'toproduce';
672 if ($this->mrptype == 1) {
673 $moline->role =
'toconsume';
675 $moline->role =
'toproduce';
679 $resultline = $moline->create($user,
false);
680 if ($resultline <= 0) {
682 $this->error = $moline->error;
683 $this->errors = $moline->errors;
687 if ($this->fk_bom > 0) {
691 foreach ($bom->lines as $line) {
694 $moline->fk_mo = $this->id;
695 $moline->origin_id = $line->id;
696 $moline->origin_type =
'bomline';
697 if ($line->qty_frozen) {
698 $moline->qty = $line->qty;
700 $moline->qty =
price2num(($line->qty / ( ! empty($bom->qty) ? $bom->qty : 1 ) ) * $this->qty / ( ! empty($line->efficiency) ? $line->efficiency : 1 ),
'MS');
702 if ($moline->qty <= 0) {
704 $this->error =
"BadValueForquantityToConsume";
707 $moline->fk_product = $line->fk_product;
708 $moline->role = $role;
709 $moline->position = $line->position;
710 $moline->qty_frozen = $line->qty_frozen;
711 $moline->disable_stock_change = $line->disable_stock_change;
713 $resultline = $moline->create($user,
false);
714 if ($resultline <= 0) {
716 $this->error = $moline->error;
717 $this->errors = $moline->errors;
732 $this->
db->rollback();
745 public function delete(
User $user, $notrigger =
false)
762 $langs->load(
'stocks');
764 if ($this->status < 0) {
765 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
770 $fk_movement =
GETPOST(
'fk_movement',
'int');
773 if (! empty($arrayoflines)) {
777 $stockmove->setOrigin($this->element, $this->
id);
779 if (! empty($fk_movement)) {
781 $TArrayMoLine = $moline->fetchAll(
'',
'', 1, 0, array(
'customsql' =>
'fk_stock_movement ='.$fk_movement));
782 $moline = array_shift($TArrayMoLine);
785 $movement->fetch($fk_movement);
786 $productstatic->fetch($movement->product_id);
787 $qtytoprocess = $movement->qty;
790 $labelmovementCancel = $langs->trans(
"CancelProductionForRef", $productstatic->ref);
791 $codemovementCancel = $langs->trans(
"StockIncrease");
793 if (($qtytoprocess >= 0)) {
794 $idstockmove = $stockmove->reception($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel,
'',
'', $movement->batch,
dol_now(), 0, $codemovementCancel);
796 $idstockmove = $stockmove->livraison($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel,
dol_now(),
'',
'', $movement->batch, 0, $codemovementCancel);
798 if ($idstockmove < 0) {
800 $this->
db->rollback();
805 return $moline->delete($user, $notrigger);
807 foreach ($arrayoflines as $key => $arrayofline) {
808 $lineDetails = $arrayoflines[$key];
809 $productstatic->fetch($lineDetails[
'fk_product']);
810 $qtytoprocess = $lineDetails[
'qty'];
813 $labelmovementCancel = $langs->trans(
"CancelProductionForRef", $productstatic->ref);
814 $codemovementCancel = $langs->trans(
"StockIncrease");
816 if ($qtytoprocess >= 0) {
817 $idstockmove = $stockmove->reception($user, $lineDetails[
'fk_product'], $lineDetails[
'fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel,
'',
'', $lineDetails[
'batch'],
dol_now(), 0, $codemovementCancel);
819 $idstockmove = $stockmove->livraison($user, $lineDetails[
'fk_product'], $lineDetails[
'fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel,
dol_now(),
'',
'', $lineDetails[
'batch'], 0, $codemovementCancel);
821 if ($idstockmove < 0) {
823 $this->
db->rollback();
846 global $langs, $conf;
849 if (!empty($conf->global->MRP_MO_ADDON)) {
852 $file = $conf->global->MRP_MO_ADDON.
".php";
853 $classname = $conf->global->MRP_MO_ADDON;
856 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
857 foreach ($dirmodels as $reldir) {
861 $mybool |= @include_once $dir.$file;
864 if ($mybool ===
false) {
869 $obj =
new $classname();
870 $numref = $obj->getNextValue($prod, $this);
875 $this->error = $obj->error;
880 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_MRP_MO_ADDON_NotDefined");
894 global $conf, $langs;
896 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
901 if ($this->status == self::STATUS_VALIDATED) {
902 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
919 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
925 $this->newref = $num;
928 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
929 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
930 $sql .=
" status = ".self::STATUS_VALIDATED.
",";
931 $sql .=
" date_valid='".$this->db->idate($now).
"',";
932 $sql .=
" fk_user_valid = ".$user->id;
933 $sql .=
" WHERE rowid = ".((int) $this->
id);
935 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
939 $this->error = $this->
db->lasterror();
943 if (!$error && !$notrigger) {
945 $result = $this->
call_trigger(
'MRP_MO_VALIDATE', $user);
953 $this->oldref = $this->ref;
956 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
958 $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).
"'";
959 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'mrp/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
962 $error++; $this->error = $this->
db->lasterror();
968 $dirsource = $conf->mrp->dir_output.
'/'.$oldref;
969 $dirdest = $conf->mrp->dir_output.
'/'.$newref;
970 if (!$error && file_exists($dirsource)) {
971 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
973 if (@rename($dirsource, $dirdest)) {
976 $listoffiles =
dol_dir_list($conf->mrp->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
977 foreach ($listoffiles as $fileentry) {
978 $dirsource = $fileentry[
'name'];
979 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
980 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
981 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
982 @rename($dirsource, $dirdest);
992 $this->status = self::STATUS_VALIDATED;
999 $this->
db->rollback();
1014 if ($this->status <= self::STATUS_DRAFT) {
1025 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'MRP_MO_UNVALIDATE');
1035 public function cancel($user, $notrigger = 0)
1038 if ($this->status != self::STATUS_VALIDATED && $this->status != self::STATUS_INPROGRESS) {
1049 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'MRP_MO_CANCEL');
1059 public function reopen($user, $notrigger = 0)
1062 if ($this->status != self::STATUS_PRODUCED && $this->status != self::STATUS_CANCELED) {
1073 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'MRP_MO_REOPEN');
1086 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1088 global $conf, $langs, $hookmanager;
1090 if (!empty($conf->dol_no_mouse_hover)) {
1096 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ManufacturingOrder").
'</u>';
1097 if (isset($this->status)) {
1098 $label .=
' '.$this->getLibStatut(5);
1101 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1102 if (isset($this->label)) {
1103 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$this->label;
1106 $url = DOL_URL_ROOT.
'/mrp/mo_card.php?id='.$this->id;
1107 if ($option ==
'production') {
1108 $url = DOL_URL_ROOT.
'/mrp/mo_production.php?id='.$this->id;
1111 if ($option !=
'nolink') {
1113 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1114 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1115 $add_save_lastsearch_values = 1;
1117 if ($add_save_lastsearch_values) {
1118 $url .=
'&save_lastsearch_values=1';
1123 if (empty($notooltip)) {
1124 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1125 $label = $langs->trans(
"ShowMo");
1126 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1128 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1129 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1131 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1134 $linkstart =
'<a href="'.$url.
'"';
1135 $linkstart .= $linkclose.
'>';
1138 $result .= $linkstart;
1140 $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);
1142 if ($withpicto != 2) {
1143 $result .= $this->ref;
1145 $result .= $linkend;
1148 global $action, $hookmanager;
1149 $hookmanager->initHooks(array(
'modao'));
1150 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1151 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1153 $result = $hookmanager->resPrint;
1155 $result .= $hookmanager->resPrint;
1169 return $this->
LibStatut($this->status, $mode);
1183 if (empty($this->labelStatus)) {
1186 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1187 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ValidatedToProduce');
1188 $this->labelStatus[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv(
'InProgress');
1189 $this->labelStatus[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv(
'StatusMOProduced');
1190 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Canceled');
1192 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'Draft');
1193 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'Validated');
1194 $this->labelStatusShort[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv(
'InProgress');
1195 $this->labelStatusShort[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv(
'StatusMOProduced');
1196 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Canceled');
1199 $statusType =
'status'.$status;
1200 if ($status == self::STATUS_VALIDATED) {
1201 $statusType =
'status1';
1203 if ($status == self::STATUS_INPROGRESS) {
1204 $statusType =
'status4';
1206 if ($status == self::STATUS_PRODUCED) {
1207 $statusType =
'status6';
1209 if ($status == self::STATUS_CANCELED) {
1210 $statusType =
'status9';
1213 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1224 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
1225 $sql .=
' fk_user_creat, fk_user_modif';
1226 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1227 $sql .=
' WHERE t.rowid = '.((int) $id);
1228 $result = $this->
db->query($sql);
1230 if ($this->
db->num_rows($result)) {
1231 $obj = $this->
db->fetch_object($result);
1232 $this->
id = $obj->rowid;
1234 $this->user_creation_id = $obj->fk_user_creat;
1235 $this->user_modification_id = $obj->fk_user_modif;
1236 $this->date_creation = $this->
db->jdate($obj->datec);
1237 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
1240 $this->
db->free($result);
1256 $this->lines = array();
1266 $this->lines = array();
1268 $objectline =
new MoLine($this->
db);
1270 $TFilters = array(
'customsql'=>
'fk_mo = '.((
int) $this->
id));
1271 if (!empty($rolefilter)) $TFilters[
'role'] = $rolefilter;
1272 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, $TFilters);
1274 if (is_numeric($result)) {
1275 $this->error = $this->error;
1276 $this->errors = $this->errors;
1279 $this->lines = $result;
1280 return $this->lines;
1295 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1297 global $conf, $langs;
1299 $langs->load(
"mrp");
1305 if ($this->model_pdf) {
1306 $modele = $this->model_pdf;
1307 } elseif (!empty($conf->global->MO_ADDON_PDF)) {
1308 $modele = $conf->global->MO_ADDON_PDF;
1312 $modelpath =
"core/modules/mrp/doc/";
1314 if (empty($modele)) {
1318 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1330 global $conf, $langs;
1346 $this->
db->commit();
1363 global $langs, $hookmanager, $conf,
$form;
1365 $langs->load(
'stocks');
1366 $text_stock_options = $langs->trans(
"RealStockDesc").
'<br>';
1367 $text_stock_options .= $langs->trans(
"RealStockWillAutomaticallyWhen").
'<br>';
1368 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ?
'- '.$langs->trans(
"DeStockOnShipment").
'<br>' :
'');
1369 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ?
'- '.$langs->trans(
"DeStockOnValidateOrder").
'<br>' :
'');
1370 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_BILL) ?
'- '.$langs->trans(
"DeStockOnBill").
'<br>' :
'');
1371 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ?
'- '.$langs->trans(
"ReStockOnBill").
'<br>' :
'');
1372 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ?
'- '.$langs->trans(
"ReStockOnValidateOrder").
'<br>' :
'');
1373 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ?
'- '.$langs->trans(
"ReStockOnDispatchOrder").
'<br>' :
'');
1374 $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE) ?
'- '.$langs->trans(
"StockOnReception").
'<br>' :
'');
1376 print
'<tr class="liste_titre">';
1378 print
'<td class="linecoldescription">'.$langs->trans(
'Ref');
1379 if (!empty($conf->global->BOM_SUB_BOM)) {
1380 print
' <a id="show_all" href="#">'.img_picto(
'',
'folder-open',
'class="paddingright"').$langs->trans(
"ExpandAll").
'</a> ';
1381 print
'<a id="hide_all" href="#">'.img_picto(
'',
'folder',
'class="paddingright"').$langs->trans(
"UndoExpandAll").
'</a> ';
1385 print
'<td class="right">'.$langs->trans(
'Qty');
1386 if ($this->bom->bomtype == 0) {
1387 print
' <span class="opacitymedium">('.$langs->trans(
"ForAQuantityOf", $this->bom->qty).
')</span>';
1389 print
' <span class="opacitymedium">('.$langs->trans(
"ForAQuantityToConsumeOf", $this->bom->qty).
')</span>';
1392 print
'<td class="center">'.$form->textwithpicto($langs->trans(
"PhysicalStock"), $text_stock_options, 1).
'</td>';
1393 print
'<td class="center">'.$form->textwithpicto($langs->trans(
"VirtualStock"), $langs->trans(
"VirtualStockDesc")).
'</td>';
1394 print
'<td class="center">'.$langs->trans(
'QtyFrozen').
'</td>';
1395 print
'<td class="center">'.$langs->trans(
'DisableStockChange').
'</td>';
1396 print
'<td class="center">'.$langs->trans(
'MoChildGenerate').
'</td>';
1402 if (!empty($this->lines)) {
1403 foreach ($this->lines as $line) {
1405 if (is_object($hookmanager)) {
1406 $parameters = array(
'line'=>$line,
'i'=>$i,
'restrictlist'=>$restrictlist,
'selectedLines'=> $selectedLines);
1407 if (!empty($line->fk_parent_line)) { $parameters[
'fk_parent_line'] = $line->fk_parent_line; }
1408 $reshook = $hookmanager->executeHooks(
'printOriginObjectLine', $parameters, $this, $action);
1410 if (empty($reshook)) {
1411 $this->
printOriginLine($line,
'', $restrictlist,
'/core/tpl', $selectedLines);
1433 public function printOriginLine($line, $var, $restrictlist =
'', $defaulttpldir =
'/core/tpl', $selectedLines = array())
1435 global $langs, $conf;
1437 $this->tpl[
'id'] = $line->id;
1439 $this->tpl[
'label'] =
'';
1440 if (!empty($line->fk_product)) {
1441 $productstatic =
new Product($this->
db);
1442 $productstatic->fetch($line->fk_product);
1443 $productstatic->load_virtual_stock();
1444 $this->tpl[
'label'] .= $productstatic->getNomUrl(1);
1451 $this->tpl[
'qty_bom'] = 1;
1452 if (is_object($this->bom) && $this->bom->qty > 1) {
1453 $this->tpl[
'qty_bom'] = $this->bom->qty;
1456 $this->tpl[
'stock'] = $productstatic->stock_reel;
1457 $this->tpl[
'seuil_stock_alerte'] = $productstatic->seuil_stock_alerte;
1458 $this->tpl[
'virtual_stock'] = $productstatic->stock_theorique;
1459 $this->tpl[
'qty'] = $line->qty;
1460 $this->tpl[
'qty_frozen'] = $line->qty_frozen;
1461 $this->tpl[
'disable_stock_change'] = $line->disable_stock_change;
1462 $this->tpl[
'efficiency'] = $line->efficiency;
1464 $tpl = DOL_DOCUMENT_ROOT.
'/mrp/tpl/originproductline.tpl.php';
1465 $res = include $tpl;
1478 $tables = array(
'mrp_mo');
1492 $TMoChilds = array();
1495 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"mrp_mo as mo_child";
1496 $sql.=
" WHERE fk_parent_line IN ";
1497 $sql.=
" (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mrp_production as line_parent";
1498 $sql.=
" WHERE fk_mo=".((int) $this->
id).
")";
1503 if ($this->
db->num_rows(
$resql) > 0) {
1504 while ($obj = $this->
db->fetch_object(
$resql)) {
1505 $MoChild =
new Mo($this->
db);
1506 $res = $MoChild->fetch($obj->rowid);
1507 if ($res > 0) $TMoChilds[$MoChild->id] = $MoChild;
1530 $MoParent =
new Mo($this->
db);
1533 $sql =
"SELECT lineparent.fk_mo as id_moparent FROM ".MAIN_DB_PREFIX.
"mrp_mo as mo";
1534 $sql.=
" LEFT JOIN ".MAIN_DB_PREFIX.
"mrp_production lineparent ON mo.fk_parent_line = lineparent.rowid";
1535 $sql.=
" WHERE mo.rowid = ".((int) $this->
id);
1540 if ($this->
db->num_rows(
$resql) > 0) {
1541 $obj = $this->
db->fetch_object(
$resql);
1542 $res = $MoParent->fetch($obj->id_moparent);
1543 if ($res < 0) $error++;
1567 public $element =
'mrp_production';
1572 public $table_element =
'mrp_production';
1577 public $ismultientitymanaged = 0;
1582 public $isextrafieldmanaged = 0;
1584 public $fields = array(
1585 'rowid' =>array(
'type'=>
'integer',
'label'=>
'ID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
1586 'fk_mo' =>array(
'type'=>
'integer',
'label'=>
'Mo',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>15),
1587 'origin_id' =>array(
'type'=>
'integer',
'label'=>
'Origin',
'enabled'=>1,
'visible'=>-1,
'notnull'=>0,
'position'=>17),
1588 'origin_type' =>array(
'type'=>
'varchar(10)',
'label'=>
'Origin type',
'enabled'=>1,
'visible'=>-1,
'notnull'=>0,
'position'=>18),
1589 'position' =>array(
'type'=>
'integer',
'label'=>
'Position',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>20),
1590 'fk_product' =>array(
'type'=>
'integer',
'label'=>
'Product',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>25),
1591 'fk_warehouse' =>array(
'type'=>
'integer',
'label'=>
'Warehouse',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
1592 'qty' =>array(
'type'=>
'real',
'label'=>
'Qty',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
1593 'qty_frozen' => array(
'type'=>
'smallint',
'label'=>
'QuantityFrozen',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>105,
'css'=>
'maxwidth50imp',
'help'=>
'QuantityConsumedInvariable'),
1594 'disable_stock_change' => array(
'type'=>
'smallint',
'label'=>
'DisableStockChange',
'enabled'=>1,
'visible'=>1,
'default'=>0,
'position'=>108,
'css'=>
'maxwidth50imp',
'help'=>
'DisableStockChangeHelp'),
1595 'batch' =>array(
'type'=>
'varchar(30)',
'label'=>
'Batch',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
1596 'role' =>array(
'type'=>
'varchar(10)',
'label'=>
'Role',
'enabled'=>1,
'visible'=>-1,
'position'=>145),
1597 'fk_mrp_production' =>array(
'type'=>
'integer',
'label'=>
'Fk mrp production',
'enabled'=>1,
'visible'=>-1,
'position'=>150),
1598 'fk_stock_movement' =>array(
'type'=>
'integer',
'label'=>
'StockMovement',
'enabled'=>1,
'visible'=>-1,
'position'=>155),
1599 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>160),
1600 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'Tms',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>165),
1601 'fk_user_creat' =>array(
'type'=>
'integer',
'label'=>
'UserCreation',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>170),
1602 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModification',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
1603 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
1609 public $origin_type;
1612 public $fk_warehouse;
1615 public $disable_stock_change;
1618 public $fk_mrp_production;
1619 public $fk_stock_movement;
1620 public $date_creation;
1622 public $fk_user_creat;
1623 public $fk_user_modif;
1633 global $conf, $langs;
1637 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
1638 $this->fields[
'rowid'][
'visible'] = 0;
1640 if (empty($conf->multicompany->enabled) && isset($this->fields[
'entity'])) {
1641 $this->fields[
'entity'][
'enabled'] = 0;
1645 foreach ($this->fields as $key => $val) {
1646 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
1647 unset($this->fields[$key]);
1652 if (is_object($langs)) {
1653 foreach ($this->fields as $key => $val) {
1654 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
1655 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
1656 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
1672 if (empty($this->qty)) {
1673 $this->error =
'BadValueForQty';
1704 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
1714 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
1715 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
1716 $sql .=
' WHERE t.entity IN ('.getEntity($this->table_element).
')';
1718 $sql .=
' WHERE 1 = 1';
1721 $sqlwhere = array();
1722 if (count($filter) > 0) {
1723 foreach ($filter as $key => $value) {
1724 if ($key ==
't.rowid') {
1725 $sqlwhere[] = $key.
" = ".((int) $value);
1726 } elseif (strpos($key,
'date') !==
false) {
1727 $sqlwhere[] = $key.
" = '".$this->
db->idate($value).
"'";
1728 } elseif ($key ==
'customsql') {
1729 $sqlwhere[] = $value;
1731 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
1735 if (count($sqlwhere) > 0) {
1736 $sql .=
' AND ('.implode(
' '.$this->
db->escape($filtermode).
' ', $sqlwhere).
')';
1739 if (!empty($sortfield)) {
1740 $sql .= $this->
db->order($sortfield, $sortorder);
1742 if (!empty($limit)) {
1743 $sql .= $this->
db->plimit($limit, $offset);
1748 $num = $this->
db->num_rows(
$resql);
1750 while ($i < ($limit ? min($limit, $num) : $num)) {
1751 $obj = $this->
db->fetch_object(
$resql);
1753 $record =
new self($this->db);
1754 $record->setVarsFromFetchObj($obj);
1756 $records[$record->id] = $record;
1764 $this->errors[] =
'Error '.$this->db->lasterror();
1765 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
1790 public function delete(
User $user, $notrigger =
false)