| Total Complexity | 88 |
| Total Lines | 702 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PaymentSocialContribution often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PaymentSocialContribution, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class PaymentSocialContribution extends CommonObject |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string ID to identify managed object |
||
| 36 | */ |
||
| 37 | public $element='paiementcharge'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string Name of table without prefix where object is stored |
||
| 41 | */ |
||
| 42 | public $table_element='paiementcharge'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 46 | */ |
||
| 47 | public $picto = 'payment'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int ID |
||
| 51 | */ |
||
| 52 | public $fk_charge; |
||
| 53 | |||
| 54 | public $datec=''; |
||
| 55 | public $tms=''; |
||
| 56 | public $datep=''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @deprecated |
||
| 60 | * @see amount |
||
| 61 | */ |
||
| 62 | public $total; |
||
| 63 | |||
| 64 | public $amount; // Total amount of payment |
||
| 65 | public $amounts=array(); // Array of amounts |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var int ID |
||
| 69 | */ |
||
| 70 | public $fk_typepaiement; |
||
| 71 | |||
| 72 | public $num_paiement; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int ID |
||
| 76 | */ |
||
| 77 | public $fk_bank; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int ID |
||
| 81 | */ |
||
| 82 | public $fk_user_creat; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var int ID |
||
| 86 | */ |
||
| 87 | public $fk_user_modif; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Constructor |
||
| 91 | * |
||
| 92 | * @param DoliDB $db Database handler |
||
| 93 | */ |
||
| 94 | public function __construct($db) |
||
| 95 | { |
||
| 96 | $this->db = $db; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create payment of social contribution into database. |
||
| 101 | * Use this->amounts to have list of lines for the payment |
||
| 102 | * |
||
| 103 | * @param User $user User making payment |
||
| 104 | * @param int $closepaidcontrib 1=Also close payed contributions to paid, 0=Do nothing more |
||
| 105 | * @return int <0 if KO, id of payment if OK |
||
| 106 | */ |
||
| 107 | public function create($user, $closepaidcontrib = 0) |
||
| 108 | { |
||
| 109 | global $conf, $langs; |
||
| 110 | |||
| 111 | $error=0; |
||
| 112 | |||
| 113 | $now=dol_now(); |
||
| 114 | |||
| 115 | dol_syslog(get_class($this)."::create", LOG_DEBUG); |
||
| 116 | |||
| 117 | // Validate parametres |
||
| 118 | if (! $this->datepaye) |
||
|
|
|||
| 119 | { |
||
| 120 | $this->error='ErrorBadValueForParameterCreatePaymentSocialContrib'; |
||
| 121 | return -1; |
||
| 122 | } |
||
| 123 | |||
| 124 | // Clean parameters |
||
| 125 | if (isset($this->fk_charge)) $this->fk_charge= (int) $this->fk_charge; |
||
| 126 | if (isset($this->amount)) $this->amount=trim($this->amount); |
||
| 127 | if (isset($this->fk_typepaiement)) $this->fk_typepaiement= (int) $this->fk_typepaiement; |
||
| 128 | if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement); |
||
| 129 | if (isset($this->note)) $this->note=trim($this->note); |
||
| 130 | if (isset($this->fk_bank)) $this->fk_bank= (int) $this->fk_bank; |
||
| 131 | if (isset($this->fk_user_creat)) $this->fk_user_creat= (int) $this->fk_user_creat; |
||
| 132 | if (isset($this->fk_user_modif)) $this->fk_user_modif= (int) $this->fk_user_modif; |
||
| 133 | |||
| 134 | $totalamount = 0; |
||
| 135 | foreach ($this->amounts as $key => $value) // How payment is dispatch |
||
| 136 | { |
||
| 137 | $newvalue = price2num($value, 'MT'); |
||
| 138 | $this->amounts[$key] = $newvalue; |
||
| 139 | $totalamount += $newvalue; |
||
| 140 | } |
||
| 141 | $totalamount = price2num($totalamount); |
||
| 142 | |||
| 143 | // Check parameters |
||
| 144 | if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null |
||
| 145 | |||
| 146 | |||
| 147 | $this->db->begin(); |
||
| 148 | |||
| 149 | if ($totalamount != 0) |
||
| 150 | { |
||
| 151 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,"; |
||
| 152 | $sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)"; |
||
| 153 | $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',"; |
||
| 154 | $sql.= " '".$this->db->idate($this->datepaye)."',"; |
||
| 155 | $sql.= " ".$totalamount.","; |
||
| 156 | $sql.= " ".$this->paiementtype.", '".$this->db->escape($this->num_paiement)."', '".$this->db->escape($this->note)."', ".$user->id.","; |
||
| 157 | $sql.= " 0)"; |
||
| 158 | |||
| 159 | $resql=$this->db->query($sql); |
||
| 160 | if ($resql) |
||
| 161 | { |
||
| 162 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge"); |
||
| 163 | |||
| 164 | // Insere tableau des montants / factures |
||
| 165 | foreach ($this->amounts as $key => $amount) |
||
| 166 | { |
||
| 167 | $contribid = $key; |
||
| 168 | if (is_numeric($amount) && $amount <> 0) |
||
| 169 | { |
||
| 170 | $amount = price2num($amount); |
||
| 171 | |||
| 172 | // If we want to closed payed invoices |
||
| 173 | if ($closepaidcontrib) |
||
| 174 | { |
||
| 175 | |||
| 176 | $contrib=new ChargeSociales($this->db); |
||
| 177 | $contrib->fetch($contribid); |
||
| 178 | $paiement = $contrib->getSommePaiement(); |
||
| 179 | //$creditnotes=$contrib->getSumCreditNotesUsed(); |
||
| 180 | $creditnotes=0; |
||
| 181 | //$deposits=$contrib->getSumDepositsUsed(); |
||
| 182 | $deposits=0; |
||
| 183 | $alreadypayed=price2num($paiement + $creditnotes + $deposits, 'MT'); |
||
| 184 | $remaintopay=price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT'); |
||
| 185 | if ($remaintopay == 0) |
||
| 186 | { |
||
| 187 | $result=$contrib->set_paid($user, '', ''); |
||
| 188 | } |
||
| 189 | else dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing."); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | else |
||
| 195 | { |
||
| 196 | $error++; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | $result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user); |
||
| 201 | if($result < 0) $error++; |
||
| 202 | |||
| 203 | if ($totalamount != 0 && ! $error) |
||
| 204 | { |
||
| 205 | $this->amount=$totalamount; |
||
| 206 | $this->total=$totalamount; // deprecated |
||
| 207 | $this->db->commit(); |
||
| 208 | return $this->id; |
||
| 209 | } |
||
| 210 | else |
||
| 211 | { |
||
| 212 | $this->error=$this->db->error(); |
||
| 213 | $this->db->rollback(); |
||
| 214 | return -1; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Load object in memory from database |
||
| 220 | * |
||
| 221 | * @param int $id Id object |
||
| 222 | * @return int <0 if KO, >0 if OK |
||
| 223 | */ |
||
| 224 | public function fetch($id) |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Update database |
||
| 290 | * |
||
| 291 | * @param User $user User that modify |
||
| 292 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 293 | * @return int <0 if KO, >0 if OK |
||
| 294 | */ |
||
| 295 | public function update($user = null, $notrigger = 0) |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Delete object in database |
||
| 377 | * |
||
| 378 | * @param User $user User that delete |
||
| 379 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 380 | * @return int <0 if KO, >0 if OK |
||
| 381 | */ |
||
| 382 | public function delete($user, $notrigger = 0) |
||
| 383 | { |
||
| 384 | global $conf, $langs; |
||
| 385 | $error=0; |
||
| 386 | |||
| 387 | dol_syslog(get_class($this)."::delete"); |
||
| 388 | |||
| 389 | $this->db->begin(); |
||
| 390 | |||
| 391 | if ($this->bank_line > 0) |
||
| 392 | { |
||
| 393 | $accline = new AccountLine($this->db); |
||
| 394 | $accline->fetch($this->bank_line); |
||
| 395 | $result = $accline->delete(); |
||
| 396 | if($result < 0) { |
||
| 397 | $this->errors[] = $accline->error; |
||
| 398 | $error++; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | if (! $error) |
||
| 403 | { |
||
| 404 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge"; |
||
| 405 | $sql.= " WHERE rowid=".$this->id; |
||
| 406 | |||
| 407 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
| 408 | $resql = $this->db->query($sql); |
||
| 409 | if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } |
||
| 410 | } |
||
| 411 | |||
| 412 | //if (! $error) |
||
| 413 | //{ |
||
| 414 | // if (! $notrigger) |
||
| 415 | // { |
||
| 416 | // Uncomment this and change MYOBJECT to your own tag if you |
||
| 417 | // want this action call a trigger. |
||
| 418 | |||
| 419 | //// Call triggers |
||
| 420 | //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; |
||
| 421 | //$interface=new Interfaces($this->db); |
||
| 422 | //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf); |
||
| 423 | //if ($result < 0) { $error++; $this->errors=$interface->errors; } |
||
| 424 | //// End call triggers |
||
| 425 | // } |
||
| 426 | //} |
||
| 427 | |||
| 428 | // Commit or rollback |
||
| 429 | if ($error) |
||
| 430 | { |
||
| 431 | foreach($this->errors as $errmsg) |
||
| 432 | { |
||
| 433 | dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); |
||
| 434 | $this->error.=($this->error?', '.$errmsg:$errmsg); |
||
| 435 | } |
||
| 436 | $this->db->rollback(); |
||
| 437 | return -1*$error; |
||
| 438 | } |
||
| 439 | else |
||
| 440 | { |
||
| 441 | $this->db->commit(); |
||
| 442 | return 1; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * Load an object from its id and create a new one in database |
||
| 450 | * |
||
| 451 | * @param User $user User making the clone |
||
| 452 | * @param int $fromid Id of object to clone |
||
| 453 | * @return int New id of clone |
||
| 454 | */ |
||
| 455 | public function createFromClone(User $user, $fromid) |
||
| 456 | { |
||
| 457 | $error=0; |
||
| 458 | |||
| 459 | $object=new PaymentSocialContribution($this->db); |
||
| 460 | |||
| 461 | $this->db->begin(); |
||
| 462 | |||
| 463 | // Load source object |
||
| 464 | $object->fetch($fromid); |
||
| 465 | $object->id=0; |
||
| 466 | $object->statut=0; |
||
| 467 | |||
| 468 | // Clear fields |
||
| 469 | // ... |
||
| 470 | |||
| 471 | // Create clone |
||
| 472 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 473 | $result=$object->create($user); |
||
| 474 | |||
| 475 | // Other options |
||
| 476 | if ($result < 0) |
||
| 477 | { |
||
| 478 | $this->error=$object->error; |
||
| 479 | $error++; |
||
| 480 | } |
||
| 481 | |||
| 482 | unset($object->context['createfromclone']); |
||
| 483 | |||
| 484 | // End |
||
| 485 | if (! $error) |
||
| 486 | { |
||
| 487 | $this->db->commit(); |
||
| 488 | return $object->id; |
||
| 489 | } |
||
| 490 | else |
||
| 491 | { |
||
| 492 | $this->db->rollback(); |
||
| 493 | return -1; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Initialise an instance with random values. |
||
| 500 | * Used to build previews or test instances. |
||
| 501 | * id must be 0 if object instance is a specimen. |
||
| 502 | * |
||
| 503 | * @return void |
||
| 504 | */ |
||
| 505 | public function initAsSpecimen() |
||
| 506 | { |
||
| 507 | $this->id=0; |
||
| 508 | |||
| 509 | $this->fk_charge=''; |
||
|
1 ignored issue
–
show
|
|||
| 510 | $this->datec=''; |
||
| 511 | $this->tms=''; |
||
| 512 | $this->datep=''; |
||
| 513 | $this->amount=''; |
||
| 514 | $this->fk_typepaiement=''; |
||
|
1 ignored issue
–
show
|
|||
| 515 | $this->num_paiement=''; |
||
| 516 | $this->note=''; |
||
|
1 ignored issue
–
show
|
|||
| 517 | $this->fk_bank=''; |
||
|
1 ignored issue
–
show
|
|||
| 518 | $this->fk_user_creat=''; |
||
|
1 ignored issue
–
show
|
|||
| 519 | $this->fk_user_modif=''; |
||
|
1 ignored issue
–
show
|
|||
| 520 | } |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Add record into bank for payment with links between this bank record and invoices of payment. |
||
| 525 | * All payment properties must have been set first like after a call to create(). |
||
| 526 | * |
||
| 527 | * @param User $user Object of user making payment |
||
| 528 | * @param string $mode 'payment_sc' |
||
| 529 | * @param string $label Label to use in bank record |
||
| 530 | * @param int $accountid Id of bank account to do link with |
||
| 531 | * @param string $emetteur_nom Name of transmitter |
||
| 532 | * @param string $emetteur_banque Name of bank |
||
| 533 | * @return int <0 if KO, >0 if OK |
||
| 534 | */ |
||
| 535 | public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque) |
||
| 536 | { |
||
| 537 | global $conf; |
||
| 538 | |||
| 539 | $error=0; |
||
| 540 | |||
| 541 | if (! empty($conf->banque->enabled)) |
||
| 542 | { |
||
| 543 | include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; |
||
| 544 | |||
| 545 | $acc = new Account($this->db); |
||
| 546 | $acc->fetch($accountid); |
||
| 547 | |||
| 548 | $total=$this->total; |
||
|
1 ignored issue
–
show
|
|||
| 549 | if ($mode == 'payment_sc') $total=-$total; |
||
| 550 | |||
| 551 | // Insert payment into llx_bank |
||
| 552 | $bank_line_id = $acc->addline( |
||
| 553 | $this->datepaye, |
||
| 554 | $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example") |
||
| 555 | $label, |
||
| 556 | $total, |
||
| 557 | $this->num_paiement, |
||
| 558 | '', |
||
| 559 | $user, |
||
| 560 | $emetteur_nom, |
||
| 561 | $emetteur_banque |
||
| 562 | ); |
||
| 563 | |||
| 564 | // Mise a jour fk_bank dans llx_paiement. |
||
| 565 | // On connait ainsi le paiement qui a genere l'ecriture bancaire |
||
| 566 | if ($bank_line_id > 0) |
||
| 567 | { |
||
| 568 | $result=$this->update_fk_bank($bank_line_id); |
||
| 569 | if ($result <= 0) |
||
| 570 | { |
||
| 571 | $error++; |
||
| 572 | dol_print_error($this->db); |
||
| 573 | } |
||
| 574 | |||
| 575 | // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction |
||
| 576 | $url=''; |
||
| 577 | if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/card.php?id='; |
||
| 578 | if ($url) |
||
| 579 | { |
||
| 580 | $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode); |
||
| 581 | if ($result <= 0) |
||
| 582 | { |
||
| 583 | $error++; |
||
| 584 | dol_print_error($this->db); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment) |
||
| 589 | $linkaddedforthirdparty=array(); |
||
| 590 | foreach ($this->amounts as $key => $value) |
||
| 591 | { |
||
| 592 | if ($mode == 'payment_sc') |
||
| 593 | { |
||
| 594 | $socialcontrib = new ChargeSociales($this->db); |
||
| 595 | $socialcontrib->fetch($key); |
||
| 596 | $result=$acc->add_url_line($bank_line_id, $socialcontrib->id, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''), 'sc'); |
||
| 597 | if ($result <= 0) dol_print_error($this->db); |
||
| 598 | } |
||
| 599 | } |
||
| 600 | } |
||
| 601 | else |
||
| 602 | { |
||
| 603 | $this->error=$acc->error; |
||
| 604 | $error++; |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | if (! $error) |
||
| 609 | { |
||
| 610 | return 1; |
||
| 611 | } |
||
| 612 | else |
||
| 613 | { |
||
| 614 | return -1; |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | |||
| 619 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 620 | /** |
||
| 621 | * Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee |
||
| 622 | * |
||
| 623 | * @param int $id_bank Id if bank |
||
| 624 | * @return int >0 if OK, <=0 if KO |
||
| 625 | */ |
||
| 626 | public function update_fk_bank($id_bank) |
||
| 627 | { |
||
| 628 | // phpcs:enable |
||
| 629 | $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id; |
||
| 630 | |||
| 631 | dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG); |
||
| 632 | $result = $this->db->query($sql); |
||
| 633 | if ($result) |
||
| 634 | { |
||
| 635 | return 1; |
||
| 636 | } |
||
| 637 | else |
||
| 638 | { |
||
| 639 | $this->error=$this->db->error(); |
||
| 640 | return 0; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) |
||
| 647 | * |
||
| 648 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 649 | * @return string Libelle |
||
| 650 | */ |
||
| 651 | public function getLibStatut($mode = 0) |
||
| 652 | { |
||
| 653 | return $this->LibStatut($this->statut, $mode); |
||
| 654 | } |
||
| 655 | |||
| 656 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 657 | /** |
||
| 658 | * Renvoi le libelle d'un statut donne |
||
| 659 | * |
||
| 660 | * @param int $status Statut |
||
| 661 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 662 | * @return string Libelle du statut |
||
| 663 | */ |
||
| 664 | public function LibStatut($status, $mode = 0) |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Return clicable name (with picto eventually) |
||
| 710 | * |
||
| 711 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 712 | * @param int $maxlen Longueur max libelle |
||
| 713 | * @return string Chaine avec URL |
||
| 714 | */ |
||
| 715 | public function getNomUrl($withpicto = 0, $maxlen = 0) |
||
| 734 | } |
||
| 735 | } |
||
| 736 |