318 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'write')) {
319 throw new RestException(403,
'Not enough permission');
321 $result = $this->mo->fetch($id);
323 throw new RestException(404,
'MO not found');
326 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
327 throw new RestException(405,
'Error bad status of MO');
331 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
332 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
333 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
340 $arraytoconsume = array();
341 $arraytoproduce = array();
343 foreach ($request_data as $field => $value) {
344 if ($field ==
'inventorylabel') {
345 $labelmovement = $value;
347 if ($field ==
'inventorycode') {
348 $codemovement = $value;
350 if ($field ==
'autoclose') {
353 if ($field ==
'arraytoconsume') {
354 $arraytoconsume = $value;
356 if ($field ==
'arraytoproduce') {
357 $arraytoproduce = $value;
359 if ($field ===
'caller') {
361 $stockmove->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
366 if (empty($labelmovement)) {
367 throw new RestException(500,
"Field inventorylabel not provided");
369 if (empty($codemovement)) {
370 throw new RestException(500,
"Field inventorycode not provided");
373 $consumptioncomplete =
true;
374 $productioncomplete =
true;
376 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
378 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
379 foreach ($arrayofarrayname as $arrayname) {
380 foreach (${$arrayname} as $value) {
381 $tmpproduct =
new Product($this->db);
382 if (empty($value[
"objectid"])) {
383 throw new RestException(500,
"Field objectid required in ".$arrayname);
385 $tmpproduct->fetch($value[
"qty"]);
386 if (empty($value[
"qty"])) {
387 throw new RestException(500,
"Field qty required in ".$arrayname);
389 if ($value[
"qty"] != 0) {
390 $qtytoprocess = $value[
"qty"];
391 if (isset($value[
"fk_warehouse"])) {
392 if (!($value[
"fk_warehouse"] > 0)) {
394 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
396 if ($tmpproduct->status_batch) {
398 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
402 if (!$error && $value[
"fk_warehouse"] > 0) {
404 $id_product_batch = 0;
406 $stockmove->setOrigin($this->mo->element, $this->mo->id);
408 if ($arrayname ==
'arraytoconsume') {
409 $moline =
new MoLine($this->db);
410 $moline->fk_mo = $this->mo->id;
411 $moline->position = $pos;
412 $moline->fk_product = $value[
"objectid"];
413 $moline->fk_warehouse = $value[
"fk_warehouse"];
414 $moline->qty = $qtytoprocess;
415 $moline->batch = $tmpproduct->status_batch;
416 $moline->role =
'toproduce';
417 $moline->fk_mrp_production =
"";
418 $moline->fk_stock_movement = $idstockmove;
419 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
421 $resultmoline = $moline->create(DolibarrApiAccess::$user);
422 if ($resultmoline <= 0) {
424 throw new RestException(500, $moline->error);
426 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
428 $moline =
new MoLine($this->db);
429 $moline->fk_mo = $this->mo->id;
430 $moline->position = $pos;
431 $moline->fk_product = $value[
"objectid"];
432 $moline->fk_warehouse = $value[
"fk_warehouse"];
433 $moline->qty = $qtytoprocess;
434 $moline->batch = $tmpproduct->status_batch;
435 $moline->role =
'toconsume';
436 $moline->fk_mrp_production =
"";
437 $moline->fk_stock_movement = $idstockmove;
438 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
440 $resultmoline = $moline->create(DolibarrApiAccess::$user);
441 if ($resultmoline <= 0) {
443 throw new RestException(500, $moline->error);
445 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
447 if ($idstockmove < 0) {
449 throw new RestException(500, $stockmove->error);
454 $moline =
new MoLine($this->db);
455 $moline->fk_mo = $this->mo->id;
456 $moline->position = $pos;
457 $moline->fk_product = $value[
"objectid"];
458 $moline->fk_warehouse = $value[
"fk_warehouse"];
459 $moline->qty = $qtytoprocess;
460 $moline->batch = $tmpproduct->status_batch;
461 if ($arrayname ==
"arraytoconsume") {
462 $moline->role =
'consumed';
464 $moline->role =
'produced';
466 $moline->fk_mrp_production =
"";
467 $moline->fk_stock_movement = $idstockmove;
468 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
470 $resultmoline = $moline->create(DolibarrApiAccess::$user);
471 if ($resultmoline <= 0) {
473 throw new RestException(500, $moline->error);
482 if ($autoclose <= 0) {
483 $consumptioncomplete =
false;
484 $productioncomplete =
false;
489 foreach ($this->mo->lines as $line) {
490 if ($line->role ==
'toconsume') {
491 $tmpproduct =
new Product($this->db);
492 $tmpproduct->fetch($line->fk_product);
493 if ($line->qty != 0) {
494 $qtytoprocess = $line->qty;
495 if (isset($line->fk_warehouse)) {
496 if (!($line->fk_warehouse > 0)) {
497 $langs->load(
"errors");
499 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
501 if ($tmpproduct->status_batch) {
502 $langs->load(
"errors");
504 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
508 if (!$error && $line->fk_warehouse > 0) {
510 $id_product_batch = 0;
511 $stockmove->origin_type =
'mo';
512 $stockmove->origin_id = $this->mo->id;
513 if ($qtytoprocess >= 0) {
514 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
516 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
518 if ($idstockmove < 0) {
520 throw new RestException(500, $stockmove->error);
525 $moline =
new MoLine($this->db);
526 $moline->fk_mo = $this->mo->id;
527 $moline->position = $pos;
528 $moline->fk_product = $line->fk_product;
529 $moline->fk_warehouse = $line->fk_warehouse;
530 $moline->qty = $qtytoprocess;
531 $moline->batch = $tmpproduct->status_batch;
532 $moline->role =
'consumed';
533 $moline->fk_mrp_production = $line->id;
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 foreach ($this->mo->lines as $line) {
550 if ($line->role ==
'toproduce') {
551 $tmpproduct =
new Product($this->db);
552 $tmpproduct->fetch($line->fk_product);
553 if ($line->qty != 0) {
554 $qtytoprocess = $line->qty;
555 if (isset($line->fk_warehouse)) {
556 if (!($line->fk_warehouse > 0)) {
557 $langs->load(
"errors");
559 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
561 if ($tmpproduct->status_batch) {
562 $langs->load(
"errors");
564 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
568 if (!$error && $line->fk_warehouse > 0) {
570 $id_product_batch = 0;
571 $stockmove->origin_type =
'mo';
572 $stockmove->origin_id = $this->mo->id;
573 if ($qtytoprocess >= 0) {
574 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
576 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
578 if ($idstockmove < 0) {
580 throw new RestException(500, $stockmove->error);
585 $moline =
new MoLine($this->db);
586 $moline->fk_mo = $this->mo->id;
587 $moline->position = $pos;
588 $moline->fk_product = $line->fk_product;
589 $moline->fk_warehouse = $line->fk_warehouse;
590 $moline->qty = $qtytoprocess;
591 $moline->batch = $tmpproduct->status_batch;
592 $moline->role =
'produced';
593 $moline->fk_mrp_production = $line->id;
594 $moline->fk_stock_movement = $idstockmove;
595 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
597 $resultmoline = $moline->create(DolibarrApiAccess::$user);
598 if ($resultmoline <= 0) {
600 throw new RestException(500, $moline->error);
610 if ($autoclose > 0) {
611 foreach ($this->mo->lines as $line) {
612 if ($line->role ==
'toconsume') {
613 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
614 $alreadyconsumed = 0;
615 foreach ($arrayoflines as $line2) {
616 $alreadyconsumed += $line2[
'qty'];
619 if ($alreadyconsumed < $line->qty) {
620 $consumptioncomplete =
false;
623 if ($line->role ==
'toproduce') {
624 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
625 $alreadyproduced = 0;
626 foreach ($arrayoflines as $line2) {
627 $alreadyproduced += $line2[
'qty'];
630 if ($alreadyproduced < $line->qty) {
631 $productioncomplete =
false;
636 $consumptioncomplete =
false;
637 $productioncomplete =
false;
643 dol_syslog(
"consumptioncomplete = ".json_encode($consumptioncomplete).
" productioncomplete = ".json_encode($productioncomplete));
644 if ($consumptioncomplete && $productioncomplete) {
645 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
647 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
650 throw new RestException(500, $this->mo->error);
653 return $this->mo->id;
690 if (!DolibarrApiAccess::$user->hasRight(
"mrp",
"write")) {
691 throw new RestException(403,
'Not enough permission');
693 $result = $this->mo->fetch($id);
695 throw new RestException(404,
'MO not found');
698 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
699 throw new RestException(405,
'Error bad status of MO');
703 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
704 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
705 require_once DOL_DOCUMENT_ROOT.
'/mrp/lib/mrp_mo.lib.php';
712 $arraytoconsume = array();
713 $arraytoproduce = array();
715 foreach ($request_data as $field => $value) {
716 if ($field ==
'inventorylabel') {
717 $labelmovement = $value;
719 if ($field ==
'inventorycode') {
720 $codemovement = $value;
722 if ($field ==
'autoclose') {
725 if ($field ==
'arraytoconsume') {
726 $arraytoconsume = $value;
728 if ($field ==
'arraytoproduce') {
729 $arraytoproduce = $value;
731 if ($field ===
'caller') {
733 $stockmove->context[
'caller'] = $request_data[
'caller'];
738 if (empty($labelmovement)) {
739 throw new RestException(500,
"Field inventorylabel not provided");
741 if (empty($codemovement)) {
742 throw new RestException(500,
"Field inventorycode not provided");
748 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
749 foreach ($arrayofarrayname as $arrayname) {
750 foreach (${$arrayname} as $value) {
751 if (empty($value[
"objectid"])) {
752 throw new RestException(500,
"Field objectid required in " . $arrayname);
755 $molinetoprocess =
new MoLine($this->db);
756 $tmpmolineid = $molinetoprocess->fetch($value[
"objectid"]);
757 if ($tmpmolineid <= 0) {
758 throw new RestException(500,
"MoLine with rowid " . $value[
"objectid"] .
" not exist.");
761 $tmpproduct =
new Product($this->db);
762 $tmpproduct->fetch($molinetoprocess->fk_product);
763 if ($tmpproduct->status_batch) {
764 throw new RestException(500,
"Product " . $tmpproduct->ref .
" must be in batch, this API can't handle it currently.");
767 if (empty($value[
"qty"]) && $value[
"qty"] != 0) {
768 throw new RestException(500,
"Field qty with lower or higher then 0 required in " . $arrayname);
770 $qtytoprocess = $value[
"qty"];
772 $fk_warehousetoprocess = 0;
773 if ($molinetoprocess->disable_stock_change ==
false) {
774 if (isset($value[
"fk_warehouse"])) {
775 if (!($value[
"fk_warehouse"] > 0)) {
776 throw new RestException(500,
"Field fk_warehouse required in " . $arrayname);
779 $fk_warehousetoprocess = (int) $value[
"fk_warehouse"];
783 if (isset($value[
"pricetoproduce"])) {
784 if ($value[
"pricetoproduce"] > 0) {
785 $pricetoproduce = $value[
"pricetoproduce"];
791 if ($molinetoprocess->disable_stock_change ==
false) {
793 $id_product_batch = 0;
794 $stockmove->origin_type =
'mo';
795 $stockmove->origin_id = $this->mo->id;
796 if ($arrayname ==
"arraytoconsume") {
797 if ($qtytoprocess >= 0) {
798 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
800 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
803 if ($qtytoprocess >= 0) {
804 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, $pricetoproduce, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
806 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $molinetoprocess->fk_product, $fk_warehousetoprocess, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
809 if ($idstockmove <= 0) {
810 throw new RestException(500, $stockmove->error);
815 $moline =
new MoLine($this->db);
816 $moline->fk_mo = $this->mo->id;
817 $moline->position = $pos;
818 $moline->fk_product = $tmpproduct->id;
819 $moline->fk_warehouse = $idstockmove > 0 ? $fk_warehousetoprocess :
null;
820 $moline->qty = $qtytoprocess;
822 $moline->fk_mrp_production = $molinetoprocess->id;
823 $moline->fk_stock_movement = $idstockmove > 0 ? $idstockmove :
null;
824 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
826 if ($arrayname ==
"arraytoconsume") {
827 $moline->role =
'consumed';
829 $moline->role =
'produced';
832 $resultmoline = $moline->create(DolibarrApiAccess::$user);
833 if ($resultmoline <= 0) {
834 throw new RestException(500, $moline->error);
841 $consumptioncomplete =
true;
842 $productioncomplete =
true;
844 if ($autoclose > 0) {
846 $this->mo->fetchLines();
848 foreach ($this->mo->lines as $line) {
849 if ($line->role ==
'toconsume') {
850 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
851 $alreadyconsumed = 0;
852 foreach ($arrayoflines as $line2) {
853 $alreadyconsumed += $line2[
'qty'];
856 if ($alreadyconsumed < $line->qty) {
857 $consumptioncomplete =
false;
860 if ($line->role ==
'toproduce') {
861 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
862 $alreadyproduced = 0;
863 foreach ($arrayoflines as $line2) {
864 $alreadyproduced += $line2[
'qty'];
867 if ($alreadyproduced < $line->qty) {
868 $productioncomplete =
false;
873 $consumptioncomplete =
false;
874 $productioncomplete =
false;
878 dol_syslog(
"consumptioncomplete = " . (
string) $consumptioncomplete .
" productioncomplete = " . (
string) $productioncomplete);
880 if ($consumptioncomplete && $productioncomplete) {
881 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
883 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
886 throw new RestException(500, $this->mo->error);
890 return $this->mo->id;