Conditions | 37 |
Paths | > 20000 |
Total Lines | 159 |
Code Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
442 | public function update($user = null, $notrigger = 0) |
||
443 | { |
||
444 | $error = 0; |
||
445 | |||
446 | dol_syslog(get_class($this) . "::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty"); |
||
447 | |||
448 | $this->db->begin(); |
||
449 | |||
450 | // Clean parameters |
||
451 | if (empty($this->qty)) { |
||
452 | $this->qty = 0; |
||
453 | } |
||
454 | $qty = price2num($this->qty); |
||
455 | $remainingQty = 0; |
||
456 | $batch = null; |
||
457 | $batch_id = null; |
||
458 | $expedition_batch_id = null; |
||
459 | if (is_array($this->detail_batch)) { // array of ExpeditionLineBatch |
||
460 | if (count($this->detail_batch) > 1) { |
||
461 | dol_syslog(get_class($this) . '::update only possible for one batch', LOG_ERR); |
||
462 | $this->errors[] = 'ErrorBadParameters'; |
||
463 | $error++; |
||
464 | } else { |
||
465 | $batch = $this->detail_batch[0]->batch; |
||
466 | $batch_id = $this->detail_batch[0]->fk_origin_stock; |
||
467 | $expedition_batch_id = $this->detail_batch[0]->id; |
||
468 | if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) { |
||
469 | dol_syslog(get_class($this) . '::update only possible for batch of same warehouse', LOG_ERR); |
||
470 | $this->errors[] = 'ErrorBadParameters'; |
||
471 | $error++; |
||
472 | } |
||
473 | $qty = price2num($this->detail_batch[0]->qty); |
||
474 | } |
||
475 | } elseif (!empty($this->detail_batch)) { |
||
476 | $batch = $this->detail_batch->batch; |
||
477 | $batch_id = $this->detail_batch->fk_origin_stock; |
||
478 | $expedition_batch_id = $this->detail_batch->id; |
||
479 | if ($this->entrepot_id != $this->detail_batch->entrepot_id) { |
||
480 | dol_syslog(get_class($this) . '::update only possible for batch of same warehouse', LOG_ERR); |
||
481 | $this->errors[] = 'ErrorBadParameters'; |
||
482 | $error++; |
||
483 | } |
||
484 | $qty = price2num($this->detail_batch->qty); |
||
485 | } |
||
486 | |||
487 | // check parameters |
||
488 | if (!isset($this->id) || !isset($this->entrepot_id)) { |
||
489 | dol_syslog(get_class($this) . '::update missing line id and/or warehouse id', LOG_ERR); |
||
490 | $this->errors[] = 'ErrorMandatoryParametersNotProvided'; |
||
491 | $error++; |
||
492 | return -1; |
||
493 | } |
||
494 | |||
495 | // update lot |
||
496 | |||
497 | if (!empty($batch) && isModEnabled('productbatch')) { |
||
498 | dol_syslog(get_class($this) . "::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch"); |
||
499 | |||
500 | if (empty($batch_id) || empty($this->fk_product)) { |
||
501 | dol_syslog(get_class($this) . '::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR); |
||
502 | $this->errors[] = 'ErrorMandatoryParametersNotProvided'; |
||
503 | $error++; |
||
504 | } |
||
505 | |||
506 | // fetch remaining lot qty |
||
507 | $shipmentlinebatch = new ExpeditionLineBatch($this->db); |
||
508 | |||
509 | if (!$error && ($lotArray = $shipmentlinebatch->fetchAll($this->id)) < 0) { |
||
510 | $this->errors[] = $this->db->lasterror() . " - ExpeditionLineBatch::fetchAll"; |
||
511 | $error++; |
||
512 | } else { |
||
513 | // calculate new total line qty |
||
514 | foreach ($lotArray as $lot) { |
||
515 | if ($expedition_batch_id != $lot->id) { |
||
516 | $remainingQty += $lot->qty; |
||
517 | } |
||
518 | } |
||
519 | $qty += $remainingQty; |
||
520 | |||
521 | //fetch lot details |
||
522 | |||
523 | // fetch from product_lot |
||
524 | require_once DOL_DOCUMENT_ROOT . '/product/stock/class/productlot.class.php'; |
||
525 | $lot = new Productlot($this->db); |
||
526 | if ($lot->fetch(0, $this->fk_product, $batch) < 0) { |
||
527 | $this->errors[] = $lot->errors; |
||
528 | $error++; |
||
529 | } |
||
530 | if (!$error && !empty($expedition_batch_id)) { |
||
531 | // delete lot expedition line |
||
532 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "expeditiondet_batch"; |
||
533 | $sql .= " WHERE fk_expeditiondet = " . ((int) $this->id); |
||
534 | $sql .= " AND rowid = " . ((int) $expedition_batch_id); |
||
535 | |||
536 | if (!$this->db->query($sql)) { |
||
537 | $this->errors[] = $this->db->lasterror() . " - sql=$sql"; |
||
538 | $error++; |
||
539 | } |
||
540 | } |
||
541 | if (!$error && $this->detail_batch->qty > 0) { |
||
542 | // create lot expedition line |
||
543 | if (isset($lot->id)) { |
||
544 | $shipmentLot = new ExpeditionLineBatch($this->db); |
||
545 | $shipmentLot->batch = $lot->batch; |
||
546 | $shipmentLot->eatby = $lot->eatby; |
||
547 | $shipmentLot->sellby = $lot->sellby; |
||
548 | $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id; |
||
549 | $shipmentLot->qty = $this->detail_batch->qty; |
||
550 | $shipmentLot->fk_origin_stock = $batch_id; |
||
551 | if ($shipmentLot->create($this->id) < 0) { |
||
552 | $this->errors = $shipmentLot->errors; |
||
553 | $error++; |
||
554 | } |
||
555 | } |
||
556 | } |
||
557 | } |
||
558 | } |
||
559 | if (!$error) { |
||
560 | // update line |
||
561 | $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET"; |
||
562 | $sql .= " fk_entrepot = " . ($this->entrepot_id > 0 ? $this->entrepot_id : 'null'); |
||
563 | $sql .= " , qty = " . ((float) price2num($qty, 'MS')); |
||
564 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
565 | |||
566 | if (!$this->db->query($sql)) { |
||
567 | $this->errors[] = $this->db->lasterror() . " - sql=$sql"; |
||
568 | $error++; |
||
569 | } |
||
570 | } |
||
571 | |||
572 | if (!$error) { |
||
573 | if (!$error) { |
||
574 | $result = $this->insertExtraFields(); |
||
575 | if ($result < 0) { |
||
576 | $this->errors[] = $this->error; |
||
577 | $error++; |
||
578 | } |
||
579 | } |
||
580 | } |
||
581 | |||
582 | if (!$error && !$notrigger) { |
||
583 | // Call trigger |
||
584 | $result = $this->call_trigger('LINESHIPPING_MODIFY', $user); |
||
585 | if ($result < 0) { |
||
586 | $this->errors[] = $this->error; |
||
587 | $error++; |
||
588 | } |
||
589 | // End call triggers |
||
590 | } |
||
591 | if (!$error) { |
||
592 | $this->db->commit(); |
||
593 | return 1; |
||
594 | } else { |
||
595 | foreach ($this->errors as $errmsg) { |
||
596 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
597 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
598 | } |
||
599 | $this->db->rollback(); |
||
600 | return -1 * $error; |
||
601 | } |
||
604 |