dolibarr 20.0.0
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
281 public function getTooltipContentArray($params)
282 {
283 global $langs;
284
285 $langs->load('compta');
286
287 $datas = [];
288 $datas['picto'] = img_picto('', $this->picto).' <b><u>'.$langs->trans("FiscalPeriod").'</u></b>';
289 if (isset($this->status)) {
290 $datas['picto'] .= ' '.$this->getLibStatut(5);
291 }
292 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
293 if (isset($this->date_start)) {
294 $datas['date_start'] = '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->date_start, 'day');
295 }
296 if (isset($this->date_start)) {
297 $datas['date_end'] = '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->date_end, 'day');
298 }
299
300 return $datas;
301 }
302
311 public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
312 {
313 global $conf, $langs, $user;
314
315 if (empty($this->ref)) {
316 $this->ref = (string) $this->id;
317 }
318
319 if (!empty($conf->dol_no_mouse_hover)) {
320 $notooltip = 1; // Force disable tooltips
321 }
322 $option = '';
323 if (!$user->hasRight('accounting', 'fiscalyear', 'write')) {
324 $option = 'nolink';
325 }
326 $result = '';
327 $params = [
328 'id' => $this->id,
329 'objecttype' => $this->element,
330 'option' => $option,
331 'nofetch' => 1,
332 ];
333 $classfortooltip = 'classfortooltip';
334 $dataparams = '';
335 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
336 $classfortooltip = 'classforajaxtooltip';
337 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
338 $label = 'ToComplete';
339 } else {
340 $label = implode($this->getTooltipContentArray($params));
341 }
342 $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
343
344 if ($option !== 'nolink') {
345 // Add param to save lastsearch_values or not
346 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
347 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
348 $add_save_lastsearch_values = 1;
349 }
350 if ($add_save_lastsearch_values) {
351 $url .= '&save_lastsearch_values=1';
352 }
353 }
354
355 $linkclose = '';
356 if (empty($notooltip) && $user->hasRight('accounting', 'fiscalyear', 'write')) {
357 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
358 $label = $langs->trans("FiscalPeriod");
359 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
360 }
361 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
362 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
363 }
364
365 $linkstart = '<a href="'.$url.'"';
366 $linkstart .= $linkclose.'>';
367 $linkend = '</a>';
368
369 if ($option === 'nolink') {
370 $linkstart = '';
371 $linkend = '';
372 }
373
374 $result .= $linkstart;
375 if ($withpicto) {
376 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
377 }
378 if ($withpicto != 2) {
379 $result .= $this->ref;
380 }
381 $result .= $linkend;
382
383 return $result;
384 }
385
392 public function getLibStatut($mode = 0)
393 {
394 return $this->LibStatut($this->status, $mode);
395 }
396
397 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
405 public function LibStatut($status, $mode = 0)
406 {
407 // phpcs:enable
408 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
409 global $langs;
410 //$langs->load("mymodule@mymodule");
411 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Draft');
412 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Enabled');
413 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
414 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Disabled');
415 }
416
417 $statusType = 'status4';
418 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
419 if ($status == self::STATUS_CLOSED) {
420 $statusType = 'status6';
421 }
422
423 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
424 }
425
432 public function info($id)
433 {
434 $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
435 $sql .= " fy.tms as datem";
436 $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
437 $sql .= " WHERE fy.rowid = ".((int) $id);
438
439 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
440 $result = $this->db->query($sql);
441
442 if ($result) {
443 if ($this->db->num_rows($result)) {
444 $obj = $this->db->fetch_object($result);
445
446 $this->id = $obj->rowid;
447
448 $this->user_creation_id = $obj->fk_user_author;
449 $this->user_modification_id = $obj->fk_user_modif;
450 $this->date_creation = $this->db->jdate($obj->datec);
451 $this->date_modification = $this->db->jdate($obj->datem);
452 }
453 $this->db->free($result);
454 } else {
455 dol_print_error($this->db);
456 }
457 }
458
466 public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
467 {
468 global $conf;
469
470 if (empty($datestart)) {
471 $datestart = $this->date_start;
472 }
473 if (empty($dateend)) {
474 $dateend = $this->date_end;
475 }
476
477 $sql = "SELECT count(DISTINCT piece_num) as nb";
478 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
479 $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
480 $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
481
482 $resql = $this->db->query($sql);
483 if ($resql) {
484 $obj = $this->db->fetch_object($resql);
485 $nb = $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 $resql = $this->db->query($sql);
517 if ($resql) {
518 $obj = $this->db->fetch_object($resql);
519 $nb = $obj->nb;
520 } else {
521 dol_print_error($this->db);
522 }
523
524 return $nb;
525 }
526}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:637
$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.
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).
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 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 TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1991