Conditions | 71 |
Paths | > 20000 |
Total Lines | 342 |
Code Lines | 242 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
316 | public function produceAndConsumeAll($id, $request_data = null) |
||
317 | { |
||
318 | global $langs; |
||
319 | |||
320 | $error = 0; |
||
321 | |||
322 | if (!DolibarrApiAccess::$user->hasRight('mrp', 'write')) { |
||
323 | throw new RestException(403, 'Not enough permission'); |
||
324 | } |
||
325 | $result = $this->mo->fetch($id); |
||
326 | if (!$result) { |
||
327 | throw new RestException(404, 'MO not found'); |
||
328 | } |
||
329 | |||
330 | if ($this->mo->status != Mo::STATUS_VALIDATED && $this->mo->status != Mo::STATUS_INPROGRESS) { |
||
331 | throw new RestException(405, 'Error bad status of MO'); |
||
332 | } |
||
333 | |||
334 | // Code for consume and produce... |
||
335 | require_once constant('DOL_DOCUMENT_ROOT') . '/product/class/product.class.php'; |
||
336 | require_once constant('DOL_DOCUMENT_ROOT') . '/product/stock/class/mouvementstock.class.php'; |
||
337 | require_once constant('DOL_DOCUMENT_ROOT') . '/mrp/lib/mrp_mo.lib.php'; |
||
338 | |||
339 | $stockmove = new MouvementStock($this->db); |
||
|
|||
340 | |||
341 | $labelmovement = ''; |
||
342 | $codemovement = ''; |
||
343 | $autoclose = 1; |
||
344 | $arraytoconsume = array(); |
||
345 | $arraytoproduce = array(); |
||
346 | |||
347 | foreach ($request_data as $field => $value) { |
||
348 | if ($field == 'inventorylabel') { |
||
349 | $labelmovement = $value; |
||
350 | } |
||
351 | if ($field == 'inventorycode') { |
||
352 | $codemovement = $value; |
||
353 | } |
||
354 | if ($field == 'autoclose') { |
||
355 | $autoclose = $value; |
||
356 | } |
||
357 | if ($field == 'arraytoconsume') { |
||
358 | $arraytoconsume = $value; |
||
359 | } |
||
360 | if ($field == 'arraytoproduce') { |
||
361 | $arraytoproduce = $value; |
||
362 | } |
||
363 | if ($field === 'caller') { |
||
364 | // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
||
365 | $stockmove->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
366 | continue; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | if (empty($labelmovement)) { |
||
371 | throw new RestException(500, "Field inventorylabel not provided"); |
||
372 | } |
||
373 | if (empty($codemovement)) { |
||
374 | throw new RestException(500, "Field inventorycode not provided"); |
||
375 | } |
||
376 | |||
377 | $consumptioncomplete = true; |
||
378 | $productioncomplete = true; |
||
379 | |||
380 | if (!empty($arraytoconsume) && !empty($arraytoproduce)) { |
||
381 | $pos = 0; |
||
382 | $arrayofarrayname = array("arraytoconsume","arraytoproduce"); |
||
383 | foreach ($arrayofarrayname as $arrayname) { |
||
384 | foreach (${$arrayname} as $value) { |
||
385 | $tmpproduct = new Product($this->db); |
||
386 | if (empty($value["objectid"])) { |
||
387 | throw new RestException(500, "Field objectid required in " . $arrayname); |
||
388 | } |
||
389 | $tmpproduct->fetch($value["qty"]); |
||
390 | if (empty($value["qty"])) { |
||
391 | throw new RestException(500, "Field qty required in " . $arrayname); |
||
392 | } |
||
393 | if ($value["qty"] != 0) { |
||
394 | $qtytoprocess = $value["qty"]; |
||
395 | if (isset($value["fk_warehouse"])) { // If there is a warehouse to set |
||
396 | if (!($value["fk_warehouse"] > 0)) { // If there is no warehouse set. |
||
397 | $error++; |
||
398 | throw new RestException(500, "Field fk_warehouse must be > 0 in " . $arrayname); |
||
399 | } |
||
400 | if ($tmpproduct->status_batch) { |
||
401 | $error++; |
||
402 | throw new RestException(500, "Product " . $tmpproduct->ref . "must be in batch"); |
||
403 | } |
||
404 | } |
||
405 | $idstockmove = 0; |
||
406 | if (!$error && $value["fk_warehouse"] > 0) { |
||
407 | // Record consumption to do and stock movement |
||
408 | $id_product_batch = 0; |
||
409 | |||
410 | $stockmove->setOrigin($this->mo->element, $this->mo->id); |
||
411 | |||
412 | if ($arrayname == 'arraytoconsume') { |
||
413 | $moline = new MoLine($this->db); |
||
414 | $moline->fk_mo = $this->mo->id; |
||
415 | $moline->position = $pos; |
||
416 | $moline->fk_product = $value["objectid"]; |
||
417 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
418 | $moline->qty = $qtytoprocess; |
||
419 | $moline->batch = $tmpproduct->status_batch; |
||
420 | $moline->role = 'toproduce'; |
||
421 | $moline->fk_mrp_production = ""; |
||
422 | $moline->fk_stock_movement = $idstockmove; |
||
423 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
424 | |||
425 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
426 | if ($resultmoline <= 0) { |
||
427 | $error++; |
||
428 | throw new RestException(500, $moline->error); |
||
429 | } |
||
430 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
431 | } else { |
||
432 | $moline = new MoLine($this->db); |
||
433 | $moline->fk_mo = $this->mo->id; |
||
434 | $moline->position = $pos; |
||
435 | $moline->fk_product = $value["objectid"]; |
||
436 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
437 | $moline->qty = $qtytoprocess; |
||
438 | $moline->batch = $tmpproduct->status_batch; |
||
439 | $moline->role = 'toconsume'; |
||
440 | $moline->fk_mrp_production = ""; |
||
441 | $moline->fk_stock_movement = $idstockmove; |
||
442 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
443 | |||
444 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
445 | if ($resultmoline <= 0) { |
||
446 | $error++; |
||
447 | throw new RestException(500, $moline->error); |
||
448 | } |
||
449 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $value["objectid"], $value["fk_warehouse"], $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
450 | } |
||
451 | if ($idstockmove < 0) { |
||
452 | $error++; |
||
453 | throw new RestException(500, $stockmove->error); |
||
454 | } |
||
455 | } |
||
456 | if (!$error) { |
||
457 | // Record consumption done |
||
458 | $moline = new MoLine($this->db); |
||
459 | $moline->fk_mo = $this->mo->id; |
||
460 | $moline->position = $pos; |
||
461 | $moline->fk_product = $value["objectid"]; |
||
462 | $moline->fk_warehouse = $value["fk_warehouse"]; |
||
463 | $moline->qty = $qtytoprocess; |
||
464 | $moline->batch = $tmpproduct->status_batch; |
||
465 | if ($arrayname == "arraytoconsume") { |
||
466 | $moline->role = 'consumed'; |
||
467 | } else { |
||
468 | $moline->role = 'produced'; |
||
469 | } |
||
470 | $moline->fk_mrp_production = ""; |
||
471 | $moline->fk_stock_movement = $idstockmove; |
||
472 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
473 | |||
474 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
475 | if ($resultmoline <= 0) { |
||
476 | $error++; |
||
477 | throw new RestException(500, $moline->error); |
||
478 | } |
||
479 | |||
480 | $pos++; |
||
481 | } |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | if (!$error) { |
||
486 | if ($autoclose <= 0) { |
||
487 | $consumptioncomplete = false; |
||
488 | $productioncomplete = false; |
||
489 | } |
||
490 | } |
||
491 | } else { |
||
492 | $pos = 0; |
||
493 | foreach ($this->mo->lines as $line) { |
||
494 | if ($line->role == 'toconsume') { |
||
495 | $tmpproduct = new Product($this->db); |
||
496 | $tmpproduct->fetch($line->fk_product); |
||
497 | if ($line->qty != 0) { |
||
498 | $qtytoprocess = $line->qty; |
||
499 | if (isset($line->fk_warehouse)) { // If there is a warehouse to set |
||
500 | if (!($line->fk_warehouse > 0)) { // If there is no warehouse set. |
||
501 | $langs->load("errors"); |
||
502 | $error++; |
||
503 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref)); |
||
504 | } |
||
505 | if ($tmpproduct->status_batch) { |
||
506 | $langs->load("errors"); |
||
507 | $error++; |
||
508 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref)); |
||
509 | } |
||
510 | } |
||
511 | $idstockmove = 0; |
||
512 | if (!$error && $line->fk_warehouse > 0) { |
||
513 | // Record stock movement |
||
514 | $id_product_batch = 0; |
||
515 | $stockmove->origin_type = 'mo'; |
||
516 | $stockmove->origin_id = $this->mo->id; |
||
517 | if ($qtytoprocess >= 0) { |
||
518 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
519 | } else { |
||
520 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
521 | } |
||
522 | if ($idstockmove < 0) { |
||
523 | $error++; |
||
524 | throw new RestException(500, $stockmove->error); |
||
525 | } |
||
526 | } |
||
527 | if (!$error) { |
||
528 | // Record consumption |
||
529 | $moline = new MoLine($this->db); |
||
530 | $moline->fk_mo = $this->mo->id; |
||
531 | $moline->position = $pos; |
||
532 | $moline->fk_product = $line->fk_product; |
||
533 | $moline->fk_warehouse = $line->fk_warehouse; |
||
534 | $moline->qty = $qtytoprocess; |
||
535 | $moline->batch = $tmpproduct->status_batch; |
||
536 | $moline->role = 'consumed'; |
||
537 | $moline->fk_mrp_production = $line->id; |
||
538 | $moline->fk_stock_movement = $idstockmove; |
||
539 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
540 | |||
541 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
542 | if ($resultmoline <= 0) { |
||
543 | $error++; |
||
544 | throw new RestException(500, $moline->error); |
||
545 | } |
||
546 | |||
547 | $pos++; |
||
548 | } |
||
549 | } |
||
550 | } |
||
551 | } |
||
552 | $pos = 0; |
||
553 | foreach ($this->mo->lines as $line) { |
||
554 | if ($line->role == 'toproduce') { |
||
555 | $tmpproduct = new Product($this->db); |
||
556 | $tmpproduct->fetch($line->fk_product); |
||
557 | if ($line->qty != 0) { |
||
558 | $qtytoprocess = $line->qty; |
||
559 | if (isset($line->fk_warehouse)) { // If there is a warehouse to set |
||
560 | if (!($line->fk_warehouse > 0)) { // If there is no warehouse set. |
||
561 | $langs->load("errors"); |
||
562 | $error++; |
||
563 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref)); |
||
564 | } |
||
565 | if ($tmpproduct->status_batch) { |
||
566 | $langs->load("errors"); |
||
567 | $error++; |
||
568 | throw new RestException(500, $langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref)); |
||
569 | } |
||
570 | } |
||
571 | $idstockmove = 0; |
||
572 | if (!$error && $line->fk_warehouse > 0) { |
||
573 | // Record stock movement |
||
574 | $id_product_batch = 0; |
||
575 | $stockmove->origin_type = 'mo'; |
||
576 | $stockmove->origin_id = $this->mo->id; |
||
577 | if ($qtytoprocess >= 0) { |
||
578 | $idstockmove = $stockmove->reception(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
579 | } else { |
||
580 | $idstockmove = $stockmove->livraison(DolibarrApiAccess::$user, $line->fk_product, $line->fk_warehouse, $qtytoprocess, 0, $labelmovement, dol_now(), '', '', $tmpproduct->status_batch, $id_product_batch, $codemovement); |
||
581 | } |
||
582 | if ($idstockmove < 0) { |
||
583 | $error++; |
||
584 | throw new RestException(500, $stockmove->error); |
||
585 | } |
||
586 | } |
||
587 | if (!$error) { |
||
588 | // Record consumption |
||
589 | $moline = new MoLine($this->db); |
||
590 | $moline->fk_mo = $this->mo->id; |
||
591 | $moline->position = $pos; |
||
592 | $moline->fk_product = $line->fk_product; |
||
593 | $moline->fk_warehouse = $line->fk_warehouse; |
||
594 | $moline->qty = $qtytoprocess; |
||
595 | $moline->batch = $tmpproduct->status_batch; |
||
596 | $moline->role = 'produced'; |
||
597 | $moline->fk_mrp_production = $line->id; |
||
598 | $moline->fk_stock_movement = $idstockmove; |
||
599 | $moline->fk_user_creat = DolibarrApiAccess::$user->id; |
||
600 | |||
601 | $resultmoline = $moline->create(DolibarrApiAccess::$user); |
||
602 | if ($resultmoline <= 0) { |
||
603 | $error++; |
||
604 | throw new RestException(500, $moline->error); |
||
605 | } |
||
606 | |||
607 | $pos++; |
||
608 | } |
||
609 | } |
||
610 | } |
||
611 | } |
||
612 | |||
613 | if (!$error) { |
||
614 | if ($autoclose > 0) { |
||
615 | foreach ($this->mo->lines as $line) { |
||
616 | if ($line->role == 'toconsume') { |
||
617 | $arrayoflines = $this->mo->fetchLinesLinked('consumed', $line->id); |
||
618 | $alreadyconsumed = 0; |
||
619 | foreach ($arrayoflines as $line2) { |
||
620 | $alreadyconsumed += $line2['qty']; |
||
621 | } |
||
622 | |||
623 | if ($alreadyconsumed < $line->qty) { |
||
624 | $consumptioncomplete = false; |
||
625 | } |
||
626 | } |
||
627 | if ($line->role == 'toproduce') { |
||
628 | $arrayoflines = $this->mo->fetchLinesLinked('produced', $line->id); |
||
629 | $alreadyproduced = 0; |
||
630 | foreach ($arrayoflines as $line2) { |
||
631 | $alreadyproduced += $line2['qty']; |
||
632 | } |
||
633 | |||
634 | if ($alreadyproduced < $line->qty) { |
||
635 | $productioncomplete = false; |
||
636 | } |
||
637 | } |
||
638 | } |
||
639 | } else { |
||
640 | $consumptioncomplete = false; |
||
641 | $productioncomplete = false; |
||
642 | } |
||
643 | } |
||
644 | } |
||
645 | |||
646 | // Update status of MO |
||
647 | dol_syslog("consumptioncomplete = " . json_encode($consumptioncomplete) . " productioncomplete = " . json_encode($productioncomplete)); |
||
648 | if ($consumptioncomplete && $productioncomplete) { |
||
649 | $result = $this->mo->setStatut(Mo::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED'); |
||
650 | } else { |
||
651 | $result = $this->mo->setStatut(Mo::STATUS_INPROGRESS, 0, '', 'MRP_MO_PRODUCED'); |
||
652 | } |
||
653 | if ($result <= 0) { |
||
654 | throw new RestException(500, $this->mo->error); |
||
655 | } |
||
656 | |||
657 | return $this->mo->id; |
||
658 | } |
||
1001 |