dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 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 $this->labelStatusShort = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
123 $this->labelStatus = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
124 }
125
132 public function create($user)
133 {
134 global $conf;
135
136 $error = 0;
137
138 $now = dol_now();
139
140 $this->db->begin();
141
142 $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
143 $sql .= "label";
144 $sql .= ", date_start";
145 $sql .= ", date_end";
146 $sql .= ", statut";
147 $sql .= ", entity";
148 $sql .= ", datec";
149 $sql .= ", fk_user_author";
150 $sql .= ") VALUES (";
151 $sql .= " '".$this->db->escape($this->label)."'";
152 $sql .= ", '".$this->db->idate($this->date_start)."'";
153 $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
154 $sql .= ", 0";
155 $sql .= ", ".((int) $conf->entity);
156 $sql .= ", '".$this->db->idate($now)."'";
157 $sql .= ", ".((int) $user->id);
158 $sql .= ")";
159
160 dol_syslog(get_class($this)."::create", LOG_DEBUG);
161 $result = $this->db->query($sql);
162 if ($result) {
163 $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
164
165 $result = $this->update($user);
166 if ($result > 0) {
167 $this->db->commit();
168 return $this->id;
169 } else {
170 $this->error = $this->db->lasterror();
171 $this->db->rollback();
172 return $result;
173 }
174 } else {
175 $this->error = $this->db->lasterror()." sql=".$sql;
176 $this->db->rollback();
177 return -1;
178 }
179 }
180
187 public function update($user)
188 {
189 // Check parameters
190 if (empty($this->date_start) && empty($this->date_end)) {
191 $this->error = 'ErrorBadParameter';
192 return -1;
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 ? $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
224 public function fetch($id)
225 {
226 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
227 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
228 $sql .= " WHERE rowid = ".((int) $id);
229
230 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
231 $result = $this->db->query($sql);
232 if ($result) {
233 $obj = $this->db->fetch_object($result);
234
235 $this->id = $obj->rowid;
236 $this->ref = $obj->rowid;
237 $this->date_start = $this->db->jdate($obj->date_start);
238 $this->date_end = $this->db->jdate($obj->date_end);
239 $this->label = $obj->label;
240 $this->statut = $obj->status;
241 $this->status = $obj->status;
242
243 return 1;
244 } else {
245 $this->error = $this->db->lasterror();
246 return -1;
247 }
248 }
249
256 public function delete($user)
257 {
258 $this->db->begin();
259
260 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear";
261 $sql .= " WHERE rowid = ".((int) $this->id);
262
263 $result = $this->db->query($sql);
264 if ($result) {
265 $this->db->commit();
266 return 1;
267 } else {
268 $this->error = $this->db->lasterror();
269 $this->db->rollback();
270 return -1;
271 }
272 }
273
280 public function getTooltipContentArray($params)
281 {
282 global $langs;
283
284 $langs->load('compta');
285
286 $datas = [];
287 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
288 if (isset($this->status)) {
289 $datas['picto'] .= ' '.$this->getLibStatut(5);
290 }
291 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
292 if (isset($this->date_start)) {
293 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
294 }
295 if (isset($this->date_start)) {
296 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
297 }
298
299 return $datas;
300 }
301
310 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
311 {
312 global $conf, $langs, $user;
313
314 if (empty($this->ref)) {
315 $this->ref = (string) $this->id;
316 }
317
318 if (!empty($conf->dol_no_mouse_hover)) {
319 $notooltip = 1; // Force disable tooltips
320 }
321 $option = '';
322 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
323 $option = 'nolink';
324 }
325 $result = '';
326 $params = [
327 'id' => $this->id,
328 'objecttype' => $this->element,
329 'option' => $option,
330 'nofetch' => 1,
331 ];
332 $classfortooltip = 'classfortooltip';
333 $dataparams = '';
334 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
335 $classfortooltip = 'classforajaxtooltip';
336 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
337 $label = 'ToComplete';
338 } else {
339 $label = implode($this->getTooltipContentArray($params));
340 }
341 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
342
343 if ($option !== 'nolink') {
344 // Add param to save lastsearch_values or not
345 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
346 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
347 $add_save_lastsearch_values = 1;
348 }
349 if ($add_save_lastsearch_values) {
350 $url .= '&save_lastsearch_values=1';
351 }
352 }
353
354 $linkclose = '';
355 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
356 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
357 $label = $langs->trans("FiscalPeriod");
358 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
359 }
360 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
361 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
362 }
363
364 $linkstart = '<a href="'.$url.'"';
365 $linkstart .= $linkclose.'>';
366 $linkend = '</a>';
367
368 if ($option === 'nolink') {
369 $linkstart = '';
370 $linkend = '';
371 }
372
373 $result .= $linkstart;
374 if ($withpicto) {
375 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
376 }
377 if ($withpicto != 2) {
378 $result .= $this->ref;
379 }
380 $result .= $linkend;
381
382 return $result;
383 }
384
391 public function getLibStatut($mode = 0)
392 {
393 return $this->LibStatut($this->status, $mode);
394 }
395
396 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
404 public function LibStatut($status, $mode = 0)
405 {
406 // phpcs:enable
407 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
408 global $langs;
409 //$langs->load("mymodule@mymodule");
410 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Draft');
411 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Enabled');
412 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
413 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Disabled');
414 }
415
416 $statusType = 'status4';
417 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
418 if ($status == self::STATUS_CLOSED) {
419 $statusType = 'status6';
420 }
421
422 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
423 }
424
431 public function info($id)
432 {
433 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
434 $sql .= " fy.tms as datem";
435 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
436 $sql .= " WHERE fy.rowid = ".((int) $id);
437
438 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
439 $result = $this->db->query($sql);
440
441 if ($result) {
442 if ($this->db->num_rows($result)) {
443 $obj = $this->db->fetch_object($result);
444
445 $this->id = $obj->rowid;
446
447 $this->user_creation_id = $obj->fk_user_author;
448 $this->user_modification_id = $obj->fk_user_modif;
449 $this->date_creation = $this->db->jdate($obj->datec);
450 $this->date_modification = $this->db->jdate($obj->datem);
451 }
452 $this->db->free($result);
453 } else {
454 dol_print_error($this->db);
455 }
456 }
457
465 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
466 {
467 global $conf;
468
469 if (empty($datestart)) {
470 $datestart = $this->date_start;
471 }
472 if (empty($dateend)) {
473 $dateend = $this->date_end;
474 }
475
476 $sql = "SELECT count(DISTINCT piece_num) as nb";
477 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
478 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
479 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
480
481 $nb = 0;
482 $resql = $this->db->query($sql);
483 if ($resql) {
484 $obj = $this->db->fetch_object($resql);
485 $nb = (int) $obj->nb;
486 } else {
487 dol_print_error($this->db);
488 }
489
490 return $nb;
491 }
492
500 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
501 {
502 global $conf;
503
504 if (empty($datestart)) {
505 $datestart = $this->date_start;
506 }
507 if (empty($dateend)) {
508 $dateend = $this->date_end;
509 }
510
511 $sql = "SELECT count(rowid) as nb";
512 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
513 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
514 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
515
516 $nb = 0;
517 $resql = $this->db->query($sql);
518 if ($resql) {
519 $obj = $this->db->fetch_object($resql);
520 $nb = (int) $obj->nb;
521 } else {
522 dol_print_error($this->db);
523 }
524
525 return $nb;
526 }
527}
$object ref
Definition info.php:89
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.
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_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (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