456 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
457 throw new RestException(403,
'Not enough permission');
459 $result = $this->mo->fetch(
$id);
461 throw new RestException(404,
'MO not found');
464 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
465 throw new RestException(405,
'Error bad status of MO');
469 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
470 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
471 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
478 $arraytoconsume = array();
479 $arraytoproduce = array();
481 foreach ($request_data as $field => $value) {
482 if ($field ==
'inventorylabel') {
483 $labelmovement = $value;
485 if ($field ==
'inventorycode') {
486 $codemovement = $value;
488 if ($field ==
'autoclose') {
491 if ($field ==
'arraytoconsume') {
492 $arraytoconsume = $value;
494 if ($field ==
'arraytoproduce') {
495 $arraytoproduce = $value;
497 if ($field ===
'caller') {
499 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
504 if (empty($labelmovement)) {
505 throw new RestException(500,
"Field inventorylabel not provided");
507 if (empty($codemovement)) {
508 throw new RestException(500,
"Field inventorycode not provided");
511 $consumptioncomplete =
true;
512 $productioncomplete =
true;
514 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
516 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
517 foreach ($arrayofarrayname as $arrayname) {
518 foreach (${$arrayname} as $value) {
519 $tmpproduct =
new Product($this->db);
520 if (empty($value[
"objectid"])) {
521 throw new RestException(500,
"Field objectid required in ".$arrayname);
523 $tmpproduct->fetch($value[
"qty"]);
524 if (empty($value[
"qty"])) {
525 throw new RestException(500,
"Field qty required in ".$arrayname);
527 if ($value[
"qty"] != 0) {
528 $qtytoprocess = $value[
"qty"];
529 if (isset($value[
"fk_warehouse"])) {
530 if (!($value[
"fk_warehouse"] > 0)) {
532 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
534 if ($tmpproduct->status_batch) {
536 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
540 if (!$error && $value[
"fk_warehouse"] > 0) {
542 $id_product_batch = 0;
544 $stockmove->setOrigin($this->mo->element, $this->mo->id);
546 if ($arrayname ==
'arraytoconsume') {
547 $moline =
new MoLine($this->db);
548 $moline->fk_mo = $this->mo->id;
549 $moline->position = $pos;
550 $moline->fk_product = $value[
"objectid"];
551 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
552 $moline->qty = $qtytoprocess;
553 $moline->batch = (
string) $tmpproduct->status_batch;
554 $moline->role =
'toproduce';
555 $moline->fk_mrp_production = 0;
556 $moline->fk_stock_movement = $idstockmove;
557 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
559 $resultmoline = $moline->create(DolibarrApiAccess::$user);
560 if ($resultmoline <= 0) {
562 throw new RestException(500, $moline->error);
564 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
566 $moline =
new MoLine($this->db);
567 $moline->fk_mo = $this->mo->id;
568 $moline->position = $pos;
569 $moline->fk_product = $value[
"objectid"];
570 $moline->fk_warehouse = $value[
"fk_warehouse"];
571 $moline->qty = $qtytoprocess;
572 $moline->batch = (
string) $tmpproduct->status_batch;
573 $moline->role =
'toconsume';
574 $moline->fk_mrp_production = 0;
575 $moline->fk_stock_movement = $idstockmove;
576 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
578 $resultmoline = $moline->create(DolibarrApiAccess::$user);
579 if ($resultmoline <= 0) {
581 throw new RestException(500, $moline->error);
583 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
585 if ($idstockmove < 0) {
587 throw new RestException(500, $stockmove->error);
592 $moline =
new MoLine($this->db);
593 $moline->fk_mo = $this->mo->id;
594 $moline->position = $pos;
595 $moline->fk_product = $value[
"objectid"];
596 $moline->fk_warehouse = $value[
"fk_warehouse"];
597 $moline->qty = $qtytoprocess;
598 $moline->batch = (
string) $tmpproduct->status_batch;
599 if ($arrayname ==
"arraytoconsume") {
600 $moline->role =
'consumed';
602 $moline->role =
'produced';
604 $moline->fk_mrp_production = 0;
605 $moline->fk_stock_movement = $idstockmove;
606 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
608 $resultmoline = $moline->create(DolibarrApiAccess::$user);
609 if ($resultmoline <= 0) {
611 throw new RestException(500, $moline->error);
620 if ($autoclose <= 0) {
621 $consumptioncomplete =
false;
622 $productioncomplete =
false;
627 foreach ($this->mo->lines as $line) {
628 if ($line->role ==
'toconsume') {
629 $tmpproduct =
new Product($this->db);
630 $tmpproduct->fetch($line->fk_product);
631 if ($line->qty != 0) {
632 $qtytoprocess = $line->qty;
633 if (isset($line->fk_warehouse)) {
634 if (!($line->fk_warehouse > 0)) {
635 $langs->load(
"errors");
637 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
639 if ($tmpproduct->status_batch) {
640 $langs->load(
"errors");
642 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
646 if (!$error && $line->fk_warehouse > 0) {
648 $id_product_batch = 0;
649 $stockmove->origin_type =
'mo';
650 $stockmove->origin_id = $this->mo->id;
651 if ($qtytoprocess >= 0) {
652 $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);
654 $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);
656 if ($idstockmove < 0) {
658 throw new RestException(500, $stockmove->error);
663 $moline =
new MoLine($this->db);
664 $moline->fk_mo = $this->mo->id;
665 $moline->position = $pos;
666 $moline->fk_product = $line->fk_product;
667 $moline->fk_warehouse = $line->fk_warehouse;
668 $moline->qty = $qtytoprocess;
669 $moline->batch = (
string) $tmpproduct->status_batch;
670 $moline->role =
'consumed';
671 $moline->fk_mrp_production = $line->id;
672 $moline->fk_stock_movement = $idstockmove;
673 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
675 $resultmoline = $moline->create(DolibarrApiAccess::$user);
676 if ($resultmoline <= 0) {
678 throw new RestException(500, $moline->error);
687 foreach ($this->mo->lines as $line) {
688 if ($line->role ==
'toproduce') {
689 $tmpproduct =
new Product($this->db);
690 $tmpproduct->fetch($line->fk_product);
691 if ($line->qty != 0) {
692 $qtytoprocess = $line->qty;
693 if (isset($line->fk_warehouse)) {
694 if (!($line->fk_warehouse > 0)) {
695 $langs->load(
"errors");
697 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
699 if ($tmpproduct->status_batch) {
700 $langs->load(
"errors");
702 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
706 if (!$error && $line->fk_warehouse > 0) {
708 $id_product_batch = 0;
709 $stockmove->origin_type =
'mo';
710 $stockmove->origin_id = $this->mo->id;
711 if ($qtytoprocess >= 0) {
712 $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);
714 $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);
716 if ($idstockmove < 0) {
718 throw new RestException(500, $stockmove->error);
723 $moline =
new MoLine($this->db);
724 $moline->fk_mo = $this->mo->id;
725 $moline->position = $pos;
726 $moline->fk_product = $line->fk_product;
727 $moline->fk_warehouse = $line->fk_warehouse;
728 $moline->qty = $qtytoprocess;
729 $moline->batch = (
string) $tmpproduct->status_batch;
730 $moline->role =
'produced';
731 $moline->fk_mrp_production = $line->id;
732 $moline->fk_stock_movement = $idstockmove;
733 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
735 $resultmoline = $moline->create(DolibarrApiAccess::$user);
736 if ($resultmoline <= 0) {
738 throw new RestException(500, $moline->error);
748 if ($autoclose > 0) {
749 foreach ($this->mo->lines as $line) {
750 if ($line->role ==
'toconsume') {
751 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
752 $alreadyconsumed = 0;
753 foreach ($arrayoflines as $line2) {
754 $alreadyconsumed += $line2[
'qty'];
757 if ($alreadyconsumed < $line->qty) {
758 $consumptioncomplete =
false;
761 if ($line->role ==
'toproduce') {
762 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
763 $alreadyproduced = 0;
764 foreach ($arrayoflines as $line2) {
765 $alreadyproduced += $line2[
'qty'];
768 if ($alreadyproduced < $line->qty) {
769 $productioncomplete =
false;
774 $consumptioncomplete =
false;
775 $productioncomplete =
false;
781 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
782 if ($consumptioncomplete && $productioncomplete) {
783 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
785 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
788 throw new RestException(500, $this->mo->error);
791 return $this->mo->id;
830 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
831 throw new RestException(403,
'Not enough permission');
833 $result = $this->mo->fetch(
$id);
835 throw new RestException(404,
'MO not found');
838 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
839 throw new RestException(405,
'Error bad status of MO');
843 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
844 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
845 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
852 $arraytoconsume = array();
853 $arraytoproduce = array();
855 foreach ($request_data as $field => $value) {
856 if ($field ==
'inventorylabel') {
857 $labelmovement = $value;
859 if ($field ==
'inventorycode') {
860 $codemovement = $value;
862 if ($field ==
'autoclose') {
865 if ($field ==
'arraytoconsume') {
866 $arraytoconsume = $value;
868 if ($field ==
'arraytoproduce') {
869 $arraytoproduce = $value;
871 if ($field ===
'caller') {
873 $stockmove->context[
'caller'] = $request_data[
'caller'];
878 if (empty($labelmovement)) {
879 throw new RestException(500,
"Field inventorylabel not provided");
881 if (empty($codemovement)) {
882 throw new RestException(500,
"Field inventorycode not provided");
888 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
889 foreach ($arrayofarrayname as $arrayname) {
890 foreach (${$arrayname} as $value) {
891 if (empty($value[
"objectid"])) {
892 throw new RestException(500,
"Field objectid required in " . $arrayname);
895 $molinetoprocess =
new MoLine($this->db);
896 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
897 if ($tmpmolineid <= 0) {
898 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
901 $tmpproduct =
new Product($this->db);
902 $tmpproduct->fetch($molinetoprocess->fk_product);
903 if ($tmpproduct->status_batch) {
904 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
907 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
908 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
910 $qtytoprocess = $value[
"qty"];
912 $fk_warehousetoprocess = 0;
913 if ($molinetoprocess->disable_stock_change ==
false) {
914 if (isset($value[
"fk_warehouse"])) {
915 if (!($value[
"fk_warehouse"] > 0)) {
916 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
919 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
923 if (isset($value[
"pricetoproduce"])) {
924 if ($value[
"pricetoproduce"] > 0) {
925 $pricetoproduce = $value[
"pricetoproduce"];
931 if ($molinetoprocess->disable_stock_change ==
false) {
933 $id_product_batch = 0;
934 $stockmove->origin_type =
'mo';
935 $stockmove->origin_id = $this->mo->id;
936 if ($arrayname ==
"arraytoconsume") {
937 if ($qtytoprocess >= 0) {
938 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
940 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
943 if ($qtytoprocess >= 0) {
944 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
946 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
949 if ($idstockmove <= 0) {
950 throw new RestException(500, $stockmove->error);
955 $moline =
new MoLine($this->db);
956 $moline->fk_mo = $this->mo->id;
957 $moline->position = $pos;
958 $moline->fk_product = $tmpproduct->id;
959 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
960 $moline->qty = $qtytoprocess;
962 $moline->fk_mrp_production = $molinetoprocess->id;
963 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
964 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
966 if ($arrayname ==
"arraytoconsume") {
967 $moline->role =
'consumed';
969 $moline->role =
'produced';
972 $resultmoline = $moline->create(DolibarrApiAccess::$user);
973 if ($resultmoline <= 0) {
974 throw new RestException(500, $moline->error);
981 $consumptioncomplete =
true;
982 $productioncomplete =
true;
984 if ($autoclose > 0) {
986 $this->mo->fetchLines();
988 foreach ($this->mo->lines as $line) {
989 if ($line->role ==
'toconsume') {
990 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
991 $alreadyconsumed = 0;
992 foreach ($arrayoflines as $line2) {
993 $alreadyconsumed += $line2[
'qty'];
996 if ($alreadyconsumed < $line->qty) {
997 $consumptioncomplete =
false;
1000 if ($line->role ==
'toproduce') {
1001 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
1002 $alreadyproduced = 0;
1003 foreach ($arrayoflines as $line2) {
1004 $alreadyproduced += $line2[
'qty'];
1007 if ($alreadyproduced < $line->qty) {
1008 $productioncomplete =
false;
1013 $consumptioncomplete =
false;
1014 $productioncomplete =
false;
1018 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
1020 if ($consumptioncomplete && $productioncomplete) {
1021 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
1023 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
1026 throw new RestException(500, $this->mo->error);
1029 $this->db->commit();
1030 return $this->mo->id;