333 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
334 throw new RestException(403,
'Not enough permission');
336 $result = $this->mo->fetch($id);
338 throw new RestException(404,
'MO not found');
341 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
342 throw new RestException(405,
'Error bad status of MO');
346 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
347 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
348 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
355 $arraytoconsume = array();
356 $arraytoproduce = array();
358 foreach ($request_data as $field => $value) {
359 if ($field ==
'inventorylabel') {
360 $labelmovement = $value;
362 if ($field ==
'inventorycode') {
363 $codemovement = $value;
365 if ($field ==
'autoclose') {
368 if ($field ==
'arraytoconsume') {
369 $arraytoconsume = $value;
371 if ($field ==
'arraytoproduce') {
372 $arraytoproduce = $value;
374 if ($field ===
'caller') {
376 $stockmove->context[
'caller'] = $request_data[
'caller'];
381 if (empty($labelmovement)) {
382 throw new RestException(500,
"Field inventorylabel not provided");
384 if (empty($codemovement)) {
385 throw new RestException(500,
"Field inventorycode not provided");
388 $consumptioncomplete =
true;
389 $productioncomplete =
true;
391 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
393 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
394 foreach ($arrayofarrayname as $arrayname) {
395 foreach (${$arrayname} as $value) {
396 $tmpproduct =
new Product($this->db);
397 if (empty($value[
"objectid"])) {
398 throw new RestException(500,
"Field objectid required in ".$arrayname);
400 $tmpproduct->fetch($value[
"qty"]);
401 if (empty($value[
"qty"])) {
402 throw new RestException(500,
"Field qty required in ".$arrayname);
404 if ($value[
"qty"] != 0) {
405 $qtytoprocess = $value[
"qty"];
406 if (isset($value[
"fk_warehouse"])) {
407 if (!($value[
"fk_warehouse"] > 0)) {
409 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
411 if ($tmpproduct->status_batch) {
413 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
417 if (!$error && $value[
"fk_warehouse"] > 0) {
419 $id_product_batch = 0;
421 $stockmove->setOrigin($this->mo->element, $this->mo->id);
423 if ($arrayname ==
'arraytoconsume') {
424 $moline =
new MoLine($this->db);
425 $moline->fk_mo = $this->mo->id;
426 $moline->position = $pos;
427 $moline->fk_product = $value[
"objectid"];
428 $moline->fk_warehouse = $value[
"fk_warehouse"];
429 $moline->qty = $qtytoprocess;
430 $moline->batch = $tmpproduct->status_batch;
431 $moline->role =
'toproduce';
432 $moline->fk_mrp_production =
"";
433 $moline->fk_stock_movement = $idstockmove;
434 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
436 $resultmoline = $moline->create(DolibarrApiAccess::$user);
437 if ($resultmoline <= 0) {
439 throw new RestException(500, $moline->error);
441 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
443 $moline =
new MoLine($this->db);
444 $moline->fk_mo = $this->mo->id;
445 $moline->position = $pos;
446 $moline->fk_product = $value[
"objectid"];
447 $moline->fk_warehouse = $value[
"fk_warehouse"];
448 $moline->qty = $qtytoprocess;
449 $moline->batch = $tmpproduct->status_batch;
450 $moline->role =
'toconsume';
451 $moline->fk_mrp_production =
"";
452 $moline->fk_stock_movement = $idstockmove;
453 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
455 $resultmoline = $moline->create(DolibarrApiAccess::$user);
456 if ($resultmoline <= 0) {
458 throw new RestException(500, $moline->error);
460 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
462 if ($idstockmove < 0) {
464 throw new RestException(500, $stockmove->error);
469 $moline =
new MoLine($this->db);
470 $moline->fk_mo = $this->mo->id;
471 $moline->position = $pos;
472 $moline->fk_product = $value[
"objectid"];
473 $moline->fk_warehouse = $value[
"fk_warehouse"];
474 $moline->qty = $qtytoprocess;
475 $moline->batch = $tmpproduct->status_batch;
476 if ($arrayname ==
"arraytoconsume") {
477 $moline->role =
'consumed';
479 $moline->role =
'produced';
481 $moline->fk_mrp_production =
"";
482 $moline->fk_stock_movement = $idstockmove;
483 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
485 $resultmoline = $moline->create(DolibarrApiAccess::$user);
486 if ($resultmoline <= 0) {
488 throw new RestException(500, $moline->error);
497 if ($autoclose <= 0) {
498 $consumptioncomplete =
false;
499 $productioncomplete =
false;
504 foreach ($this->mo->lines as $line) {
505 if ($line->role ==
'toconsume') {
506 $tmpproduct =
new Product($this->db);
507 $tmpproduct->fetch($line->fk_product);
508 if ($line->qty != 0) {
509 $qtytoprocess = $line->qty;
510 if (isset($line->fk_warehouse)) {
511 if (!($line->fk_warehouse > 0)) {
512 $langs->load(
"errors");
514 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
516 if ($tmpproduct->status_batch) {
517 $langs->load(
"errors");
519 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
523 if (!$error && $line->fk_warehouse > 0) {
525 $id_product_batch = 0;
526 $stockmove->origin_type =
'mo';
527 $stockmove->origin_id = $this->mo->id;
528 if ($qtytoprocess >= 0) {
529 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
531 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
533 if ($idstockmove < 0) {
535 throw new RestException(500, $stockmove->error);
540 $moline =
new MoLine($this->db);
541 $moline->fk_mo = $this->mo->id;
542 $moline->position = $pos;
543 $moline->fk_product = $line->fk_product;
544 $moline->fk_warehouse = $line->fk_warehouse;
545 $moline->qty = $qtytoprocess;
546 $moline->batch = $tmpproduct->status_batch;
547 $moline->role =
'consumed';
548 $moline->fk_mrp_production = $line->id;
549 $moline->fk_stock_movement = $idstockmove;
550 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
552 $resultmoline = $moline->create(DolibarrApiAccess::$user);
553 if ($resultmoline <= 0) {
555 throw new RestException(500, $moline->error);
564 foreach ($this->mo->lines as $line) {
565 if ($line->role ==
'toproduce') {
566 $tmpproduct =
new Product($this->db);
567 $tmpproduct->fetch($line->fk_product);
568 if ($line->qty != 0) {
569 $qtytoprocess = $line->qty;
570 if (isset($line->fk_warehouse)) {
571 if (!($line->fk_warehouse > 0)) {
572 $langs->load(
"errors");
574 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
576 if ($tmpproduct->status_batch) {
577 $langs->load(
"errors");
579 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
583 if (!$error && $line->fk_warehouse > 0) {
585 $id_product_batch = 0;
586 $stockmove->origin_type =
'mo';
587 $stockmove->origin_id = $this->mo->id;
588 if ($qtytoprocess >= 0) {
589 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
591 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
593 if ($idstockmove < 0) {
595 throw new RestException(500, $stockmove->error);
600 $moline =
new MoLine($this->db);
601 $moline->fk_mo = $this->mo->id;
602 $moline->position = $pos;
603 $moline->fk_product = $line->fk_product;
604 $moline->fk_warehouse = $line->fk_warehouse;
605 $moline->qty = $qtytoprocess;
606 $moline->batch = $tmpproduct->status_batch;
607 $moline->role =
'produced';
608 $moline->fk_mrp_production = $line->id;
609 $moline->fk_stock_movement = $idstockmove;
610 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
612 $resultmoline = $moline->create(DolibarrApiAccess::$user);
613 if ($resultmoline <= 0) {
615 throw new RestException(500, $moline->error);
625 if ($autoclose > 0) {
626 foreach ($this->mo->lines as $line) {
627 if ($line->role ==
'toconsume') {
628 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
629 $alreadyconsumed = 0;
630 foreach ($arrayoflines as $line2) {
631 $alreadyconsumed += $line2[
'qty'];
634 if ($alreadyconsumed < $line->qty) {
635 $consumptioncomplete =
false;
638 if ($line->role ==
'toproduce') {
639 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
640 $alreadyproduced = 0;
641 foreach ($arrayoflines as $line2) {
642 $alreadyproduced += $line2[
'qty'];
645 if ($alreadyproduced < $line->qty) {
646 $productioncomplete =
false;
651 $consumptioncomplete =
false;
652 $productioncomplete =
false;
658 dol_syslog(
"consumptioncomplete = ".$consumptioncomplete.
" productioncomplete = ".$productioncomplete);
660 if ($consumptioncomplete && $productioncomplete) {
661 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
663 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
666 throw new RestException(500, $this->mo->error);
669 return $this->mo->id;
706 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
707 throw new RestException(403,
'Not enough permission');
709 $result = $this->mo->fetch($id);
711 throw new RestException(404,
'MO not found');
714 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
715 throw new RestException(405,
'Error bad status of MO');
719 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
720 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
721 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
728 $arraytoconsume = array();
729 $arraytoproduce = array();
731 foreach ($request_data as $field => $value) {
732 if ($field ==
'inventorylabel') {
733 $labelmovement = $value;
735 if ($field ==
'inventorycode') {
736 $codemovement = $value;
738 if ($field ==
'autoclose') {
741 if ($field ==
'arraytoconsume') {
742 $arraytoconsume = $value;
744 if ($field ==
'arraytoproduce') {
745 $arraytoproduce = $value;
747 if ($field ===
'caller') {
749 $stockmove->context[
'caller'] = $request_data[
'caller'];
754 if (empty($labelmovement)) {
755 throw new RestException(500,
"Field inventorylabel not provided");
757 if (empty($codemovement)) {
758 throw new RestException(500,
"Field inventorycode not provided");
764 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
765 foreach ($arrayofarrayname as $arrayname) {
766 foreach (${$arrayname} as $value) {
767 if (empty($value[
"objectid"])) {
768 throw new RestException(500,
"Field objectid required in " . $arrayname);
771 $molinetoprocess =
new MoLine($this->db);
772 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
773 if ($tmpmolineid <= 0) {
774 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
777 $tmpproduct =
new Product($this->db);
778 $tmpproduct->fetch($molinetoprocess->fk_product);
779 if ($tmpproduct->status_batch) {
780 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
783 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
784 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
786 $qtytoprocess = $value[
"qty"];
788 $fk_warehousetoprocess = 0;
789 if ($molinetoprocess->disable_stock_change ==
false) {
790 if (isset($value[
"fk_warehouse"])) {
791 if (!($value[
"fk_warehouse"] > 0)) {
792 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
795 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
799 if (isset($value[
"pricetoproduce"])) {
800 if ($value[
"pricetoproduce"] > 0) {
801 $pricetoproduce = $value[
"pricetoproduce"];
808 if ($molinetoprocess->disable_stock_change ==
false) {
810 $id_product_batch = 0;
811 $stockmove->origin_type =
'mo';
812 $stockmove->origin_id = $this->mo->id;
813 if ($arrayname ==
"arraytoconsume") {
814 if ($qtytoprocess >= 0) {
815 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
817 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
820 if ($qtytoprocess >= 0) {
821 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
823 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
826 if ($idstockmove <= 0) {
827 throw new RestException(500, $stockmove->error);
832 $moline =
new MoLine($this->db);
833 $moline->fk_mo = $this->mo->id;
834 $moline->position = $pos;
835 $moline->fk_product = $tmpproduct->id;
836 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
837 $moline->qty = $qtytoprocess;
839 $moline->fk_mrp_production = $molinetoprocess->id;
840 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
841 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
843 if ($arrayname ==
"arraytoconsume") {
844 $moline->role =
'consumed';
846 $moline->role =
'produced';
849 $resultmoline = $moline->create(DolibarrApiAccess::$user);
850 if ($resultmoline <= 0) {
851 throw new RestException(500, $moline->error);
856 $consumptioncomplete =
true;
857 $productioncomplete =
true;
859 if ($autoclose > 0) {
861 $this->mo->fetchLines();
863 foreach ($this->mo->lines as $line) {
864 if ($line->role ==
'toconsume') {
865 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
866 $alreadyconsumed = 0;
867 foreach ($arrayoflines as $line2) {
868 $alreadyconsumed += $line2[
'qty'];
871 if ($alreadyconsumed < $line->qty) {
872 $consumptioncomplete =
false;
875 if ($line->role ==
'toproduce') {
876 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
877 $alreadyproduced = 0;
878 foreach ($arrayoflines as $line2) {
879 $alreadyproduced += $line2[
'qty'];
882 if ($alreadyproduced < $line->qty) {
883 $productioncomplete =
false;
888 $consumptioncomplete =
false;
889 $productioncomplete =
false;
893 dol_syslog(
"consumptioncomplete = " . $consumptioncomplete .
" productioncomplete = " . $productioncomplete);
895 if ($consumptioncomplete && $productioncomplete) {
896 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
898 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
901 throw new RestException(500, $this->mo->error);
905 return $this->mo->id;
919 $object = parent::_cleanObjectDatas($object);
921 unset($object->rowid);
922 unset($object->canvas);
924 unset($object->name);
925 unset($object->lastname);
926 unset($object->firstname);
927 unset($object->civility_id);
928 unset($object->statut);
929 unset($object->state);
930 unset($object->state_id);
931 unset($object->state_code);
932 unset($object->region);
933 unset($object->region_code);
934 unset($object->country);
935 unset($object->country_id);
936 unset($object->country_code);
937 unset($object->barcode_type);
938 unset($object->barcode_type_code);
939 unset($object->barcode_type_label);
940 unset($object->barcode_type_coder);
941 unset($object->total_ht);
942 unset($object->total_tva);
943 unset($object->total_localtax1);
944 unset($object->total_localtax2);
945 unset($object->total_ttc);
946 unset($object->fk_account);
947 unset($object->comments);
948 unset($object->note);
949 unset($object->mode_reglement_id);
950 unset($object->cond_reglement_id);
951 unset($object->cond_reglement);
952 unset($object->shipping_method_id);
953 unset($object->fk_incoterms);
954 unset($object->label_incoterms);
955 unset($object->location_incoterms);
958 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
959 $nboflines = count($object->lines);
960 for ($i = 0; $i < $nboflines; $i++) {
963 unset($object->lines[$i]->lines);
964 unset($object->lines[$i]->note);