340 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
341 throw new RestException(403,
'Not enough permission');
343 $result = $this->mo->fetch($id);
345 throw new RestException(404,
'MO not found');
348 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
349 throw new RestException(405,
'Error bad status of MO');
353 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
354 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
355 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
362 $arraytoconsume = array();
363 $arraytoproduce = array();
365 foreach ($request_data as $field => $value) {
366 if ($field ==
'inventorylabel') {
367 $labelmovement = $value;
369 if ($field ==
'inventorycode') {
370 $codemovement = $value;
372 if ($field ==
'autoclose') {
375 if ($field ==
'arraytoconsume') {
376 $arraytoconsume = $value;
378 if ($field ==
'arraytoproduce') {
379 $arraytoproduce = $value;
381 if ($field ===
'caller') {
383 $stockmove->context[
'caller'] = $request_data[
'caller'];
388 if (empty($labelmovement)) {
389 throw new RestException(500,
"Field inventorylabel not provided");
391 if (empty($codemovement)) {
392 throw new RestException(500,
"Field inventorycode not provided");
395 $consumptioncomplete =
true;
396 $productioncomplete =
true;
398 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
400 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
401 foreach ($arrayofarrayname as $arrayname) {
402 foreach (${$arrayname} as $value) {
403 $tmpproduct =
new Product($this->db);
404 if (empty($value[
"objectid"])) {
405 throw new RestException(500,
"Field objectid required in ".$arrayname);
407 $tmpproduct->fetch($value[
"qty"]);
408 if (empty($value[
"qty"])) {
409 throw new RestException(500,
"Field qty required in ".$arrayname);
411 if ($value[
"qty"] != 0) {
412 $qtytoprocess = $value[
"qty"];
413 if (isset($value[
"fk_warehouse"])) {
414 if (!($value[
"fk_warehouse"] > 0)) {
416 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
418 if ($tmpproduct->status_batch) {
420 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
424 if (!$error && $value[
"fk_warehouse"] > 0) {
426 $id_product_batch = 0;
428 $stockmove->setOrigin($this->mo->element, $this->mo->id);
430 if ($arrayname ==
'arraytoconsume') {
431 $moline =
new MoLine($this->db);
432 $moline->fk_mo = $this->mo->id;
433 $moline->position = $pos;
434 $moline->fk_product = $value[
"objectid"];
435 $moline->fk_warehouse = $value[
"fk_warehouse"];
436 $moline->qty = $qtytoprocess;
437 $moline->batch = $tmpproduct->status_batch;
438 $moline->role =
'toproduce';
439 $moline->fk_mrp_production =
"";
440 $moline->fk_stock_movement = $idstockmove;
441 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
443 $resultmoline = $moline->create(DolibarrApiAccess::$user);
444 if ($resultmoline <= 0) {
446 throw new RestException(500, $moline->error);
448 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
450 $moline =
new MoLine($this->db);
451 $moline->fk_mo = $this->mo->id;
452 $moline->position = $pos;
453 $moline->fk_product = $value[
"objectid"];
454 $moline->fk_warehouse = $value[
"fk_warehouse"];
455 $moline->qty = $qtytoprocess;
456 $moline->batch = $tmpproduct->status_batch;
457 $moline->role =
'toconsume';
458 $moline->fk_mrp_production =
"";
459 $moline->fk_stock_movement = $idstockmove;
460 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
462 $resultmoline = $moline->create(DolibarrApiAccess::$user);
463 if ($resultmoline <= 0) {
465 throw new RestException(500, $moline->error);
467 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
469 if ($idstockmove < 0) {
471 throw new RestException(500, $stockmove->error);
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 = $value[
"fk_warehouse"];
481 $moline->qty = $qtytoprocess;
482 $moline->batch = $tmpproduct->status_batch;
483 if ($arrayname ==
"arraytoconsume") {
484 $moline->role =
'consumed';
486 $moline->role =
'produced';
488 $moline->fk_mrp_production =
"";
489 $moline->fk_stock_movement = $idstockmove;
490 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
492 $resultmoline = $moline->create(DolibarrApiAccess::$user);
493 if ($resultmoline <= 0) {
495 throw new RestException(500, $moline->error);
504 if ($autoclose <= 0) {
505 $consumptioncomplete =
false;
506 $productioncomplete =
false;
511 foreach ($this->mo->lines as $line) {
512 if ($line->role ==
'toconsume') {
513 $tmpproduct =
new Product($this->db);
514 $tmpproduct->fetch($line->fk_product);
515 if ($line->qty != 0) {
516 $qtytoprocess = $line->qty;
517 if (isset($line->fk_warehouse)) {
518 if (!($line->fk_warehouse > 0)) {
519 $langs->load(
"errors");
521 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
523 if ($tmpproduct->status_batch) {
524 $langs->load(
"errors");
526 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
530 if (!$error && $line->fk_warehouse > 0) {
532 $id_product_batch = 0;
533 $stockmove->origin_type =
'mo';
534 $stockmove->origin_id = $this->mo->id;
535 if ($qtytoprocess >= 0) {
536 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
538 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
540 if ($idstockmove < 0) {
542 throw new RestException(500, $stockmove->error);
547 $moline =
new MoLine($this->db);
548 $moline->fk_mo = $this->mo->id;
549 $moline->position = $pos;
550 $moline->fk_product = $line->fk_product;
551 $moline->fk_warehouse = $line->fk_warehouse;
552 $moline->qty = $qtytoprocess;
553 $moline->batch = $tmpproduct->status_batch;
554 $moline->role =
'consumed';
555 $moline->fk_mrp_production = $line->id;
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);
571 foreach ($this->mo->lines as $line) {
572 if ($line->role ==
'toproduce') {
573 $tmpproduct =
new Product($this->db);
574 $tmpproduct->fetch($line->fk_product);
575 if ($line->qty != 0) {
576 $qtytoprocess = $line->qty;
577 if (isset($line->fk_warehouse)) {
578 if (!($line->fk_warehouse > 0)) {
579 $langs->load(
"errors");
581 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
583 if ($tmpproduct->status_batch) {
584 $langs->load(
"errors");
586 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
590 if (!$error && $line->fk_warehouse > 0) {
592 $id_product_batch = 0;
593 $stockmove->origin_type =
'mo';
594 $stockmove->origin_id = $this->mo->id;
595 if ($qtytoprocess >= 0) {
596 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
598 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
600 if ($idstockmove < 0) {
602 throw new RestException(500, $stockmove->error);
607 $moline =
new MoLine($this->db);
608 $moline->fk_mo = $this->mo->id;
609 $moline->position = $pos;
610 $moline->fk_product = $line->fk_product;
611 $moline->fk_warehouse = $line->fk_warehouse;
612 $moline->qty = $qtytoprocess;
613 $moline->batch = $tmpproduct->status_batch;
614 $moline->role =
'produced';
615 $moline->fk_mrp_production = $line->id;
616 $moline->fk_stock_movement = $idstockmove;
617 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
619 $resultmoline = $moline->create(DolibarrApiAccess::$user);
620 if ($resultmoline <= 0) {
622 throw new RestException(500, $moline->error);
632 if ($autoclose > 0) {
633 foreach ($this->mo->lines as $line) {
634 if ($line->role ==
'toconsume') {
635 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
636 $alreadyconsumed = 0;
637 foreach ($arrayoflines as $line2) {
638 $alreadyconsumed += $line2[
'qty'];
641 if ($alreadyconsumed < $line->qty) {
642 $consumptioncomplete =
false;
645 if ($line->role ==
'toproduce') {
646 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
647 $alreadyproduced = 0;
648 foreach ($arrayoflines as $line2) {
649 $alreadyproduced += $line2[
'qty'];
652 if ($alreadyproduced < $line->qty) {
653 $productioncomplete =
false;
658 $consumptioncomplete =
false;
659 $productioncomplete =
false;
665 dol_syslog(
"consumptioncomplete = ".$consumptioncomplete.
" productioncomplete = ".$productioncomplete);
667 if ($consumptioncomplete && $productioncomplete) {
668 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
670 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
673 throw new RestException(500, $this->mo->error);
676 return $this->mo->id;
713 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
714 throw new RestException(403,
'Not enough permission');
716 $result = $this->mo->fetch($id);
718 throw new RestException(404,
'MO not found');
721 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
722 throw new RestException(405,
'Error bad status of MO');
726 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
727 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
728 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
735 $arraytoconsume = array();
736 $arraytoproduce = array();
738 foreach ($request_data as $field => $value) {
739 if ($field ==
'inventorylabel') {
740 $labelmovement = $value;
742 if ($field ==
'inventorycode') {
743 $codemovement = $value;
745 if ($field ==
'autoclose') {
748 if ($field ==
'arraytoconsume') {
749 $arraytoconsume = $value;
751 if ($field ==
'arraytoproduce') {
752 $arraytoproduce = $value;
754 if ($field ===
'caller') {
756 $stockmove->context[
'caller'] = $request_data[
'caller'];
761 if (empty($labelmovement)) {
762 throw new RestException(500,
"Field inventorylabel not provided");
764 if (empty($codemovement)) {
765 throw new RestException(500,
"Field inventorycode not provided");
771 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
772 foreach ($arrayofarrayname as $arrayname) {
773 foreach (${$arrayname} as $value) {
774 if (empty($value[
"objectid"])) {
775 throw new RestException(500,
"Field objectid required in " . $arrayname);
778 $molinetoprocess =
new MoLine($this->db);
779 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
780 if ($tmpmolineid <= 0) {
781 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
784 $tmpproduct =
new Product($this->db);
785 $tmpproduct->fetch($molinetoprocess->fk_product);
786 if ($tmpproduct->status_batch) {
787 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
790 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
791 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
793 $qtytoprocess = $value[
"qty"];
795 $fk_warehousetoprocess = 0;
796 if ($molinetoprocess->disable_stock_change ==
false) {
797 if (isset($value[
"fk_warehouse"])) {
798 if (!($value[
"fk_warehouse"] > 0)) {
799 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
802 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
806 if (isset($value[
"pricetoproduce"])) {
807 if ($value[
"pricetoproduce"] > 0) {
808 $pricetoproduce = $value[
"pricetoproduce"];
815 if ($molinetoprocess->disable_stock_change ==
false) {
817 $id_product_batch = 0;
818 $stockmove->origin_type =
'mo';
819 $stockmove->origin_id = $this->mo->id;
820 if ($arrayname ==
"arraytoconsume") {
821 if ($qtytoprocess >= 0) {
822 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
824 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
827 if ($qtytoprocess >= 0) {
828 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
830 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
833 if ($idstockmove <= 0) {
834 throw new RestException(500, $stockmove->error);
839 $moline =
new MoLine($this->db);
840 $moline->fk_mo = $this->mo->id;
841 $moline->position = $pos;
842 $moline->fk_product = $tmpproduct->id;
843 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
844 $moline->qty = $qtytoprocess;
846 $moline->fk_mrp_production = $molinetoprocess->id;
847 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
848 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
850 if ($arrayname ==
"arraytoconsume") {
851 $moline->role =
'consumed';
853 $moline->role =
'produced';
856 $resultmoline = $moline->create(DolibarrApiAccess::$user);
857 if ($resultmoline <= 0) {
858 throw new RestException(500, $moline->error);
863 $consumptioncomplete =
true;
864 $productioncomplete =
true;
866 if ($autoclose > 0) {
868 $this->mo->fetchLines();
870 foreach ($this->mo->lines as $line) {
871 if ($line->role ==
'toconsume') {
872 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
873 $alreadyconsumed = 0;
874 foreach ($arrayoflines as $line2) {
875 $alreadyconsumed += $line2[
'qty'];
878 if ($alreadyconsumed < $line->qty) {
879 $consumptioncomplete =
false;
882 if ($line->role ==
'toproduce') {
883 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
884 $alreadyproduced = 0;
885 foreach ($arrayoflines as $line2) {
886 $alreadyproduced += $line2[
'qty'];
889 if ($alreadyproduced < $line->qty) {
890 $productioncomplete =
false;
895 $consumptioncomplete =
false;
896 $productioncomplete =
false;
900 dol_syslog(
"consumptioncomplete = " . $consumptioncomplete .
" productioncomplete = " . $productioncomplete);
902 if ($consumptioncomplete && $productioncomplete) {
903 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
905 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
908 throw new RestException(500, $this->mo->error);
912 return $this->mo->id;
926 $object = parent::_cleanObjectDatas($object);
928 unset($object->rowid);
929 unset($object->canvas);
931 unset($object->name);
932 unset($object->lastname);
933 unset($object->firstname);
934 unset($object->civility_id);
935 unset($object->statut);
936 unset($object->state);
937 unset($object->state_id);
938 unset($object->state_code);
939 unset($object->region);
940 unset($object->region_code);
941 unset($object->country);
942 unset($object->country_id);
943 unset($object->country_code);
944 unset($object->barcode_type);
945 unset($object->barcode_type_code);
946 unset($object->barcode_type_label);
947 unset($object->barcode_type_coder);
948 unset($object->total_ht);
949 unset($object->total_tva);
950 unset($object->total_localtax1);
951 unset($object->total_localtax2);
952 unset($object->total_ttc);
953 unset($object->fk_account);
954 unset($object->comments);
955 unset($object->note);
956 unset($object->mode_reglement_id);
957 unset($object->cond_reglement_id);
958 unset($object->cond_reglement);
959 unset($object->shipping_method_id);
960 unset($object->fk_incoterms);
961 unset($object->label_incoterms);
962 unset($object->location_incoterms);
965 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
966 $nboflines = count($object->lines);
967 for ($i = 0; $i < $nboflines; $i++) {
970 unset($object->lines[$i]->lines);
971 unset($object->lines[$i]->note);