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