| Total Complexity | 63 |
| Total Lines | 423 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PaymentTerm 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 PaymentTerm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class PaymentTerm // extends CommonObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var DoliDB Database handler. |
||
|
|
|||
| 37 | */ |
||
| 38 | public $db; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string Error code (or message) |
||
| 42 | */ |
||
| 43 | public $error = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string[] Error codes (or messages) |
||
| 47 | */ |
||
| 48 | public $errors = array(); |
||
| 49 | |||
| 50 | //public $element='c_payment_term'; //!< Id that identify managed objects |
||
| 51 | //public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored |
||
| 52 | public $context = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int ID |
||
| 56 | */ |
||
| 57 | public $id; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int Entity ID |
||
| 62 | */ |
||
| 63 | public $entity; |
||
| 64 | |||
| 65 | public $code; |
||
| 66 | public $sortorder; |
||
| 67 | public $active; |
||
| 68 | public $libelle; |
||
| 69 | public $libelle_facture; |
||
| 70 | public $type_cdr; |
||
| 71 | public $nbjour; |
||
| 72 | public $decalage; |
||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Constructor |
||
| 79 | * |
||
| 80 | * @param DoliDB $db Database handler |
||
| 81 | */ |
||
| 82 | public function __construct(DoliDB $db) |
||
| 83 | { |
||
| 84 | $this->db = $db; |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Create in database |
||
| 90 | * |
||
| 91 | * @param User $user User that create |
||
| 92 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 93 | * @return int Return integer <0 if KO, Id of created object if OK |
||
| 94 | */ |
||
| 95 | public function create($user, $notrigger = 0) |
||
| 96 | { |
||
| 97 | global $conf, $langs; |
||
| 98 | $error = 0; |
||
| 99 | |||
| 100 | // Clean parameters |
||
| 101 | |||
| 102 | if (isset($this->code)) { |
||
| 103 | $this->code = trim($this->code); |
||
| 104 | } |
||
| 105 | if (isset($this->sortorder)) { |
||
| 106 | $this->sortorder = trim($this->sortorder); |
||
| 107 | } |
||
| 108 | if (isset($this->active)) { |
||
| 109 | $this->active = trim($this->active); |
||
| 110 | } |
||
| 111 | if (isset($this->libelle)) { |
||
| 112 | $this->libelle = trim($this->libelle); |
||
| 113 | } |
||
| 114 | if (isset($this->libelle_facture)) { |
||
| 115 | $this->libelle_facture = trim($this->libelle_facture); |
||
| 116 | } |
||
| 117 | if (isset($this->type_cdr)) { |
||
| 118 | $this->type_cdr = trim($this->type_cdr); |
||
| 119 | } |
||
| 120 | if (isset($this->nbjour)) { |
||
| 121 | $this->nbjour = trim($this->nbjour); |
||
| 122 | } |
||
| 123 | if (isset($this->decalage)) { |
||
| 124 | $this->decalage = trim($this->decalage); |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | // Check parameters |
||
| 129 | // Put here code to add control on parameters values |
||
| 130 | |||
| 131 | // Insert request |
||
| 132 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "c_payment_term("; |
||
| 133 | $sql .= "entity,"; |
||
| 134 | $sql .= "code,"; |
||
| 135 | $sql .= "sortorder,"; |
||
| 136 | $sql .= "active,"; |
||
| 137 | $sql .= "libelle,"; |
||
| 138 | $sql .= "libelle_facture,"; |
||
| 139 | $sql .= "type_cdr,"; |
||
| 140 | $sql .= "nbjour,"; |
||
| 141 | $sql .= "decalage"; |
||
| 142 | $sql .= ") VALUES ("; |
||
| 143 | $sql .= " " . (!isset($this->entity) ? getEntity('c_payment_term') : "'" . $this->db->escape($this->entity) . "'") . ","; |
||
| 144 | $sql .= " " . (!isset($this->code) ? 'NULL' : "'" . $this->db->escape($this->code) . "'") . ","; |
||
| 145 | $sql .= " " . (!isset($this->sortorder) ? 'NULL' : "'" . $this->db->escape($this->sortorder) . "'") . ","; |
||
| 146 | $sql .= " " . (!isset($this->active) ? 'NULL' : "'" . $this->db->escape($this->active) . "'") . ","; |
||
| 147 | $sql .= " " . (!isset($this->libelle) ? 'NULL' : "'" . $this->db->escape($this->libelle) . "'") . ","; |
||
| 148 | $sql .= " " . (!isset($this->libelle_facture) ? 'NULL' : "'" . $this->db->escape($this->libelle_facture) . "'") . ","; |
||
| 149 | $sql .= " " . (!isset($this->type_cdr) ? 'NULL' : "'" . $this->db->escape($this->type_cdr) . "'") . ","; |
||
| 150 | $sql .= " " . (!isset($this->nbjour) ? 'NULL' : "'" . $this->db->escape($this->nbjour) . "'") . ","; |
||
| 151 | $sql .= " " . (!isset($this->decalage) ? 'NULL' : "'" . $this->db->escape($this->decalage) . "'"); |
||
| 152 | $sql .= ")"; |
||
| 153 | |||
| 154 | $this->db->begin(); |
||
| 155 | |||
| 156 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 157 | $resql = $this->db->query($sql); |
||
| 158 | if (!$resql) { |
||
| 159 | $error++; |
||
| 160 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 161 | } |
||
| 162 | |||
| 163 | if (!$error) { |
||
| 164 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "c_payment_term"); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Commit or rollback |
||
| 168 | if ($error) { |
||
| 169 | foreach ($this->errors as $errmsg) { |
||
| 170 | dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
||
| 171 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 172 | } |
||
| 173 | $this->db->rollback(); |
||
| 174 | return -1 * $error; |
||
| 175 | } else { |
||
| 176 | $this->db->commit(); |
||
| 177 | return $this->id; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Load object in memory from database |
||
| 184 | * |
||
| 185 | * @param int $id Id object |
||
| 186 | * @param string $code Code object |
||
| 187 | * @return int Return integer <0 if KO, >0 if OK |
||
| 188 | */ |
||
| 189 | public function fetch($id, $code = '') |
||
| 190 | { |
||
| 191 | $sql = "SELECT"; |
||
| 192 | $sql .= " t.rowid,"; |
||
| 193 | $sql .= " t.entity,"; |
||
| 194 | $sql .= " t.code,"; |
||
| 195 | $sql .= " t.sortorder,"; |
||
| 196 | $sql .= " t.active,"; |
||
| 197 | $sql .= " t.libelle,"; |
||
| 198 | $sql .= " t.libelle_facture,"; |
||
| 199 | $sql .= " t.type_cdr,"; |
||
| 200 | $sql .= " t.nbjour,"; |
||
| 201 | $sql .= " t.decalage"; |
||
| 202 | $sql .= " FROM " . MAIN_DB_PREFIX . "c_payment_term as t"; |
||
| 203 | if ($id) { |
||
| 204 | $sql .= " WHERE t.rowid = " . ((int) $id); |
||
| 205 | } |
||
| 206 | if ($code) { |
||
| 207 | $sql .= " WHERE t.code='" . $this->db->escape($code) . "' AND t.entity IN (" . getEntity('payment_term') . ")"; |
||
| 208 | } |
||
| 209 | |||
| 210 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
| 211 | $resql = $this->db->query($sql); |
||
| 212 | if ($resql) { |
||
| 213 | if ($this->db->num_rows($resql)) { |
||
| 214 | $obj = $this->db->fetch_object($resql); |
||
| 215 | |||
| 216 | $this->id = $obj->rowid; |
||
| 217 | |||
| 218 | $this->code = $obj->code; |
||
| 219 | $this->sortorder = $obj->sortorder; |
||
| 220 | $this->active = $obj->active; |
||
| 221 | $this->libelle = $obj->libelle; |
||
| 222 | $this->libelle_facture = $obj->libelle_facture; |
||
| 223 | $this->type_cdr = $obj->type_cdr; |
||
| 224 | $this->nbjour = $obj->nbjour; |
||
| 225 | $this->decalage = $obj->decalage; |
||
| 226 | } |
||
| 227 | $this->db->free($resql); |
||
| 228 | |||
| 229 | return 1; |
||
| 230 | } else { |
||
| 231 | $this->error = "Error " . $this->db->lasterror(); |
||
| 232 | return -1; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Return id of default payment term |
||
| 239 | * |
||
| 240 | * @return int Return integer <0 if KO, >0 if OK |
||
| 241 | */ |
||
| 242 | public function getDefaultId() |
||
| 243 | { |
||
| 244 | global $langs; |
||
| 245 | |||
| 246 | $ret = 0; |
||
| 247 | |||
| 248 | $sql = "SELECT"; |
||
| 249 | $sql .= " t.rowid"; |
||
| 250 | $sql .= " FROM " . MAIN_DB_PREFIX . "c_payment_term as t"; |
||
| 251 | $sql .= " WHERE t.code = 'RECEP'"; |
||
| 252 | $sql .= " AND t.entity IN (" . getEntity('c_payment_term') . ")"; |
||
| 253 | |||
| 254 | dol_syslog(get_class($this) . "::getDefaultId", LOG_DEBUG); |
||
| 255 | $resql = $this->db->query($sql); |
||
| 256 | if ($resql) { |
||
| 257 | if ($this->db->num_rows($resql)) { |
||
| 258 | $obj = $this->db->fetch_object($resql); |
||
| 259 | if ($obj) { |
||
| 260 | $ret = $obj->rowid; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | $this->db->free($resql); |
||
| 264 | return $ret; |
||
| 265 | } else { |
||
| 266 | $this->error = "Error " . $this->db->lasterror(); |
||
| 267 | return -1; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Update database |
||
| 274 | * |
||
| 275 | * @param User $user User that modify |
||
| 276 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 277 | * @return int Return integer <0 if KO, >0 if OK |
||
| 278 | */ |
||
| 279 | public function update($user = null, $notrigger = 0) |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Delete object in database |
||
| 355 | * |
||
| 356 | * @param User $user User that delete |
||
| 357 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 358 | * @return int Return integer <0 if KO, >0 if OK |
||
| 359 | */ |
||
| 360 | public function delete($user, $notrigger = 0) |
||
| 361 | { |
||
| 362 | global $conf, $langs; |
||
| 363 | $error = 0; |
||
| 364 | |||
| 365 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "c_payment_term"; |
||
| 366 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 367 | |||
| 368 | $this->db->begin(); |
||
| 369 | |||
| 370 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
| 371 | $resql = $this->db->query($sql); |
||
| 372 | if (!$resql) { |
||
| 373 | $error++; |
||
| 374 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 375 | } |
||
| 376 | |||
| 377 | // Commit or rollback |
||
| 378 | if ($error) { |
||
| 379 | foreach ($this->errors as $errmsg) { |
||
| 380 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
| 381 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 382 | } |
||
| 383 | $this->db->rollback(); |
||
| 384 | return -1 * $error; |
||
| 385 | } else { |
||
| 386 | $this->db->commit(); |
||
| 387 | return 1; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Load an object from its id and create a new one in database |
||
| 395 | * |
||
| 396 | * @param User $user User making the clone |
||
| 397 | * @param int $fromid Id of object to clone |
||
| 398 | * @return int New id of clone |
||
| 399 | */ |
||
| 400 | public function createFromClone(User $user, $fromid) |
||
| 401 | { |
||
| 402 | $error = 0; |
||
| 403 | |||
| 404 | $object = new PaymentTerm($this->db); |
||
| 405 | |||
| 406 | $this->db->begin(); |
||
| 407 | |||
| 408 | // Load source object |
||
| 409 | $object->fetch($fromid); |
||
| 410 | $object->id = 0; |
||
| 411 | |||
| 412 | // Create clone |
||
| 413 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 414 | $result = $object->create($user); |
||
| 415 | |||
| 416 | // Other options |
||
| 417 | if ($result < 0) { |
||
| 418 | $this->error = $object->error; |
||
| 419 | $error++; |
||
| 420 | } |
||
| 421 | |||
| 422 | unset($object->context['createfromclone']); |
||
| 423 | |||
| 424 | // End |
||
| 425 | if (!$error) { |
||
| 426 | $this->db->commit(); |
||
| 427 | return $object->id; |
||
| 428 | } else { |
||
| 429 | $this->db->rollback(); |
||
| 430 | return -1; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Initialise an instance with random values. |
||
| 437 | * Used to build previews or test instances. |
||
| 438 | * id must be 0 if object instance is a specimen. |
||
| 439 | * |
||
| 440 | * @return int |
||
| 441 | */ |
||
| 442 | public function initAsSpecimen() |
||
| 456 | } |
||
| 457 | } |
||
| 458 |