dolibarr 18.0.6
fiscalyear.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (C) 2020 OScss-Shop <support@oscss-shop.fr>
4 * Copyright (C) 2023 Frédéric France <frederic.france@netlogic.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 = 'technic';
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 public $statuts = array();
113 public $statuts_short = array();
114
115 const STATUS_OPEN = 0;
116 const STATUS_CLOSED = 1;
117
118
124 public function __construct(DoliDB $db)
125 {
126 global $langs;
127
128 $this->db = $db;
129
130 $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
131 $this->statuts = array(0 => 'Opened', 1 => 'Closed');
132 }
133
140 public function create($user)
141 {
142 global $conf;
143
144 $error = 0;
145
146 $now = dol_now();
147
148 $this->db->begin();
149
150 $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
151 $sql .= "label";
152 $sql .= ", date_start";
153 $sql .= ", date_end";
154 $sql .= ", statut";
155 $sql .= ", entity";
156 $sql .= ", datec";
157 $sql .= ", fk_user_author";
158 $sql .= ") VALUES (";
159 $sql .= " '".$this->db->escape($this->label)."'";
160 $sql .= ", '".$this->db->idate($this->date_start)."'";
161 $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
162 $sql .= ", 0";
163 $sql .= ", ".((int) $conf->entity);
164 $sql .= ", '".$this->db->idate($now)."'";
165 $sql .= ", ".((int) $user->id);
166 $sql .= ")";
167
168 dol_syslog(get_class($this)."::create", LOG_DEBUG);
169 $result = $this->db->query($sql);
170 if ($result) {
171 $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
172
173 $result = $this->update($user);
174 if ($result > 0) {
175 $this->db->commit();
176 return $this->id;
177 } else {
178 $this->error = $this->db->lasterror();
179 $this->db->rollback();
180 return $result;
181 }
182 } else {
183 $this->error = $this->db->lasterror()." sql=".$sql;
184 $this->db->rollback();
185 return -1;
186 }
187 }
188
195 public function update($user)
196 {
197 global $langs;
198
199 // Check parameters
200 if (empty($this->date_start) && empty($this->date_end)) {
201 $this->error = 'ErrorBadParameter';
202 return -1;
203 }
204
205 $this->db->begin();
206
207 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
208 $sql .= " SET label = '".$this->db->escape($this->label)."'";
209 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
210 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
211 $sql .= ", statut = '".$this->db->escape($this->statut ? $this->statut : 0)."'";
212 $sql .= ", fk_user_modif = ".((int) $user->id);
213 $sql .= " WHERE rowid = ".((int) $this->id);
214
215 dol_syslog(get_class($this)."::update", LOG_DEBUG);
216 $result = $this->db->query($sql);
217 if ($result) {
218 $this->db->commit();
219 return 1;
220 } else {
221 $this->error = $this->db->lasterror();
222 dol_syslog($this->error, LOG_ERR);
223 $this->db->rollback();
224 return -1;
225 }
226 }
227
234 public function fetch($id)
235 {
236 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
237 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
238 $sql .= " WHERE rowid = ".((int) $id);
239
240 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
241 $result = $this->db->query($sql);
242 if ($result) {
243 $obj = $this->db->fetch_object($result);
244
245 $this->id = $obj->rowid;
246 $this->ref = $obj->rowid;
247 $this->date_start = $this->db->jdate($obj->date_start);
248 $this->date_end = $this->db->jdate($obj->date_end);
249 $this->label = $obj->label;
250 $this->statut = $obj->status;
251 $this->status = $obj->status;
252
253 return 1;
254 } else {
255 $this->error = $this->db->lasterror();
256 return -1;
257 }
258 }
259
266 public function delete($id)
267 {
268 $this->db->begin();
269
270 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear WHERE rowid = ".((int) $id);
271
272 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
273 $result = $this->db->query($sql);
274 if ($result) {
275 $this->db->commit();
276 return 1;
277 } else {
278 $this->error = $this->db->lasterror();
279 $this->db->rollback();
280 return -1;
281 }
282 }
283
291 public function getTooltipContentArray($params)
292 {
293 global $langs;
294
295 $langs->load('compta');
296
297 $datas = [];
298 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
299 if (isset($this->status)) {
300 $datas['picto'] .= ' '.$this->getLibStatut(5);
301 }
302 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
303 if (isset($this->date_start)) {
304 $datas['date_start'] .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
305 }
306 if (isset($this->date_start)) {
307 $datas['date_end'] .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
308 }
309
310 return $datas;
311 }
312
321 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
322 {
323 global $conf, $langs, $user;
324
325 if (empty($this->ref)) {
326 $this->ref = $this->id;
327 }
328
329 if (!empty($conf->dol_no_mouse_hover)) {
330 $notooltip = 1; // Force disable tooltips
331 }
332 $option = '';
333 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
334 $option = 'nolink';
335 }
336 $result = '';
337 $params = [
338 'id' => $this->id,
339 'objecttype' => $this->element,
340 'option', $option,
341 'nofetch' => 1,
342 ];
343 $classfortooltip = 'classfortooltip';
344 $dataparams = '';
345 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
346 $classfortooltip = 'classforajaxtooltip';
347 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
348 $label = 'ToComplete';
349 } else {
350 $label = implode($this->getTooltipContentArray($params));
351 }
352 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
353
354 if ($option !== 'nolink') {
355 // Add param to save lastsearch_values or not
356 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
357 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
358 $add_save_lastsearch_values = 1;
359 }
360 if ($add_save_lastsearch_values) {
361 $url .= '&save_lastsearch_values=1';
362 }
363 }
364
365 $linkclose = '';
366 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
367 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
368 $label = $langs->trans("FiscalYear");
369 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
370 }
371 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
372 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
373 }
374
375 $linkstart = '<a href="'.$url.'"';
376 $linkstart .= $linkclose.'>';
377 $linkend = '</a>';
378
379 if ($option === 'nolink') {
380 $linkstart = '';
381 $linkend = '';
382 }
383
384 $result .= $linkstart;
385 if ($withpicto) {
386 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
387 }
388 if ($withpicto != 2) {
389 $result .= $this->ref;
390 }
391 $result .= $linkend;
392
393 return $result;
394 }
395
402 public function getLibStatut($mode = 0)
403 {
404 return $this->LibStatut($this->status, $mode);
405 }
406
407 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
415 public function LibStatut($status, $mode = 0)
416 {
417 // phpcs:enable
418 global $langs;
419
420 if ($mode == 0) {
421 return $langs->trans($this->statuts[$status]);
422 } elseif ($mode == 1) {
423 return $langs->trans($this->statuts_short[$status]);
424 } elseif ($mode == 2) {
425 if ($status == 0) {
426 return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts_short[$status]);
427 } elseif ($status == 1) {
428 return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts_short[$status]);
429 }
430 } elseif ($mode == 3) {
431 if ($status == 0 && !empty($this->statuts_short[$status])) {
432 return img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
433 } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
434 return img_picto($langs->trans($this->statuts_short[$status]), 'statut8');
435 }
436 } elseif ($mode == 4) {
437 if ($status == 0 && !empty($this->statuts_short[$status])) {
438 return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
439 } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
440 return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
441 }
442 } elseif ($mode == 5) {
443 if ($status == 0 && !empty($this->statuts_short[$status])) {
444 return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
445 } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
446 return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
447 }
448 }
449 return "";
450 }
451
458 public function info($id)
459 {
460 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
461 $sql .= " fy.tms as datem";
462 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
463 $sql .= " WHERE fy.rowid = ".((int) $id);
464
465 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
466 $result = $this->db->query($sql);
467
468 if ($result) {
469 if ($this->db->num_rows($result)) {
470 $obj = $this->db->fetch_object($result);
471 $this->id = $obj->rowid;
472 $this->user_creation_id = $obj->fk_user_author;
473 $this->user_modification_id = $obj->fk_user_modif;
474 $this->date_creation = $this->db->jdate($obj->datec);
475 $this->date_modification = $this->db->jdate($obj->datem);
476 }
477 $this->db->free($result);
478 } else {
479 dol_print_error($this->db);
480 }
481 }
482
490 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
491 {
492 global $conf;
493
494 if (empty($datestart)) {
495 $datestart = $this->date_start;
496 }
497 if (empty($dateend)) {
498 $dateend = $this->date_end;
499 }
500
501 $sql = "SELECT count(DISTINCT piece_num) as nb";
502 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
503 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
504 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
505
506 $resql = $this->db->query($sql);
507 if ($resql) {
508 $obj = $this->db->fetch_object($resql);
509 $nb = $obj->nb;
510 } else {
511 dol_print_error($this->db);
512 }
513
514 return $nb;
515 }
516
524 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
525 {
526 global $conf;
527
528 if (empty($datestart)) {
529 $datestart = $this->date_start;
530 }
531 if (empty($dateend)) {
532 $dateend = $this->date_end;
533 }
534
535 $sql = "SELECT count(rowid) as nb";
536 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
537 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
538 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
539
540 $resql = $this->db->query($sql);
541 if ($resql) {
542 $obj = $this->db->fetch_object($resql);
543 $nb = $obj->nb;
544 } else {
545 dol_print_error($this->db);
546 }
547
548 return $nb;
549 }
550}
$object ref
Definition info.php:78
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.
print $langs trans("Ref").' m m m statut
Definition index.php:152
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 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)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.