311 if (!DolibarrApiAccess::$user->rights->mrp->write) {
312 throw new RestException(401,
'Not enough permission');
314 $result = $this->mo->fetch($id);
316 throw new RestException(404,
'MO not found');
319 if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) {
320 throw new RestException(401,
'Error bad status of MO');
326 $arraytoconsume = array();
327 $arraytoproduce = array();
329 foreach ($request_data as $field => $value) {
330 if ($field ==
'inventorylabel') {
331 $labelmovement = $value;
333 if ($field ==
'inventorycode') {
334 $codemovement = $value;
336 if ($field ==
'autoclose') {
339 if ($field ==
'arraytoconsume') {
340 $arraytoconsume = $value;
342 if ($field ==
'arraytoproduce') {
343 $arraytoproduce = $value;
347 if (empty($labelmovement)) {
348 throw new RestException(500,
"Field inventorylabel not provided");
350 if (empty($codemovement)) {
351 throw new RestException(500,
"Field inventorycode not provided");
355 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
356 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
361 $consumptioncomplete =
true;
362 $productioncomplete =
true;
364 if (!empty($arraytoconsume) && !empty($arraytoproduce)) {
366 $arrayofarrayname = array(
"arraytoconsume",
"arraytoproduce");
367 foreach ($arrayofarrayname as $arrayname) {
368 foreach ($arrayname as $value) {
369 $tmpproduct =
new Product($this->db);
370 if (empty($value[
"objectid"])) {
371 throw new RestException(500,
"Field objectid required in ".$arrayname);
373 $tmpproduct->fetch($value[
"qty"]);
374 if (empty($value[
"qty"])) {
375 throw new RestException(500,
"Field qty required in ".$arrayname);
377 if ($value[
"qty"]!=0) {
378 $qtytoprocess = $value[
"qty"];
379 if (isset($value[
"fk_warehouse"])) {
380 if (!($value[
"fk_warehouse"] > 0)) {
382 throw new RestException(500,
"Field fk_warehouse must be > 0 in ".$arrayname);
384 if ($tmpproduct->status_batch) {
386 throw new RestException(500,
"Product ".$tmpproduct->ref.
"must be in batch");
390 if (!$error && $value[
"fk_warehouse"] > 0) {
392 $id_product_batch = 0;
394 $stockmove->setOrigin($this->mo->element, $this->mo->id);
396 if ($qtytoprocess >= 0) {
397 $moline =
new MoLine($this->db);
398 $moline->fk_mo = $this->mo->id;
399 $moline->position = $pos;
400 $moline->fk_product = $value[
"objectid"];
401 $moline->fk_warehouse = $value[
"fk_warehouse"];
402 $moline->qty = $qtytoprocess;
403 $moline->batch = $tmpproduct->status_batch;
404 $moline->role =
'toproduce';
405 $moline->fk_mrp_production =
"";
406 $moline->fk_stock_movement = $idstockmove;
407 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
409 $resultmoline = $moline->create(DolibarrApiAccess::$user);
410 if ($resultmoline <= 0) {
412 throw new RestException(500, $moline->error);
414 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
416 $moline =
new MoLine($this->db);
417 $moline->fk_mo = $this->mo->id;
418 $moline->position = $pos;
419 $moline->fk_product = $value[
"objectid"];
420 $moline->fk_warehouse = $value[
"fk_warehouse"];
421 $moline->qty = $qtytoprocess;
422 $moline->batch = $tmpproduct->status_batch;
423 $moline->role =
'toconsume';
424 $moline->fk_mrp_production =
"";
425 $moline->fk_stock_movement = $idstockmove;
426 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
428 $resultmoline = $moline->create(DolibarrApiAccess::$user);
429 if ($resultmoline <= 0) {
431 throw new RestException(500, $moline->error);
433 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value[
"objectid"], $value[
"fk_warehouse"], $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
435 if ($idstockmove < 0) {
437 throw new RestException(500, $stockmove->error);
442 $moline =
new MoLine($this->db);
443 $moline->fk_mo = $this->mo->id;
444 $moline->position = $pos;
445 $moline->fk_product = $value[
"objectid"];
446 $moline->fk_warehouse = $value[
"fk_warehouse"];
447 $moline->qty = $qtytoprocess;
448 $moline->batch = $tmpproduct->status_batch;
449 if ($arrayname ==
"arraytoconsume") {
450 $moline->role =
'consumed';
452 $moline->role =
'produced';
454 $moline->fk_mrp_production =
"";
455 $moline->fk_stock_movement = $idstockmove;
456 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
458 $resultmoline = $moline->create(DolibarrApiAccess::$user);
459 if ($resultmoline <= 0) {
461 throw new RestException(500, $moline->error);
470 if ($autoclose <= 0) {
471 $consumptioncomplete =
false;
472 $productioncomplete =
false;
477 foreach ($this->mo->lines as $line) {
478 if ($line->role ==
'toconsume') {
479 $tmpproduct =
new Product($this->db);
480 $tmpproduct->fetch($line->fk_product);
481 if ($line->qty != 0) {
482 $qtytoprocess = $line->qty;
483 if (isset($line->fk_warehouse)) {
484 if (!($line->fk_warehouse > 0)) {
485 $langs->load(
"errors");
487 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
489 if ($tmpproduct->status_batch) {
490 $langs->load(
"errors");
492 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
496 if (!$error && $line->fk_warehouse > 0) {
498 $id_product_batch = 0;
499 $stockmove->origin_type =
'mo';
500 $stockmove->origin_id = $this->mo->id;
501 if ($qtytoprocess >= 0) {
502 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
504 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $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 = $line->fk_product;
517 $moline->fk_warehouse = $line->fk_warehouse;
518 $moline->qty = $qtytoprocess;
519 $moline->batch = $tmpproduct->status_batch;
520 $moline->role =
'consumed';
521 $moline->fk_mrp_production = $line->id;
522 $moline->fk_stock_movement = $idstockmove;
523 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
525 $resultmoline = $moline->create(DolibarrApiAccess::$user);
526 if ($resultmoline <= 0) {
528 throw new RestException(500, $moline->error);
537 foreach ($this->mo->lines as $line) {
538 if ($line->role ==
'toproduce') {
539 $tmpproduct =
new Product($this->db);
540 $tmpproduct->fetch($line->fk_product);
541 if ($line->qty != 0) {
542 $qtytoprocess = $line->qty;
543 if (isset($line->fk_warehouse)) {
544 if (!($line->fk_warehouse > 0)) {
545 $langs->load(
"errors");
547 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Warehouse"), $tmpproduct->ref));
549 if ($tmpproduct->status_batch) {
550 $langs->load(
"errors");
552 throw new RestException(500, $langs->trans(
"ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv(
"Batch"), $tmpproduct->ref));
556 if (!$error && $line->fk_warehouse > 0) {
558 $id_product_batch = 0;
559 $stockmove->origin_type =
'mo';
560 $stockmove->origin_id = $this->mo->id;
561 if ($qtytoprocess >= 0) {
562 $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
564 $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement,
dol_now(),
'',
'', $tmpproduct->status_batch, $id_product_batch, $codemovement);
566 if ($idstockmove < 0) {
568 throw new RestException(500, $stockmove->error);
573 $moline =
new MoLine($this->db);
574 $moline->fk_mo = $this->mo->id;
575 $moline->position = $pos;
576 $moline->fk_product = $line->fk_product;
577 $moline->fk_warehouse = $line->fk_warehouse;
578 $moline->qty = $qtytoprocess;
579 $moline->batch = $tmpproduct->status_batch;
580 $moline->role =
'produced';
581 $moline->fk_mrp_production = $line->id;
582 $moline->fk_stock_movement = $idstockmove;
583 $moline->fk_user_creat = DolibarrApiAccess::$user->id;
585 $resultmoline = $moline->create(DolibarrApiAccess::$user);
586 if ($resultmoline <= 0) {
588 throw new RestException(500, $moline->error);
598 if ($autoclose > 0) {
599 foreach ($this->mo->lines as $line) {
600 if ($line->role ==
'toconsume') {
601 $arrayoflines = $this->mo->fetchLinesLinked(
'consumed', $line->id);
602 $alreadyconsumed = 0;
603 foreach ($arrayoflines as $line2) {
604 $alreadyconsumed += $line2[
'qty'];
607 if ($alreadyconsumed < $line->qty) {
608 $consumptioncomplete =
false;
611 if ($line->role ==
'toproduce') {
612 $arrayoflines = $this->mo->fetchLinesLinked(
'produced', $line->id);
613 $alreadyproduced = 0;
614 foreach ($arrayoflines as $line2) {
615 $alreadyproduced += $line2[
'qty'];
618 if ($alreadyproduced < $line->qty) {
619 $productioncomplete =
false;
624 $consumptioncomplete =
false;
625 $productioncomplete =
false;
631 dol_syslog(
"consumptioncomplete = ".$consumptioncomplete.
" productioncomplete = ".$productioncomplete);
633 if ($consumptioncomplete && $productioncomplete) {
634 $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0,
'',
'MRP_MO_PRODUCED');
636 $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0,
'',
'MRP_MO_PRODUCED');
639 throw new RestException(500, $this->mo->error);
642 return $this->mo->id;
656 $object = parent::_cleanObjectDatas($object);
658 unset($object->rowid);
659 unset($object->canvas);
661 unset($object->name);
662 unset($object->lastname);
663 unset($object->firstname);
664 unset($object->civility_id);
665 unset($object->statut);
666 unset($object->state);
667 unset($object->state_id);
668 unset($object->state_code);
669 unset($object->region);
670 unset($object->region_code);
671 unset($object->country);
672 unset($object->country_id);
673 unset($object->country_code);
674 unset($object->barcode_type);
675 unset($object->barcode_type_code);
676 unset($object->barcode_type_label);
677 unset($object->barcode_type_coder);
678 unset($object->total_ht);
679 unset($object->total_tva);
680 unset($object->total_localtax1);
681 unset($object->total_localtax2);
682 unset($object->total_ttc);
683 unset($object->fk_account);
684 unset($object->comments);
685 unset($object->note);
686 unset($object->mode_reglement_id);
687 unset($object->cond_reglement_id);
688 unset($object->cond_reglement);
689 unset($object->shipping_method_id);
690 unset($object->fk_incoterms);
691 unset($object->label_incoterms);
692 unset($object->location_incoterms);
695 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
696 $nboflines = count($object->lines);
697 for ($i = 0; $i < $nboflines; $i++) {
700 unset($object->lines[$i]->lines);
701 unset($object->lines[$i]->note);