383 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
384 throw new RestException(403,
'Not enough permission');
386 $result = $this->mo->fetch(
$id);
388 throw new RestException(404,
'MO not found');
391 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
392 throw new RestException(405,
'Error bad status of MO');
396 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
397 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
398 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
405 $arraytoconsume = array();
406 $arraytoproduce = array();
408 foreach ($request_data as $field => $value) {
409 if ($field ==
'inventorylabel') {
410 $labelmovement = $value;
412 if ($field ==
'inventorycode') {
413 $codemovement = $value;
415 if ($field ==
'autoclose') {
418 if ($field ==
'arraytoconsume') {
419 $arraytoconsume = $value;
421 if ($field ==
'arraytoproduce') {
422 $arraytoproduce = $value;
424 if ($field ===
'caller') {
426 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
431 if (empty($labelmovement)) {
432 throw new RestException(500,
"Field inventorylabel not provided");
434 if (empty($codemovement)) {
435 throw new RestException(500,
"Field inventorycode not provided");
438 $consumptioncomplete =
true;
439 $productioncomplete =
true;
441 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
443 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
444 foreach ($arrayofarrayname as $arrayname) {
445 foreach (${$arrayname} as $value) {
446 $tmpproduct =
new Product($this->db);
447 if (empty($value[
"objectid"])) {
448 throw new RestException(500,
"Field objectid required in ".$arrayname);
450 $tmpproduct->fetch($value[
"qty"]);
451 if (empty($value[
"qty"])) {
452 throw new RestException(500,
"Field qty required in ".$arrayname);
454 if ($value[
"qty"] != 0) {
455 $qtytoprocess = $value[
"qty"];
456 if (isset($value[
"fk_warehouse"])) {
457 if (!($value[
"fk_warehouse"] > 0)) {
459 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
461 if ($tmpproduct->status_batch) {
463 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
467 if (!$error && $value[
"fk_warehouse"] > 0) {
469 $id_product_batch = 0;
471 $stockmove->setOrigin($this->mo->element, $this->mo->id);
473 if ($arrayname ==
'arraytoconsume') {
474 $moline =
new MoLine($this->db);
475 $moline->fk_mo = $this->mo->id;
476 $moline->position = $pos;
477 $moline->fk_product = $value[
"objectid"];
478 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
479 $moline->qty = $qtytoprocess;
480 $moline->batch = (string) $tmpproduct->status_batch;
481 $moline->role =
'toproduce';
482 $moline->fk_mrp_production = 0;
483 $moline->fk_stock_movement = $idstockmove;
484 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
486 $resultmoline = $moline->create(DolibarrApiAccess::$user);
487 if ($resultmoline <= 0) {
489 throw new RestException(500, $moline->error);
491 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
493 $moline =
new MoLine($this->db);
494 $moline->fk_mo = $this->mo->id;
495 $moline->position = $pos;
496 $moline->fk_product = $value[
"objectid"];
497 $moline->fk_warehouse = $value[
"fk_warehouse"];
498 $moline->qty = $qtytoprocess;
499 $moline->batch = (string) $tmpproduct->status_batch;
500 $moline->role =
'toconsume';
501 $moline->fk_mrp_production = 0;
502 $moline->fk_stock_movement = $idstockmove;
503 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
505 $resultmoline = $moline->create(DolibarrApiAccess::$user);
506 if ($resultmoline <= 0) {
508 throw new RestException(500, $moline->error);
510 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
512 if ($idstockmove < 0) {
514 throw new RestException(500, $stockmove->error);
519 $moline =
new MoLine($this->db);
520 $moline->fk_mo = $this->mo->id;
521 $moline->position = $pos;
522 $moline->fk_product = $value[
"objectid"];
523 $moline->fk_warehouse = $value[
"fk_warehouse"];
524 $moline->qty = $qtytoprocess;
525 $moline->batch = (string) $tmpproduct->status_batch;
526 if ($arrayname ==
"arraytoconsume") {
527 $moline->role =
'consumed';
529 $moline->role =
'produced';
531 $moline->fk_mrp_production = 0;
532 $moline->fk_stock_movement = $idstockmove;
533 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
535 $resultmoline = $moline->create(DolibarrApiAccess::$user);
536 if ($resultmoline <= 0) {
538 throw new RestException(500, $moline->error);
547 if ($autoclose <= 0) {
548 $consumptioncomplete =
false;
549 $productioncomplete =
false;
554 foreach ($this->mo->lines as $line) {
555 if ($line->role ==
'toconsume') {
556 $tmpproduct =
new Product($this->db);
557 $tmpproduct->fetch($line->fk_product);
558 if ($line->qty != 0) {
559 $qtytoprocess = $line->qty;
560 if (isset($line->fk_warehouse)) {
561 if (!($line->fk_warehouse > 0)) {
562 $langs->load(
"errors");
564 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
566 if ($tmpproduct->status_batch) {
567 $langs->load(
"errors");
569 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
573 if (!$error && $line->fk_warehouse > 0) {
575 $id_product_batch = 0;
576 $stockmove->origin_type =
'mo';
577 $stockmove->origin_id = $this->mo->id;
578 if ($qtytoprocess >= 0) {
579 $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);
581 $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);
583 if ($idstockmove < 0) {
585 throw new RestException(500, $stockmove->error);
590 $moline =
new MoLine($this->db);
591 $moline->fk_mo = $this->mo->id;
592 $moline->position = $pos;
593 $moline->fk_product = $line->fk_product;
594 $moline->fk_warehouse = $line->fk_warehouse;
595 $moline->qty = $qtytoprocess;
596 $moline->batch = (string) $tmpproduct->status_batch;
597 $moline->role =
'consumed';
598 $moline->fk_mrp_production = $line->id;
599 $moline->fk_stock_movement = $idstockmove;
600 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
602 $resultmoline = $moline->create(DolibarrApiAccess::$user);
603 if ($resultmoline <= 0) {
605 throw new RestException(500, $moline->error);
614 foreach ($this->mo->lines as $line) {
615 if ($line->role ==
'toproduce') {
616 $tmpproduct =
new Product($this->db);
617 $tmpproduct->fetch($line->fk_product);
618 if ($line->qty != 0) {
619 $qtytoprocess = $line->qty;
620 if (isset($line->fk_warehouse)) {
621 if (!($line->fk_warehouse > 0)) {
622 $langs->load(
"errors");
624 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
626 if ($tmpproduct->status_batch) {
627 $langs->load(
"errors");
629 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
633 if (!$error && $line->fk_warehouse > 0) {
635 $id_product_batch = 0;
636 $stockmove->origin_type =
'mo';
637 $stockmove->origin_id = $this->mo->id;
638 if ($qtytoprocess >= 0) {
639 $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);
641 $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);
643 if ($idstockmove < 0) {
645 throw new RestException(500, $stockmove->error);
650 $moline =
new MoLine($this->db);
651 $moline->fk_mo = $this->mo->id;
652 $moline->position = $pos;
653 $moline->fk_product = $line->fk_product;
654 $moline->fk_warehouse = $line->fk_warehouse;
655 $moline->qty = $qtytoprocess;
656 $moline->batch = (string) $tmpproduct->status_batch;
657 $moline->role =
'produced';
658 $moline->fk_mrp_production = $line->id;
659 $moline->fk_stock_movement = $idstockmove;
660 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
662 $resultmoline = $moline->create(DolibarrApiAccess::$user);
663 if ($resultmoline <= 0) {
665 throw new RestException(500, $moline->error);
675 if ($autoclose > 0) {
676 foreach ($this->mo->lines as $line) {
677 if ($line->role ==
'toconsume') {
678 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
679 $alreadyconsumed = 0;
680 foreach ($arrayoflines as $line2) {
681 $alreadyconsumed += $line2[
'qty'];
684 if ($alreadyconsumed < $line->qty) {
685 $consumptioncomplete =
false;
688 if ($line->role ==
'toproduce') {
689 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
690 $alreadyproduced = 0;
691 foreach ($arrayoflines as $line2) {
692 $alreadyproduced += $line2[
'qty'];
695 if ($alreadyproduced < $line->qty) {
696 $productioncomplete =
false;
701 $consumptioncomplete =
false;
702 $productioncomplete =
false;
708 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
709 if ($consumptioncomplete && $productioncomplete) {
710 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
712 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
715 throw new RestException(500, $this->mo->error);
718 return $this->mo->id;
757 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
758 throw new RestException(403,
'Not enough permission');
760 $result = $this->mo->fetch(
$id);
762 throw new RestException(404,
'MO not found');
765 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
766 throw new RestException(405,
'Error bad status of MO');
770 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
771 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
772 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
779 $arraytoconsume = array();
780 $arraytoproduce = array();
782 foreach ($request_data as $field => $value) {
783 if ($field ==
'inventorylabel') {
784 $labelmovement = $value;
786 if ($field ==
'inventorycode') {
787 $codemovement = $value;
789 if ($field ==
'autoclose') {
792 if ($field ==
'arraytoconsume') {
793 $arraytoconsume = $value;
795 if ($field ==
'arraytoproduce') {
796 $arraytoproduce = $value;
798 if ($field ===
'caller') {
800 $stockmove->context[
'caller'] = $request_data[
'caller'];
805 if (empty($labelmovement)) {
806 throw new RestException(500,
"Field inventorylabel not provided");
808 if (empty($codemovement)) {
809 throw new RestException(500,
"Field inventorycode not provided");
815 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
816 foreach ($arrayofarrayname as $arrayname) {
817 foreach (${$arrayname} as $value) {
818 if (empty($value[
"objectid"])) {
819 throw new RestException(500,
"Field objectid required in " . $arrayname);
822 $molinetoprocess =
new MoLine($this->db);
823 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
824 if ($tmpmolineid <= 0) {
825 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
828 $tmpproduct =
new Product($this->db);
829 $tmpproduct->fetch($molinetoprocess->fk_product);
830 if ($tmpproduct->status_batch) {
831 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
834 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
835 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
837 $qtytoprocess = $value[
"qty"];
839 $fk_warehousetoprocess = 0;
840 if ($molinetoprocess->disable_stock_change ==
false) {
841 if (isset($value[
"fk_warehouse"])) {
842 if (!($value[
"fk_warehouse"] > 0)) {
843 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
846 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
850 if (isset($value[
"pricetoproduce"])) {
851 if ($value[
"pricetoproduce"] > 0) {
852 $pricetoproduce = $value[
"pricetoproduce"];
858 if ($molinetoprocess->disable_stock_change ==
false) {
860 $id_product_batch = 0;
861 $stockmove->origin_type =
'mo';
862 $stockmove->origin_id = $this->mo->id;
863 if ($arrayname ==
"arraytoconsume") {
864 if ($qtytoprocess >= 0) {
865 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
867 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
870 if ($qtytoprocess >= 0) {
871 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
873 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
876 if ($idstockmove <= 0) {
877 throw new RestException(500, $stockmove->error);
882 $moline =
new MoLine($this->db);
883 $moline->fk_mo = $this->mo->id;
884 $moline->position = $pos;
885 $moline->fk_product = $tmpproduct->id;
886 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
887 $moline->qty = $qtytoprocess;
889 $moline->fk_mrp_production = $molinetoprocess->id;
890 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
891 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
893 if ($arrayname ==
"arraytoconsume") {
894 $moline->role =
'consumed';
896 $moline->role =
'produced';
899 $resultmoline = $moline->create(DolibarrApiAccess::$user);
900 if ($resultmoline <= 0) {
901 throw new RestException(500, $moline->error);
908 $consumptioncomplete =
true;
909 $productioncomplete =
true;
911 if ($autoclose > 0) {
913 $this->mo->fetchLines();
915 foreach ($this->mo->lines as $line) {
916 if ($line->role ==
'toconsume') {
917 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
918 $alreadyconsumed = 0;
919 foreach ($arrayoflines as $line2) {
920 $alreadyconsumed += $line2[
'qty'];
923 if ($alreadyconsumed < $line->qty) {
924 $consumptioncomplete =
false;
927 if ($line->role ==
'toproduce') {
928 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
929 $alreadyproduced = 0;
930 foreach ($arrayoflines as $line2) {
931 $alreadyproduced += $line2[
'qty'];
934 if ($alreadyproduced < $line->qty) {
935 $productioncomplete =
false;
940 $consumptioncomplete =
false;
941 $productioncomplete =
false;
945 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
947 if ($consumptioncomplete && $productioncomplete) {
948 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
950 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
953 throw new RestException(500, $this->mo->error);
957 return $this->mo->id;