dolibarr  17.0.4
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  *
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
31 class Fiscalyear extends CommonObject
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 
95  public $statut; // 0=open, 1=closed
96 
100  public $entity;
101 
102  public $statuts = array();
103  public $statuts_short = array();
104 
105 
111  public function __construct(DoliDB $db)
112  {
113  global $langs;
114 
115  $this->db = $db;
116 
117  $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
118  $this->statuts = array(0 => 'Opened', 1 => 'Closed');
119  }
120 
127  public function create($user)
128  {
129  global $conf;
130 
131  $error = 0;
132 
133  $now = dol_now();
134 
135  $this->db->begin();
136 
137  $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
138  $sql .= "label";
139  $sql .= ", date_start";
140  $sql .= ", date_end";
141  $sql .= ", statut";
142  $sql .= ", entity";
143  $sql .= ", datec";
144  $sql .= ", fk_user_author";
145  $sql .= ") VALUES (";
146  $sql .= " '".$this->db->escape($this->label)."'";
147  $sql .= ", '".$this->db->idate($this->date_start)."'";
148  $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
149  $sql .= ", 0";
150  $sql .= ", ".((int) $conf->entity);
151  $sql .= ", '".$this->db->idate($now)."'";
152  $sql .= ", ".((int) $user->id);
153  $sql .= ")";
154 
155  dol_syslog(get_class($this)."::create", LOG_DEBUG);
156  $result = $this->db->query($sql);
157  if ($result) {
158  $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
159 
160  $result = $this->update($user);
161  if ($result > 0) {
162  $this->db->commit();
163  return $this->id;
164  } else {
165  $this->error = $this->db->lasterror();
166  $this->db->rollback();
167  return $result;
168  }
169  } else {
170  $this->error = $this->db->lasterror()." sql=".$sql;
171  $this->db->rollback();
172  return -1;
173  }
174  }
175 
182  public function update($user)
183  {
184  global $langs;
185 
186  // Check parameters
187  if (empty($this->date_start) && empty($this->date_end)) {
188  $this->error = 'ErrorBadParameter';
189  return -1;
190  }
191 
192  $this->db->begin();
193 
194  $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
195  $sql .= " SET label = '".$this->db->escape($this->label)."'";
196  $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
197  $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
198  $sql .= ", statut = '".$this->db->escape($this->statut ? $this->statut : 0)."'";
199  $sql .= ", fk_user_modif = ".((int) $user->id);
200  $sql .= " WHERE rowid = ".((int) $this->id);
201 
202  dol_syslog(get_class($this)."::update", LOG_DEBUG);
203  $result = $this->db->query($sql);
204  if ($result) {
205  $this->db->commit();
206  return 1;
207  } else {
208  $this->error = $this->db->lasterror();
209  dol_syslog($this->error, LOG_ERR);
210  $this->db->rollback();
211  return -1;
212  }
213  }
214 
221  public function fetch($id)
222  {
223  $sql = "SELECT rowid, label, date_start, date_end, statut";
224  $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
225  $sql .= " WHERE rowid = ".((int) $id);
226 
227  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
228  $result = $this->db->query($sql);
229  if ($result) {
230  $obj = $this->db->fetch_object($result);
231 
232  $this->id = $obj->rowid;
233  $this->ref = $obj->rowid;
234  $this->date_start = $this->db->jdate($obj->date_start);
235  $this->date_end = $this->db->jdate($obj->date_end);
236  $this->label = $obj->label;
237  $this->statut = $obj->statut;
238 
239  return 1;
240  } else {
241  $this->error = $this->db->lasterror();
242  return -1;
243  }
244  }
245 
252  public function delete($id)
253  {
254  $this->db->begin();
255 
256  $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear WHERE rowid = ".((int) $id);
257 
258  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
259  $result = $this->db->query($sql);
260  if ($result) {
261  $this->db->commit();
262  return 1;
263  } else {
264  $this->error = $this->db->lasterror();
265  $this->db->rollback();
266  return -1;
267  }
268  }
269 
278  public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
279  {
280  global $conf, $langs, $user;
281 
282  if (empty($this->ref)) {
283  $this->ref = $this->id;
284  }
285 
286  if (!empty($conf->dol_no_mouse_hover)) {
287  $notooltip = 1; // Force disable tooltips
288  }
289 
290  $result = '';
291 
292  $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
293 
294  if (empty($user->rights->accounting->fiscalyear->write)) {
295  $option = 'nolink';
296  }
297 
298  if ($option !== 'nolink') {
299  // Add param to save lastsearch_values or not
300  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
301  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
302  $add_save_lastsearch_values = 1;
303  }
304  if ($add_save_lastsearch_values) {
305  $url .= '&save_lastsearch_values=1';
306  }
307  }
308 
309  if ($short) {
310  return $url;
311  }
312 
313  $label = '';
314 
315  if ($user->rights->accounting->fiscalyear->write) {
316  $label = '<u>'.$langs->trans("FiscalPeriod").'</u>';
317  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
318  if (isset($this->statut)) {
319  $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
320  }
321  }
322 
323  $linkclose = '';
324  if (empty($notooltip) && $user->rights->accounting->fiscalyear->write) {
325  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
326  $label = $langs->trans("FiscalYear");
327  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
328  }
329  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
330  $linkclose .= ' class="classfortooltip"';
331  }
332 
333  $linkstart = '<a href="'.$url.'"';
334  $linkstart .= $linkclose.'>';
335  $linkend = '</a>';
336 
337  if ($option === 'nolink') {
338  $linkstart = '';
339  $linkend = '';
340  }
341 
342  $result .= $linkstart;
343  if ($withpicto) {
344  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
345  }
346  if ($withpicto != 2) {
347  $result .= $this->ref;
348  }
349  $result .= $linkend;
350 
351  return $result;
352  }
353 
360  public function getLibStatut($mode = 0)
361  {
362  return $this->LibStatut($this->statut, $mode);
363  }
364 
365  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
373  public function LibStatut($status, $mode = 0)
374  {
375  // phpcs:enable
376  global $langs;
377 
378  if ($mode == 0) {
379  return $langs->trans($this->statuts[$status]);
380  } elseif ($mode == 1) {
381  return $langs->trans($this->statuts_short[$status]);
382  } elseif ($mode == 2) {
383  if ($status == 0) {
384  return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts_short[$status]);
385  } elseif ($status == 1) {
386  return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts_short[$status]);
387  }
388  } elseif ($mode == 3) {
389  if ($status == 0 && !empty($this->statuts_short[$status])) {
390  return img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
391  } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
392  return img_picto($langs->trans($this->statuts_short[$status]), 'statut8');
393  }
394  } elseif ($mode == 4) {
395  if ($status == 0 && !empty($this->statuts_short[$status])) {
396  return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
397  } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
398  return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
399  }
400  } elseif ($mode == 5) {
401  if ($status == 0 && !empty($this->statuts_short[$status])) {
402  return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
403  } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
404  return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
405  }
406  }
407  }
408 
415  public function info($id)
416  {
417  $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
418  $sql .= " fy.tms as datem";
419  $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
420  $sql .= " WHERE fy.rowid = ".((int) $id);
421 
422  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
423  $result = $this->db->query($sql);
424 
425  if ($result) {
426  if ($this->db->num_rows($result)) {
427  $obj = $this->db->fetch_object($result);
428  $this->id = $obj->rowid;
429  $this->user_creation_id = $obj->fk_user_author;
430  $this->user_modification_id = $obj->fk_user_modif;
431  $this->date_creation = $this->db->jdate($obj->datec);
432  $this->date_modification = $this->db->jdate($obj->datem);
433  }
434  $this->db->free($result);
435  } else {
436  dol_print_error($this->db);
437  }
438  }
439 
447  public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
448  {
449  global $conf;
450 
451  if (empty($datestart)) {
452  $datestart = $this->date_start;
453  }
454  if (empty($dateend)) {
455  $dateend = $this->date_end;
456  }
457 
458  $sql = "SELECT count(DISTINCT piece_num) as nb";
459  $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
460  $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
461  $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
462 
463  $resql = $this->db->query($sql);
464  if ($resql) {
465  $obj = $this->db->fetch_object($resql);
466  $nb = $obj->nb;
467  } else {
468  dol_print_error($this->db);
469  }
470 
471  return $nb;
472  }
473 
481  public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
482  {
483  global $conf;
484 
485  if (empty($datestart)) {
486  $datestart = $this->date_start;
487  }
488  if (empty($dateend)) {
489  $dateend = $this->date_end;
490  }
491 
492  $sql = "SELECT count(rowid) as nb";
493  $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
494  $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
495  $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
496 
497  $resql = $this->db->query($sql);
498  if ($resql) {
499  $obj = $this->db->fetch_object($resql);
500  $nb = $obj->nb;
501  } else {
502  dol_print_error($this->db);
503  }
504 
505  return $nb;
506  }
507 }
$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.
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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_now($mode='auto')
Return date for now.
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.
$conf db
API class for accounts.
Definition: inc.php:41