| Total Complexity | 47 |
| Total Lines | 433 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Deplacement 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 Deplacement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Deplacement extends CommonObject |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string ID to identify managed object |
||
| 42 | */ |
||
| 43 | public $element = 'deplacement'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Name of table without prefix where object is stored |
||
| 47 | */ |
||
| 48 | public $table_element = 'deplacement'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string Name of subtable line |
||
| 52 | */ |
||
| 53 | public $table_element_line = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string Fieldname with ID of parent key if this field has a parent |
||
| 57 | */ |
||
| 58 | public $fk_element = ''; |
||
| 59 | |||
| 60 | public $fk_soc; |
||
| 61 | public $date; |
||
| 62 | public $type; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Date creation record (datec) |
||
| 66 | * |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | public $datec; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Date (dated) |
||
| 73 | * |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | public $dated; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int ID |
||
| 80 | */ |
||
| 81 | public $fk_user_author; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var int User ID |
||
| 85 | */ |
||
| 86 | public $fk_user; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var float km value formatted |
||
| 90 | */ |
||
| 91 | public $km; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var int Thirdparty id |
||
| 95 | */ |
||
| 96 | public $socid; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int Status 0=draft, 1=validated, 2=Refunded |
||
| 100 | */ |
||
| 101 | public $statut; |
||
| 102 | public $extraparams = array(); |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Draft status |
||
| 107 | */ |
||
| 108 | const STATUS_DRAFT = 0; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Validated status |
||
| 112 | */ |
||
| 113 | const STATUS_VALIDATED = 1; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Refunded status |
||
| 117 | */ |
||
| 118 | const STATUS_REFUNDED = 2; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Constructor |
||
| 122 | * |
||
| 123 | * @param DoliDB $db Database handler |
||
| 124 | */ |
||
| 125 | public function __construct(DoliDB $db) |
||
| 126 | { |
||
| 127 | $this->db = $db; |
||
| 128 | |||
| 129 | $this->ismultientitymanaged = 0; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create object in database |
||
| 134 | * TODO Add ref number |
||
| 135 | * |
||
| 136 | * @param User $user User that creates |
||
| 137 | * @return int Return integer <0 if KO, >0 if OK |
||
| 138 | */ |
||
| 139 | public function create($user) |
||
| 140 | { |
||
| 141 | global $conf; |
||
| 142 | |||
| 143 | // Check parameters |
||
| 144 | if (empty($this->type) || $this->type < 0) { |
||
| 145 | $this->error = 'ErrorBadParameter'; |
||
| 146 | return -1; |
||
| 147 | } |
||
| 148 | if (empty($this->fk_user) || $this->fk_user < 0) { |
||
| 149 | $this->error = 'ErrorBadParameter'; |
||
| 150 | return -1; |
||
| 151 | } |
||
| 152 | |||
| 153 | $now = dol_now(); |
||
| 154 | |||
| 155 | $this->db->begin(); |
||
| 156 | |||
| 157 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "deplacement ("; |
||
| 158 | $sql .= "datec"; |
||
| 159 | //$sql.= ", dated"; |
||
| 160 | $sql .= ", entity"; |
||
| 161 | $sql .= ", fk_user_author"; |
||
| 162 | $sql .= ", fk_user"; |
||
| 163 | $sql .= ", type"; |
||
| 164 | $sql .= ", note_private"; |
||
| 165 | $sql .= ", note_public"; |
||
| 166 | $sql .= ", fk_projet"; |
||
| 167 | $sql .= ", fk_soc"; |
||
| 168 | $sql .= ") VALUES ("; |
||
| 169 | $sql .= " '" . $this->db->idate($now) . "'"; |
||
| 170 | $sql .= ", " . ((int) $conf->entity); |
||
| 171 | $sql .= ", " . ((int) $user->id); |
||
| 172 | $sql .= ", " . ((int) $this->fk_user); |
||
| 173 | $sql .= ", '" . $this->db->escape($this->type) . "'"; |
||
| 174 | $sql .= ", " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null"); |
||
| 175 | $sql .= ", " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null"); |
||
| 176 | $sql .= ", " . ($this->fk_project > 0 ? ((int) $this->fk_project) : 0); |
||
| 177 | $sql .= ", " . ($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null"); |
||
| 178 | $sql .= ")"; |
||
| 179 | |||
| 180 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 181 | $result = $this->db->query($sql); |
||
| 182 | if ($result) { |
||
| 183 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "deplacement"); |
||
| 184 | |||
| 185 | // Call trigger |
||
| 186 | $result = $this->call_trigger('DEPLACEMENT_CREATE', $user); |
||
| 187 | if ($result < 0) { |
||
| 188 | $this->db->rollback(); |
||
| 189 | return -2; |
||
| 190 | } |
||
| 191 | // End call triggers |
||
| 192 | |||
| 193 | $result = $this->update($user); |
||
| 194 | if ($result > 0) { |
||
| 195 | $this->db->commit(); |
||
| 196 | return $this->id; |
||
| 197 | } else { |
||
| 198 | $this->error = $this->db->error(); |
||
| 199 | $this->db->rollback(); |
||
| 200 | return $result; |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | $this->error = $this->db->error() . " sql=" . $sql; |
||
| 204 | $this->db->rollback(); |
||
| 205 | return -1; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Update record |
||
| 211 | * |
||
| 212 | * @param User $user User making update |
||
| 213 | * @return int Return integer <0 if KO, >0 if OK |
||
| 214 | */ |
||
| 215 | public function update($user) |
||
| 216 | { |
||
| 217 | // Clean parameters |
||
| 218 | $this->km = (float) price2num($this->km); |
||
| 219 | |||
| 220 | // Check parameters |
||
| 221 | if (!is_numeric($this->km)) { |
||
| 222 | $this->km = 0; |
||
| 223 | } |
||
| 224 | if (empty($this->date)) { |
||
| 225 | $this->error = 'ErrorBadParameter'; |
||
| 226 | return -1; |
||
| 227 | } |
||
| 228 | if (empty($this->type) || $this->type < 0) { |
||
| 229 | $this->error = 'ErrorBadParameter'; |
||
| 230 | return -1; |
||
| 231 | } |
||
| 232 | if (empty($this->fk_user) || $this->fk_user < 0) { |
||
| 233 | $this->error = 'ErrorBadParameter'; |
||
| 234 | return -1; |
||
| 235 | } |
||
| 236 | |||
| 237 | $this->db->begin(); |
||
| 238 | |||
| 239 | $sql = "UPDATE " . MAIN_DB_PREFIX . "deplacement "; |
||
| 240 | $sql .= " SET km = " . ((float) $this->km); // This is a distance or amount |
||
| 241 | $sql .= " , dated = '" . $this->db->idate($this->date) . "'"; |
||
| 242 | $sql .= " , type = '" . $this->db->escape($this->type) . "'"; |
||
| 243 | $sql .= " , fk_statut = '" . $this->db->escape($this->statut) . "'"; |
||
| 244 | $sql .= " , fk_user = " . ((int) $this->fk_user); |
||
| 245 | $sql .= " , fk_user_modif = " . ((int) $user->id); |
||
| 246 | $sql .= " , fk_soc = " . ($this->socid > 0 ? $this->socid : 'null'); |
||
| 247 | $sql .= " , note_private = " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null"); |
||
| 248 | $sql .= " , note_public = " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null"); |
||
| 249 | $sql .= " , fk_projet = " . ($this->fk_project > 0 ? $this->fk_project : 0); |
||
| 250 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 251 | |||
| 252 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
| 253 | $result = $this->db->query($sql); |
||
| 254 | if ($result) { |
||
| 255 | $this->db->commit(); |
||
| 256 | return 1; |
||
| 257 | } else { |
||
| 258 | $this->error = $this->db->lasterror(); |
||
| 259 | $this->db->rollback(); |
||
| 260 | return -1; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Load an object from database |
||
| 266 | * |
||
| 267 | * @param int $id Id of record to load |
||
| 268 | * @param string $ref Ref of record |
||
| 269 | * @return int Return integer <0 if KO, >0 if OK |
||
| 270 | */ |
||
| 271 | public function fetch($id, $ref = '') |
||
| 272 | { |
||
| 273 | $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams"; |
||
| 274 | $sql .= " FROM " . MAIN_DB_PREFIX . "deplacement"; |
||
| 275 | $sql .= " WHERE entity IN (" . getEntity('deplacement') . ")"; |
||
| 276 | if ($ref) { |
||
| 277 | $sql .= " AND ref ='" . $this->db->escape($ref) . "'"; |
||
| 278 | } else { |
||
| 279 | $sql .= " AND rowid = " . ((int) $id); |
||
| 280 | } |
||
| 281 | |||
| 282 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
| 283 | $result = $this->db->query($sql); |
||
| 284 | if ($result) { |
||
| 285 | $obj = $this->db->fetch_object($result); |
||
| 286 | |||
| 287 | $this->id = $obj->rowid; |
||
| 288 | $this->ref = $obj->rowid; |
||
| 289 | $this->date = $this->db->jdate($obj->dated); |
||
| 290 | $this->fk_user = $obj->fk_user; |
||
| 291 | $this->socid = $obj->fk_soc; |
||
| 292 | $this->km = $obj->km; |
||
| 293 | $this->type = $obj->type; |
||
| 294 | $this->statut = $obj->fk_statut; |
||
| 295 | $this->note_private = $obj->note_private; |
||
| 296 | $this->note_public = $obj->note_public; |
||
| 297 | $this->fk_project = $obj->fk_project; |
||
| 298 | |||
| 299 | $this->extraparams = (array) json_decode($obj->extraparams, true); |
||
| 300 | |||
| 301 | return 1; |
||
| 302 | } else { |
||
| 303 | $this->error = $this->db->error(); |
||
| 304 | return -1; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Delete record |
||
| 310 | * |
||
| 311 | * @param User $user USer that Delete |
||
| 312 | * @return int Return integer <0 if KO, >0 if OK |
||
| 313 | */ |
||
| 314 | public function delete($user) |
||
| 315 | { |
||
| 316 | $this->db->begin(); |
||
| 317 | |||
| 318 | $id = $this->id; |
||
| 319 | |||
| 320 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "deplacement WHERE rowid = " . ((int) $id); |
||
| 321 | |||
| 322 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
| 323 | $result = $this->db->query($sql); |
||
| 324 | if ($result) { |
||
| 325 | $this->db->commit(); |
||
| 326 | return 1; |
||
| 327 | } else { |
||
| 328 | $this->error = $this->db->error(); |
||
| 329 | $this->db->rollback(); |
||
| 330 | return -1; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Return the label of the status |
||
| 337 | * |
||
| 338 | * @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 |
||
| 339 | * @return string Label of status |
||
| 340 | */ |
||
| 341 | public function getLibStatut($mode = 0) |
||
| 342 | { |
||
| 343 | return $this->LibStatut($this->statut, $mode); |
||
| 344 | } |
||
| 345 | |||
| 346 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 347 | /** |
||
| 348 | * Return the label of a given status |
||
| 349 | * |
||
| 350 | * @param int $status Id status |
||
| 351 | * @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 |
||
| 352 | * @return string Label of status |
||
| 353 | */ |
||
| 354 | public function LibStatut($status, $mode = 0) |
||
| 355 | { |
||
| 356 | // phpcs:enable |
||
| 357 | global $langs; |
||
| 358 | |||
| 359 | if (empty($this->labelStatus) || empty($this->labelStatusShort)) { |
||
| 360 | global $langs; |
||
| 361 | //$langs->load("mymodule@mymodule"); |
||
| 362 | $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft'); |
||
| 363 | $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated'); |
||
| 364 | $this->labelStatus[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded'); |
||
| 365 | $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft'); |
||
| 366 | $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated'); |
||
| 367 | $this->labelStatusShort[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded'); |
||
| 368 | } |
||
| 369 | |||
| 370 | $status_logo = array(0 => 'status0', 1 => 'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5'); |
||
| 371 | $statusType = $status_logo[$status]; |
||
| 372 | |||
| 373 | return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Return clicable name (with picto eventually) |
||
| 378 | * |
||
| 379 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 380 | * @return string Chaine avec URL |
||
| 381 | */ |
||
| 382 | public function getNomUrl($withpicto = 0) |
||
| 405 | } |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * List of types |
||
| 410 | * |
||
| 411 | * @param int $active Active or not |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function listOfTypes($active = 1) |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Information on record |
||
| 443 | * |
||
| 444 | * @param int $id Id of record |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | public function info($id) |
||
| 471 | } |
||
| 472 | } |
||
| 473 | } |
||
| 474 |