dolibarr  13.0.2
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 ".MAIN_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 .= ", ".$conf->entity;
151  $sql .= ", '".$this->db->idate($now)."'";
152  $sql .= ", ".$user->id;
153  $sql .= ")";
154 
155  dol_syslog(get_class($this)."::create", LOG_DEBUG);
156  $result = $this->db->query($sql);
157  if ($result)
158  {
159  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_fiscalyear");
160 
161  $result = $this->update($user);
162  if ($result > 0)
163  {
164  $this->db->commit();
165  return $this->id;
166  } else {
167  $this->error = $this->db->lasterror();
168  $this->db->rollback();
169  return $result;
170  }
171  } else {
172  $this->error = $this->db->lasterror()." sql=".$sql;
173  $this->db->rollback();
174  return -1;
175  }
176  }
177 
184  public function update($user)
185  {
186  global $langs;
187 
188  // Check parameters
189  if (empty($this->date_start) && empty($this->date_end))
190  {
191  $this->error = 'ErrorBadParameter';
192  return -1;
193  }
194 
195  $this->db->begin();
196 
197  $sql = "UPDATE ".MAIN_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->statut ? $this->statut : 0)."'";
202  $sql .= ", fk_user_modif = ".$user->id;
203  $sql .= " WHERE rowid = ".$this->id;
204 
205  dol_syslog(get_class($this)."::update", LOG_DEBUG);
206  $result = $this->db->query($sql);
207  if ($result)
208  {
209  $this->db->commit();
210  return 1;
211  } else {
212  $this->error = $this->db->lasterror();
213  dol_syslog($this->error, LOG_ERR);
214  $this->db->rollback();
215  return -1;
216  }
217  }
218 
225  public function fetch($id)
226  {
227  $sql = "SELECT rowid, label, date_start, date_end, statut";
228  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
229  $sql .= " WHERE rowid = ".$id;
230 
231  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
232  $result = $this->db->query($sql);
233  if ($result)
234  {
235  $obj = $this->db->fetch_object($result);
236 
237  $this->id = $obj->rowid;
238  $this->ref = $obj->rowid;
239  $this->date_start = $this->db->jdate($obj->date_start);
240  $this->date_end = $this->db->jdate($obj->date_end);
241  $this->label = $obj->label;
242  $this->statut = $obj->statut;
243 
244  return 1;
245  } else {
246  $this->error = $this->db->lasterror();
247  return -1;
248  }
249  }
250 
257  public function delete($id)
258  {
259  $this->db->begin();
260 
261  $sql = "DELETE FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE rowid = ".$id;
262 
263  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
264  $result = $this->db->query($sql);
265  if ($result)
266  {
267  $this->db->commit();
268  return 1;
269  } else {
270  $this->error = $this->db->lasterror();
271  $this->db->rollback();
272  return -1;
273  }
274  }
275 
284  public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
285  {
286  global $conf, $langs, $user;
287 
288  if (empty($this->ref)) $this->ref = $this->id;
289 
290  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
291 
292  $result = '';
293 
294  $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
295 
296  if (!$user->rights->accounting->fiscalyear->write)
297  $option = 'nolink';
298 
299  if ($option !== 'nolink')
300  {
301  // Add param to save lastsearch_values or not
302  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
303  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
304  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
305  }
306 
307  if ($short) return $url;
308 
309  $label = '';
310 
311  if ($user->rights->accounting->fiscalyear->write) {
312  $label = '<u>'.$langs->trans("FiscalPeriod").'</u>';
313  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
314  if (isset($this->statut)) {
315  $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
316  }
317  }
318 
319  $linkclose = '';
320  if (empty($notooltip) && $user->rights->accounting->fiscalyear->write)
321  {
322  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
323  {
324  $label = $langs->trans("FiscalYear");
325  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
326  }
327  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
328  $linkclose .= ' class="classfortooltip"';
329  }
330 
331  $linkstart = '<a href="'.$url.'"';
332  $linkstart .= $linkclose.'>';
333  $linkend = '</a>';
334 
335  if ($option === 'nolink') {
336  $linkstart = '';
337  $linkend = '';
338  }
339 
340  $result .= $linkstart;
341  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
342  if ($withpicto != 2) $result .= $this->ref;
343  $result .= $linkend;
344 
345  return $result;
346  }
347 
354  public function getLibStatut($mode = 0)
355  {
356  return $this->LibStatut($this->statut, $mode);
357  }
358 
359  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
367  public function LibStatut($status, $mode = 0)
368  {
369  // phpcs:enable
370  global $langs;
371 
372  if ($mode == 0)
373  {
374  return $langs->trans($this->statuts[$status]);
375  } elseif ($mode == 1)
376  {
377  return $langs->trans($this->statuts_short[$status]);
378  } elseif ($mode == 2)
379  {
380  if ($status == 0) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts_short[$status]);
381  elseif ($status == 1) return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts_short[$status]);
382  } elseif ($mode == 3)
383  {
384  if ($status == 0 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
385  elseif ($status == 1 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut8');
386  } elseif ($mode == 4)
387  {
388  if ($status == 0 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
389  elseif ($status == 1 && !empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
390  } elseif ($mode == 5)
391  {
392  if ($status == 0 && !empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
393  elseif ($status == 1 && !empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
394  }
395  }
396 
403  public function info($id)
404  {
405  $sql = 'SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,';
406  $sql .= ' fy.tms';
407  $sql .= ' FROM '.MAIN_DB_PREFIX.'accounting_fiscalyear as fy';
408  $sql .= ' WHERE fy.rowid = '.$id;
409 
410  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
411  $result = $this->db->query($sql);
412 
413  if ($result)
414  {
415  if ($this->db->num_rows($result))
416  {
417  $obj = $this->db->fetch_object($result);
418  $this->id = $obj->rowid;
419  if ($obj->fk_user_author)
420  {
421  $cuser = new User($this->db);
422  $cuser->fetch($obj->fk_user_author);
423  $this->user_creation = $cuser;
424  }
425  if ($obj->fk_user_modif)
426  {
427  $muser = new User($this->db);
428  $muser->fetch($obj->fk_user_modif);
429  $this->user_modification = $muser;
430  }
431  $this->date_creation = $this->db->jdate($obj->datec);
432  $this->date_modification = $this->db->jdate($obj->tms);
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  if (empty($dateend))
454  $dateend = $this->date_end;
455 
456  $sql = "SELECT count(DISTINCT piece_num) as nb";
457  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
458  $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
459  $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
460 
461  $resql = $this->db->query($sql);
462  if ($resql)
463  {
464  $obj = $this->db->fetch_object($resql);
465  $nb = $obj->nb;
466  } else dol_print_error($this->db);
467 
468  return $nb;
469  }
470 
478  public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
479  {
480  global $conf;
481 
482  if (empty($datestart))
483  $datestart = $this->date_start;
484  if (empty($dateend))
485  $dateend = $this->date_end;
486 
487  $sql = "SELECT count(rowid) as nb";
488  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping ";
489  $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
490  $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
491 
492  $resql = $this->db->query($sql);
493  if ($resql)
494  {
495  $obj = $this->db->fetch_object($resql);
496  $nb = $obj->nb;
497  } else dol_print_error($this->db);
498 
499  return $nb;
500  }
501 }
update($user)
Update record.
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Give a label from a status.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
$conf db
API class for accounts.
Definition: inc.php:54
info($id)
Information on record.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
Class to manage fiscal year.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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.
create($user)
Create object in database.
print $_SERVER["PHP_SELF"]
Edit parameters.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getAccountancyEntriesByFiscalYear($datestart= '', $dateend= '')
Return the number of entries by fiscal year.
fetch($id)
Load an object from database.
getAccountancyMovementsByFiscalYear($datestart= '', $dateend= '')
Return the number of movements by fiscal year.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)