dolibarr 25.0.0-alpha
fiscalyear.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2020 OScss-Shop <support@oscss-shop.fr>
4 * Copyright (C) 2023-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
33{
37 public $element = 'fiscalyear';
38
42 public $picto = 'calendar';
43
47 public $table_element = 'accounting_fiscalyear';
48
52 public $table_element_line = '';
53
57 public $fk_element = '';
58
62 public $rowid;
63
67 public $label;
68
74 public $date_start;
75
81 public $date_end;
82
88 public $datec;
89
95 public $statut;
96
100 public $status;
101
105 public $entity;
106
107
108 const STATUS_OPEN = 0;
109 const STATUS_CLOSED = 1;
110
111
117 public function __construct(DoliDB $db)
118 {
119 $this->db = $db;
120
121 $this->ismultientitymanaged = 1;
122 }
123
130 public function create($user)
131 {
132 global $conf;
133
134 $now = dol_now();
135
136 // Check for date overlaps with existing fiscal years
137 $checkresult = $this->checkOverlap();
138 if ($checkresult < 0) {
139 return -5; // Overlap error detected
140 }
141
142 $this->db->begin();
143
144 $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
145 $sql .= "label";
146 $sql .= ", date_start";
147 $sql .= ", date_end";
148 $sql .= ", statut";
149 $sql .= ", entity";
150 $sql .= ", datec";
151 $sql .= ", fk_user_author";
152 $sql .= ") VALUES (";
153 $sql .= " '".$this->db->escape($this->label)."'";
154 $sql .= ", '".$this->db->idate($this->date_start)."'";
155 $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
156 $sql .= ", 0";
157 $sql .= ", ".((int) $conf->entity);
158 $sql .= ", '".$this->db->idate($now)."'";
159 $sql .= ", ".((int) $user->id);
160 $sql .= ")";
161
162 dol_syslog(get_class($this)."::create", LOG_DEBUG);
163 $result = $this->db->query($sql);
164 if ($result) {
165 $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
166
167 // Call trigger
168 $triggerres = $this->call_trigger('FISCALYEAR_CREATE', $user);
169 if ($triggerres < 0) {
170 $this->db->rollback();
171 return -1;
172 }
173 // End call triggers
174
175 $this->db->commit();
176 return $this->id;
177 } else {
178 $this->error = $this->db->lasterror()." sql=".$sql;
179 $this->db->rollback();
180 return -1;
181 }
182 }
183
190 public function update($user)
191 {
192 // Check parameters
193 if (empty($this->date_start) && empty($this->date_end)) {
194 $this->error = 'ErrorBadParameter';
195 return -1;
196 }
197
198 // Check for date overlaps with existing fiscal years
199 $checkresult = $this->checkOverlap();
200 if ($checkresult < 0) {
201 return -5; // Overlap error detected
202 }
203
204 $this->db->begin();
205
206 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
207 $sql .= " SET label = '".$this->db->escape($this->label)."'";
208 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
209 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
210 $sql .= ", statut = '".$this->db->escape($this->status ? (string) $this->status : '0')."'";
211 $sql .= ", fk_user_modif = ".((int) $user->id);
212 $sql .= " WHERE rowid = ".((int) $this->id);
213
214 dol_syslog(get_class($this)."::update", LOG_DEBUG);
215 $result = $this->db->query($sql);
216 if ($result) {
217 // Call trigger
218 $triggerres = $this->call_trigger('FISCALYEAR_MODIFY', $user);
219 if ($triggerres < 0) {
220 $this->db->rollback();
221 return -1;
222 }
223 // End call triggers
224
225 $this->db->commit();
226 return 1;
227 } else {
228 $this->error = $this->db->lasterror();
229 dol_syslog($this->error, LOG_ERR);
230 $this->db->rollback();
231 return -1;
232 }
233 }
234
242 public function fetch($idorref, $label = '')
243 {
244 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
245 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
246 if ($label) {
247 $sql .= " WHERE label = '".$this->db->escape($label)."'";
248 } else {
249 $sql .= " WHERE rowid = ".((int) $idorref);
250 }
251
252 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
253 $result = $this->db->query($sql);
254 if ($result) {
255 $obj = $this->db->fetch_object($result);
256
257 $this->id = $obj->rowid;
258 $this->ref = $obj->rowid;
259 $this->label = $obj->label;
260 $this->date_start = $this->db->jdate($obj->date_start);
261 $this->date_end = $this->db->jdate($obj->date_end);
262 $this->statut = $obj->status;
263 $this->status = $obj->status;
264
265 return 1;
266 } else {
267 $this->error = $this->db->lasterror();
268 return -1;
269 }
270 }
271
278 public function delete($user)
279 {
280 $this->db->begin();
281
282 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear";
283 $sql .= " WHERE rowid = ".((int) $this->id);
284
285 $result = $this->db->query($sql);
286 if ($result) {
287 // Call trigger
288 $triggerres = $this->call_trigger('FISCALYEAR_DELETE', $user);
289 if ($triggerres < 0) {
290 $this->db->rollback();
291 return -1;
292 }
293 // End call triggers
294
295 $this->db->commit();
296 return 1;
297 } else {
298 $this->error = $this->db->lasterror();
299 $this->db->rollback();
300 return -1;
301 }
302 }
303
309 public function checkOverlap()
310 {
311 global $conf;
312
313 // Get entity value
314 $entity = (!empty($this->entity) ? $this->entity : $conf->entity);
315
316 // Query to checks if any existing fiscal year overlaps with the current date range
317 $sql = "SELECT label";
318 $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear";
319 $sql .= " WHERE entity = " . ((int) $entity);
320 $sql .= " AND date_start <= '" . $this->db->idate($this->date_end) . "'";
321 $sql .= " AND date_end >= '" . $this->db->idate($this->date_start) . "'";
322
323 // Exclude current fiscal year when updating
324 if (!empty($this->id)) {
325 $sql .= " AND rowid != " . ((int) $this->id);
326 }
327
328 dol_syslog(get_class($this) . "::checkOverlap", LOG_DEBUG);
329
330 $result = $this->db->query($sql);
331 if ($result) {
332 if ($this->db->num_rows($result) > 0) {
333 $obj = $this->db->fetch_object($result);
334 $this->error = 'ErrorFiscalYearOverlapWithFiscalYear';
335 $this->errors[] = $obj->label;
336 return -1;
337 }
338 return 1; // No overlap found
339 } else {
340 $this->error = $this->db->lasterror();
341 return -2;
342 }
343 }
344
351 public function getTooltipContentArray($params)
352 {
353 global $langs;
354
355 $langs->load('compta');
356
357 $datas = [];
358 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
359 if (isset($this->status)) {
360 $datas['picto'] .= ' '.$this->getLibStatut(5);
361 }
362 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
363 if (isset($this->date_start)) {
364 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
365 }
366 if (isset($this->date_start)) {
367 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
368 }
369
370 return $datas;
371 }
372
381 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
382 {
383 global $conf, $langs, $user;
384
385 if (empty($this->ref)) {
386 $this->ref = (string) $this->id;
387 }
388
389 if (!empty($conf->dol_no_mouse_hover)) {
390 $notooltip = 1; // Force disable tooltips
391 }
392 $option = '';
393 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
394 $option = 'nolink';
395 }
396 $result = '';
397 $params = [
398 'id' => $this->id,
399 'objecttype' => $this->element,
400 'option' => $option,
401 'nofetch' => 1,
402 ];
403 $classfortooltip = 'classfortooltip';
404 $dataparams = '';
405 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
406 $classfortooltip = 'classforajaxtooltip';
407 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
408 $label = 'ToComplete';
409 } else {
410 $label = implode($this->getTooltipContentArray($params));
411 }
412 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
413
414 if ($option !== 'nolink') {
415 // Add param to save lastsearch_values or not
416 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
417 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
418 $add_save_lastsearch_values = 1;
419 }
420 if ($add_save_lastsearch_values) {
421 $url .= '&save_lastsearch_values=1';
422 }
423 }
424
425 $linkclose = '';
426 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
427 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
428 $label = $langs->trans("FiscalPeriod");
429 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
430 }
431 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
432 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
433 }
434
435 $linkstart = '<a href="'.$url.'"';
436 $linkstart .= $linkclose.'>';
437 $linkend = '</a>';
438
439 if ($option === 'nolink') {
440 $linkstart = '';
441 $linkend = '';
442 }
443
444 $result .= $linkstart;
445 if ($withpicto) {
446 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
447 }
448 if ($withpicto != 2) {
449 $result .= $this->ref;
450 }
451 $result .= $linkend;
452
453 return $result;
454 }
455
462 public function getLibStatut($mode = 0)
463 {
464 return $this->LibStatut($this->status, $mode);
465 }
466
467 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
475 public function LibStatut($status, $mode = 0)
476 {
477 // phpcs:enable
478 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
479 global $langs;
480
481 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpened');
482 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosed');
483 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpenedShort');
484 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosedShort');
485 }
486
487 $statusType = 'status4';
488 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
489 if ($status == self::STATUS_CLOSED) {
490 $statusType = 'status6';
491 }
492
493 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
494 }
495
502 public function info($id)
503 {
504 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
505 $sql .= " fy.tms as datem";
506 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
507 $sql .= " WHERE fy.rowid = ".((int) $id);
508
509 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
510 $result = $this->db->query($sql);
511
512 if ($result) {
513 if ($this->db->num_rows($result)) {
514 $obj = $this->db->fetch_object($result);
515
516 $this->id = $obj->rowid;
517
518 $this->user_creation_id = $obj->fk_user_author;
519 $this->user_modification_id = $obj->fk_user_modif;
520 $this->date_creation = $this->db->jdate($obj->datec);
521 $this->date_modification = $this->db->jdate($obj->datem);
522 }
523 $this->db->free($result);
524 } else {
525 dol_print_error($this->db);
526 }
527 }
528
536 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
537 {
538 if (empty($datestart)) {
539 $datestart = $this->date_start;
540 }
541 if (empty($dateend)) {
542 $dateend = $this->date_end;
543 }
544
545 $sql = "SELECT count(DISTINCT piece_num) as nb";
546 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
547 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
548 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
549
550 $nb = 0;
551 $resql = $this->db->query($sql);
552 if ($resql) {
553 $obj = $this->db->fetch_object($resql);
554 $nb = (int) $obj->nb;
555 } else {
556 dol_print_error($this->db);
557 }
558
559 return $nb;
560 }
561
569 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
570 {
571 if (empty($datestart)) {
572 $datestart = $this->date_start;
573 }
574 if (empty($dateend)) {
575 $dateend = $this->date_end;
576 }
577
578 $sql = "SELECT count(rowid) as nb";
579 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
580 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
581 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
582
583 $nb = 0;
584 $resql = $this->db->query($sql);
585 if ($resql) {
586 $obj = $this->db->fetch_object($resql);
587 $nb = (int) $obj->nb;
588 } else {
589 dol_print_error($this->db);
590 }
591
592 return $nb;
593 }
594}
$object ref
Definition info.php:90
Class to manage Dolibarr database access.
Class to manage fiscal year.
getTooltipContentArray($params)
getTooltipContentArray
create($user)
Create object in database.
getAccountancyEntriesByFiscalYear($datestart='', $dateend='')
Return the number of entries by fiscal year.
getAccountancyMovementsByFiscalYear($datestart='', $dateend='')
Return the number of movements by fiscal year.
update($user)
Update record.
__construct(DoliDB $db)
Constructor.
info($id)
Information on record.
getLibStatut($mode=0)
Give a label from a status.
getNomUrl($withpicto=0, $notooltip=0, $save_lastsearch_value=-1)
Return clickable link of object (with eventually picto)
LibStatut($status, $mode=0)
Give a label from a status.
checkOverlap()
Check if fiscal year dates overlap with existing fiscal years.
fetch($idorref, $label='')
Load an object from database.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.