19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/mrp/class/mo.class.php';
50 $this->mo =
new Mo($this->db);
64 public function get($id)
66 if (!DolibarrApiAccess::$user->rights->mrp->read) {
67 throw new RestException(401);
70 $result = $this->mo->fetch($id);
72 throw new RestException(404,
'MO not found');
76 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
97 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
101 if (!DolibarrApiAccess::$user->rights->mrp->read) {
102 throw new RestException(401);
106 $tmpobject =
new Mo($this->db);
108 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
110 $restrictonsocid = 0;
114 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
115 $search_sale = DolibarrApiAccess::$user->id;
118 $sql =
"SELECT t.rowid";
119 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
120 $sql .=
", sc.fk_soc, sc.fk_user";
122 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
124 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
125 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
127 $sql .=
" WHERE 1 = 1";
133 if ($tmpobject->ismultientitymanaged) {
134 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
136 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
137 $sql .=
" AND t.fk_soc = sc.fk_soc";
139 if ($restrictonsocid && $socid) {
140 $sql .=
" AND t.fk_soc = ".((int) $socid);
142 if ($restrictonsocid && $search_sale > 0) {
143 $sql .=
" AND t.rowid = sc.fk_soc";
146 if ($restrictonsocid && $search_sale > 0) {
147 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
153 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
157 $sql .= $this->db->order($sortfield, $sortorder);
162 $offset = $limit * $page;
164 $sql .= $this->db->plimit($limit + 1, $offset);
167 $result = $this->db->query(
$sql);
169 $num = $this->db->num_rows($result);
172 $obj = $this->db->fetch_object($result);
173 $tmp_object =
new Mo($this->db);
174 if ($tmp_object->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieve MO list');
182 if (!count($obj_ret)) {
183 throw new RestException(404,
'No MO found');
194 public function post($request_data =
null)
196 if (!DolibarrApiAccess::$user->rights->mrp->write) {
197 throw new RestException(401);
200 $result = $this->
_validate($request_data);
202 foreach ($request_data as $field => $value) {
203 $this->mo->$field = $value;
208 if (!$this->mo->create(DolibarrApiAccess::$user)) {
209 throw new RestException(500,
"Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
211 return $this->mo->id;
222 public function put($id, $request_data =
null)
224 if (!DolibarrApiAccess::$user->rights->mrp->write) {
225 throw new RestException(401);
228 $result = $this->mo->fetch($id);
230 throw new RestException(404,
'MO not found');
234 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 foreach ($request_data as $field => $value) {
238 if ($field ==
'id') {
241 $this->mo->$field = $value;
246 if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
247 return $this->
get($id);
249 throw new RestException(500, $this->mo->error);
259 public function delete($id)
261 if (!DolibarrApiAccess::$user->rights->mrp->delete) {
262 throw new RestException(401);
264 $result = $this->mo->fetch($id);
266 throw new RestException(404,
'MO not found');
270 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
273 if (!$this->mo->delete(DolibarrApiAccess::$user)) {
274 throw new RestException(500,
'Error when deleting MO : '.$this->mo->error);
280 'message' =>
'MO deleted'
311 if (!DolibarrApiAccess::$user->rights->mrp->write) {
312 throw new RestException(401,
'Not enough permission');
314 $result = $this->mo->fetch($id);
316 throw new RestException(404,
'MO not found');
319 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
320 throw new RestException(401,
'Error bad status of MO');
326 $arraytoconsume = array();
327 $arraytoproduce = array();
329 foreach ($request_data as $field => $value) {
330 if ($field ==
'inventorylabel') {
331 $labelmovement = $value;
333 if ($field ==
'inventorycode') {
334 $codemovement = $value;
336 if ($field ==
'autoclose') {
339 if ($field ==
'arraytoconsume') {
340 $arraytoconsume = $value;
342 if ($field ==
'arraytoproduce') {
343 $arraytoproduce = $value;
347 if (empty($labelmovement)) {
348 throw new RestException(500,
"Field inventorylabel not provided");
350 if (empty($codemovement)) {
351 throw new RestException(500,
"Field inventorycode not provided");
355 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
356 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
361 $consumptioncomplete =
true;
362 $productioncomplete =
true;
364 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
366 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
367 foreach ($arrayofarrayname as $arrayname) {
368 foreach ($arrayname as $value) {
369 $tmpproduct =
new Product($this->db);
370 if (empty($value[
"objectid"])) {
371 throw new RestException(500,
"Field objectid required in ".$arrayname);
373 $tmpproduct->fetch($value[
"qty"]);
374 if (empty($value[
"qty"])) {
375 throw new RestException(500,
"Field qty required in ".$arrayname);
377 if ($value[
"qty"]!=0) {
378 $qtytoprocess = $value[
"qty"];
379 if (isset($value[
"fk_warehouse"])) {
380 if (!($value[
"fk_warehouse"] > 0)) {
382 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
384 if ($tmpproduct->status_batch) {
386 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
390 if (!$error && $value[
"fk_warehouse"] > 0) {
392 $id_product_batch = 0;
394 $stockmove->setOrigin($this->mo->element, $this->mo->id);
396 if ($qtytoprocess >= 0) {
397 $moline =
new MoLine($this->db);
398 $moline->fk_mo = $this->mo->id;
399 $moline->position = $pos;
400 $moline->fk_product = $value[
"objectid"];
401 $moline->fk_warehouse = $value[
"fk_warehouse"];
402 $moline->qty = $qtytoprocess;
403 $moline->batch = $tmpproduct->status_batch;
404 $moline->role =
'toproduce';
405 $moline->fk_mrp_production =
"";
406 $moline->fk_stock_movement = $idstockmove;
407 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
409 $resultmoline = $moline->create(DolibarrApiAccess::$user);
410 if ($resultmoline <= 0) {
412 throw new RestException(500, $moline->error);
414 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
416 $moline =
new MoLine($this->db);
417 $moline->fk_mo = $this->mo->id;
418 $moline->position = $pos;
419 $moline->fk_product = $value[
"objectid"];
420 $moline->fk_warehouse = $value[
"fk_warehouse"];
421 $moline->qty = $qtytoprocess;
422 $moline->batch = $tmpproduct->status_batch;
423 $moline->role =
'toconsume';
424 $moline->fk_mrp_production =
"";
425 $moline->fk_stock_movement = $idstockmove;
426 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
428 $resultmoline = $moline->create(DolibarrApiAccess::$user);
429 if ($resultmoline <= 0) {
431 throw new RestException(500, $moline->error);
433 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
435 if ($idstockmove < 0) {
437 throw new RestException(500, $stockmove->error);
442 $moline =
new MoLine($this->db);
443 $moline->fk_mo = $this->mo->id;
444 $moline->position = $pos;
445 $moline->fk_product = $value[
"objectid"];
446 $moline->fk_warehouse = $value[
"fk_warehouse"];
447 $moline->qty = $qtytoprocess;
448 $moline->batch = $tmpproduct->status_batch;
449 if ($arrayname ==
"arraytoconsume") {
450 $moline->role =
'consumed';
452 $moline->role =
'produced';
454 $moline->fk_mrp_production =
"";
455 $moline->fk_stock_movement = $idstockmove;
456 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
458 $resultmoline = $moline->create(DolibarrApiAccess::$user);
459 if ($resultmoline <= 0) {
461 throw new RestException(500, $moline->error);
470 if ($autoclose <= 0) {
471 $consumptioncomplete =
false;
472 $productioncomplete =
false;
477 foreach ($this->mo->lines as $line) {
478 if ($line->role ==
'toconsume') {
479 $tmpproduct =
new Product($this->db);
480 $tmpproduct->fetch($line->fk_product);
481 if ($line->qty != 0) {
482 $qtytoprocess = $line->qty;
483 if (isset($line->fk_warehouse)) {
484 if (!($line->fk_warehouse > 0)) {
485 $langs->load(
"errors");
487 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
489 if ($tmpproduct->status_batch) {
490 $langs->load(
"errors");
492 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
496 if (!$error && $line->fk_warehouse > 0) {
498 $id_product_batch = 0;
499 $stockmove->origin_type =
'mo';
500 $stockmove->origin_id = $this->mo->id;
501 if ($qtytoprocess >= 0) {
502 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
504 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
506 if ($idstockmove < 0) {
508 throw new RestException(500, $stockmove->error);
513 $moline =
new MoLine($this->db);
514 $moline->fk_mo = $this->mo->id;
515 $moline->position = $pos;
516 $moline->fk_product = $line->fk_product;
517 $moline->fk_warehouse = $line->fk_warehouse;
518 $moline->qty = $qtytoprocess;
519 $moline->batch = $tmpproduct->status_batch;
520 $moline->role =
'consumed';
521 $moline->fk_mrp_production = $line->id;
522 $moline->fk_stock_movement = $idstockmove;
523 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
525 $resultmoline = $moline->create(DolibarrApiAccess::$user);
526 if ($resultmoline <= 0) {
528 throw new RestException(500, $moline->error);
537 foreach ($this->mo->lines as $line) {
538 if ($line->role ==
'toproduce') {
539 $tmpproduct =
new Product($this->db);
540 $tmpproduct->fetch($line->fk_product);
541 if ($line->qty != 0) {
542 $qtytoprocess = $line->qty;
543 if (isset($line->fk_warehouse)) {
544 if (!($line->fk_warehouse > 0)) {
545 $langs->load(
"errors");
547 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
549 if ($tmpproduct->status_batch) {
550 $langs->load(
"errors");
552 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
556 if (!$error && $line->fk_warehouse > 0) {
558 $id_product_batch = 0;
559 $stockmove->origin_type =
'mo';
560 $stockmove->origin_id = $this->mo->id;
561 if ($qtytoprocess >= 0) {
562 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
564 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
566 if ($idstockmove < 0) {
568 throw new RestException(500, $stockmove->error);
573 $moline =
new MoLine($this->db);
574 $moline->fk_mo = $this->mo->id;
575 $moline->position = $pos;
576 $moline->fk_product = $line->fk_product;
577 $moline->fk_warehouse = $line->fk_warehouse;
578 $moline->qty = $qtytoprocess;
579 $moline->batch = $tmpproduct->status_batch;
580 $moline->role =
'produced';
581 $moline->fk_mrp_production = $line->id;
582 $moline->fk_stock_movement = $idstockmove;
583 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
585 $resultmoline = $moline->create(DolibarrApiAccess::$user);
586 if ($resultmoline <= 0) {
588 throw new RestException(500, $moline->error);
598 if ($autoclose > 0) {
599 foreach ($this->mo->lines as $line) {
600 if ($line->role ==
'toconsume') {
601 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
602 $alreadyconsumed = 0;
603 foreach ($arrayoflines as $line2) {
604 $alreadyconsumed += $line2[
'qty'];
607 if ($alreadyconsumed < $line->qty) {
608 $consumptioncomplete =
false;
611 if ($line->role ==
'toproduce') {
612 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
613 $alreadyproduced = 0;
614 foreach ($arrayoflines as $line2) {
615 $alreadyproduced += $line2[
'qty'];
618 if ($alreadyproduced < $line->qty) {
619 $productioncomplete =
false;
624 $consumptioncomplete =
false;
625 $productioncomplete =
false;
631 dol_syslog(
"consumptioncomplete = ".$consumptioncomplete.
" productioncomplete = ".$productioncomplete);
633 if ($consumptioncomplete && $productioncomplete) {
634 $result = $this->mo->setStatut(self::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
636 $result = $this->mo->setStatut(self::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
639 throw new RestException(500, $this->mo->error);
642 return $this->mo->id;
656 $object = parent::_cleanObjectDatas($object);
658 unset($object->rowid);
659 unset($object->canvas);
661 unset($object->name);
662 unset($object->lastname);
663 unset($object->firstname);
664 unset($object->civility_id);
665 unset($object->statut);
666 unset($object->state);
667 unset($object->state_id);
668 unset($object->state_code);
669 unset($object->region);
670 unset($object->region_code);
671 unset($object->country);
672 unset($object->country_id);
673 unset($object->country_code);
674 unset($object->barcode_type);
675 unset($object->barcode_type_code);
676 unset($object->barcode_type_label);
677 unset($object->barcode_type_coder);
678 unset($object->total_ht);
679 unset($object->total_tva);
680 unset($object->total_localtax1);
681 unset($object->total_localtax2);
682 unset($object->total_ttc);
683 unset($object->fk_account);
684 unset($object->comments);
685 unset($object->note);
686 unset($object->mode_reglement_id);
687 unset($object->cond_reglement_id);
688 unset($object->cond_reglement);
689 unset($object->shipping_method_id);
690 unset($object->fk_incoterms);
691 unset($object->label_incoterms);
692 unset($object->location_incoterms);
695 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
696 $nboflines = count($object->lines);
697 for ($i = 0; $i < $nboflines; $i++) {
700 unset($object->lines[$i]->lines);
701 unset($object->lines[$i]->note);
719 foreach ($this->mo->fields as $field => $propfield) {
720 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
723 if (!isset($data[$field])) {
724 throw new RestException(400,
"$field field missing");
726 $myobject[$field] = $data[$field];
738 $ref = substr($this->mo->ref, 1, 4);
739 if ($this->mo->status > 0 && $ref ==
'PROV') {
740 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
743 if (strtolower($this->mo->ref) ==
'auto') {
744 if (empty($this->mo->id) && $this->mo->status == 0) {
747 $this->mo->fetch_product();
748 $numref = $this->mo->getNextNumRef($this->mo->product);
749 $this->mo->ref = $numref;
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
produceAndConsume($id, $request_data=null)
Produce and consume.
__construct()
Constructor.
put($id, $request_data=null)
Update MO.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List Mos.
post($request_data=null)
Create MO object.
_cleanObjectDatas($object)
Clean sensible object datas.
checkRefNumbering()
Validate the ref field and get the next Number if it's necessary.
_validate($data)
Validate fields before create or update object.
Class to manage stock movements.
Class to manage products or services.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.