418 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
419 throw new RestException(403,
'Not enough permission');
421 $result = $this->mo->fetch(
$id);
423 throw new RestException(404,
'MO not found');
426 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
427 throw new RestException(405,
'Error bad status of MO');
431 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
432 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
433 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
440 $arraytoconsume = array();
441 $arraytoproduce = array();
443 foreach ($request_data as $field => $value) {
444 if ($field ==
'inventorylabel') {
445 $labelmovement = $value;
447 if ($field ==
'inventorycode') {
448 $codemovement = $value;
450 if ($field ==
'autoclose') {
453 if ($field ==
'arraytoconsume') {
454 $arraytoconsume = $value;
456 if ($field ==
'arraytoproduce') {
457 $arraytoproduce = $value;
459 if ($field ===
'caller') {
461 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
466 if (empty($labelmovement)) {
467 throw new RestException(500,
"Field inventorylabel not provided");
469 if (empty($codemovement)) {
470 throw new RestException(500,
"Field inventorycode not provided");
473 $consumptioncomplete =
true;
474 $productioncomplete =
true;
476 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
478 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
479 foreach ($arrayofarrayname as $arrayname) {
480 foreach (${$arrayname} as $value) {
481 $tmpproduct =
new Product($this->db);
482 if (empty($value[
"objectid"])) {
483 throw new RestException(500,
"Field objectid required in ".$arrayname);
485 $tmpproduct->fetch($value[
"objectid"]);
486 if (empty($value[
"qty"])) {
487 throw new RestException(500,
"Field qty required in ".$arrayname);
489 if ($value[
"qty"] != 0) {
490 $qtytoprocess = $value[
"qty"];
491 if (isset($value[
"fk_warehouse"])) {
492 if (!($value[
"fk_warehouse"] > 0)) {
494 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
496 if ($tmpproduct->status_batch) {
498 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
502 if (!$error && $value[
"fk_warehouse"] > 0) {
504 $id_product_batch = 0;
506 $stockmove->setOrigin($this->mo->element, $this->mo->id);
510 if ($arrayname ==
'arraytoconsume') {
511 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
513 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
515 if ($idstockmove < 0) {
517 throw new RestException(500, $stockmove->error);
520 $moline =
new MoLine($this->db);
521 $moline->fk_mo = $this->mo->id;
522 $moline->position = $pos;
523 $moline->fk_product = $value[
"objectid"];
524 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
525 $moline->qty = $qtytoprocess;
526 $moline->batch = (
string) $tmpproduct->status_batch;
527 $moline->role = ($arrayname ==
'arraytoconsume' ?
'toproduce' :
'toconsume');
528 $moline->fk_mrp_production = 0;
529 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
530 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
532 $resultmoline = $moline->create(DolibarrApiAccess::$user);
533 if ($resultmoline <= 0) {
535 throw new RestException(500, $moline->error ? $moline->error : implode(
', ', $moline->errors));
540 $moline =
new MoLine($this->db);
541 $moline->fk_mo = $this->mo->id;
542 $moline->position = $pos;
543 $moline->fk_product = $value[
"objectid"];
544 $moline->fk_warehouse = $value[
"fk_warehouse"];
545 $moline->qty = $qtytoprocess;
546 $moline->batch = (
string) $tmpproduct->status_batch;
547 if ($arrayname ==
"arraytoconsume") {
548 $moline->role =
'consumed';
550 $moline->role =
'produced';
552 $moline->fk_mrp_production = 0;
553 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
554 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
556 $resultmoline = $moline->create(DolibarrApiAccess::$user);
557 if ($resultmoline <= 0) {
559 throw new RestException(500, $moline->error ? $moline->error : implode(
', ', $moline->errors));
568 if ($autoclose <= 0) {
569 $consumptioncomplete =
false;
570 $productioncomplete =
false;
575 foreach ($this->mo->lines as $line) {
576 if ($line->role ==
'toconsume') {
577 $tmpproduct =
new Product($this->db);
578 $tmpproduct->fetch($line->fk_product);
579 if ($line->qty != 0) {
580 $qtytoprocess = $line->qty;
581 if (isset($line->fk_warehouse)) {
582 if (!($line->fk_warehouse > 0)) {
583 $langs->load(
"errors");
585 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
587 if ($tmpproduct->status_batch) {
588 $langs->load(
"errors");
590 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
594 if (!$error && $line->fk_warehouse > 0) {
596 $id_product_batch = 0;
597 $stockmove->origin_type =
'mo';
598 $stockmove->origin_id = $this->mo->id;
599 if ($qtytoprocess >= 0) {
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 $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);
604 if ($idstockmove < 0) {
606 throw new RestException(500, $stockmove->error);
611 $moline =
new MoLine($this->db);
612 $moline->fk_mo = $this->mo->id;
613 $moline->position = $pos;
614 $moline->fk_product = $line->fk_product;
615 $moline->fk_warehouse = $line->fk_warehouse;
616 $moline->qty = $qtytoprocess;
617 $moline->batch = (
string) $tmpproduct->status_batch;
618 $moline->role =
'consumed';
619 $moline->fk_mrp_production = $line->id;
620 $moline->fk_stock_movement = $idstockmove;
621 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
623 $resultmoline = $moline->create(DolibarrApiAccess::$user);
624 if ($resultmoline <= 0) {
626 throw new RestException(500, $moline->error);
635 foreach ($this->mo->lines as $line) {
636 if ($line->role ==
'toproduce') {
637 $tmpproduct =
new Product($this->db);
638 $tmpproduct->fetch($line->fk_product);
639 if ($line->qty != 0) {
640 $qtytoprocess = $line->qty;
641 if (isset($line->fk_warehouse)) {
642 if (!($line->fk_warehouse > 0)) {
643 $langs->load(
"errors");
645 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
647 if ($tmpproduct->status_batch) {
648 $langs->load(
"errors");
650 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
654 if (!$error && $line->fk_warehouse > 0) {
656 $id_product_batch = 0;
657 $stockmove->origin_type =
'mo';
658 $stockmove->origin_id = $this->mo->id;
659 if ($qtytoprocess >= 0) {
667 foreach ($this->mo->lines as $consumedline) {
668 if ($consumedline->role ==
'toconsume') {
669 $consumedproduct =
new Product($this->db);
670 $consumedproduct->fetch($consumedline->fk_product);
671 $consumedcost =
price2num(!empty($consumedproduct->cost_price) ? $consumedproduct->cost_price : $consumedproduct->pmp);
672 if (empty($consumedcost)) {
673 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
675 if ($productFournisseur->find_min_price_product_fournisseur($consumedline->fk_product, $consumedline->qty) > 0) {
676 $consumedcost = $productFournisseur->fourn_unitprice;
679 $mfgcost +=
price2num(($consumedline->qty * $consumedcost) / ($this->mo->qty > 0 ? $this->mo->qty : 1),
'MU');
682 $mfgcost = (float)
price2num($mfgcost,
'MU');
683 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, (
int) $line->fk_warehouse, $qtytoprocess, $mfgcost, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
685 $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);
687 if ($idstockmove < 0) {
689 throw new RestException(500, $stockmove->error);
694 $moline =
new MoLine($this->db);
695 $moline->fk_mo = $this->mo->id;
696 $moline->position = $pos;
697 $moline->fk_product = $line->fk_product;
698 $moline->fk_warehouse = $line->fk_warehouse;
699 $moline->qty = $qtytoprocess;
700 $moline->batch = (
string) $tmpproduct->status_batch;
701 $moline->role =
'produced';
702 $moline->fk_mrp_production = $line->id;
703 $moline->fk_stock_movement = $idstockmove;
704 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
706 $resultmoline = $moline->create(DolibarrApiAccess::$user);
707 if ($resultmoline <= 0) {
709 throw new RestException(500, $moline->error);
719 if ($autoclose > 0) {
720 foreach ($this->mo->lines as $line) {
721 if ($line->role ==
'toconsume') {
722 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
723 $alreadyconsumed = 0;
724 foreach ($arrayoflines as $line2) {
725 $alreadyconsumed += $line2[
'qty'];
728 if ($alreadyconsumed < $line->qty) {
729 $consumptioncomplete =
false;
732 if ($line->role ==
'toproduce') {
733 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
734 $alreadyproduced = 0;
735 foreach ($arrayoflines as $line2) {
736 $alreadyproduced += $line2[
'qty'];
739 if ($alreadyproduced < $line->qty) {
740 $productioncomplete =
false;
745 $consumptioncomplete =
false;
746 $productioncomplete =
false;
752 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
753 if ($consumptioncomplete && $productioncomplete) {
754 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
756 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
759 throw new RestException(500, $this->mo->error);
762 return $this->mo->id;
801 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
802 throw new RestException(403,
'Not enough permission');
804 $result = $this->mo->fetch(
$id);
806 throw new RestException(404,
'MO not found');
809 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
810 throw new RestException(405,
'Error bad status of MO');
814 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
815 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
816 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
823 $arraytoconsume = array();
824 $arraytoproduce = array();
826 foreach ($request_data as $field => $value) {
827 if ($field ==
'inventorylabel') {
828 $labelmovement = $value;
830 if ($field ==
'inventorycode') {
831 $codemovement = $value;
833 if ($field ==
'autoclose') {
836 if ($field ==
'arraytoconsume') {
837 $arraytoconsume = $value;
839 if ($field ==
'arraytoproduce') {
840 $arraytoproduce = $value;
842 if ($field ===
'caller') {
844 $stockmove->context[
'caller'] = $request_data[
'caller'];
849 if (empty($labelmovement)) {
850 throw new RestException(500,
"Field inventorylabel not provided");
852 if (empty($codemovement)) {
853 throw new RestException(500,
"Field inventorycode not provided");
859 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
860 foreach ($arrayofarrayname as $arrayname) {
861 foreach (${$arrayname} as $value) {
862 if (empty($value[
"objectid"])) {
863 throw new RestException(500,
"Field objectid required in " . $arrayname);
866 $molinetoprocess =
new MoLine($this->db);
867 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
868 if ($tmpmolineid <= 0) {
869 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
872 $tmpproduct =
new Product($this->db);
873 $tmpproduct->fetch($molinetoprocess->fk_product);
874 if ($tmpproduct->status_batch) {
875 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
878 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
879 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
881 $qtytoprocess = $value[
"qty"];
883 $fk_warehousetoprocess = 0;
884 if ($molinetoprocess->disable_stock_change ==
false) {
885 if (isset($value[
"fk_warehouse"])) {
886 if (!($value[
"fk_warehouse"] > 0)) {
887 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
890 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
894 if (isset($value[
"pricetoproduce"])) {
895 if ($value[
"pricetoproduce"] > 0) {
896 $pricetoproduce = $value[
"pricetoproduce"];
902 if ($molinetoprocess->disable_stock_change ==
false) {
904 $id_product_batch = 0;
905 $stockmove->origin_type =
'mo';
906 $stockmove->origin_id = $this->mo->id;
907 if ($arrayname ==
"arraytoconsume") {
908 if ($qtytoprocess >= 0) {
909 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
911 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
914 if ($qtytoprocess >= 0) {
915 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
917 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
920 if ($idstockmove <= 0) {
921 throw new RestException(500, $stockmove->error);
926 $moline =
new MoLine($this->db);
927 $moline->fk_mo = $this->mo->id;
928 $moline->position = $pos;
929 $moline->fk_product = $tmpproduct->id;
930 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
931 $moline->qty = $qtytoprocess;
933 $moline->fk_mrp_production = $molinetoprocess->id;
934 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
935 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
937 if ($arrayname ==
"arraytoconsume") {
938 $moline->role =
'consumed';
940 $moline->role =
'produced';
943 $resultmoline = $moline->create(DolibarrApiAccess::$user);
944 if ($resultmoline <= 0) {
945 throw new RestException(500, $moline->error);
952 $consumptioncomplete =
true;
953 $productioncomplete =
true;
955 if ($autoclose > 0) {
957 $this->mo->fetchLines();
959 foreach ($this->mo->lines as $line) {
960 if ($line->role ==
'toconsume') {
961 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
962 $alreadyconsumed = 0;
963 foreach ($arrayoflines as $line2) {
964 $alreadyconsumed += $line2[
'qty'];
967 if ($alreadyconsumed < $line->qty) {
968 $consumptioncomplete =
false;
971 if ($line->role ==
'toproduce') {
972 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
973 $alreadyproduced = 0;
974 foreach ($arrayoflines as $line2) {
975 $alreadyproduced += $line2[
'qty'];
978 if ($alreadyproduced < $line->qty) {
979 $productioncomplete =
false;
984 $consumptioncomplete =
false;
985 $productioncomplete =
false;
989 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
991 if ($consumptioncomplete && $productioncomplete) {
992 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
994 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
997 throw new RestException(500, $this->mo->error);
1000 $this->db->commit();
1001 return $this->mo->id;