dolibarr 19.0.4
fiscalyear.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2020 OScss-Shop <support@oscss-shop.fr>
4 * Copyright (C) 2023 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
32{
36 public $element = 'fiscalyear';
37
41 public $picto = 'calendar';
42
46 public $table_element = 'accounting_fiscalyear';
47
51 public $table_element_line = '';
52
56 public $fk_element = '';
57
62 public $ismultientitymanaged = 1;
63
67 public $rowid;
68
72 public $label;
73
79 public $date_start;
80
86 public $date_end;
87
93 public $datec;
94
100 public $statut;
101
105 public $status;
106
110 public $entity;
111
112
113 const STATUS_OPEN = 0;
114 const STATUS_CLOSED = 1;
115
116
122 public function __construct(DoliDB $db)
123 {
124 $this->db = $db;
125 }
126
133 public function create($user)
134 {
135 global $conf;
136
137 $error = 0;
138
139 $now = dol_now();
140
141 $this->db->begin();
142
143 $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
144 $sql .= "label";
145 $sql .= ", date_start";
146 $sql .= ", date_end";
147 $sql .= ", statut";
148 $sql .= ", entity";
149 $sql .= ", datec";
150 $sql .= ", fk_user_author";
151 $sql .= ") VALUES (";
152 $sql .= " '".$this->db->escape($this->label)."'";
153 $sql .= ", '".$this->db->idate($this->date_start)."'";
154 $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
155 $sql .= ", 0";
156 $sql .= ", ".((int) $conf->entity);
157 $sql .= ", '".$this->db->idate($now)."'";
158 $sql .= ", ".((int) $user->id);
159 $sql .= ")";
160
161 dol_syslog(get_class($this)."::create", LOG_DEBUG);
162 $result = $this->db->query($sql);
163 if ($result) {
164 $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
165
166 $result = $this->update($user);
167 if ($result > 0) {
168 $this->db->commit();
169 return $this->id;
170 } else {
171 $this->error = $this->db->lasterror();
172 $this->db->rollback();
173 return $result;
174 }
175 } else {
176 $this->error = $this->db->lasterror()." sql=".$sql;
177 $this->db->rollback();
178 return -1;
179 }
180 }
181
188 public function update($user)
189 {
190 // Check parameters
191 if (empty($this->date_start) && empty($this->date_end)) {
192 $this->error = 'ErrorBadParameter';
193 return -1;
194 }
195
196 $this->db->begin();
197
198 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
199 $sql .= " SET label = '".$this->db->escape($this->label)."'";
200 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
201 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
202 $sql .= ", statut = '".$this->db->escape($this->status ? $this->status : 0)."'";
203 $sql .= ", fk_user_modif = ".((int) $user->id);
204 $sql .= " WHERE rowid = ".((int) $this->id);
205
206 dol_syslog(get_class($this)."::update", LOG_DEBUG);
207 $result = $this->db->query($sql);
208 if ($result) {
209 $this->db->commit();
210 return 1;
211 } else {
212 $this->error = $this->db->lasterror();
213 dol_syslog($this->error, LOG_ERR);
214 $this->db->rollback();
215 return -1;
216 }
217 }
218
225 public function fetch($id)
226 {
227 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
228 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
229 $sql .= " WHERE rowid = ".((int) $id);
230
231 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
232 $result = $this->db->query($sql);
233 if ($result) {
234 $obj = $this->db->fetch_object($result);
235
236 $this->id = $obj->rowid;
237 $this->ref = $obj->rowid;
238 $this->date_start = $this->db->jdate($obj->date_start);
239 $this->date_end = $this->db->jdate($obj->date_end);
240 $this->label = $obj->label;
241 $this->statut = $obj->status;
242 $this->status = $obj->status;
243
244 return 1;
245 } else {
246 $this->error = $this->db->lasterror();
247 return -1;
248 }
249 }
250
257 public function delete($id)
258 {
259 $this->db->begin();
260
261 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear WHERE rowid = ".((int) $id);
262
263 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
264 $result = $this->db->query($sql);
265 if ($result) {
266 $this->db->commit();
267 return 1;
268 } else {
269 $this->error = $this->db->lasterror();
270 $this->db->rollback();
271 return -1;
272 }
273 }
274
282 public function getTooltipContentArray($params)
283 {
284 global $langs;
285
286 $langs->load('compta');
287
288 $datas = [];
289 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
290 if (isset($this->status)) {
291 $datas['picto'] .= ' '.$this->getLibStatut(5);
292 }
293 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
294 if (isset($this->date_start)) {
295 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
296 }
297 if (isset($this->date_start)) {
298 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
299 }
300
301 return $datas;
302 }
303
312 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
313 {
314 global $conf, $langs, $user;
315
316 if (empty($this->ref)) {
317 $this->ref = $this->id;
318 }
319
320 if (!empty($conf->dol_no_mouse_hover)) {
321 $notooltip = 1; // Force disable tooltips
322 }
323 $option = '';
324 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
325 $option = 'nolink';
326 }
327 $result = '';
328 $params = [
329 'id' => $this->id,
330 'objecttype' => $this->element,
331 'option', $option,
332 'nofetch' => 1,
333 ];
334 $classfortooltip = 'classfortooltip';
335 $dataparams = '';
336 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
337 $classfortooltip = 'classforajaxtooltip';
338 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
339 $label = 'ToComplete';
340 } else {
341 $label = implode($this->getTooltipContentArray($params));
342 }
343 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
344
345 if ($option !== 'nolink') {
346 // Add param to save lastsearch_values or not
347 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
348 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
349 $add_save_lastsearch_values = 1;
350 }
351 if ($add_save_lastsearch_values) {
352 $url .= '&save_lastsearch_values=1';
353 }
354 }
355
356 $linkclose = '';
357 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
358 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
359 $label = $langs->trans("FiscalYear");
360 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
361 }
362 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
363 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
364 }
365
366 $linkstart = '<a href="'.$url.'"';
367 $linkstart .= $linkclose.'>';
368 $linkend = '</a>';
369
370 if ($option === 'nolink') {
371 $linkstart = '';
372 $linkend = '';
373 }
374
375 $result .= $linkstart;
376 if ($withpicto) {
377 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
378 }
379 if ($withpicto != 2) {
380 $result .= $this->ref;
381 }
382 $result .= $linkend;
383
384 return $result;
385 }
386
393 public function getLibStatut($mode = 0)
394 {
395 return $this->LibStatut($this->status, $mode);
396 }
397
398 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
406 public function LibStatut($status, $mode = 0)
407 {
408 // phpcs:enable
409 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
410 global $langs;
411
412 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpened');
413 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosed');
414 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('FiscalYearOpenedShort');
415 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('FiscalYearClosedShort');
416 }
417
418 $statusType = 'status4';
419 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
420 if ($status == self::STATUS_CLOSED) {
421 $statusType = 'status6';
422 }
423
424 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
425 }
426
433 public function info($id)
434 {
435 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
436 $sql .= " fy.tms as datem";
437 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
438 $sql .= " WHERE fy.rowid = ".((int) $id);
439
440 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
441 $result = $this->db->query($sql);
442
443 if ($result) {
444 if ($this->db->num_rows($result)) {
445 $obj = $this->db->fetch_object($result);
446
447 $this->id = $obj->rowid;
448
449 $this->user_creation_id = $obj->fk_user_author;
450 $this->user_modification_id = $obj->fk_user_modif;
451 $this->date_creation = $this->db->jdate($obj->datec);
452 $this->date_modification = $this->db->jdate($obj->datem);
453 }
454 $this->db->free($result);
455 } else {
456 dol_print_error($this->db);
457 }
458 }
459
467 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
468 {
469 global $conf;
470
471 if (empty($datestart)) {
472 $datestart = $this->date_start;
473 }
474 if (empty($dateend)) {
475 $dateend = $this->date_end;
476 }
477
478 $sql = "SELECT count(DISTINCT piece_num) as nb";
479 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
480 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
481 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
482
483 $resql = $this->db->query($sql);
484 if ($resql) {
485 $obj = $this->db->fetch_object($resql);
486 $nb = $obj->nb;
487 } else {
488 dol_print_error($this->db);
489 }
490
491 return $nb;
492 }
493
501 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
502 {
503 global $conf;
504
505 if (empty($datestart)) {
506 $datestart = $this->date_start;
507 }
508 if (empty($dateend)) {
509 $dateend = $this->date_end;
510 }
511
512 $sql = "SELECT count(rowid) as nb";
513 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
514 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
515 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
516
517 $resql = $this->db->query($sql);
518 if ($resql) {
519 $obj = $this->db->fetch_object($resql);
520 $nb = $obj->nb;
521 } else {
522 dol_print_error($this->db);
523 }
524
525 return $nb;
526 }
527}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
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 clicable link of object (with eventually picto)
LibStatut($status, $mode=0)
Give a label from a status.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926