| Total Complexity | 97 |
| Total Lines | 651 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PaymentVarious 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 PaymentVarious, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class PaymentVarious extends CommonObject |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string ID to identify managed object |
||
| 36 | */ |
||
| 37 | public $element='variouspayment'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string Name of table without prefix where object is stored |
||
| 41 | */ |
||
| 42 | public $table_element='payment_various'; |
||
| 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 $id; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string Ref |
||
| 56 | */ |
||
| 57 | public $ref; |
||
| 58 | |||
| 59 | public $tms; |
||
| 60 | public $datep; |
||
| 61 | public $datev; |
||
| 62 | public $sens; |
||
| 63 | public $amount; |
||
| 64 | public $type_payment; |
||
| 65 | public $num_payment; |
||
| 66 | public $category_transaction; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string various payments label |
||
| 70 | */ |
||
| 71 | public $label; |
||
| 72 | |||
| 73 | public $accountancy_code; |
||
| 74 | |||
| 75 | public $subledger_account; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var int ID |
||
| 79 | */ |
||
| 80 | public $fk_project; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var int ID |
||
| 84 | */ |
||
| 85 | public $fk_bank; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var int ID |
||
| 89 | */ |
||
| 90 | public $fk_user_author; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int ID |
||
| 94 | */ |
||
| 95 | public $fk_user_modif; |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Constructor |
||
| 100 | * |
||
| 101 | * @param DoliDB $db Database handler |
||
| 102 | */ |
||
| 103 | public function __construct($db) |
||
| 104 | { |
||
| 105 | $this->db = $db; |
||
| 106 | $this->element = 'payment_various'; |
||
| 107 | $this->table_element = 'payment_various'; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Update database |
||
| 112 | * |
||
| 113 | * @param User $user User that modify |
||
| 114 | * @param int $notrigger 0=no, 1=yes (no update trigger) |
||
| 115 | * @return int <0 if KO, >0 if OK |
||
| 116 | */ |
||
| 117 | public function update($user = null, $notrigger = 0) |
||
| 118 | { |
||
| 119 | global $conf, $langs; |
||
| 120 | |||
| 121 | $error=0; |
||
| 122 | |||
| 123 | // Clean parameters |
||
| 124 | $this->amount=trim($this->amount); |
||
| 125 | $this->label=trim($this->label); |
||
| 126 | $this->note=trim($this->note); |
||
|
1 ignored issue
–
show
|
|||
| 127 | $this->fk_bank = (int) $this->fk_bank; |
||
| 128 | $this->fk_user_author = (int) $this->fk_user_author; |
||
| 129 | $this->fk_user_modif = (int) $this->fk_user_modif; |
||
| 130 | |||
| 131 | $this->db->begin(); |
||
| 132 | |||
| 133 | // Update request |
||
| 134 | $sql = "UPDATE ".MAIN_DB_PREFIX."payment_various SET"; |
||
| 135 | if ($this->tms) $sql.= " tms='".$this->db->idate($this->tms)."',"; |
||
| 136 | $sql.= " datep='".$this->db->idate($this->datep)."',"; |
||
| 137 | $sql.= " datev='".$this->db->idate($this->datev)."',"; |
||
| 138 | $sql.= " sens=".$this->sens.","; |
||
| 139 | $sql.= " amount=".price2num($this->amount).","; |
||
| 140 | $sql.= " fk_typepayment=".$this->fk_typepayment."',"; |
||
| 141 | $sql.= " num_payment='".$this->db->escape($this->num_payment)."',"; |
||
| 142 | $sql.= " label='".$this->db->escape($this->label)."',"; |
||
| 143 | $sql.= " note='".$this->db->escape($this->note)."',"; |
||
|
1 ignored issue
–
show
|
|||
| 144 | $sql.= " accountancy_code='".$this->db->escape($this->accountancy_code)."',"; |
||
| 145 | $sql.= " subledger_account='".$this->db->escape($this->subledger_account)."',"; |
||
| 146 | $sql.= " fk_projet='".$this->db->escape($this->fk_project)."',"; |
||
| 147 | $sql.= " fk_bank=".($this->fk_bank > 0 ? $this->fk_bank:"null").","; |
||
| 148 | $sql.= " fk_user_author=".$this->fk_user_author.","; |
||
| 149 | $sql.= " fk_user_modif=".$this->fk_user_modif; |
||
| 150 | $sql.= " WHERE rowid=".$this->id; |
||
| 151 | |||
| 152 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 153 | $resql = $this->db->query($sql); |
||
| 154 | if (! $resql) |
||
| 155 | { |
||
| 156 | $this->error="Error ".$this->db->lasterror(); |
||
| 157 | return -1; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (! $notrigger) |
||
| 161 | { |
||
| 162 | // Call trigger |
||
| 163 | $result=$this->call_trigger('PAYMENT_VARIOUS_MODIFY', $user); |
||
| 164 | if ($result < 0) $error++; |
||
| 165 | // End call triggers |
||
| 166 | } |
||
| 167 | |||
| 168 | if (! $error) |
||
| 169 | { |
||
| 170 | $this->db->commit(); |
||
| 171 | return 1; |
||
| 172 | } |
||
| 173 | else |
||
| 174 | { |
||
| 175 | $this->db->rollback(); |
||
| 176 | return -1; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Load object in memory from database |
||
| 183 | * |
||
| 184 | * @param int $id id object |
||
| 185 | * @param User $user User that load |
||
| 186 | * @return int <0 if KO, >0 if OK |
||
| 187 | */ |
||
| 188 | public function fetch($id, $user = null) |
||
| 189 | { |
||
| 190 | global $langs; |
||
| 191 | $sql = "SELECT"; |
||
| 192 | $sql.= " v.rowid,"; |
||
| 193 | $sql.= " v.tms,"; |
||
| 194 | $sql.= " v.datep,"; |
||
| 195 | $sql.= " v.datev,"; |
||
| 196 | $sql.= " v.sens,"; |
||
| 197 | $sql.= " v.amount,"; |
||
| 198 | $sql.= " v.fk_typepayment,"; |
||
| 199 | $sql.= " v.num_payment,"; |
||
| 200 | $sql.= " v.label,"; |
||
| 201 | $sql.= " v.note,"; |
||
| 202 | $sql.= " v.accountancy_code,"; |
||
| 203 | $sql.= " v.subledger_account,"; |
||
| 204 | $sql.= " v.fk_projet as fk_project,"; |
||
| 205 | $sql.= " v.fk_bank,"; |
||
| 206 | $sql.= " v.fk_user_author,"; |
||
| 207 | $sql.= " v.fk_user_modif,"; |
||
| 208 | $sql.= " b.fk_account,"; |
||
| 209 | $sql.= " b.fk_type,"; |
||
| 210 | $sql.= " b.rappro"; |
||
| 211 | $sql.= " FROM ".MAIN_DB_PREFIX."payment_various as v"; |
||
| 212 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON v.fk_bank = b.rowid"; |
||
| 213 | $sql.= " WHERE v.rowid = ".$id; |
||
| 214 | |||
| 215 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 216 | $resql=$this->db->query($sql); |
||
| 217 | if ($resql) |
||
| 218 | { |
||
| 219 | if ($this->db->num_rows($resql)) |
||
| 220 | { |
||
| 221 | $obj = $this->db->fetch_object($resql); |
||
| 222 | |||
| 223 | $this->id = $obj->rowid; |
||
| 224 | $this->ref = $obj->rowid; |
||
| 225 | $this->tms = $this->db->jdate($obj->tms); |
||
| 226 | $this->datep = $this->db->jdate($obj->datep); |
||
| 227 | $this->datev = $this->db->jdate($obj->datev); |
||
| 228 | $this->sens = $obj->sens; |
||
| 229 | $this->amount = $obj->amount; |
||
| 230 | $this->type_payment = $obj->fk_typepayment; |
||
| 231 | $this->num_payment = $obj->num_payment; |
||
| 232 | $this->label = $obj->label; |
||
| 233 | $this->note = $obj->note; |
||
| 234 | $this->subledger_account = $obj->subledger_account; |
||
| 235 | $this->accountancy_code = $obj->accountancy_code; |
||
| 236 | $this->fk_project = $obj->fk_project; |
||
| 237 | $this->fk_bank = $obj->fk_bank; |
||
| 238 | $this->fk_user_author = $obj->fk_user_author; |
||
| 239 | $this->fk_user_modif = $obj->fk_user_modif; |
||
| 240 | $this->fk_account = $obj->fk_account; |
||
| 241 | $this->fk_type = $obj->fk_type; |
||
| 242 | $this->rappro = $obj->rappro; |
||
| 243 | } |
||
| 244 | $this->db->free($resql); |
||
| 245 | |||
| 246 | return 1; |
||
| 247 | } |
||
| 248 | else |
||
| 249 | { |
||
| 250 | $this->error="Error ".$this->db->lasterror(); |
||
| 251 | return -1; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Delete object in database |
||
| 258 | * |
||
| 259 | * @param User $user User that delete |
||
| 260 | * @return int <0 if KO, >0 if OK |
||
| 261 | */ |
||
| 262 | public function delete($user) |
||
| 263 | { |
||
| 264 | global $conf, $langs; |
||
| 265 | |||
| 266 | $error=0; |
||
| 267 | |||
| 268 | // Call trigger |
||
| 269 | $result=$this->call_trigger('PAYMENT_VARIOUS_DELETE', $user); |
||
| 270 | if ($result < 0) return -1; |
||
| 271 | // End call triggers |
||
| 272 | |||
| 273 | |||
| 274 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_various"; |
||
| 275 | $sql.= " WHERE rowid=".$this->id; |
||
| 276 | |||
| 277 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
| 278 | $resql = $this->db->query($sql); |
||
| 279 | if (! $resql) |
||
| 280 | { |
||
| 281 | $this->error="Error ".$this->db->lasterror(); |
||
| 282 | return -1; |
||
| 283 | } |
||
| 284 | |||
| 285 | return 1; |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Initialise an instance with random values. |
||
| 291 | * Used to build previews or test instances. |
||
| 292 | * id must be 0 if object instance is a specimen. |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | public function initAsSpecimen() |
||
| 297 | { |
||
| 298 | $this->id=0; |
||
| 299 | |||
| 300 | $this->tms=''; |
||
| 301 | $this->datep=''; |
||
| 302 | $this->datev=''; |
||
| 303 | $this->sens=''; |
||
| 304 | $this->amount=''; |
||
| 305 | $this->label=''; |
||
| 306 | $this->accountancy_code=''; |
||
| 307 | $this->subledger_account=''; |
||
| 308 | $this->note=''; |
||
|
1 ignored issue
–
show
|
|||
| 309 | $this->fk_bank=''; |
||
|
1 ignored issue
–
show
|
|||
| 310 | $this->fk_user_author=''; |
||
|
1 ignored issue
–
show
|
|||
| 311 | $this->fk_user_modif=''; |
||
|
1 ignored issue
–
show
|
|||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Create in database |
||
| 316 | * |
||
| 317 | * @param User $user User that create |
||
| 318 | * @return int <0 if KO, >0 if OK |
||
| 319 | */ |
||
| 320 | public function create($user) |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 487 | /** |
||
| 488 | * Update link between payment various and line generate into llx_bank |
||
| 489 | * |
||
| 490 | * @param int $id_bank Id bank account |
||
| 491 | * @return int <0 if KO, >0 if OK |
||
| 492 | */ |
||
| 493 | public function update_fk_bank($id_bank) |
||
| 494 | { |
||
| 495 | // phpcs:enable |
||
| 496 | $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_various SET fk_bank = '.$id_bank; |
||
| 497 | $sql.= ' WHERE rowid = '.$this->id; |
||
| 498 | $result = $this->db->query($sql); |
||
| 499 | if ($result) |
||
| 500 | { |
||
| 501 | return 1; |
||
| 502 | } |
||
| 503 | else |
||
| 504 | { |
||
| 505 | dol_print_error($this->db); |
||
| 506 | return -1; |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * Retourne le libelle du statut |
||
| 513 | * |
||
| 514 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto |
||
| 515 | * @return string Libelle |
||
| 516 | */ |
||
| 517 | public function getLibStatut($mode = 0) |
||
| 518 | { |
||
| 519 | return $this->LibStatut($this->statut, $mode); |
||
| 520 | } |
||
| 521 | |||
| 522 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 523 | /** |
||
| 524 | * Renvoi le libelle d'un statut donne |
||
| 525 | * |
||
| 526 | * @param int $statut Id status |
||
| 527 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto |
||
| 528 | * @return string Libelle |
||
| 529 | */ |
||
| 530 | public function LibStatut($statut, $mode = 0) |
||
| 531 | { |
||
| 532 | // phpcs:enable |
||
| 533 | global $langs; |
||
| 534 | |||
| 535 | if ($mode == 0) |
||
| 536 | { |
||
| 537 | return $langs->trans($this->statuts[$statut]); |
||
| 538 | } |
||
| 539 | elseif ($mode == 1) |
||
| 540 | { |
||
| 541 | return $langs->trans($this->statuts_short[$statut]); |
||
| 542 | } |
||
| 543 | elseif ($mode == 2) |
||
| 544 | { |
||
| 545 | if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0').' '.$langs->trans($this->statuts_short[$statut]); |
||
| 546 | elseif ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4').' '.$langs->trans($this->statuts_short[$statut]); |
||
| 547 | elseif ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6').' '.$langs->trans($this->statuts_short[$statut]); |
||
| 548 | } |
||
| 549 | elseif ($mode == 3) |
||
| 550 | { |
||
| 551 | if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0'); |
||
| 552 | elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4'); |
||
| 553 | elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6'); |
||
| 554 | } |
||
| 555 | elseif ($mode == 4) |
||
| 556 | { |
||
| 557 | if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0').' '.$langs->trans($this->statuts[$statut]); |
||
| 558 | elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4').' '.$langs->trans($this->statuts[$statut]); |
||
| 559 | elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6').' '.$langs->trans($this->statuts[$statut]); |
||
| 560 | } |
||
| 561 | elseif ($mode == 5) |
||
| 562 | { |
||
| 563 | if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]), 'statut0'); |
||
| 564 | elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]), 'statut4'); |
||
| 565 | elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]), 'statut6'); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * Send name clicable (with possibly the picto) |
||
| 572 | * |
||
| 573 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 574 | * @param string $option link option |
||
| 575 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 576 | * @param int $notooltip 1=Disable tooltip |
||
| 577 | * @return string String with URL |
||
| 578 | */ |
||
| 579 | public function getNomUrl($withpicto = 0, $option = '', $save_lastsearch_value = -1, $notooltip = 0) |
||
| 580 | { |
||
| 581 | global $db, $conf, $langs, $hookmanager; |
||
| 582 | global $langs; |
||
| 583 | |||
| 584 | if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips |
||
| 585 | |||
| 586 | $result=''; |
||
| 587 | |||
| 588 | $label='<u>'.$langs->trans("ShowVariousPayment").'</u>'; |
||
| 589 | $label.= '<br>'; |
||
| 590 | $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
| 591 | |||
| 592 | $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$this->id; |
||
| 593 | |||
| 594 | if ($option != 'nolink') |
||
| 595 | { |
||
| 596 | // Add param to save lastsearch_values or not |
||
| 597 | $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
||
| 598 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; |
||
| 599 | if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; |
||
| 600 | } |
||
| 601 | |||
| 602 | $linkclose=''; |
||
| 603 | if (empty($notooltip)) |
||
| 604 | { |
||
| 605 | if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
| 606 | { |
||
| 607 | $label=$langs->trans("ShowMyObject"); |
||
| 608 | $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 609 | } |
||
| 610 | $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 611 | $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"'; |
||
| 612 | |||
| 613 | /* |
||
| 614 | $hookmanager->initHooks(array('myobjectdao')); |
||
| 615 | $parameters=array('id'=>$this->id); |
||
| 616 | $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
| 617 | if ($reshook > 0) $linkclose = $hookmanager->resPrint; |
||
| 618 | */ |
||
| 619 | } |
||
| 620 | else $linkclose = ($morecss?' class="'.$morecss.'"':''); |
||
| 621 | |||
| 622 | $linkstart = '<a href="'.$url.'"'; |
||
| 623 | $linkstart.=$linkclose.'>'; |
||
| 624 | $linkend='</a>'; |
||
| 625 | |||
| 626 | $result .= $linkstart; |
||
| 627 | if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); |
||
| 628 | if ($withpicto != 2) $result.= $this->ref; |
||
| 629 | $result .= $linkend; |
||
| 630 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
| 631 | |||
| 632 | global $action; |
||
| 633 | $hookmanager->initHooks(array('variouspayment')); |
||
| 634 | $parameters=array('id'=>$this->id, 'getnomurl'=>$result); |
||
| 635 | $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 636 | if ($reshook > 0) $result = $hookmanager->resPrint; |
||
| 637 | else $result .= $hookmanager->resPrint; |
||
| 638 | |||
| 639 | return $result; |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Information on record |
||
| 644 | * |
||
| 645 | * @param int $id Id of record |
||
| 646 | * @return void |
||
| 647 | */ |
||
| 648 | public function info($id) |
||
| 683 | } |
||
| 684 | } |
||
| 685 | } |
||
| 686 |