385 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
386 throw new RestException(403,
'Not enough permission');
388 $result = $this->mo->fetch(
$id);
390 throw new RestException(404,
'MO not found');
393 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
394 throw new RestException(405,
'Error bad status of MO');
398 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
399 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
400 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
407 $arraytoconsume = array();
408 $arraytoproduce = array();
410 foreach ($request_data as $field => $value) {
411 if ($field ==
'inventorylabel') {
412 $labelmovement = $value;
414 if ($field ==
'inventorycode') {
415 $codemovement = $value;
417 if ($field ==
'autoclose') {
420 if ($field ==
'arraytoconsume') {
421 $arraytoconsume = $value;
423 if ($field ==
'arraytoproduce') {
424 $arraytoproduce = $value;
426 if ($field ===
'caller') {
428 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
433 if (empty($labelmovement)) {
434 throw new RestException(500,
"Field inventorylabel not provided");
436 if (empty($codemovement)) {
437 throw new RestException(500,
"Field inventorycode not provided");
440 $consumptioncomplete =
true;
441 $productioncomplete =
true;
443 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
445 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
446 foreach ($arrayofarrayname as $arrayname) {
447 foreach (${$arrayname} as $value) {
448 $tmpproduct =
new Product($this->db);
449 if (empty($value[
"objectid"])) {
450 throw new RestException(500,
"Field objectid required in ".$arrayname);
452 $tmpproduct->fetch($value[
"qty"]);
453 if (empty($value[
"qty"])) {
454 throw new RestException(500,
"Field qty required in ".$arrayname);
456 if ($value[
"qty"] != 0) {
457 $qtytoprocess = $value[
"qty"];
458 if (isset($value[
"fk_warehouse"])) {
459 if (!($value[
"fk_warehouse"] > 0)) {
461 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
463 if ($tmpproduct->status_batch) {
465 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
469 if (!$error && $value[
"fk_warehouse"] > 0) {
471 $id_product_batch = 0;
473 $stockmove->setOrigin($this->mo->element, $this->mo->id);
475 if ($arrayname ==
'arraytoconsume') {
476 $moline =
new MoLine($this->db);
477 $moline->fk_mo = $this->mo->id;
478 $moline->position = $pos;
479 $moline->fk_product = $value[
"objectid"];
480 $moline->fk_warehouse = (int) $value[
"fk_warehouse"];
481 $moline->qty = $qtytoprocess;
482 $moline->batch = (string) $tmpproduct->status_batch;
483 $moline->role =
'toproduce';
484 $moline->fk_mrp_production = 0;
485 $moline->fk_stock_movement = $idstockmove;
486 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
488 $resultmoline = $moline->create(DolibarrApiAccess::$user);
489 if ($resultmoline <= 0) {
491 throw new RestException(500, $moline->error);
493 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (
string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
495 $moline =
new MoLine($this->db);
496 $moline->fk_mo = $this->mo->id;
497 $moline->position = $pos;
498 $moline->fk_product = $value[
"objectid"];
499 $moline->fk_warehouse = $value[
"fk_warehouse"];
500 $moline->qty = $qtytoprocess;
501 $moline->batch = (string) $tmpproduct->status_batch;
502 $moline->role =
'toconsume';
503 $moline->fk_mrp_production = 0;
504 $moline->fk_stock_movement = $idstockmove;
505 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
507 $resultmoline = $moline->create(DolibarrApiAccess::$user);
508 if ($resultmoline <= 0) {
510 throw new RestException(500, $moline->error);
512 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
514 if ($idstockmove < 0) {
516 throw new RestException(500, $stockmove->error);
521 $moline =
new MoLine($this->db);
522 $moline->fk_mo = $this->mo->id;
523 $moline->position = $pos;
524 $moline->fk_product = $value[
"objectid"];
525 $moline->fk_warehouse = $value[
"fk_warehouse"];
526 $moline->qty = $qtytoprocess;
527 $moline->batch = (string) $tmpproduct->status_batch;
528 if ($arrayname ==
"arraytoconsume") {
529 $moline->role =
'consumed';
531 $moline->role =
'produced';
533 $moline->fk_mrp_production = 0;
534 $moline->fk_stock_movement = $idstockmove;
535 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
537 $resultmoline = $moline->create(DolibarrApiAccess::$user);
538 if ($resultmoline <= 0) {
540 throw new RestException(500, $moline->error);
549 if ($autoclose <= 0) {
550 $consumptioncomplete =
false;
551 $productioncomplete =
false;
556 foreach ($this->mo->lines as $line) {
557 if ($line->role ==
'toconsume') {
558 $tmpproduct =
new Product($this->db);
559 $tmpproduct->fetch($line->fk_product);
560 if ($line->qty != 0) {
561 $qtytoprocess = $line->qty;
562 if (isset($line->fk_warehouse)) {
563 if (!($line->fk_warehouse > 0)) {
564 $langs->load(
"errors");
566 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
568 if ($tmpproduct->status_batch) {
569 $langs->load(
"errors");
571 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
575 if (!$error && $line->fk_warehouse > 0) {
577 $id_product_batch = 0;
578 $stockmove->origin_type =
'mo';
579 $stockmove->origin_id = $this->mo->id;
580 if ($qtytoprocess >= 0) {
581 $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);
583 $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);
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 = $line->fk_product;
596 $moline->fk_warehouse = $line->fk_warehouse;
597 $moline->qty = $qtytoprocess;
598 $moline->batch = (string) $tmpproduct->status_batch;
599 $moline->role =
'consumed';
600 $moline->fk_mrp_production = $line->id;
601 $moline->fk_stock_movement = $idstockmove;
602 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
604 $resultmoline = $moline->create(DolibarrApiAccess::$user);
605 if ($resultmoline <= 0) {
607 throw new RestException(500, $moline->error);
616 foreach ($this->mo->lines as $line) {
617 if ($line->role ==
'toproduce') {
618 $tmpproduct =
new Product($this->db);
619 $tmpproduct->fetch($line->fk_product);
620 if ($line->qty != 0) {
621 $qtytoprocess = $line->qty;
622 if (isset($line->fk_warehouse)) {
623 if (!($line->fk_warehouse > 0)) {
624 $langs->load(
"errors");
626 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
628 if ($tmpproduct->status_batch) {
629 $langs->load(
"errors");
631 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
635 if (!$error && $line->fk_warehouse > 0) {
637 $id_product_batch = 0;
638 $stockmove->origin_type =
'mo';
639 $stockmove->origin_id = $this->mo->id;
640 if ($qtytoprocess >= 0) {
641 $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);
643 $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);
645 if ($idstockmove < 0) {
647 throw new RestException(500, $stockmove->error);
652 $moline =
new MoLine($this->db);
653 $moline->fk_mo = $this->mo->id;
654 $moline->position = $pos;
655 $moline->fk_product = $line->fk_product;
656 $moline->fk_warehouse = $line->fk_warehouse;
657 $moline->qty = $qtytoprocess;
658 $moline->batch = (string) $tmpproduct->status_batch;
659 $moline->role =
'produced';
660 $moline->fk_mrp_production = $line->id;
661 $moline->fk_stock_movement = $idstockmove;
662 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
664 $resultmoline = $moline->create(DolibarrApiAccess::$user);
665 if ($resultmoline <= 0) {
667 throw new RestException(500, $moline->error);
677 if ($autoclose > 0) {
678 foreach ($this->mo->lines as $line) {
679 if ($line->role ==
'toconsume') {
680 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
681 $alreadyconsumed = 0;
682 foreach ($arrayoflines as $line2) {
683 $alreadyconsumed += $line2[
'qty'];
686 if ($alreadyconsumed < $line->qty) {
687 $consumptioncomplete =
false;
690 if ($line->role ==
'toproduce') {
691 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
692 $alreadyproduced = 0;
693 foreach ($arrayoflines as $line2) {
694 $alreadyproduced += $line2[
'qty'];
697 if ($alreadyproduced < $line->qty) {
698 $productioncomplete =
false;
703 $consumptioncomplete =
false;
704 $productioncomplete =
false;
710 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
711 if ($consumptioncomplete && $productioncomplete) {
712 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
714 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
717 throw new RestException(500, $this->mo->error);
720 return $this->mo->id;
759 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
760 throw new RestException(403,
'Not enough permission');
762 $result = $this->mo->fetch(
$id);
764 throw new RestException(404,
'MO not found');
767 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
768 throw new RestException(405,
'Error bad status of MO');
772 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
773 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
774 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
781 $arraytoconsume = array();
782 $arraytoproduce = array();
784 foreach ($request_data as $field => $value) {
785 if ($field ==
'inventorylabel') {
786 $labelmovement = $value;
788 if ($field ==
'inventorycode') {
789 $codemovement = $value;
791 if ($field ==
'autoclose') {
794 if ($field ==
'arraytoconsume') {
795 $arraytoconsume = $value;
797 if ($field ==
'arraytoproduce') {
798 $arraytoproduce = $value;
800 if ($field ===
'caller') {
802 $stockmove->context[
'caller'] = $request_data[
'caller'];
807 if (empty($labelmovement)) {
808 throw new RestException(500,
"Field inventorylabel not provided");
810 if (empty($codemovement)) {
811 throw new RestException(500,
"Field inventorycode not provided");
817 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
818 foreach ($arrayofarrayname as $arrayname) {
819 foreach (${$arrayname} as $value) {
820 if (empty($value[
"objectid"])) {
821 throw new RestException(500,
"Field objectid required in " . $arrayname);
824 $molinetoprocess =
new MoLine($this->db);
825 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
826 if ($tmpmolineid <= 0) {
827 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
830 $tmpproduct =
new Product($this->db);
831 $tmpproduct->fetch($molinetoprocess->fk_product);
832 if ($tmpproduct->status_batch) {
833 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
836 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
837 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
839 $qtytoprocess = $value[
"qty"];
841 $fk_warehousetoprocess = 0;
842 if ($molinetoprocess->disable_stock_change ==
false) {
843 if (isset($value[
"fk_warehouse"])) {
844 if (!($value[
"fk_warehouse"] > 0)) {
845 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
848 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
852 if (isset($value[
"pricetoproduce"])) {
853 if ($value[
"pricetoproduce"] > 0) {
854 $pricetoproduce = $value[
"pricetoproduce"];
860 if ($molinetoprocess->disable_stock_change ==
false) {
862 $id_product_batch = 0;
863 $stockmove->origin_type =
'mo';
864 $stockmove->origin_id = $this->mo->id;
865 if ($arrayname ==
"arraytoconsume") {
866 if ($qtytoprocess >= 0) {
867 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
869 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
872 if ($qtytoprocess >= 0) {
873 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
'',
'', (
string) $tmpproduct->status_batch,
dol_now(), $id_product_batch, $codemovement);
875 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', (string) $tmpproduct->status_batch, $id_product_batch, $codemovement);
878 if ($idstockmove <= 0) {
879 throw new RestException(500, $stockmove->error);
884 $moline =
new MoLine($this->db);
885 $moline->fk_mo = $this->mo->id;
886 $moline->position = $pos;
887 $moline->fk_product = $tmpproduct->id;
888 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
889 $moline->qty = $qtytoprocess;
891 $moline->fk_mrp_production = $molinetoprocess->id;
892 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
893 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
895 if ($arrayname ==
"arraytoconsume") {
896 $moline->role =
'consumed';
898 $moline->role =
'produced';
901 $resultmoline = $moline->create(DolibarrApiAccess::$user);
902 if ($resultmoline <= 0) {
903 throw new RestException(500, $moline->error);
910 $consumptioncomplete =
true;
911 $productioncomplete =
true;
913 if ($autoclose > 0) {
915 $this->mo->fetchLines();
917 foreach ($this->mo->lines as $line) {
918 if ($line->role ==
'toconsume') {
919 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
920 $alreadyconsumed = 0;
921 foreach ($arrayoflines as $line2) {
922 $alreadyconsumed += $line2[
'qty'];
925 if ($alreadyconsumed < $line->qty) {
926 $consumptioncomplete =
false;
929 if ($line->role ==
'toproduce') {
930 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
931 $alreadyproduced = 0;
932 foreach ($arrayoflines as $line2) {
933 $alreadyproduced += $line2[
'qty'];
936 if ($alreadyproduced < $line->qty) {
937 $productioncomplete =
false;
942 $consumptioncomplete =
false;
943 $productioncomplete =
false;
947 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
949 if ($consumptioncomplete && $productioncomplete) {
950 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
952 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
955 throw new RestException(500, $this->mo->error);
959 return $this->mo->id;