Total Complexity | 102 |
Total Lines | 671 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PaymentLoan 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 PaymentLoan, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class PaymentLoan extends CommonObject |
||
37 | { |
||
38 | /** |
||
39 | * @var string ID to identify managed object |
||
40 | */ |
||
41 | public $element = 'payment_loan'; |
||
42 | |||
43 | /** |
||
44 | * @var string Name of table without prefix where object is stored |
||
45 | */ |
||
46 | public $table_element = 'payment_loan'; |
||
47 | |||
48 | /** |
||
49 | * @var string String with name of icon for PaymentLoan |
||
50 | */ |
||
51 | public $picto = 'money-bill-alt'; |
||
52 | |||
53 | /** |
||
54 | * @var int Loan ID |
||
55 | */ |
||
56 | public $fk_loan; |
||
57 | |||
58 | /** |
||
59 | * @var string Create date |
||
60 | */ |
||
61 | public $datec = ''; |
||
62 | |||
63 | /** |
||
64 | * @var string Payment date |
||
65 | */ |
||
66 | public $datep = ''; |
||
67 | |||
68 | /** |
||
69 | * @var array<float|int> Array of amounts |
||
70 | */ |
||
71 | public $amounts = array(); |
||
72 | |||
73 | /** |
||
74 | * @var float|int Total amount of payment |
||
75 | */ |
||
76 | public $amount_capital; |
||
77 | |||
78 | /** |
||
79 | * @var float|int |
||
80 | */ |
||
81 | public $amount_insurance; |
||
82 | |||
83 | /** |
||
84 | * @var float|int |
||
85 | */ |
||
86 | public $amount_interest; |
||
87 | |||
88 | /** |
||
89 | * @var int Payment mode ID |
||
90 | */ |
||
91 | public $fk_typepayment; |
||
92 | |||
93 | /** |
||
94 | * @var string Payment reference |
||
95 | * (Cheque or bank transfer reference. Can be "ABC123") |
||
96 | */ |
||
97 | public $num_payment; |
||
98 | |||
99 | /** |
||
100 | * @var int Bank ID |
||
101 | */ |
||
102 | public $fk_bank; |
||
103 | |||
104 | /** |
||
105 | * @var int User ID |
||
106 | */ |
||
107 | public $fk_user_creat; |
||
108 | |||
109 | /** |
||
110 | * @var int user ID |
||
111 | */ |
||
112 | public $fk_user_modif; |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | public $type_code; |
||
118 | /** |
||
119 | * @var string |
||
120 | */ |
||
121 | public $type_label; |
||
122 | public $chid; |
||
123 | /** |
||
124 | * @var string |
||
125 | */ |
||
126 | public $label; |
||
127 | |||
128 | /** |
||
129 | * @var int |
||
130 | */ |
||
131 | public $paymenttype; |
||
132 | |||
133 | /** |
||
134 | * @var int |
||
135 | */ |
||
136 | public $bank_account; |
||
137 | public $bank_line; |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Constructor |
||
142 | * |
||
143 | * @param DoliDB $db Database handler |
||
|
|||
144 | */ |
||
145 | public function __construct($db) |
||
146 | { |
||
147 | $this->db = $db; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Create payment of loan into database. |
||
152 | * Use this->amounts to have list of lines for the payment |
||
153 | * |
||
154 | * @param User $user User making payment |
||
155 | * @return int Return integer <0 if KO, id of payment if OK |
||
156 | */ |
||
157 | public function create($user) |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Load object in memory from database |
||
251 | * |
||
252 | * @param int $id Id object |
||
253 | * @return int Return integer <0 if KO, >0 if OK |
||
254 | */ |
||
255 | public function fetch($id) |
||
317 | } |
||
318 | } |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Update database |
||
323 | * |
||
324 | * @param User $user User that modify |
||
325 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
326 | * @return int Return integer <0 if KO, >0 if OK |
||
327 | */ |
||
328 | public function update($user = null, $notrigger = 0) |
||
329 | { |
||
330 | global $conf, $langs; |
||
331 | $error = 0; |
||
332 | |||
333 | // Clean parameters |
||
334 | if (isset($this->fk_loan)) { |
||
335 | $this->fk_loan = (int) $this->fk_loan; |
||
336 | } |
||
337 | if (isset($this->amount_capital)) { |
||
338 | $this->amount_capital = (float) $this->amount_capital; |
||
339 | } |
||
340 | if (isset($this->amount_insurance)) { |
||
341 | $this->amount_insurance = (float) $this->amount_insurance; |
||
342 | } |
||
343 | if (isset($this->amount_interest)) { |
||
344 | $this->amount_interest = (float) $this->amount_interest; |
||
345 | } |
||
346 | if (isset($this->fk_typepayment)) { |
||
347 | $this->fk_typepayment = (int) $this->fk_typepayment; |
||
348 | } |
||
349 | if (isset($this->num_payment)) { |
||
350 | $this->num_payment = trim($this->num_payment); |
||
351 | } |
||
352 | if (isset($this->note_private)) { |
||
353 | $this->note = trim($this->note_private); |
||
354 | } |
||
355 | if (isset($this->note_public)) { |
||
356 | $this->note = trim($this->note_public); |
||
357 | } |
||
358 | if (isset($this->fk_bank)) { |
||
359 | $this->fk_bank = (int) $this->fk_bank; |
||
360 | } |
||
361 | if (isset($this->fk_user_creat)) { |
||
362 | $this->fk_user_creat = (int) $this->fk_user_creat; |
||
363 | } |
||
364 | if (isset($this->fk_user_modif)) { |
||
365 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
366 | } |
||
367 | |||
368 | // Check parameters |
||
369 | |||
370 | // Update request |
||
371 | $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_loan SET"; |
||
372 | $sql .= " fk_loan=" . (isset($this->fk_loan) ? $this->fk_loan : "null") . ","; |
||
373 | $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ","; |
||
374 | $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ","; |
||
375 | $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ","; |
||
376 | $sql .= " amount_capital=" . (isset($this->amount_capital) ? $this->amount_capital : "null") . ","; |
||
377 | $sql .= " amount_insurance=" . (isset($this->amount_insurance) ? $this->amount_insurance : "null") . ","; |
||
378 | $sql .= " amount_interest=" . (isset($this->amount_interest) ? $this->amount_interest : "null") . ","; |
||
379 | $sql .= " fk_typepayment=" . (isset($this->fk_typepayment) ? $this->fk_typepayment : "null") . ","; |
||
380 | $sql .= " num_payment=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ","; |
||
381 | $sql .= " note_private=" . (isset($this->note_private) ? "'" . $this->db->escape($this->note_private) . "'" : "null") . ","; |
||
382 | $sql .= " note_public=" . (isset($this->note_public) ? "'" . $this->db->escape($this->note_public) . "'" : "null") . ","; |
||
383 | $sql .= " fk_bank=" . (isset($this->fk_bank) ? ((int) $this->fk_bank) : "null") . ","; |
||
384 | $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null") . ","; |
||
385 | $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null"); |
||
386 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
387 | |||
388 | $this->db->begin(); |
||
389 | |||
390 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
391 | $resql = $this->db->query($sql); |
||
392 | if (!$resql) { |
||
393 | $error++; |
||
394 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
395 | } |
||
396 | |||
397 | // Commit or rollback |
||
398 | if ($error) { |
||
399 | foreach ($this->errors as $errmsg) { |
||
400 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
401 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
402 | } |
||
403 | $this->db->rollback(); |
||
404 | return -1 * $error; |
||
405 | } else { |
||
406 | $this->db->commit(); |
||
407 | return 1; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | |||
412 | /** |
||
413 | * Delete object in database |
||
414 | * |
||
415 | * @param User $user User that delete |
||
416 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
417 | * @return int Return integer <0 if KO, >0 if OK |
||
418 | */ |
||
419 | public function delete($user, $notrigger = 0) |
||
420 | { |
||
421 | global $conf, $langs; |
||
422 | $error = 0; |
||
423 | |||
424 | $this->db->begin(); |
||
425 | |||
426 | if (!$error) { |
||
427 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_url"; |
||
428 | $sql .= " WHERE type='payment_loan' AND url_id=" . ((int) $this->id); |
||
429 | |||
430 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
431 | $resql = $this->db->query($sql); |
||
432 | if (!$resql) { |
||
433 | $error++; |
||
434 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
435 | } |
||
436 | } |
||
437 | |||
438 | if (!$error) { |
||
439 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_loan"; |
||
440 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
441 | |||
442 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
443 | $resql = $this->db->query($sql); |
||
444 | if (!$resql) { |
||
445 | $error++; |
||
446 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
447 | } |
||
448 | } |
||
449 | |||
450 | // Set loan unpaid if loan has no other payment |
||
451 | if (!$error) { |
||
452 | require_once constant('DOL_DOCUMENT_ROOT') . '/loan/class/loan.class.php'; |
||
453 | $loan = new Loan($this->db); |
||
454 | $loan->fetch($this->fk_loan); |
||
455 | $sum_payment = $loan->getSumPayment(); |
||
456 | if ($sum_payment == 0) { |
||
457 | dol_syslog(get_class($this) . "::delete : set loan to unpaid", LOG_DEBUG); |
||
458 | if ($loan->setUnpaid($user) < 1) { |
||
459 | $error++; |
||
460 | dol_print_error($this->db); |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | |||
465 | //if (! $error) |
||
466 | //{ |
||
467 | // if (! $notrigger) |
||
468 | // { |
||
469 | // Uncomment this and change MYOBJECT to your own tag if you |
||
470 | // want this action call a trigger. |
||
471 | |||
472 | //// Call triggers |
||
473 | //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; |
||
474 | //$interface=new Interfaces($this->db); |
||
475 | //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf); |
||
476 | //if ($result < 0) { $error++; $this->errors=$interface->errors; } |
||
477 | //// End call triggers |
||
478 | // } |
||
479 | //} |
||
480 | |||
481 | // Commit or rollback |
||
482 | if ($error) { |
||
483 | foreach ($this->errors as $errmsg) { |
||
484 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
485 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
486 | } |
||
487 | $this->db->rollback(); |
||
488 | return -1 * $error; |
||
489 | } else { |
||
490 | $this->db->commit(); |
||
491 | return 1; |
||
492 | } |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Return the label of the status |
||
497 | * |
||
498 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
499 | * @return string Label of status |
||
500 | */ |
||
501 | public function getLibStatut($mode = 0) |
||
502 | { |
||
503 | return $this->LibStatut($this->statut, $mode); |
||
504 | } |
||
505 | |||
506 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
507 | /** |
||
508 | * Renvoi le libelle d'un statut donne |
||
509 | * |
||
510 | * @param int $status Statut |
||
511 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
512 | * @return string Libelle du statut |
||
513 | */ |
||
514 | public function LibStatut($status, $mode = 0) |
||
515 | { |
||
516 | // phpcs:enable |
||
517 | return ''; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Add record into bank for payment with links between this bank record and invoices of payment. |
||
522 | * All payment properties must have been set first like after a call to create(). |
||
523 | * |
||
524 | * @param User $user Object of user making payment |
||
525 | * @param int $fk_loan Id of fk_loan to do link with this payment |
||
526 | * @param string $mode 'payment_loan' |
||
527 | * @param string $label Label to use in bank record |
||
528 | * @param int $accountid Id of bank account to do link with |
||
529 | * @param string $emetteur_nom Name of transmitter |
||
530 | * @param string $emetteur_banque Name of bank |
||
531 | * @return int Return integer <0 if KO, >0 if OK |
||
532 | */ |
||
533 | public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque) |
||
621 | } |
||
622 | } |
||
623 | |||
624 | |||
625 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
626 | /** |
||
627 | * Update link between loan's payment and the line generate in llx_bank |
||
628 | * |
||
629 | * @param int $id_bank Id if bank |
||
630 | * @return int >0 if OK, <=0 if KO |
||
631 | */ |
||
632 | public function update_fk_bank($id_bank) |
||
645 | } |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * Return clicable name (with eventually a picto) |
||
650 | * |
||
651 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=No picto |
||
652 | * @param int $maxlen Max length label |
||
653 | * @param int $notooltip 1=Disable tooltip |
||
654 | * @param string $moretitle Add more text to title tooltip |
||
655 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
656 | * @return string String with URL |
||
657 | */ |
||
658 | public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1) |
||
707 | } |
||
708 | } |
||
709 |