| Total Complexity | 46 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Cpaiement 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 Cpaiement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | * Class Cpaiement |
||
| 37 | */ |
||
| 38 | class Cpaiement extends CommonDict |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string Id to identify managed objects |
||
| 42 | */ |
||
| 43 | public $element = 'cpaiement'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Name of table without prefix where object is stored |
||
| 47 | */ |
||
| 48 | public $table_element = 'c_paiement'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | * @deprecated |
||
| 53 | * @see $label |
||
| 54 | */ |
||
| 55 | public $libelle; |
||
| 56 | |||
| 57 | public $type; |
||
| 58 | public $active; |
||
| 59 | public $accountancy_code; |
||
| 60 | public $module; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Constructor |
||
| 65 | * |
||
| 66 | * @param DoliDB $db Database handler |
||
| 67 | */ |
||
| 68 | public function __construct(DoliDB $db) |
||
|
|
|||
| 69 | { |
||
| 70 | $this->db = $db; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Create object into database |
||
| 75 | * |
||
| 76 | * @param User $user User that creates |
||
| 77 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 78 | * @return int Return integer <0 if KO, Id of created object if OK |
||
| 79 | */ |
||
| 80 | public function create(User $user, $notrigger = 0) |
||
| 81 | { |
||
| 82 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 83 | |||
| 84 | $error = 0; |
||
| 85 | |||
| 86 | // Clean parameters |
||
| 87 | |||
| 88 | if (isset($this->code)) { |
||
| 89 | $this->code = trim($this->code); |
||
| 90 | } |
||
| 91 | if (isset($this->libelle)) { |
||
| 92 | $this->libelle = trim($this->libelle); |
||
| 93 | } |
||
| 94 | if (isset($this->label)) { |
||
| 95 | $this->label = trim($this->label); |
||
| 96 | } |
||
| 97 | if (isset($this->type)) { |
||
| 98 | $this->type = trim($this->type); |
||
| 99 | } |
||
| 100 | if (isset($this->active)) { |
||
| 101 | $this->active = (int) $this->active; |
||
| 102 | } |
||
| 103 | if (isset($this->accountancy_code)) { |
||
| 104 | $this->accountancy_code = trim($this->accountancy_code); |
||
| 105 | } |
||
| 106 | if (isset($this->module)) { |
||
| 107 | $this->module = trim($this->module); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | // Check parameters |
||
| 113 | // Put here code to add control on parameters values |
||
| 114 | |||
| 115 | // Insert request |
||
| 116 | $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '('; |
||
| 117 | $sql .= 'entity,'; |
||
| 118 | $sql .= 'code,'; |
||
| 119 | $sql .= 'libelle,'; |
||
| 120 | $sql .= 'type,'; |
||
| 121 | $sql .= 'active,'; |
||
| 122 | $sql .= 'accountancy_code,'; |
||
| 123 | $sql .= 'module'; |
||
| 124 | $sql .= ') VALUES ('; |
||
| 125 | $sql .= ' ' . (!isset($this->entity) ? getEntity('c_paiement') : $this->entity) . ','; |
||
| 126 | $sql .= ' ' . (!isset($this->code) ? 'NULL' : "'" . $this->db->escape($this->code) . "'") . ','; |
||
| 127 | $sql .= ' ' . (!isset($this->libelle) ? 'NULL' : "'" . $this->db->escape($this->libelle) . "'") . ','; |
||
| 128 | $sql .= ' ' . (!isset($this->type) ? 'NULL' : $this->type) . ','; |
||
| 129 | $sql .= ' ' . (!isset($this->active) ? 'NULL' : $this->active) . ','; |
||
| 130 | $sql .= ' ' . (!isset($this->accountancy_code) ? 'NULL' : "'" . $this->db->escape($this->accountancy_code) . "'") . ','; |
||
| 131 | $sql .= ' ' . (!isset($this->module) ? 'NULL' : "'" . $this->db->escape($this->module) . "'"); |
||
| 132 | $sql .= ')'; |
||
| 133 | |||
| 134 | $this->db->begin(); |
||
| 135 | |||
| 136 | $resql = $this->db->query($sql); |
||
| 137 | if (!$resql) { |
||
| 138 | $error++; |
||
| 139 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 140 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!$error) { |
||
| 144 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
||
| 145 | |||
| 146 | // Uncomment this and change MYOBJECT to your own tag if you |
||
| 147 | // want this action to call a trigger. |
||
| 148 | //if (!$notrigger) { |
||
| 149 | |||
| 150 | // // Call triggers |
||
| 151 | // $result=$this->call_trigger('MYOBJECT_CREATE',$user); |
||
| 152 | // if ($result < 0) $error++; |
||
| 153 | // // End call triggers |
||
| 154 | //} |
||
| 155 | } |
||
| 156 | |||
| 157 | // Commit or rollback |
||
| 158 | if ($error) { |
||
| 159 | $this->db->rollback(); |
||
| 160 | |||
| 161 | return -1 * $error; |
||
| 162 | } else { |
||
| 163 | $this->db->commit(); |
||
| 164 | |||
| 165 | return $this->id; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Load object in memory from the database |
||
| 171 | * |
||
| 172 | * @param int $id Id object |
||
| 173 | * @param string $ref Ref |
||
| 174 | * |
||
| 175 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
| 176 | */ |
||
| 177 | public function fetch($id, $ref = null) |
||
| 178 | { |
||
| 179 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 180 | |||
| 181 | $sql = 'SELECT'; |
||
| 182 | $sql .= ' t.id,'; |
||
| 183 | $sql .= " t.code,"; |
||
| 184 | $sql .= " t.libelle as label,"; |
||
| 185 | $sql .= " t.type,"; |
||
| 186 | $sql .= " t.active,"; |
||
| 187 | $sql .= " t.accountancy_code,"; |
||
| 188 | $sql .= " t.module"; |
||
| 189 | $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t'; |
||
| 190 | if (null !== $ref) { |
||
| 191 | $sql .= ' WHERE t.entity IN (' . getEntity('c_paiement') . ')'; |
||
| 192 | $sql .= " AND t.code = '" . $this->db->escape($ref) . "'"; |
||
| 193 | } else { |
||
| 194 | $sql .= ' WHERE t.id = ' . ((int) $id); |
||
| 195 | } |
||
| 196 | |||
| 197 | $resql = $this->db->query($sql); |
||
| 198 | if ($resql) { |
||
| 199 | $numrows = $this->db->num_rows($resql); |
||
| 200 | if ($numrows) { |
||
| 201 | $obj = $this->db->fetch_object($resql); |
||
| 202 | |||
| 203 | $this->id = $obj->id; |
||
| 204 | |||
| 205 | $this->code = $obj->code; |
||
| 206 | $this->libelle = $obj->label; |
||
| 207 | $this->label = $obj->label; |
||
| 208 | $this->type = $obj->type; |
||
| 209 | $this->active = $obj->active; |
||
| 210 | $this->accountancy_code = $obj->accountancy_code; |
||
| 211 | $this->module = $obj->module; |
||
| 212 | } |
||
| 213 | $this->db->free($resql); |
||
| 214 | |||
| 215 | if ($numrows) { |
||
| 216 | return 1; |
||
| 217 | } else { |
||
| 218 | return 0; |
||
| 219 | } |
||
| 220 | } else { |
||
| 221 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 222 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
| 223 | |||
| 224 | return -1; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Update object into database |
||
| 230 | * |
||
| 231 | * @param User $user User that modifies |
||
| 232 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 233 | * @return int Return integer <0 if KO, >0 if OK |
||
| 234 | */ |
||
| 235 | public function update(User $user, $notrigger = 0) |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete object in database |
||
| 314 | * |
||
| 315 | * @param User $user User that deletes |
||
| 316 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 317 | * @return int Return integer <0 if KO, >0 if OK |
||
| 318 | */ |
||
| 319 | public function delete(User $user, $notrigger = 0) |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Initialise object with example values |
||
| 364 | * Id must be 0 if object instance is a specimen |
||
| 365 | * |
||
| 366 | * @return int |
||
| 367 | */ |
||
| 368 | public function initAsSpecimen() |
||
| 369 | { |
||
| 370 | $this->id = 0; |
||
| 371 | $this->code = ''; |
||
| 372 | $this->libelle = ''; |
||
| 373 | $this->label = ''; |
||
| 374 | $this->type = ''; |
||
| 375 | $this->active = 1; |
||
| 376 | $this->accountancy_code = ''; |
||
| 377 | $this->module = ''; |
||
| 378 | |||
| 382 |