342 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
343 throw new RestException(403,
'Not enough permission');
345 $result = $this->mo->fetch(
$id);
347 throw new RestException(404,
'MO not found');
350 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
351 throw new RestException(405,
'Error bad status of MO');
355 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
356 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
357 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
364 $arraytoconsume = array();
365 $arraytoproduce = array();
367 foreach ($request_data as $field => $value) {
368 if ($field ==
'inventorylabel') {
369 $labelmovement = $value;
371 if ($field ==
'inventorycode') {
372 $codemovement = $value;
374 if ($field ==
'autoclose') {
377 if ($field ==
'arraytoconsume') {
378 $arraytoconsume = $value;
380 if ($field ==
'arraytoproduce') {
381 $arraytoproduce = $value;
383 if ($field ===
'caller') {
385 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
390 if (empty($labelmovement)) {
391 throw new RestException(500,
"Field inventorylabel not provided");
393 if (empty($codemovement)) {
394 throw new RestException(500,
"Field inventorycode not provided");
397 $consumptioncomplete =
true;
398 $productioncomplete =
true;
400 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
402 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
403 foreach ($arrayofarrayname as $arrayname) {
404 foreach (${$arrayname} as $value) {
405 $tmpproduct =
new Product($this->db);
406 if (empty($value[
"objectid"])) {
407 throw new RestException(500,
"Field objectid required in ".$arrayname);
409 $tmpproduct->fetch($value[
"qty"]);
410 if (empty($value[
"qty"])) {
411 throw new RestException(500,
"Field qty required in ".$arrayname);
413 if ($value[
"qty"] != 0) {
414 $qtytoprocess = $value[
"qty"];
415 if (isset($value[
"fk_warehouse"])) {
416 if (!($value[
"fk_warehouse"] > 0)) {
418 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
420 if ($tmpproduct->status_batch) {
422 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
426 if (!$error && $value[
"fk_warehouse"] > 0) {
428 $id_product_batch = 0;
430 $stockmove->setOrigin($this->mo->element, $this->mo->id);
432 if ($arrayname ==
'arraytoconsume') {
433 $moline =
new MoLine($this->db);
434 $moline->fk_mo = $this->mo->id;
435 $moline->position = $pos;
436 $moline->fk_product = $value[
"objectid"];
437 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
438 $moline->qty = $qtytoprocess;
439 $moline->batch = (string) $tmpproduct->status_batch;
440 $moline->role =
'toproduce';
441 $moline->fk_mrp_production = 0;
442 $moline->fk_stock_movement = $idstockmove;
443 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
445 $resultmoline = $moline->create(DolibarrApiAccess::$user);
446 if ($resultmoline <= 0) {
448 throw new RestException(500, $moline->error);
450 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
452 $moline =
new MoLine($this->db);
453 $moline->fk_mo = $this->mo->id;
454 $moline->position = $pos;
455 $moline->fk_product = $value[
"objectid"];
456 $moline->fk_warehouse = $value[
"fk_warehouse"];
457 $moline->qty = $qtytoprocess;
458 $moline->batch = (string) $tmpproduct->status_batch;
459 $moline->role =
'toconsume';
460 $moline->fk_mrp_production = 0;
461 $moline->fk_stock_movement = $idstockmove;
462 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
464 $resultmoline = $moline->create(DolibarrApiAccess::$user);
465 if ($resultmoline <= 0) {
467 throw new RestException(500, $moline->error);
469 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
471 if ($idstockmove < 0) {
473 throw new RestException(500, $stockmove->error);
478 $moline =
new MoLine($this->db);
479 $moline->fk_mo = $this->mo->id;
480 $moline->position = $pos;
481 $moline->fk_product = $value[
"objectid"];
482 $moline->fk_warehouse = $value[
"fk_warehouse"];
483 $moline->qty = $qtytoprocess;
484 $moline->batch = (string) $tmpproduct->status_batch;
485 if ($arrayname ==
"arraytoconsume") {
486 $moline->role =
'consumed';
488 $moline->role =
'produced';
490 $moline->fk_mrp_production = 0;
491 $moline->fk_stock_movement = $idstockmove;
492 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
494 $resultmoline = $moline->create(DolibarrApiAccess::$user);
495 if ($resultmoline <= 0) {
497 throw new RestException(500, $moline->error);
506 if ($autoclose <= 0) {
507 $consumptioncomplete =
false;
508 $productioncomplete =
false;
513 foreach ($this->mo->lines as $line) {
514 if ($line->role ==
'toconsume') {
515 $tmpproduct =
new Product($this->db);
516 $tmpproduct->fetch($line->fk_product);
517 if ($line->qty != 0) {
518 $qtytoprocess = $line->qty;
519 if (isset($line->fk_warehouse)) {
520 if (!($line->fk_warehouse > 0)) {
521 $langs->load(
"errors");
523 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
525 if ($tmpproduct->status_batch) {
526 $langs->load(
"errors");
528 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
532 if (!$error && $line->fk_warehouse > 0) {
534 $id_product_batch = 0;
535 $stockmove->origin_type =
'mo';
536 $stockmove->origin_id = $this->mo->id;
537 if ($qtytoprocess >= 0) {
538 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, (
int) $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
540 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, (
int) $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
542 if ($idstockmove < 0) {
544 throw new RestException(500, $stockmove->error);
549 $moline =
new MoLine($this->db);
550 $moline->fk_mo = $this->mo->id;
551 $moline->position = $pos;
552 $moline->fk_product = $line->fk_product;
553 $moline->fk_warehouse = $line->fk_warehouse;
554 $moline->qty = $qtytoprocess;
555 $moline->batch = (string) $tmpproduct->status_batch;
556 $moline->role =
'consumed';
557 $moline->fk_mrp_production = $line->id;
558 $moline->fk_stock_movement = $idstockmove;
559 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
561 $resultmoline = $moline->create(DolibarrApiAccess::$user);
562 if ($resultmoline <= 0) {
564 throw new RestException(500, $moline->error);
573 foreach ($this->mo->lines as $line) {
574 if ($line->role ==
'toproduce') {
575 $tmpproduct =
new Product($this->db);
576 $tmpproduct->fetch($line->fk_product);
577 if ($line->qty != 0) {
578 $qtytoprocess = $line->qty;
579 if (isset($line->fk_warehouse)) {
580 if (!($line->fk_warehouse > 0)) {
581 $langs->load(
"errors");
583 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
585 if ($tmpproduct->status_batch) {
586 $langs->load(
"errors");
588 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
592 if (!$error && $line->fk_warehouse > 0) {
594 $id_product_batch = 0;
595 $stockmove->origin_type =
'mo';
596 $stockmove->origin_id = $this->mo->id;
597 if ($qtytoprocess >= 0) {
598 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, (
int) $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
600 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, (
int) $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
602 if ($idstockmove < 0) {
604 throw new RestException(500, $stockmove->error);
609 $moline =
new MoLine($this->db);
610 $moline->fk_mo = $this->mo->id;
611 $moline->position = $pos;
612 $moline->fk_product = $line->fk_product;
613 $moline->fk_warehouse = $line->fk_warehouse;
614 $moline->qty = $qtytoprocess;
615 $moline->batch = (string) $tmpproduct->status_batch;
616 $moline->role =
'produced';
617 $moline->fk_mrp_production = $line->id;
618 $moline->fk_stock_movement = $idstockmove;
619 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
621 $resultmoline = $moline->create(DolibarrApiAccess::$user);
622 if ($resultmoline <= 0) {
624 throw new RestException(500, $moline->error);
634 if ($autoclose > 0) {
635 foreach ($this->mo->lines as $line) {
636 if ($line->role ==
'toconsume') {
637 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
638 $alreadyconsumed = 0;
639 foreach ($arrayoflines as $line2) {
640 $alreadyconsumed += $line2[
'qty'];
643 if ($alreadyconsumed < $line->qty) {
644 $consumptioncomplete =
false;
647 if ($line->role ==
'toproduce') {
648 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
649 $alreadyproduced = 0;
650 foreach ($arrayoflines as $line2) {
651 $alreadyproduced += $line2[
'qty'];
654 if ($alreadyproduced < $line->qty) {
655 $productioncomplete =
false;
660 $consumptioncomplete =
false;
661 $productioncomplete =
false;
667 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
668 if ($consumptioncomplete && $productioncomplete) {
669 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
671 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
674 throw new RestException(500, $this->mo->error);
677 return $this->mo->id;
716 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
717 throw new RestException(403,
'Not enough permission');
719 $result = $this->mo->fetch(
$id);
721 throw new RestException(404,
'MO not found');
724 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
725 throw new RestException(405,
'Error bad status of MO');
729 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
730 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
731 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
738 $arraytoconsume = array();
739 $arraytoproduce = array();
741 foreach ($request_data as $field => $value) {
742 if ($field ==
'inventorylabel') {
743 $labelmovement = $value;
745 if ($field ==
'inventorycode') {
746 $codemovement = $value;
748 if ($field ==
'autoclose') {
751 if ($field ==
'arraytoconsume') {
752 $arraytoconsume = $value;
754 if ($field ==
'arraytoproduce') {
755 $arraytoproduce = $value;
757 if ($field ===
'caller') {
759 $stockmove->context[
'caller'] = $request_data[
'caller'];
764 if (empty($labelmovement)) {
765 throw new RestException(500,
"Field inventorylabel not provided");
767 if (empty($codemovement)) {
768 throw new RestException(500,
"Field inventorycode not provided");
774 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
775 foreach ($arrayofarrayname as $arrayname) {
776 foreach (${$arrayname} as $value) {
777 if (empty($value[
"objectid"])) {
778 throw new RestException(500,
"Field objectid required in " . $arrayname);
781 $molinetoprocess =
new MoLine($this->db);
782 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
783 if ($tmpmolineid <= 0) {
784 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
787 $tmpproduct =
new Product($this->db);
788 $tmpproduct->fetch($molinetoprocess->fk_product);
789 if ($tmpproduct->status_batch) {
790 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
793 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
794 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
796 $qtytoprocess = $value[
"qty"];
798 $fk_warehousetoprocess = 0;
799 if ($molinetoprocess->disable_stock_change ==
false) {
800 if (isset($value[
"fk_warehouse"])) {
801 if (!($value[
"fk_warehouse"] > 0)) {
802 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
805 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
809 if (isset($value[
"pricetoproduce"])) {
810 if ($value[
"pricetoproduce"] > 0) {
811 $pricetoproduce = $value[
"pricetoproduce"];
817 if ($molinetoprocess->disable_stock_change ==
false) {
819 $id_product_batch = 0;
820 $stockmove->origin_type =
'mo';
821 $stockmove->origin_id = $this->mo->id;
822 if ($arrayname ==
"arraytoconsume") {
823 if ($qtytoprocess >= 0) {
824 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
826 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
829 if ($qtytoprocess >= 0) {
830 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
832 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
835 if ($idstockmove <= 0) {
836 throw new RestException(500, $stockmove->error);
841 $moline =
new MoLine($this->db);
842 $moline->fk_mo = $this->mo->id;
843 $moline->position = $pos;
844 $moline->fk_product = $tmpproduct->id;
845 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
846 $moline->qty = $qtytoprocess;
848 $moline->fk_mrp_production = $molinetoprocess->id;
849 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
850 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
852 if ($arrayname ==
"arraytoconsume") {
853 $moline->role =
'consumed';
855 $moline->role =
'produced';
858 $resultmoline = $moline->create(DolibarrApiAccess::$user);
859 if ($resultmoline <= 0) {
860 throw new RestException(500, $moline->error);
867 $consumptioncomplete =
true;
868 $productioncomplete =
true;
870 if ($autoclose > 0) {
872 $this->mo->fetchLines();
874 foreach ($this->mo->lines as $line) {
875 if ($line->role ==
'toconsume') {
876 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
877 $alreadyconsumed = 0;
878 foreach ($arrayoflines as $line2) {
879 $alreadyconsumed += $line2[
'qty'];
882 if ($alreadyconsumed < $line->qty) {
883 $consumptioncomplete =
false;
886 if ($line->role ==
'toproduce') {
887 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
888 $alreadyproduced = 0;
889 foreach ($arrayoflines as $line2) {
890 $alreadyproduced += $line2[
'qty'];
893 if ($alreadyproduced < $line->qty) {
894 $productioncomplete =
false;
899 $consumptioncomplete =
false;
900 $productioncomplete =
false;
904 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
906 if ($consumptioncomplete && $productioncomplete) {
907 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
909 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
912 throw new RestException(500, $this->mo->error);
916 return $this->mo->id;