dolibarr 19.0.3
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 = '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 $this->labelStatusShort = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
127 $this->labelStatus = array(self::STATUS_OPEN => 'Opened', self::STATUS_CLOSED => 'Closed');
128 }
129
136 public function create($user)
137 {
138 global $conf;
139
140 $error = 0;
141
142 $now = dol_now();
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
169 $result = $this->update($user);
170 if ($result > 0) {
171 $this->db->commit();
172 return $this->id;
173 } else {
174 $this->error = $this->db->lasterror();
175 $this->db->rollback();
176 return $result;
177 }
178 } else {
179 $this->error = $this->db->lasterror()." sql=".$sql;
180 $this->db->rollback();
181 return -1;
182 }
183 }
184
191 public function update($user)
192 {
193 // Check parameters
194 if (empty($this->date_start) && empty($this->date_end)) {
195 $this->error = 'ErrorBadParameter';
196 return -1;
197 }
198
199 $this->db->begin();
200
201 $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
202 $sql .= " SET label = '".$this->db->escape($this->label)."'";
203 $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
204 $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
205 $sql .= ", statut = '".$this->db->escape($this->status ? $this->status : 0)."'";
206 $sql .= ", fk_user_modif = ".((int) $user->id);
207 $sql .= " WHERE rowid = ".((int) $this->id);
208
209 dol_syslog(get_class($this)."::update", LOG_DEBUG);
210 $result = $this->db->query($sql);
211 if ($result) {
212 $this->db->commit();
213 return 1;
214 } else {
215 $this->error = $this->db->lasterror();
216 dol_syslog($this->error, LOG_ERR);
217 $this->db->rollback();
218 return -1;
219 }
220 }
221
228 public function fetch($id)
229 {
230 $sql = "SELECT rowid, label, date_start, date_end, statut as status";
231 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
232 $sql .= " WHERE rowid = ".((int) $id);
233
234 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
235 $result = $this->db->query($sql);
236 if ($result) {
237 $obj = $this->db->fetch_object($result);
238
239 $this->id = $obj->rowid;
240 $this->ref = $obj->rowid;
241 $this->date_start = $this->db->jdate($obj->date_start);
242 $this->date_end = $this->db->jdate($obj->date_end);
243 $this->label = $obj->label;
244 $this->statut = $obj->status;
245 $this->status = $obj->status;
246
247 return 1;
248 } else {
249 $this->error = $this->db->lasterror();
250 return -1;
251 }
252 }
253
260 public function delete($id)
261 {
262 $this->db->begin();
263
264 $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear WHERE rowid = ".((int) $id);
265
266 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
267 $result = $this->db->query($sql);
268 if ($result) {
269 $this->db->commit();
270 return 1;
271 } else {
272 $this->error = $this->db->lasterror();
273 $this->db->rollback();
274 return -1;
275 }
276 }
277
285 public function getTooltipContentArray($params)
286 {
287 global $langs;
288
289 $langs->load('compta');
290
291 $datas = [];
292 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
293 if (isset($this->status)) {
294 $datas['picto'] .= ' '.$this->getLibStatut(5);
295 }
296 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
297 if (isset($this->date_start)) {
298 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
299 }
300 if (isset($this->date_start)) {
301 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
302 }
303
304 return $datas;
305 }
306
315 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
316 {
317 global $conf, $langs, $user;
318
319 if (empty($this->ref)) {
320 $this->ref = $this->id;
321 }
322
323 if (!empty($conf->dol_no_mouse_hover)) {
324 $notooltip = 1; // Force disable tooltips
325 }
326 $option = '';
327 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
328 $option = 'nolink';
329 }
330 $result = '';
331 $params = [
332 'id' => $this->id,
333 'objecttype' => $this->element,
334 'option', $option,
335 'nofetch' => 1,
336 ];
337 $classfortooltip = 'classfortooltip';
338 $dataparams = '';
339 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
340 $classfortooltip = 'classforajaxtooltip';
341 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
342 $label = 'ToComplete';
343 } else {
344 $label = implode($this->getTooltipContentArray($params));
345 }
346 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
347
348 if ($option !== 'nolink') {
349 // Add param to save lastsearch_values or not
350 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
351 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
352 $add_save_lastsearch_values = 1;
353 }
354 if ($add_save_lastsearch_values) {
355 $url .= '&save_lastsearch_values=1';
356 }
357 }
358
359 $linkclose = '';
360 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
361 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
362 $label = $langs->trans("FiscalYear");
363 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
364 }
365 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
366 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
367 }
368
369 $linkstart = '<a href="'.$url.'"';
370 $linkstart .= $linkclose.'>';
371 $linkend = '</a>';
372
373 if ($option === 'nolink') {
374 $linkstart = '';
375 $linkend = '';
376 }
377
378 $result .= $linkstart;
379 if ($withpicto) {
380 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
381 }
382 if ($withpicto != 2) {
383 $result .= $this->ref;
384 }
385 $result .= $linkend;
386
387 return $result;
388 }
389
396 public function getLibStatut($mode = 0)
397 {
398 return $this->LibStatut($this->status, $mode);
399 }
400
401 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
409 public function LibStatut($status, $mode = 0)
410 {
411 // phpcs:enable
412 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
413 global $langs;
414 //$langs->load("mymodule@mymodule");
415 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Draft');
416 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Enabled');
417 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
418 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Disabled');
419 }
420
421 $statusType = 'status4';
422 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
423 if ($status == self::STATUS_CLOSED) {
424 $statusType = 'status6';
425 }
426
427 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
428 }
429
436 public function info($id)
437 {
438 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
439 $sql .= " fy.tms as datem";
440 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
441 $sql .= " WHERE fy.rowid = ".((int) $id);
442
443 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
444 $result = $this->db->query($sql);
445
446 if ($result) {
447 if ($this->db->num_rows($result)) {
448 $obj = $this->db->fetch_object($result);
449
450 $this->id = $obj->rowid;
451
452 $this->user_creation_id = $obj->fk_user_author;
453 $this->user_modification_id = $obj->fk_user_modif;
454 $this->date_creation = $this->db->jdate($obj->datec);
455 $this->date_modification = $this->db->jdate($obj->datem);
456 }
457 $this->db->free($result);
458 } else {
459 dol_print_error($this->db);
460 }
461 }
462
470 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
471 {
472 global $conf;
473
474 if (empty($datestart)) {
475 $datestart = $this->date_start;
476 }
477 if (empty($dateend)) {
478 $dateend = $this->date_end;
479 }
480
481 $sql = "SELECT count(DISTINCT piece_num) as nb";
482 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
483 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
484 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
485
486 $resql = $this->db->query($sql);
487 if ($resql) {
488 $obj = $this->db->fetch_object($resql);
489 $nb = $obj->nb;
490 } else {
491 dol_print_error($this->db);
492 }
493
494 return $nb;
495 }
496
504 public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
505 {
506 global $conf;
507
508 if (empty($datestart)) {
509 $datestart = $this->date_start;
510 }
511 if (empty($dateend)) {
512 $dateend = $this->date_end;
513 }
514
515 $sql = "SELECT count(rowid) as nb";
516 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
517 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
518 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
519
520 $resql = $this->db->query($sql);
521 if ($resql) {
522 $obj = $this->db->fetch_object($resql);
523 $nb = $obj->nb;
524 } else {
525 dol_print_error($this->db);
526 }
527
528 return $nb;
529 }
530}
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