dolibarr 24.0.0-beta
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 $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 $this->db->commit();
167 return $this->id;
168 } else {
169 $this->error = $this->db->lasterror()." sql=".$sql;
170 $this->db->rollback();
171 return -1;
172 }
173 }
174
181 public function update($user)
182 {
183 // Check parameters
184 if (empty($this->date_start) && empty($this->date_end)) {
185 $this->error = 'ErrorBadParameter';
186 return -1;
187 }
188
189 // Check for date overlaps with existing fiscal years
190 $checkresult = $this->checkOverlap();
191 if ($checkresult < 0) {
192 return -5; // Overlap error detected
193 }
194
195 $this->db->begin();
196
197 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
198 $sql .= " SET label = '".$this->db->escape($this->label)."'";
199 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
200 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
201 $sql .= ", statut = '".$this->db->escape($this->status ? (string) $this->status : '0')."'";
202 $sql .= ", fk_user_modif = ".((int) $user->id);
203 $sql .= " WHERE rowid = ".((int) $this->id);
204
205 dol_syslog(get_class($this)."::update", LOG_DEBUG);
206 $result = $this->db->query($sql);
207 if ($result) {
208 $this->db->commit();
209 return 1;
210 } else {
211 $this->error = $this->db->lasterror();
212 dol_syslog($this->error, LOG_ERR);
213 $this->db->rollback();
214 return -1;
215 }
216 }
217
225 public function fetch($idorref, $label = '')
226 {
227 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
228 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
229 if ($label) {
230 $sql .= " WHERE label = '".$this->db->escape($label)."'";
231 } else {
232 $sql .= " WHERE rowid = ".((int) $idorref);
233 }
234
235 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
236 $result = $this->db->query($sql);
237 if ($result) {
238 $obj = $this->db->fetch_object($result);
239
240 $this->id = $obj->rowid;
241 $this->ref = $obj->rowid;
242 $this->label = $obj->label;
243 $this->date_start = $this->db->jdate($obj->date_start);
244 $this->date_end = $this->db->jdate($obj->date_end);
245 $this->statut = $obj->status;
246 $this->status = $obj->status;
247
248 return 1;
249 } else {
250 $this->error = $this->db->lasterror();
251 return -1;
252 }
253 }
254
261 public function delete($user)
262 {
263 $this->db->begin();
264
265 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear";
266 $sql .= " WHERE rowid = ".((int) $this->id);
267
268 $result = $this->db->query($sql);
269 if ($result) {
270 $this->db->commit();
271 return 1;
272 } else {
273 $this->error = $this->db->lasterror();
274 $this->db->rollback();
275 return -1;
276 }
277 }
278
284 public function checkOverlap()
285 {
286 global $conf;
287
288 // Get entity value
289 $entity = (!empty($this->entity) ? $this->entity : $conf->entity);
290
291 // Query to checks if any existing fiscal year overlaps with the current date range
292 $sql = "SELECT label";
293 $sql .= " FROM " . $this->db->prefix() . "accounting_fiscalyear";
294 $sql .= " WHERE entity = " . ((int) $entity);
295 $sql .= " AND date_start <= '" . $this->db->idate($this->date_end) . "'";
296 $sql .= " AND date_end >= '" . $this->db->idate($this->date_start) . "'";
297
298 // Exclude current fiscal year when updating
299 if (!empty($this->id)) {
300 $sql .= " AND rowid != " . ((int) $this->id);
301 }
302
303 dol_syslog(get_class($this) . "::checkOverlap", LOG_DEBUG);
304
305 $result = $this->db->query($sql);
306 if ($result) {
307 if ($this->db->num_rows($result) > 0) {
308 $obj = $this->db->fetch_object($result);
309 $this->error = 'ErrorFiscalYearOverlapWithFiscalYear';
310 $this->errors[] = $obj->label;
311 return -1;
312 }
313 return 1; // No overlap found
314 } else {
315 $this->error = $this->db->lasterror();
316 return -2;
317 }
318 }
319
326 public function getTooltipContentArray($params)
327 {
328 global $langs;
329
330 $langs->load('compta');
331
332 $datas = [];
333 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
334 if (isset($this->status)) {
335 $datas['picto'] .= ' '.$this->getLibStatut(5);
336 }
337 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
338 if (isset($this->date_start)) {
339 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
340 }
341 if (isset($this->date_start)) {
342 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
343 }
344
345 return $datas;
346 }
347
356 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
357 {
358 global $conf, $langs, $user;
359
360 if (empty($this->ref)) {
361 $this->ref = (string) $this->id;
362 }
363
364 if (!empty($conf->dol_no_mouse_hover)) {
365 $notooltip = 1; // Force disable tooltips
366 }
367 $option = '';
368 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
369 $option = 'nolink';
370 }
371 $result = '';
372 $params = [
373 'id' => $this->id,
374 'objecttype' => $this->element,
375 'option' => $option,
376 'nofetch' => 1,
377 ];
378 $classfortooltip = 'classfortooltip';
379 $dataparams = '';
380 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
381 $classfortooltip = 'classforajaxtooltip';
382 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
383 $label = 'ToComplete';
384 } else {
385 $label = implode($this->getTooltipContentArray($params));
386 }
387 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
388
389 if ($option !== 'nolink') {
390 // Add param to save lastsearch_values or not
391 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
392 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
393 $add_save_lastsearch_values = 1;
394 }
395 if ($add_save_lastsearch_values) {
396 $url .= '&save_lastsearch_values=1';
397 }
398 }
399
400 $linkclose = '';
401 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
402 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
403 $label = $langs->trans("FiscalPeriod");
404 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
405 }
406 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
407 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
408 }
409
410 $linkstart = '<a href="'.$url.'"';
411 $linkstart .= $linkclose.'>';
412 $linkend = '</a>';
413
414 if ($option === 'nolink') {
415 $linkstart = '';
416 $linkend = '';
417 }
418
419 $result .= $linkstart;
420 if ($withpicto) {
421 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
422 }
423 if ($withpicto != 2) {
424 $result .= $this->ref;
425 }
426 $result .= $linkend;
427
428 return $result;
429 }
430
437 public function getLibStatut($mode = 0)
438 {
439 return $this->LibStatut($this->status, $mode);
440 }
441
442 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
450 public function LibStatut($status, $mode = 0)
451 {
452 // phpcs:enable
453 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
454 global $langs;
455
456 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpened');
457 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosed');
458 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpenedShort');
459 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosedShort');
460 }
461
462 $statusType = 'status4';
463 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
464 if ($status == self::STATUS_CLOSED) {
465 $statusType = 'status6';
466 }
467
468 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
469 }
470
477 public function info($id)
478 {
479 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
480 $sql .= " fy.tms as datem";
481 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
482 $sql .= " WHERE fy.rowid = ".((int) $id);
483
484 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
485 $result = $this->db->query($sql);
486
487 if ($result) {
488 if ($this->db->num_rows($result)) {
489 $obj = $this->db->fetch_object($result);
490
491 $this->id = $obj->rowid;
492
493 $this->user_creation_id = $obj->fk_user_author;
494 $this->user_modification_id = $obj->fk_user_modif;
495 $this->date_creation = $this->db->jdate($obj->datec);
496 $this->date_modification = $this->db->jdate($obj->datem);
497 }
498 $this->db->free($result);
499 } else {
500 dol_print_error($this->db);
501 }
502 }
503
511 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
512 {
513 if (empty($datestart)) {
514 $datestart = $this->date_start;
515 }
516 if (empty($dateend)) {
517 $dateend = $this->date_end;
518 }
519
520 $sql = "SELECT count(DISTINCT piece_num) as nb";
521 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
522 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
523 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
524
525 $nb = 0;
526 $resql = $this->db->query($sql);
527 if ($resql) {
528 $obj = $this->db->fetch_object($resql);
529 $nb = (int) $obj->nb;
530 } else {
531 dol_print_error($this->db);
532 }
533
534 return $nb;
535 }
536
544 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
545 {
546 if (empty($datestart)) {
547 $datestart = $this->date_start;
548 }
549 if (empty($dateend)) {
550 $dateend = $this->date_end;
551 }
552
553 $sql = "SELECT count(rowid) as nb";
554 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
555 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
556 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
557
558 $nb = 0;
559 $resql = $this->db->query($sql);
560 if ($resql) {
561 $obj = $this->db->fetch_object($resql);
562 $nb = (int) $obj->nb;
563 } else {
564 dol_print_error($this->db);
565 }
566
567 return $nb;
568 }
569}
$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.
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:168
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
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)
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).
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php