dolibarr 22.0.5
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-2024 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 $error = 0;
135
136 $now = dol_now();
137
138 // Check for date overlaps with existing fiscal years
139 $checkresult = $this->checkOverlap();
140 if ($checkresult < 0) {
141 return -5; // Overlap error detected
142 }
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 $this->db->commit();
169 return $this->id;
170 } else {
171 $this->error = $this->db->lasterror()." sql=".$sql;
172 $this->db->rollback();
173 return -1;
174 }
175 }
176
183 public function update($user)
184 {
185 // Check parameters
186 if (empty($this->date_start) && empty($this->date_end)) {
187 $this->error = 'ErrorBadParameter';
188 return -1;
189 }
190
191 // Check for date overlaps with existing fiscal years
192 $checkresult = $this->checkOverlap();
193 if ($checkresult < 0) {
194 return -5; // Overlap error detected
195 }
196
197 $this->db->begin();
198
199 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
200 $sql .= " SET label = '".$this->db->escape($this->label)."'";
201 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
202 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
203 $sql .= ", statut = '".$this->db->escape($this->status ? (string) $this->status : '0')."'";
204 $sql .= ", fk_user_modif = ".((int) $user->id);
205 $sql .= " WHERE rowid = ".((int) $this->id);
206
207 dol_syslog(get_class($this)."::update", LOG_DEBUG);
208 $result = $this->db->query($sql);
209 if ($result) {
210 $this->db->commit();
211 return 1;
212 } else {
213 $this->error = $this->db->lasterror();
214 dol_syslog($this->error, LOG_ERR);
215 $this->db->rollback();
216 return -1;
217 }
218 }
219
226 public function fetch($id)
227 {
228 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
229 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
230 $sql .= " WHERE rowid = ".((int) $id);
231
232 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
233 $result = $this->db->query($sql);
234 if ($result) {
235 $obj = $this->db->fetch_object($result);
236
237 $this->id = $obj->rowid;
238 $this->ref = $obj->rowid;
239 $this->date_start = $this->db->jdate($obj->date_start);
240 $this->date_end = $this->db->jdate($obj->date_end);
241 $this->label = $obj->label;
242 $this->statut = $obj->status;
243 $this->status = $obj->status;
244
245 return 1;
246 } else {
247 $this->error = $this->db->lasterror();
248 return -1;
249 }
250 }
251
258 public function delete($user)
259 {
260 $this->db->begin();
261
262 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear";
263 $sql .= " WHERE rowid = ".((int) $this->id);
264
265 $result = $this->db->query($sql);
266 if ($result) {
267 $this->db->commit();
268 return 1;
269 } else {
270 $this->error = $this->db->lasterror();
271 $this->db->rollback();
272 return -1;
273 }
274 }
275
281 public function checkOverlap()
282 {
283 global $conf;
284
285 // Get entity value
286 $entity = (!empty($this->entity) ? $this->entity : $conf->entity);
287
288 // Query to checks if any existing fiscal year overlaps with the current date range
289 $sql = "SELECT label";
290 $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear";
291 $sql .= " WHERE entity = " . ((int) $entity);
292 $sql .= " AND date_start <= '" . $this->db->idate($this->date_end) . "'";
293 $sql .= " AND date_end >= '" . $this->db->idate($this->date_start) . "'";
294
295 // Exclude current fiscal year when updating
296 if (!empty($this->id)) {
297 $sql .= " AND rowid != " . ((int) $this->id);
298 }
299
300 dol_syslog(get_class($this) . "::checkOverlap", LOG_DEBUG);
301
302 $result = $this->db->query($sql);
303 if ($result) {
304 if ($this->db->num_rows($result) > 0) {
305 $obj = $this->db->fetch_object($result);
306 $this->error = 'ErrorFiscalYearOverlapWithFiscalYear';
307 $this->errors[] = $obj->label;
308 return -1;
309 }
310 return 1; // No overlap found
311 } else {
312 $this->error = $this->db->lasterror();
313 return -2;
314 }
315 }
316
323 public function getTooltipContentArray($params)
324 {
325 global $langs;
326
327 $langs->load('compta');
328
329 $datas = [];
330 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
331 if (isset($this->status)) {
332 $datas['picto'] .= ' '.$this->getLibStatut(5);
333 }
334 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
335 if (isset($this->date_start)) {
336 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
337 }
338 if (isset($this->date_start)) {
339 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
340 }
341
342 return $datas;
343 }
344
353 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
354 {
355 global $conf, $langs, $user;
356
357 if (empty($this->ref)) {
358 $this->ref = (string) $this->id;
359 }
360
361 if (!empty($conf->dol_no_mouse_hover)) {
362 $notooltip = 1; // Force disable tooltips
363 }
364 $option = '';
365 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
366 $option = 'nolink';
367 }
368 $result = '';
369 $params = [
370 'id' => $this->id,
371 'objecttype' => $this->element,
372 'option' => $option,
373 'nofetch' => 1,
374 ];
375 $classfortooltip = 'classfortooltip';
376 $dataparams = '';
377 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
378 $classfortooltip = 'classforajaxtooltip';
379 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
380 $label = 'ToComplete';
381 } else {
382 $label = implode($this->getTooltipContentArray($params));
383 }
384 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
385
386 if ($option !== 'nolink') {
387 // Add param to save lastsearch_values or not
388 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
389 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
390 $add_save_lastsearch_values = 1;
391 }
392 if ($add_save_lastsearch_values) {
393 $url .= '&save_lastsearch_values=1';
394 }
395 }
396
397 $linkclose = '';
398 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
399 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
400 $label = $langs->trans("FiscalPeriod");
401 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
402 }
403 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
404 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
405 }
406
407 $linkstart = '<a href="'.$url.'"';
408 $linkstart .= $linkclose.'>';
409 $linkend = '</a>';
410
411 if ($option === 'nolink') {
412 $linkstart = '';
413 $linkend = '';
414 }
415
416 $result .= $linkstart;
417 if ($withpicto) {
418 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
419 }
420 if ($withpicto != 2) {
421 $result .= $this->ref;
422 }
423 $result .= $linkend;
424
425 return $result;
426 }
427
434 public function getLibStatut($mode = 0)
435 {
436 return $this->LibStatut($this->status, $mode);
437 }
438
439 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
447 public function LibStatut($status, $mode = 0)
448 {
449 // phpcs:enable
450 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
451 global $langs;
452
453 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpened');
454 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosed');
455 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpenedShort');
456 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosedShort');
457 }
458
459 $statusType = 'status4';
460 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
461 if ($status == self::STATUS_CLOSED) {
462 $statusType = 'status6';
463 }
464
465 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
466 }
467
474 public function info($id)
475 {
476 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
477 $sql .= " fy.tms as datem";
478 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
479 $sql .= " WHERE fy.rowid = ".((int) $id);
480
481 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
482 $result = $this->db->query($sql);
483
484 if ($result) {
485 if ($this->db->num_rows($result)) {
486 $obj = $this->db->fetch_object($result);
487
488 $this->id = $obj->rowid;
489
490 $this->user_creation_id = $obj->fk_user_author;
491 $this->user_modification_id = $obj->fk_user_modif;
492 $this->date_creation = $this->db->jdate($obj->datec);
493 $this->date_modification = $this->db->jdate($obj->datem);
494 }
495 $this->db->free($result);
496 } else {
497 dol_print_error($this->db);
498 }
499 }
500
508 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
509 {
510 global $conf;
511
512 if (empty($datestart)) {
513 $datestart = $this->date_start;
514 }
515 if (empty($dateend)) {
516 $dateend = $this->date_end;
517 }
518
519 $sql = "SELECT count(DISTINCT piece_num) as nb";
520 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
521 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
522 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
523
524 $nb = 0;
525 $resql = $this->db->query($sql);
526 if ($resql) {
527 $obj = $this->db->fetch_object($resql);
528 $nb = (int) $obj->nb;
529 } else {
530 dol_print_error($this->db);
531 }
532
533 return $nb;
534 }
535
543 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
544 {
545 global $conf;
546
547 if (empty($datestart)) {
548 $datestart = $this->date_start;
549 }
550 if (empty($dateend)) {
551 $dateend = $this->date_end;
552 }
553
554 $sql = "SELECT count(rowid) as nb";
555 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
556 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
557 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
558
559 $nb = 0;
560 $resql = $this->db->query($sql);
561 if ($resql) {
562 $obj = $this->db->fetch_object($resql);
563 $nb = (int) $obj->nb;
564 } else {
565 dol_print_error($this->db);
566 }
567
568 return $nb;
569 }
570}
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage Dolibarr database access.
Class to manage fiscal year.
fetch($id)
Load an object from database.
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.
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:171
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_now($mode='auto')
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)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79