377 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
378 throw new RestException(403,
'Not enough permission');
380 $result = $this->mo->fetch(
$id);
382 throw new RestException(404,
'MO not found');
385 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
386 throw new RestException(405,
'Error bad status of MO');
390 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
391 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
392 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
399 $arraytoconsume = array();
400 $arraytoproduce = array();
402 foreach ($request_data as $field => $value) {
403 if ($field ==
'inventorylabel') {
404 $labelmovement = $value;
406 if ($field ==
'inventorycode') {
407 $codemovement = $value;
409 if ($field ==
'autoclose') {
412 if ($field ==
'arraytoconsume') {
413 $arraytoconsume = $value;
415 if ($field ==
'arraytoproduce') {
416 $arraytoproduce = $value;
418 if ($field ===
'caller') {
420 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
425 if (empty($labelmovement)) {
426 throw new RestException(500,
"Field inventorylabel not provided");
428 if (empty($codemovement)) {
429 throw new RestException(500,
"Field inventorycode not provided");
432 $consumptioncomplete =
true;
433 $productioncomplete =
true;
435 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
437 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
438 foreach ($arrayofarrayname as $arrayname) {
439 foreach (${$arrayname} as $value) {
440 $tmpproduct =
new Product($this->db);
441 if (empty($value[
"objectid"])) {
442 throw new RestException(500,
"Field objectid required in ".$arrayname);
444 $tmpproduct->fetch($value[
"qty"]);
445 if (empty($value[
"qty"])) {
446 throw new RestException(500,
"Field qty required in ".$arrayname);
448 if ($value[
"qty"] != 0) {
449 $qtytoprocess = $value[
"qty"];
450 if (isset($value[
"fk_warehouse"])) {
451 if (!($value[
"fk_warehouse"] > 0)) {
453 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
455 if ($tmpproduct->status_batch) {
457 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
461 if (!$error && $value[
"fk_warehouse"] > 0) {
463 $id_product_batch = 0;
465 $stockmove->setOrigin($this->mo->element, $this->mo->id);
467 if ($arrayname ==
'arraytoconsume') {
468 $moline =
new MoLine($this->db);
469 $moline->fk_mo = $this->mo->id;
470 $moline->position = $pos;
471 $moline->fk_product = $value[
"objectid"];
472 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
473 $moline->qty = $qtytoprocess;
474 $moline->batch = (
string) $tmpproduct->status_batch;
475 $moline->role =
'toproduce';
476 $moline->fk_mrp_production = 0;
477 $moline->fk_stock_movement = $idstockmove;
478 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
480 $resultmoline = $moline->create(DolibarrApiAccess::$user);
481 if ($resultmoline <= 0) {
483 throw new RestException(500, $moline->error);
485 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
487 $moline =
new MoLine($this->db);
488 $moline->fk_mo = $this->mo->id;
489 $moline->position = $pos;
490 $moline->fk_product = $value[
"objectid"];
491 $moline->fk_warehouse = $value[
"fk_warehouse"];
492 $moline->qty = $qtytoprocess;
493 $moline->batch = (
string) $tmpproduct->status_batch;
494 $moline->role =
'toconsume';
495 $moline->fk_mrp_production = 0;
496 $moline->fk_stock_movement = $idstockmove;
497 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
499 $resultmoline = $moline->create(DolibarrApiAccess::$user);
500 if ($resultmoline <= 0) {
502 throw new RestException(500, $moline->error);
504 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
506 if ($idstockmove < 0) {
508 throw new RestException(500, $stockmove->error);
513 $moline =
new MoLine($this->db);
514 $moline->fk_mo = $this->mo->id;
515 $moline->position = $pos;
516 $moline->fk_product = $value[
"objectid"];
517 $moline->fk_warehouse = $value[
"fk_warehouse"];
518 $moline->qty = $qtytoprocess;
519 $moline->batch = (
string) $tmpproduct->status_batch;
520 if ($arrayname ==
"arraytoconsume") {
521 $moline->role =
'consumed';
523 $moline->role =
'produced';
525 $moline->fk_mrp_production = 0;
526 $moline->fk_stock_movement = $idstockmove;
527 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
529 $resultmoline = $moline->create(DolibarrApiAccess::$user);
530 if ($resultmoline <= 0) {
532 throw new RestException(500, $moline->error);
541 if ($autoclose <= 0) {
542 $consumptioncomplete =
false;
543 $productioncomplete =
false;
548 foreach ($this->mo->lines as $line) {
549 if ($line->role ==
'toconsume') {
550 $tmpproduct =
new Product($this->db);
551 $tmpproduct->fetch($line->fk_product);
552 if ($line->qty != 0) {
553 $qtytoprocess = $line->qty;
554 if (isset($line->fk_warehouse)) {
555 if (!($line->fk_warehouse > 0)) {
556 $langs->load(
"errors");
558 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
560 if ($tmpproduct->status_batch) {
561 $langs->load(
"errors");
563 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
567 if (!$error && $line->fk_warehouse > 0) {
569 $id_product_batch = 0;
570 $stockmove->origin_type =
'mo';
571 $stockmove->origin_id = $this->mo->id;
572 if ($qtytoprocess >= 0) {
573 $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);
575 $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);
577 if ($idstockmove < 0) {
579 throw new RestException(500, $stockmove->error);
584 $moline =
new MoLine($this->db);
585 $moline->fk_mo = $this->mo->id;
586 $moline->position = $pos;
587 $moline->fk_product = $line->fk_product;
588 $moline->fk_warehouse = $line->fk_warehouse;
589 $moline->qty = $qtytoprocess;
590 $moline->batch = (
string) $tmpproduct->status_batch;
591 $moline->role =
'consumed';
592 $moline->fk_mrp_production = $line->id;
593 $moline->fk_stock_movement = $idstockmove;
594 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
596 $resultmoline = $moline->create(DolibarrApiAccess::$user);
597 if ($resultmoline <= 0) {
599 throw new RestException(500, $moline->error);
608 foreach ($this->mo->lines as $line) {
609 if ($line->role ==
'toproduce') {
610 $tmpproduct =
new Product($this->db);
611 $tmpproduct->fetch($line->fk_product);
612 if ($line->qty != 0) {
613 $qtytoprocess = $line->qty;
614 if (isset($line->fk_warehouse)) {
615 if (!($line->fk_warehouse > 0)) {
616 $langs->load(
"errors");
618 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), (
string) $tmpproduct->ref));
620 if ($tmpproduct->status_batch) {
621 $langs->load(
"errors");
623 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), (
string) $tmpproduct->ref));
627 if (!$error && $line->fk_warehouse > 0) {
629 $id_product_batch = 0;
630 $stockmove->origin_type =
'mo';
631 $stockmove->origin_id = $this->mo->id;
632 if ($qtytoprocess >= 0) {
633 $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);
635 $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);
637 if ($idstockmove < 0) {
639 throw new RestException(500, $stockmove->error);
644 $moline =
new MoLine($this->db);
645 $moline->fk_mo = $this->mo->id;
646 $moline->position = $pos;
647 $moline->fk_product = $line->fk_product;
648 $moline->fk_warehouse = $line->fk_warehouse;
649 $moline->qty = $qtytoprocess;
650 $moline->batch = (
string) $tmpproduct->status_batch;
651 $moline->role =
'produced';
652 $moline->fk_mrp_production = $line->id;
653 $moline->fk_stock_movement = $idstockmove;
654 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
656 $resultmoline = $moline->create(DolibarrApiAccess::$user);
657 if ($resultmoline <= 0) {
659 throw new RestException(500, $moline->error);
669 if ($autoclose > 0) {
670 foreach ($this->mo->lines as $line) {
671 if ($line->role ==
'toconsume') {
672 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
673 $alreadyconsumed = 0;
674 foreach ($arrayoflines as $line2) {
675 $alreadyconsumed += $line2[
'qty'];
678 if ($alreadyconsumed < $line->qty) {
679 $consumptioncomplete =
false;
682 if ($line->role ==
'toproduce') {
683 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
684 $alreadyproduced = 0;
685 foreach ($arrayoflines as $line2) {
686 $alreadyproduced += $line2[
'qty'];
689 if ($alreadyproduced < $line->qty) {
690 $productioncomplete =
false;
695 $consumptioncomplete =
false;
696 $productioncomplete =
false;
702 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
703 if ($consumptioncomplete && $productioncomplete) {
704 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
706 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
709 throw new RestException(500, $this->mo->error);
712 return $this->mo->id;
751 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
752 throw new RestException(403,
'Not enough permission');
754 $result = $this->mo->fetch(
$id);
756 throw new RestException(404,
'MO not found');
759 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
760 throw new RestException(405,
'Error bad status of MO');
764 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
765 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
766 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
773 $arraytoconsume = array();
774 $arraytoproduce = array();
776 foreach ($request_data as $field => $value) {
777 if ($field ==
'inventorylabel') {
778 $labelmovement = $value;
780 if ($field ==
'inventorycode') {
781 $codemovement = $value;
783 if ($field ==
'autoclose') {
786 if ($field ==
'arraytoconsume') {
787 $arraytoconsume = $value;
789 if ($field ==
'arraytoproduce') {
790 $arraytoproduce = $value;
792 if ($field ===
'caller') {
794 $stockmove->context[
'caller'] = $request_data[
'caller'];
799 if (empty($labelmovement)) {
800 throw new RestException(500,
"Field inventorylabel not provided");
802 if (empty($codemovement)) {
803 throw new RestException(500,
"Field inventorycode not provided");
809 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
810 foreach ($arrayofarrayname as $arrayname) {
811 foreach (${$arrayname} as $value) {
812 if (empty($value[
"objectid"])) {
813 throw new RestException(500,
"Field objectid required in " . $arrayname);
816 $molinetoprocess =
new MoLine($this->db);
817 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
818 if ($tmpmolineid <= 0) {
819 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
822 $tmpproduct =
new Product($this->db);
823 $tmpproduct->fetch($molinetoprocess->fk_product);
824 if ($tmpproduct->status_batch) {
825 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
828 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
829 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
831 $qtytoprocess = $value[
"qty"];
833 $fk_warehousetoprocess = 0;
834 if ($molinetoprocess->disable_stock_change ==
false) {
835 if (isset($value[
"fk_warehouse"])) {
836 if (!($value[
"fk_warehouse"] > 0)) {
837 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
840 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
844 if (isset($value[
"pricetoproduce"])) {
845 if ($value[
"pricetoproduce"] > 0) {
846 $pricetoproduce = $value[
"pricetoproduce"];
852 if ($molinetoprocess->disable_stock_change ==
false) {
854 $id_product_batch = 0;
855 $stockmove->origin_type =
'mo';
856 $stockmove->origin_id = $this->mo->id;
857 if ($arrayname ==
"arraytoconsume") {
858 if ($qtytoprocess >= 0) {
859 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
861 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
864 if ($qtytoprocess >= 0) {
865 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
867 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
870 if ($idstockmove <= 0) {
871 throw new RestException(500, $stockmove->error);
876 $moline =
new MoLine($this->db);
877 $moline->fk_mo = $this->mo->id;
878 $moline->position = $pos;
879 $moline->fk_product = $tmpproduct->id;
880 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
881 $moline->qty = $qtytoprocess;
883 $moline->fk_mrp_production = $molinetoprocess->id;
884 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
885 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
887 if ($arrayname ==
"arraytoconsume") {
888 $moline->role =
'consumed';
890 $moline->role =
'produced';
893 $resultmoline = $moline->create(DolibarrApiAccess::$user);
894 if ($resultmoline <= 0) {
895 throw new RestException(500, $moline->error);
902 $consumptioncomplete =
true;
903 $productioncomplete =
true;
905 if ($autoclose > 0) {
907 $this->mo->fetchLines();
909 foreach ($this->mo->lines as $line) {
910 if ($line->role ==
'toconsume') {
911 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
912 $alreadyconsumed = 0;
913 foreach ($arrayoflines as $line2) {
914 $alreadyconsumed += $line2[
'qty'];
917 if ($alreadyconsumed < $line->qty) {
918 $consumptioncomplete =
false;
921 if ($line->role ==
'toproduce') {
922 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
923 $alreadyproduced = 0;
924 foreach ($arrayoflines as $line2) {
925 $alreadyproduced += $line2[
'qty'];
928 if ($alreadyproduced < $line->qty) {
929 $productioncomplete =
false;
934 $consumptioncomplete =
false;
935 $productioncomplete =
false;
939 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
941 if ($consumptioncomplete && $productioncomplete) {
942 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
944 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
947 throw new RestException(500, $this->mo->error);
951 return $this->mo->id;