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