| Total Complexity | 57 |
| Total Lines | 493 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Fiscalyear 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 Fiscalyear, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Fiscalyear extends CommonObject |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string ID to identify managed object |
||
| 40 | */ |
||
| 41 | public $element = 'fiscalyear'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string picto |
||
| 45 | */ |
||
| 46 | public $picto = 'calendar'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string Name of table without prefix where object is stored |
||
| 50 | */ |
||
| 51 | public $table_element = 'accounting_fiscalyear'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string Name of subtable line |
||
| 55 | */ |
||
| 56 | public $table_element_line = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string Field with ID of parent key if this field has a parent |
||
| 60 | */ |
||
| 61 | public $fk_element = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int ID |
||
| 65 | */ |
||
| 66 | public $rowid; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string fiscal year label |
||
| 70 | */ |
||
| 71 | public $label; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Date start (date_start) |
||
| 75 | * |
||
| 76 | * @var integer |
||
| 77 | */ |
||
| 78 | public $date_start; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Date end (date_end) |
||
| 82 | * |
||
| 83 | * @var integer |
||
| 84 | */ |
||
| 85 | public $date_end; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Date creation record (datec) |
||
| 89 | * |
||
| 90 | * @var integer |
||
| 91 | */ |
||
| 92 | public $datec; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var int status 0=open, 1=closed |
||
| 96 | * @deprecated |
||
| 97 | * @see $status |
||
| 98 | */ |
||
| 99 | public $statut; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int status 0=open, 1=closed |
||
| 103 | */ |
||
| 104 | public $status; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int Entity |
||
| 108 | */ |
||
| 109 | public $entity; |
||
| 110 | |||
| 111 | |||
| 112 | const STATUS_OPEN = 0; |
||
| 113 | const STATUS_CLOSED = 1; |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Constructor |
||
| 118 | * |
||
| 119 | * @param DoliDB $db Database handler |
||
| 120 | */ |
||
| 121 | public function __construct(DoliDB $db) |
||
|
|
|||
| 122 | { |
||
| 123 | $this->db = $db; |
||
| 124 | |||
| 125 | $this->ismultientitymanaged = 1; |
||
| 126 | $this->labelStatusShort = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed'); |
||
| 127 | $this->labelStatus = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed'); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Create object in database |
||
| 132 | * |
||
| 133 | * @param User $user User making creation |
||
| 134 | * @return int Return integer <0 if KO, >0 if OK |
||
| 135 | */ |
||
| 136 | public function create($user) |
||
| 137 | { |
||
| 138 | global $conf; |
||
| 139 | |||
| 140 | $error = 0; |
||
| 141 | |||
| 142 | $now = dol_now(); |
||
| 143 | |||
| 144 | $this->db->begin(); |
||
| 145 | |||
| 146 | $sql = "INSERT INTO " . $this->db->prefix() . "accounting_fiscalyear ("; |
||
| 147 | $sql .= "label"; |
||
| 148 | $sql .= ", date_start"; |
||
| 149 | $sql .= ", date_end"; |
||
| 150 | $sql .= ", statut"; |
||
| 151 | $sql .= ", entity"; |
||
| 152 | $sql .= ", datec"; |
||
| 153 | $sql .= ", fk_user_author"; |
||
| 154 | $sql .= ") VALUES ("; |
||
| 155 | $sql .= " '" . $this->db->escape($this->label) . "'"; |
||
| 156 | $sql .= ", '" . $this->db->idate($this->date_start) . "'"; |
||
| 157 | $sql .= ", " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null"); |
||
| 158 | $sql .= ", 0"; |
||
| 159 | $sql .= ", " . ((int) $conf->entity); |
||
| 160 | $sql .= ", '" . $this->db->idate($now) . "'"; |
||
| 161 | $sql .= ", " . ((int) $user->id); |
||
| 162 | $sql .= ")"; |
||
| 163 | |||
| 164 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 165 | $result = $this->db->query($sql); |
||
| 166 | if ($result) { |
||
| 167 | $this->id = $this->db->last_insert_id($this->db->prefix() . "accounting_fiscalyear"); |
||
| 168 | |||
| 169 | $result = $this->update($user); |
||
| 170 | if ($result > 0) { |
||
| 171 | $this->db->commit(); |
||
| 172 | return $this->id; |
||
| 173 | } else { |
||
| 174 | $this->error = $this->db->lasterror(); |
||
| 175 | $this->db->rollback(); |
||
| 176 | return $result; |
||
| 177 | } |
||
| 178 | } else { |
||
| 179 | $this->error = $this->db->lasterror() . " sql=" . $sql; |
||
| 180 | $this->db->rollback(); |
||
| 181 | return -1; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Update record |
||
| 187 | * |
||
| 188 | * @param User $user User making update |
||
| 189 | * @return int Return integer <0 if KO, >0 if OK |
||
| 190 | */ |
||
| 191 | public function update($user) |
||
| 192 | { |
||
| 193 | // Check parameters |
||
| 194 | if (empty($this->date_start) && empty($this->date_end)) { |
||
| 195 | $this->error = 'ErrorBadParameter'; |
||
| 196 | return -1; |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->db->begin(); |
||
| 200 | |||
| 201 | $sql = "UPDATE " . $this->db->prefix() . "accounting_fiscalyear"; |
||
| 202 | $sql .= " SET label = '" . $this->db->escape($this->label) . "'"; |
||
| 203 | $sql .= ", date_start = '" . $this->db->idate($this->date_start) . "'"; |
||
| 204 | $sql .= ", date_end = " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null"); |
||
| 205 | $sql .= ", statut = '" . $this->db->escape($this->status ? $this->status : 0) . "'"; |
||
| 206 | $sql .= ", fk_user_modif = " . ((int) $user->id); |
||
| 207 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 208 | |||
| 209 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
| 210 | $result = $this->db->query($sql); |
||
| 211 | if ($result) { |
||
| 212 | $this->db->commit(); |
||
| 213 | return 1; |
||
| 214 | } else { |
||
| 215 | $this->error = $this->db->lasterror(); |
||
| 216 | dol_syslog($this->error, LOG_ERR); |
||
| 217 | $this->db->rollback(); |
||
| 218 | return -1; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Load an object from database |
||
| 224 | * |
||
| 225 | * @param int $id Id of record to load |
||
| 226 | * @return int Return integer <0 if KO, >0 if OK |
||
| 227 | */ |
||
| 228 | public function fetch($id) |
||
| 229 | { |
||
| 230 | $sql = "SELECT rowid, label, date_start, date_end, statut as status"; |
||
| 231 | $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear"; |
||
| 232 | $sql .= " WHERE rowid = " . ((int) $id); |
||
| 233 | |||
| 234 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
| 235 | $result = $this->db->query($sql); |
||
| 236 | if ($result) { |
||
| 237 | $obj = $this->db->fetch_object($result); |
||
| 238 | |||
| 239 | $this->id = $obj->rowid; |
||
| 240 | $this->ref = $obj->rowid; |
||
| 241 | $this->date_start = $this->db->jdate($obj->date_start); |
||
| 242 | $this->date_end = $this->db->jdate($obj->date_end); |
||
| 243 | $this->label = $obj->label; |
||
| 244 | $this->statut = $obj->status; |
||
| 245 | $this->status = $obj->status; |
||
| 246 | |||
| 247 | return 1; |
||
| 248 | } else { |
||
| 249 | $this->error = $this->db->lasterror(); |
||
| 250 | return -1; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Delete record |
||
| 256 | * |
||
| 257 | * @param User $user User that delete |
||
| 258 | * @return int Return integer <0 if KO, >0 if OK |
||
| 259 | */ |
||
| 260 | public function delete($user) |
||
| 261 | { |
||
| 262 | $this->db->begin(); |
||
| 263 | |||
| 264 | $sql = "DELETE FROM " . $this->db->prefix() . "accounting_fiscalyear"; |
||
| 265 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 266 | |||
| 267 | $result = $this->db->query($sql); |
||
| 268 | if ($result) { |
||
| 269 | $this->db->commit(); |
||
| 270 | return 1; |
||
| 271 | } else { |
||
| 272 | $this->error = $this->db->lasterror(); |
||
| 273 | $this->db->rollback(); |
||
| 274 | return -1; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * getTooltipContentArray |
||
| 280 | * |
||
| 281 | * @param array $params ex option, infologin |
||
| 282 | * @since v18 |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getTooltipContentArray($params) |
||
| 286 | { |
||
| 287 | global $langs; |
||
| 288 | |||
| 289 | $langs->load('compta'); |
||
| 290 | |||
| 291 | $datas = []; |
||
| 292 | $datas['picto'] = img_picto('', $this->picto) . ' <b><u>' . $langs->trans("FiscalPeriod") . '</u></b>'; |
||
| 293 | if (isset($this->status)) { |
||
| 294 | $datas['picto'] .= ' ' . $this->getLibStatut(5); |
||
| 295 | } |
||
| 296 | $datas['ref'] = '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
| 297 | if (isset($this->date_start)) { |
||
| 298 | $datas['date_start'] = '<br><b>' . $langs->trans('DateStart') . ':</b> ' . dol_print_date($this->date_start, 'day'); |
||
| 299 | } |
||
| 300 | if (isset($this->date_start)) { |
||
| 301 | $datas['date_end'] = '<br><b>' . $langs->trans('DateEnd') . ':</b> ' . dol_print_date($this->date_end, 'day'); |
||
| 302 | } |
||
| 303 | |||
| 304 | return $datas; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return clicable link of object (with eventually picto) |
||
| 309 | * |
||
| 310 | * @param int $withpicto Add picto into link |
||
| 311 | * @param int $notooltip 1=Disable tooltip |
||
| 312 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 313 | * @return string String with URL |
||
| 314 | */ |
||
| 315 | public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1) |
||
| 316 | { |
||
| 317 | global $conf, $langs, $user; |
||
| 318 | |||
| 319 | if (empty($this->ref)) { |
||
| 320 | $this->ref = (string) $this->id; |
||
| 321 | } |
||
| 322 | |||
| 323 | if (!empty($conf->dol_no_mouse_hover)) { |
||
| 324 | $notooltip = 1; // Force disable tooltips |
||
| 325 | } |
||
| 326 | $option = ''; |
||
| 327 | if (!$user->hasRight('accounting', 'fiscalyear', 'write')) { |
||
| 328 | $option = 'nolink'; |
||
| 329 | } |
||
| 330 | $result = ''; |
||
| 331 | $params = [ |
||
| 332 | 'id' => $this->id, |
||
| 333 | 'objecttype' => $this->element, |
||
| 334 | 'option' => $option, |
||
| 335 | 'nofetch' => 1, |
||
| 336 | ]; |
||
| 337 | $classfortooltip = 'classfortooltip'; |
||
| 338 | $dataparams = ''; |
||
| 339 | if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { |
||
| 340 | $classfortooltip = 'classforajaxtooltip'; |
||
| 341 | $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"'; |
||
| 342 | $label = 'ToComplete'; |
||
| 343 | } else { |
||
| 344 | $label = implode($this->getTooltipContentArray($params)); |
||
| 345 | } |
||
| 346 | $url = constant('BASE_URL') . '/accountancy/admin/fiscalyear_card.php?id=' . $this->id; |
||
| 347 | |||
| 348 | if ($option !== 'nolink') { |
||
| 349 | // Add param to save lastsearch_values or not |
||
| 350 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 351 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 352 | $add_save_lastsearch_values = 1; |
||
| 353 | } |
||
| 354 | if ($add_save_lastsearch_values) { |
||
| 355 | $url .= '&save_lastsearch_values=1'; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | $linkclose = ''; |
||
| 360 | if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) { |
||
| 361 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
| 362 | $label = $langs->trans("FiscalPeriod"); |
||
| 363 | $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"'; |
||
| 364 | } |
||
| 365 | $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"'; |
||
| 366 | $linkclose .= $dataparams . ' class="' . $classfortooltip . '"'; |
||
| 367 | } |
||
| 368 | |||
| 369 | $linkstart = '<a href="' . $url . '"'; |
||
| 370 | $linkstart .= $linkclose . '>'; |
||
| 371 | $linkend = '</a>'; |
||
| 372 | |||
| 373 | if ($option === 'nolink') { |
||
| 374 | $linkstart = ''; |
||
| 375 | $linkend = ''; |
||
| 376 | } |
||
| 377 | |||
| 378 | $result .= $linkstart; |
||
| 379 | if ($withpicto) { |
||
| 380 | $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams . ' class="' . (($withpicto != 2) ? 'paddingright ' : '') . $classfortooltip . '"'), 0, 0, $notooltip ? 0 : 1); |
||
| 381 | } |
||
| 382 | if ($withpicto != 2) { |
||
| 383 | $result .= $this->ref; |
||
| 384 | } |
||
| 385 | $result .= $linkend; |
||
| 386 | |||
| 387 | return $result; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Give a label from a status |
||
| 392 | * |
||
| 393 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto |
||
| 394 | * @return string Label |
||
| 395 | */ |
||
| 396 | public function getLibStatut($mode = 0) |
||
| 397 | { |
||
| 398 | return $this->LibStatut($this->status, $mode); |
||
| 399 | } |
||
| 400 | |||
| 401 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 402 | /** |
||
| 403 | * Give a label from a status |
||
| 404 | * |
||
| 405 | * @param int $status Id status |
||
| 406 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto |
||
| 407 | * @return string Label |
||
| 408 | */ |
||
| 409 | public function LibStatut($status, $mode = 0) |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Information on record |
||
| 432 | * |
||
| 433 | * @param int $id Id of record |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | public function info($id) |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Return the number of entries by fiscal year |
||
| 465 | * |
||
| 466 | * @param int|string $datestart Date start to scan |
||
| 467 | * @param int|string $dateend Date end to scan |
||
| 468 | * @return string Number of entries |
||
| 469 | */ |
||
| 470 | public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '') |
||
| 471 | { |
||
| 472 | global $conf; |
||
| 473 | |||
| 474 | if (empty($datestart)) { |
||
| 475 | $datestart = $this->date_start; |
||
| 476 | } |
||
| 477 | if (empty($dateend)) { |
||
| 478 | $dateend = $this->date_end; |
||
| 479 | } |
||
| 480 | |||
| 481 | $sql = "SELECT count(DISTINCT piece_num) as nb"; |
||
| 482 | $sql .= " FROM " . $this->db->prefix() . "accounting_bookkeeping"; |
||
| 483 | $sql .= " WHERE entity IN (" . getEntity('bookkeeping', 0) . ")"; |
||
| 484 | $sql .= " AND doc_date >= '" . $this->db->idate($datestart) . "' and doc_date <= '" . $this->db->idate($dateend) . "'"; |
||
| 485 | |||
| 486 | $resql = $this->db->query($sql); |
||
| 487 | if ($resql) { |
||
| 488 | $obj = $this->db->fetch_object($resql); |
||
| 489 | $nb = $obj->nb; |
||
| 490 | } else { |
||
| 491 | dol_print_error($this->db); |
||
| 492 | } |
||
| 493 | |||
| 494 | return $nb; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return the number of movements by fiscal year |
||
| 499 | * |
||
| 500 | * @param int|string $datestart Date start to scan |
||
| 501 | * @param int|string $dateend Date end to scan |
||
| 502 | * @return string Number of movements |
||
| 503 | */ |
||
| 504 | public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '') |
||
| 529 | } |
||
| 530 | } |
||
| 531 |