| Conditions | 120 |
| Paths | > 20000 |
| Total Lines | 375 |
| Code Lines | 228 |
| 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 |
||
| 384 | public function setObjectData(&$object, $action, $amounts, $fuser = null) |
||
| 385 | { |
||
| 386 | global $langs, $user, $mysoc; |
||
| 387 | |||
| 388 | if (is_object($fuser)) { |
||
| 389 | $user = $fuser; |
||
| 390 | } |
||
| 391 | |||
| 392 | // Generic fields |
||
| 393 | |||
| 394 | // action |
||
| 395 | $this->action = $action; |
||
| 396 | // amount |
||
| 397 | $this->amounts = $amounts; |
||
| 398 | // date |
||
| 399 | if ($object->element == 'payment' || $object->element == 'payment_supplier') { |
||
| 400 | $this->date_object = empty($object->datepaye) ? $object->date : $object->datepaye; |
||
| 401 | } elseif ($object->element == 'payment_salary') { |
||
| 402 | $this->date_object = $object->datev; |
||
| 403 | } elseif ($object->element == 'payment_donation' || $object->element == 'payment_various') { |
||
| 404 | $this->date_object = empty($object->datepaid) ? $object->datep : $object->datepaid; |
||
| 405 | } elseif ($object->element == 'subscription') { |
||
| 406 | $this->date_object = $object->dateh; |
||
| 407 | } elseif ($object->element == 'cashcontrol') { |
||
| 408 | $this->date_object = $object->date_creation; |
||
| 409 | } elseif (property_exists($object, 'date')) { |
||
| 410 | // Generic case |
||
| 411 | $this->date_object = $object->date; |
||
| 412 | } elseif (property_exists($object, 'datem')) { |
||
| 413 | // Generic case (second chance, for example for stock movement) |
||
| 414 | $this->date_object = $object->datem; |
||
| 415 | } |
||
| 416 | |||
| 417 | // ref |
||
| 418 | $this->ref_object = ((!empty($object->newref)) ? $object->newref : $object->ref); // newref is set when validating a draft, ref is set in other cases |
||
| 419 | // type of object |
||
| 420 | $this->element = $object->element; |
||
| 421 | // id of object |
||
| 422 | $this->fk_object = $object->id; |
||
| 423 | |||
| 424 | |||
| 425 | // Set object_data |
||
| 426 | $this->object_data = new stdClass(); |
||
|
|
|||
| 427 | // Add fields to exclude |
||
| 428 | $arrayoffieldstoexclude = [ |
||
| 429 | 'table_element', 'fields', 'ref_previous', 'ref_next', 'origin', 'origin_id', 'oldcopy', 'picto', 'error', 'errors', 'model_pdf', 'modelpdf', 'last_main_doc', 'civility_id', 'contact', 'contact_id', |
||
| 430 | 'table_element_line', 'ismultientitymanaged', 'isextrafieldmanaged', |
||
| 431 | 'array_languages', |
||
| 432 | 'childtables', |
||
| 433 | 'contact_ids', |
||
| 434 | 'context', |
||
| 435 | 'labelStatus', |
||
| 436 | 'labelStatusShort', |
||
| 437 | 'linkedObjectsIds', |
||
| 438 | 'linkedObjects', |
||
| 439 | 'fk_delivery_address', |
||
| 440 | 'projet', // There is already ->fk_project |
||
| 441 | 'restrictiononfksoc', |
||
| 442 | 'specimen', |
||
| 443 | ]; |
||
| 444 | // Add more fields to exclude depending on object type |
||
| 445 | if ($this->element == 'cashcontrol') { |
||
| 446 | $arrayoffieldstoexclude = array_merge($arrayoffieldstoexclude, [ |
||
| 447 | 'name', 'lastname', 'firstname', 'region', 'region_id', 'region_code', 'state', 'state_id', 'state_code', 'country', 'country_id', 'country_code', |
||
| 448 | 'total_ht', 'total_tva', 'total_ttc', 'total_localtax1', 'total_localtax2', |
||
| 449 | 'barcode_type', 'barcode_type_code', 'barcode_type_label', 'barcode_type_coder', 'mode_reglement_id', 'cond_reglement_id', 'mode_reglement', 'cond_reglement', 'shipping_method_id', |
||
| 450 | 'fk_incoterms', 'label_incoterms', 'location_incoterms', 'lines', |
||
| 451 | ]); |
||
| 452 | } |
||
| 453 | |||
| 454 | // Add thirdparty info |
||
| 455 | if (empty($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) { |
||
| 456 | $object->fetch_thirdparty(); |
||
| 457 | } |
||
| 458 | if (!empty($object->thirdparty)) { |
||
| 459 | $this->object_data->thirdparty = new stdClass(); |
||
| 460 | |||
| 461 | foreach ($object->thirdparty as $key => $value) { |
||
| 462 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 463 | continue; // Discard some properties |
||
| 464 | } |
||
| 465 | if ( |
||
| 466 | !in_array($key, [ |
||
| 467 | 'name', 'name_alias', 'ref_ext', 'address', 'zip', 'town', 'state_code', 'country_code', 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6', 'phone', 'fax', 'email', 'barcode', |
||
| 468 | 'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur', |
||
| 469 | ]) |
||
| 470 | ) { |
||
| 471 | continue; // Discard if not into a dedicated list |
||
| 472 | } |
||
| 473 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 474 | $this->object_data->thirdparty->$key = $value; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | // Add company info |
||
| 480 | if (!empty($mysoc)) { |
||
| 481 | $this->object_data->mycompany = new stdClass(); |
||
| 482 | |||
| 483 | foreach ($mysoc as $key => $value) { |
||
| 484 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 485 | continue; // Discard some properties |
||
| 486 | } |
||
| 487 | if ( |
||
| 488 | !in_array($key, [ |
||
| 489 | 'name', 'name_alias', 'ref_ext', 'address', 'zip', 'town', 'state_code', 'country_code', 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6', 'phone', 'fax', 'email', 'barcode', |
||
| 490 | 'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur', |
||
| 491 | ]) |
||
| 492 | ) { |
||
| 493 | continue; // Discard if not into a dedicated list |
||
| 494 | } |
||
| 495 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 496 | $this->object_data->mycompany->$key = $value; |
||
| 497 | } |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | // Add user info |
||
| 502 | if (!empty($user)) { |
||
| 503 | $this->fk_user = $user->id; |
||
| 504 | $this->user_fullname = $user->getFullName($langs); |
||
| 505 | } |
||
| 506 | |||
| 507 | // Field specific to object |
||
| 508 | if ($this->element == 'facture') { |
||
| 509 | foreach ($object as $key => $value) { |
||
| 510 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 511 | continue; // Discard some properties |
||
| 512 | } |
||
| 513 | if ( |
||
| 514 | !in_array($key, [ |
||
| 515 | 'ref', 'ref_client', 'ref_supplier', 'date', 'datef', 'datev', 'type', 'total_ht', 'total_tva', 'total_ttc', 'localtax1', 'localtax2', 'revenuestamp', 'datepointoftax', 'note_public', 'lines', |
||
| 516 | ]) |
||
| 517 | ) { |
||
| 518 | continue; // Discard if not into a dedicated list |
||
| 519 | } |
||
| 520 | if ($key == 'lines') { |
||
| 521 | $lineid = 0; |
||
| 522 | foreach ($value as $tmpline) { // $tmpline is object FactureLine |
||
| 523 | $lineid++; |
||
| 524 | foreach ($tmpline as $keyline => $valueline) { |
||
| 525 | if ( |
||
| 526 | !in_array($keyline, [ |
||
| 527 | 'ref', 'multicurrency_code', 'multicurrency_total_ht', 'multicurrency_total_tva', 'multicurrency_total_ttc', 'qty', 'product_type', 'vat_src_code', 'tva_tx', 'info_bits', 'localtax1_tx', 'localtax2_tx', 'total_ht', 'total_tva', 'total_ttc', 'total_localtax1', 'total_localtax2', |
||
| 528 | ]) |
||
| 529 | ) { |
||
| 530 | continue; // Discard if not into a dedicated list |
||
| 531 | } |
||
| 532 | |||
| 533 | if (empty($this->object_data->invoiceline[$lineid]) || !is_object($this->object_data->invoiceline[$lineid])) { // To avoid warning |
||
| 534 | $this->object_data->invoiceline[$lineid] = new stdClass(); |
||
| 535 | } |
||
| 536 | |||
| 537 | if (!is_object($valueline) && !is_null($valueline) && $valueline !== '') { |
||
| 538 | $this->object_data->invoiceline[$lineid]->$keyline = $valueline; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | } |
||
| 542 | } elseif (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 543 | $this->object_data->$key = $value; |
||
| 544 | } |
||
| 545 | } |
||
| 546 | |||
| 547 | if (!empty($object->newref)) { |
||
| 548 | $this->object_data->ref = $object->newref; |
||
| 549 | } |
||
| 550 | } elseif ($this->element == 'invoice_supplier') { |
||
| 551 | foreach ($object as $key => $value) { |
||
| 552 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 553 | continue; // Discard some properties |
||
| 554 | } |
||
| 555 | if ( |
||
| 556 | !in_array($key, [ |
||
| 557 | 'ref', 'ref_client', 'ref_supplier', 'date', 'datef', 'type', 'total_ht', 'total_tva', 'total_ttc', 'localtax1', 'localtax2', 'revenuestamp', 'datepointoftax', 'note_public', |
||
| 558 | ]) |
||
| 559 | ) { |
||
| 560 | continue; // Discard if not into a dedicated list |
||
| 561 | } |
||
| 562 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 563 | $this->object_data->$key = $value; |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | if (!empty($object->newref)) { |
||
| 568 | $this->object_data->ref = $object->newref; |
||
| 569 | } |
||
| 570 | } elseif ($this->element == 'payment' || $this->element == 'payment_supplier' || $this->element == 'payment_donation' || $this->element == 'payment_various') { |
||
| 571 | $datepayment = $object->datepaye ? $object->datepaye : ($object->datepaid ? $object->datepaid : $object->datep); |
||
| 572 | $paymenttypeid = $object->paiementid ? $object->paiementid : ($object->paymenttype ? $object->paymenttype : $object->type_payment); |
||
| 573 | |||
| 574 | $this->object_data->ref = $object->ref; |
||
| 575 | $this->object_data->date = $datepayment; |
||
| 576 | $this->object_data->type_code = dol_getIdFromCode($this->db, $paymenttypeid, 'c_paiement', 'id', 'code'); |
||
| 577 | |||
| 578 | if (!empty($object->num_payment)) { |
||
| 579 | $this->object_data->payment_num = $object->num_payment; |
||
| 580 | } |
||
| 581 | if (!empty($object->note_private)) { |
||
| 582 | $this->object_data->note_private = $object->note_private; |
||
| 583 | } |
||
| 584 | //$this->object_data->fk_account = $object->fk_account; |
||
| 585 | //var_dump($this->object_data);exit; |
||
| 586 | |||
| 587 | $totalamount = 0; |
||
| 588 | |||
| 589 | // Loop on each invoice payment amount (payment_part) |
||
| 590 | if (is_array($object->amounts) && !empty($object->amounts)) { |
||
| 591 | $paymentpartnumber = 0; |
||
| 592 | foreach ($object->amounts as $objid => $amount) { |
||
| 593 | if (empty($amount)) { |
||
| 594 | continue; |
||
| 595 | } |
||
| 596 | |||
| 597 | $totalamount += $amount; |
||
| 598 | |||
| 599 | $tmpobject = null; |
||
| 600 | if ($this->element == 'payment_supplier') { |
||
| 601 | $tmpobject = new FactureFournisseur($this->db); |
||
| 602 | } elseif ($this->element == 'payment') { |
||
| 603 | $tmpobject = new Facture($this->db); |
||
| 604 | } elseif ($this->element == 'payment_donation') { |
||
| 605 | include_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php'; |
||
| 606 | $tmpobject = new Don($this->db); |
||
| 607 | } elseif ($this->element == 'payment_various') { |
||
| 608 | include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php'; |
||
| 609 | $tmpobject = new PaymentVarious($this->db); |
||
| 610 | } |
||
| 611 | |||
| 612 | if (!is_object($tmpobject)) { |
||
| 613 | continue; |
||
| 614 | } |
||
| 615 | |||
| 616 | $result = $tmpobject->fetch($objid); |
||
| 617 | |||
| 618 | if ($result <= 0) { |
||
| 619 | $this->error = $tmpobject->error; |
||
| 620 | $this->errors = $tmpobject->errors; |
||
| 621 | dol_syslog("Failed to fetch object with id " . $objid, LOG_ERR); |
||
| 622 | return -1; |
||
| 623 | } |
||
| 624 | |||
| 625 | $paymentpart = new stdClass(); |
||
| 626 | $paymentpart->amount = $amount; |
||
| 627 | |||
| 628 | if (!in_array($this->element, ['payment_donation', 'payment_various'])) { |
||
| 629 | $result = $tmpobject->fetch_thirdparty(); |
||
| 630 | if ($result == 0) { |
||
| 631 | $this->error = 'Failed to fetch thirdparty for object with id ' . $tmpobject->id; |
||
| 632 | $this->errors[] = $this->error; |
||
| 633 | dol_syslog("Failed to fetch thirdparty for object with id " . $tmpobject->id, LOG_ERR); |
||
| 634 | return -1; |
||
| 635 | } elseif ($result < 0) { |
||
| 636 | $this->error = $tmpobject->error; |
||
| 637 | $this->errors = $tmpobject->errors; |
||
| 638 | return -1; |
||
| 639 | } |
||
| 640 | |||
| 641 | $paymentpart->thirdparty = new stdClass(); |
||
| 642 | foreach ($tmpobject->thirdparty as $key => $value) { |
||
| 643 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 644 | continue; // Discard some properties |
||
| 645 | } |
||
| 646 | if ( |
||
| 647 | !in_array($key, [ |
||
| 648 | 'name', 'name_alias', 'ref_ext', 'address', 'zip', 'town', 'state_code', 'country_code', 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6', 'phone', 'fax', 'email', 'barcode', |
||
| 649 | 'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur', |
||
| 650 | ]) |
||
| 651 | ) { |
||
| 652 | continue; // Discard if not into a dedicated list |
||
| 653 | } |
||
| 654 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 655 | $paymentpart->thirdparty->$key = $value; |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | // Init object to avoid warnings |
||
| 661 | if ($this->element == 'payment_donation') { |
||
| 662 | $paymentpart->donation = new stdClass(); |
||
| 663 | } else { |
||
| 664 | $paymentpart->invoice = new stdClass(); |
||
| 665 | } |
||
| 666 | |||
| 667 | if ($this->element != 'payment_various') { |
||
| 668 | foreach ($tmpobject as $key => $value) { |
||
| 669 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 670 | continue; // Discard some properties |
||
| 671 | } |
||
| 672 | if ( |
||
| 673 | !in_array($key, [ |
||
| 674 | 'ref', 'ref_client', 'ref_supplier', 'date', 'datef', 'type', 'total_ht', 'total_tva', 'total_ttc', 'localtax1', 'localtax2', 'revenuestamp', 'datepointoftax', 'note_public', |
||
| 675 | ]) |
||
| 676 | ) { |
||
| 677 | continue; // Discard if not into a dedicated list |
||
| 678 | } |
||
| 679 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 680 | if ($this->element == 'payment_donation') { |
||
| 681 | $paymentpart->donation->$key = $value; |
||
| 682 | } elseif ($this->element == 'payment_various') { |
||
| 683 | $paymentpart->various->$key = $value; |
||
| 684 | } else { |
||
| 685 | $paymentpart->invoice->$key = $value; |
||
| 686 | } |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | $paymentpartnumber++; // first payment will be 1 |
||
| 691 | $this->object_data->payment_part[$paymentpartnumber] = $paymentpart; |
||
| 692 | } |
||
| 693 | } |
||
| 694 | } elseif (!empty($object->amount)) { |
||
| 695 | $totalamount = $object->amount; |
||
| 696 | } |
||
| 697 | |||
| 698 | $this->object_data->amount = $totalamount; |
||
| 699 | |||
| 700 | if (!empty($object->newref)) { |
||
| 701 | $this->object_data->ref = $object->newref; |
||
| 702 | } |
||
| 703 | } elseif ($this->element == 'payment_salary') { |
||
| 704 | $this->object_data->amounts = [$object->amount]; |
||
| 705 | |||
| 706 | if (!empty($object->newref)) { |
||
| 707 | $this->object_data->ref = $object->newref; |
||
| 708 | } |
||
| 709 | } elseif ($this->element == 'subscription') { |
||
| 710 | foreach ($object as $key => $value) { |
||
| 711 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 712 | continue; // Discard some properties |
||
| 713 | } |
||
| 714 | if ( |
||
| 715 | !in_array($key, [ |
||
| 716 | 'id', 'datec', 'dateh', 'datef', 'fk_adherent', 'amount', 'import_key', 'statut', 'note', |
||
| 717 | ]) |
||
| 718 | ) { |
||
| 719 | continue; // Discard if not into a dedicated list |
||
| 720 | } |
||
| 721 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 722 | $this->object_data->$key = $value; |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | if (!empty($object->newref)) { |
||
| 727 | $this->object_data->ref = $object->newref; |
||
| 728 | } |
||
| 729 | } elseif ($this->element == 'stockmouvement') { |
||
| 730 | foreach ($object as $key => $value) { |
||
| 731 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 732 | continue; // Discard some properties |
||
| 733 | } |
||
| 734 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 735 | $this->object_data->$key = $value; |
||
| 736 | } |
||
| 737 | } |
||
| 738 | } else { |
||
| 739 | // Generic case |
||
| 740 | foreach ($object as $key => $value) { |
||
| 741 | if (in_array($key, $arrayoffieldstoexclude)) { |
||
| 742 | continue; // Discard some properties |
||
| 743 | } |
||
| 744 | if (!is_object($value) && !is_null($value) && $value !== '') { |
||
| 745 | $this->object_data->$key = $value; |
||
| 746 | } |
||
| 747 | } |
||
| 748 | |||
| 749 | if (!empty($object->newref)) { |
||
| 750 | $this->object_data->ref = $object->newref; |
||
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | // A trick to be sure all the object_data is an associative array |
||
| 755 | // json_encode and json_decode are not able to manage mixed object (with array/object, only full arrays or full objects) |
||
| 756 | $this->object_data = json_decode(json_encode($this->object_data, JSON_FORCE_OBJECT), false); |
||
| 757 | |||
| 758 | return 1; |
||
| 759 | } |
||
| 1265 |