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";
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);
152 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
154 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
155 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
158 $sql .= $this->
db->order($sortfield, $sortorder);
163 $offset = $limit * $page;
165 $sql .= $this->
db->plimit($limit + 1, $offset);
168 $result = $this->
db->query($sql);
170 $num = $this->
db->num_rows($result);
173 $obj = $this->
db->fetch_object($result);
174 $tmp_object =
new Mo($this->
db);
175 if ($tmp_object->fetch($obj->rowid)) {
181 throw new RestException(503,
'Error when retrieve MO list');
183 if (!count($obj_ret)) {
184 throw new RestException(404,
'No MO found');
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->rights->mrp->write) {
198 throw new RestException(401);
201 $result = $this->
_validate($request_data);
203 foreach ($request_data as $field => $value) {
204 $this->mo->$field = $value;
206 if (!$this->mo->create(DolibarrApiAccess::$user)) {
207 throw new RestException(500,
"Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
209 return $this->mo->id;
220 public function put($id, $request_data =
null)
222 if (!DolibarrApiAccess::$user->rights->mrp->write) {
223 throw new RestException(401);
226 $result = $this->mo->fetch($id);
228 throw new RestException(404,
'MO not found');
232 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
235 foreach ($request_data as $field => $value) {
236 if ($field ==
'id') {
239 $this->mo->$field = $value;
242 if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
243 return $this->
get($id);
245 throw new RestException(500, $this->mo->error);
255 public function delete($id)
257 if (!DolibarrApiAccess::$user->rights->mrp->delete) {
258 throw new RestException(401);
260 $result = $this->mo->fetch($id);
262 throw new RestException(404,
'MO not found');
266 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
269 if (!$this->mo->delete(DolibarrApiAccess::$user)) {
270 throw new RestException(500,
'Error when deleting MO : '.$this->mo->error);
276 'message' =>
'MO deleted'
307 if (!DolibarrApiAccess::$user->rights->mrp->write) {
308 throw new RestException(401,
'Not enough permission');
310 $result = $this->mo->fetch($id);
312 throw new RestException(404,
'MO not found');
315 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
316 throw new RestException(401,
'Error bad status of MO');
322 $arraytoconsume = array();
323 $arraytoproduce = array();
325 foreach ($request_data as $field => $value) {
326 if ($field ==
'inventorylabel') {
327 $labelmovement = $value;
329 if ($field ==
'inventorycode') {
330 $codemovement = $value;
332 if ($field ==
'autoclose') {
335 if ($field ==
'arraytoconsume') {
336 $arraytoconsume = $value;
338 if ($field ==
'arraytoproduce') {
339 $arraytoproduce = $value;
343 if (empty($labelmovement)) {
344 throw new RestException(500,
"Field inventorylabel not prodivded");
346 if (empty($codemovement)) {
347 throw new RestException(500,
"Field inventorycode not prodivded");
351 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
352 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
357 $consumptioncomplete =
true;
358 $productioncomplete =
true;
360 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
362 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
363 foreach ($arrayofarrayname as $arrayname) {
364 foreach ($$arrayname as $value) {
366 if (empty($value[
"objectid"])) {
367 throw new RestException(500,
"Field objectid required in ".$arrayname);
369 $tmpproduct->fetch($value[
"qty"]);
370 if (empty($value[
"qty"])) {
371 throw new RestException(500,
"Field qty required in ".$arrayname);
373 if ($value[
"qty"]!=0) {
374 $qtytoprocess = $value[
"qty"];
375 if (isset($value[
"fk_warehouse"])) {
376 if (!($value[
"fk_warehouse"] > 0)) {
378 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
380 if ($tmpproduct->status_batch) {
382 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
386 if (!$error && $value[
"fk_warehouse"] > 0) {
388 $id_product_batch = 0;
390 $stockmove->setOrigin($this->mo->element, $this->mo->id);
392 if ($qtytoprocess >= 0) {
394 $moline->fk_mo = $this->mo->id;
395 $moline->position = $pos;
396 $moline->fk_product = $value[
"objectid"];
397 $moline->fk_warehouse = $value[
"fk_warehouse"];
398 $moline->qty = $qtytoprocess;
399 $moline->batch = $tmpproduct->status_batch;
400 $moline->role =
'toproduce';
401 $moline->fk_mrp_production =
"";
402 $moline->fk_stock_movement = $idstockmove;
403 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
405 $resultmoline = $moline->create(DolibarrApiAccess::$user);
406 if ($resultmoline <= 0) {
408 throw new RestException(500, $moline->error);
410 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
413 $moline->fk_mo = $this->mo->id;
414 $moline->position = $pos;
415 $moline->fk_product = $value[
"objectid"];
416 $moline->fk_warehouse = $value[
"fk_warehouse"];
417 $moline->qty = $qtytoprocess;
418 $moline->batch = $tmpproduct->status_batch;
419 $moline->role =
'toconsume';
420 $moline->fk_mrp_production =
"";
421 $moline->fk_stock_movement = $idstockmove;
422 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
424 $resultmoline = $moline->create(DolibarrApiAccess::$user);
425 if ($resultmoline <= 0) {
427 throw new RestException(500, $moline->error);
429 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
431 if ($idstockmove < 0) {
433 throw new RestException(500, $stockmove->error);
439 $moline->fk_mo = $this->mo->id;
440 $moline->position = $pos;
441 $moline->fk_product = $value[
"objectid"];
442 $moline->fk_warehouse = $value[
"fk_warehouse"];
443 $moline->qty = $qtytoprocess;
444 $moline->batch = $tmpproduct->status_batch;
445 if ($arrayname ==
"arraytoconsume") {
446 $moline->role =
'consumed';
448 $moline->role =
'produced';
450 $moline->fk_mrp_production =
"";
451 $moline->fk_stock_movement = $idstockmove;
452 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
454 $resultmoline = $moline->create(DolibarrApiAccess::$user);
455 if ($resultmoline <= 0) {
457 throw new RestException(500, $moline->error);
466 if ($autoclose <= 0) {
467 $consumptioncomplete =
false;
468 $productioncomplete =
false;
473 foreach ($this->mo->lines as $line) {
474 if ($line->role ==
'toconsume') {
476 $tmpproduct->fetch($line->fk_product);
477 if ($line->qty != 0) {
478 $qtytoprocess = $line->qty;
479 if (isset($line->fk_warehouse)) {
480 if (!($line->fk_warehouse > 0)) {
481 $langs->load(
"errors");
483 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
485 if ($tmpproduct->status_batch) {
486 $langs->load(
"errors");
488 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
492 if (!$error && $line->fk_warehouse > 0) {
494 $id_product_batch = 0;
495 $stockmove->origin_type =
'mo';
496 $stockmove->origin_id = $this->mo->id;
497 if ($qtytoprocess >= 0) {
498 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
500 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
502 if ($idstockmove < 0) {
504 throw new RestException(500, $stockmove->error);
510 $moline->fk_mo = $this->mo->id;
511 $moline->position = $pos;
512 $moline->fk_product = $line->fk_product;
513 $moline->fk_warehouse = $line->fk_warehouse;
514 $moline->qty = $qtytoprocess;
515 $moline->batch = $tmpproduct->status_batch;
516 $moline->role =
'consumed';
517 $moline->fk_mrp_production = $line->id;
518 $moline->fk_stock_movement = $idstockmove;
519 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
521 $resultmoline = $moline->create(DolibarrApiAccess::$user);
522 if ($resultmoline <= 0) {
524 throw new RestException(500, $moline->error);
533 foreach ($this->mo->lines as $line) {
534 if ($line->role ==
'toproduce') {
536 $tmpproduct->fetch($line->fk_product);
537 if ($line->qty != 0) {
538 $qtytoprocess = $line->qty;
539 if (isset($line->fk_warehouse)) {
540 if (!($line->fk_warehouse > 0)) {
541 $langs->load(
"errors");
543 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
545 if ($tmpproduct->status_batch) {
546 $langs->load(
"errors");
548 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
552 if (!$error && $line->fk_warehouse > 0) {
554 $id_product_batch = 0;
555 $stockmove->origin_type =
'mo';
556 $stockmove->origin_id = $this->mo->id;
557 if ($qtytoprocess >= 0) {
558 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
560 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
562 if ($idstockmove < 0) {
564 throw new RestException(500, $stockmove->error);
570 $moline->fk_mo = $this->mo->id;
571 $moline->position = $pos;
572 $moline->fk_product = $line->fk_product;
573 $moline->fk_warehouse = $line->fk_warehouse;
574 $moline->qty = $qtytoprocess;
575 $moline->batch = $tmpproduct->status_batch;
576 $moline->role =
'produced';
577 $moline->fk_mrp_production = $line->id;
578 $moline->fk_stock_movement = $idstockmove;
579 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
581 $resultmoline = $moline->create(DolibarrApiAccess::$user);
582 if ($resultmoline <= 0) {
584 throw new RestException(500, $moline->error);
594 if ($autoclose > 0) {
595 foreach ($this->mo->lines as $line) {
596 if ($line->role ==
'toconsume') {
597 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
598 $alreadyconsumed = 0;
599 foreach ($arrayoflines as $line2) {
600 $alreadyconsumed += $line2[
'qty'];
603 if ($alreadyconsumed < $line->qty) {
604 $consumptioncomplete =
false;
607 if ($line->role ==
'toproduce') {
608 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
609 $alreadyproduced = 0;
610 foreach ($arrayoflines as $line2) {
611 $alreadyproduced += $line2[
'qty'];
614 if ($alreadyproduced < $line->qty) {
615 $productioncomplete =
false;
620 $consumptioncomplete =
false;
621 $productioncomplete =
false;
627 dol_syslog(
"consumptioncomplete = ".$consumptioncomplete.
" productioncomplete = ".$productioncomplete);
629 if ($consumptioncomplete && $productioncomplete) {
630 $result = $this->mo->setStatut(self::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
632 $result = $this->mo->setStatut(self::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
635 throw new RestException(500, $this->mo->error);
638 return $this->mo->id;
652 $object = parent::_cleanObjectDatas($object);
654 unset($object->rowid);
655 unset($object->canvas);
657 unset($object->name);
658 unset($object->lastname);
659 unset($object->firstname);
660 unset($object->civility_id);
661 unset($object->statut);
662 unset($object->state);
663 unset($object->state_id);
664 unset($object->state_code);
665 unset($object->region);
666 unset($object->region_code);
667 unset($object->country);
668 unset($object->country_id);
669 unset($object->country_code);
670 unset($object->barcode_type);
671 unset($object->barcode_type_code);
672 unset($object->barcode_type_label);
673 unset($object->barcode_type_coder);
674 unset($object->total_ht);
675 unset($object->total_tva);
676 unset($object->total_localtax1);
677 unset($object->total_localtax2);
678 unset($object->total_ttc);
679 unset($object->fk_account);
680 unset($object->comments);
681 unset($object->note);
682 unset($object->mode_reglement_id);
683 unset($object->cond_reglement_id);
684 unset($object->cond_reglement);
685 unset($object->shipping_method_id);
686 unset($object->fk_incoterms);
687 unset($object->label_incoterms);
688 unset($object->location_incoterms);
691 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
692 $nboflines = count($object->lines);
693 for ($i = 0; $i < $nboflines; $i++) {
696 unset($object->lines[$i]->lines);
697 unset($object->lines[$i]->note);
715 foreach ($this->mo->fields as $field => $propfield) {
716 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
719 if (!isset($data[$field])) {
720 throw new RestException(400,
"$field field missing");
722 $myobject[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
_validate($data)
Validate fields before create or update object.
Class to manage stock movements.
Class to manage products or services.
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.
$conf db
API class for accounts.