dolibarr  9.0.0
fiscalyear.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2014-2017 Alexandre Spangaro <aspangaro@zendsi.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
25 
29 class Fiscalyear extends CommonObject
30 {
34  public $element='fiscalyear';
35 
39  public $table_element='accounting_fiscalyear';
40 
44  public $table_element_line = '';
45 
49  public $fk_element = '';
50 
55  public $ismultientitymanaged = 1;
56 
60  public $rowid;
61 
65  public $label;
66 
67  public $date_start;
68  public $date_end;
69  public $datec;
70  public $statut; // 0=open, 1=closed
71 
75  public $entity;
76 
77  public $statuts=array();
78  public $statuts_short=array();
79 
85  function __construct($db)
86  {
87  $this->db = $db;
88 
89  $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
90  $this->statuts = array(0 => 'Opened', 1 => 'Closed');
91  }
92 
99  function create($user)
100  {
101  global $conf;
102 
103  $error = 0;
104 
105  $now=dol_now();
106 
107  $this->db->begin();
108 
109  $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_fiscalyear (";
110  $sql.= "label";
111  $sql.= ", date_start";
112  $sql.= ", date_end";
113  $sql.= ", statut";
114  $sql.= ", entity";
115  $sql.= ", datec";
116  $sql.= ", fk_user_author";
117  $sql.= ") VALUES (";
118  $sql.= " '".$this->db->escape($this->label)."'";
119  $sql.= ", '".$this->db->idate($this->date_start)."'";
120  $sql.= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'":"null");
121  $sql.= ", 0";
122  $sql.= ", ".$conf->entity;
123  $sql.= ", '".$this->db->idate($now)."'";
124  $sql.= ", ". $user->id;
125  $sql.= ")";
126 
127  dol_syslog(get_class($this)."::create", LOG_DEBUG);
128  $result = $this->db->query($sql);
129  if ($result)
130  {
131  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_fiscalyear");
132 
133  $result=$this->update($user);
134  if ($result > 0)
135  {
136  $this->db->commit();
137  return $this->id;
138  }
139  else
140  {
141  $this->error=$this->db->lasterror();
142  $this->db->rollback();
143  return $result;
144  }
145  }
146  else
147  {
148  $this->error=$this->db->lasterror()." sql=".$sql;
149  $this->db->rollback();
150  return -1;
151  }
152  }
153 
160  function update($user)
161  {
162  global $langs;
163 
164  // Check parameters
165  if (empty($this->date_start) && empty($this->date_end))
166  {
167  $this->error='ErrorBadParameter';
168  return -1;
169  }
170 
171  $this->db->begin();
172 
173  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_fiscalyear";
174  $sql .= " SET label = '".$this->db->escape($this->label)."'";
175  $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
176  $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
177  $sql .= ", statut = '".$this->db->escape($this->statut?$this->statut:0)."'";
178  $sql .= ", datec = " . ($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
179  $sql .= ", fk_user_modif = " . $user->id;
180  $sql .= " WHERE rowid = ".$this->id;
181 
182  dol_syslog(get_class($this)."::update", LOG_DEBUG);
183  $result = $this->db->query($sql);
184  if ($result)
185  {
186  $this->db->commit();
187  return 1;
188  }
189  else
190  {
191  $this->error=$this->db->lasterror();
192  dol_syslog($this->error, LOG_ERR);
193  $this->db->rollback();
194  return -1;
195  }
196  }
197 
204  function fetch($id)
205  {
206  $sql = "SELECT rowid, label, date_start, date_end, statut";
207  $sql.= " FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
208  $sql.= " WHERE rowid = ".$id;
209 
210  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
211  $result = $this->db->query($sql);
212  if ( $result )
213  {
214  $obj = $this->db->fetch_object($result);
215 
216  $this->id = $obj->rowid;
217  $this->ref = $obj->rowid;
218  $this->date_start = $this->db->jdate($obj->date_start);
219  $this->date_end = $this->db->jdate($obj->date_end);
220  $this->label = $obj->label;
221  $this->statut = $obj->statut;
222 
223  return 1;
224  }
225  else
226  {
227  $this->error=$this->db->lasterror();
228  return -1;
229  }
230  }
231 
238  function delete($id)
239  {
240  $this->db->begin();
241 
242  $sql = "DELETE FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE rowid = ".$id;
243 
244  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
245  $result = $this->db->query($sql);
246  if ($result)
247  {
248  $this->db->commit();
249  return 1;
250  }
251  else
252  {
253  $this->error=$this->db->lasterror();
254  $this->db->rollback();
255  return -1;
256  }
257  }
258 
265  function getLibStatut($mode=0)
266  {
267  return $this->LibStatut($this->statut,$mode);
268  }
269 
270  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
278  function LibStatut($statut,$mode=0)
279  {
280  // phpcs:enable
281  global $langs;
282 
283  if ($mode == 0)
284  {
285  return $langs->trans($this->statuts[$statut]);
286  }
287  elseif ($mode == 1)
288  {
289  return $langs->trans($this->statuts_short[$statut]);
290  }
291  elseif ($mode == 2)
292  {
293  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
294  elseif ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts_short[$statut]);
295  }
296  elseif ($mode == 3)
297  {
298  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
299  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8');
300  }
301  elseif ($mode == 4)
302  {
303  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
304  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts[$statut]);
305  }
306  elseif ($mode == 5)
307  {
308  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
309  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
310  }
311  }
312 
319  function info($id)
320  {
321  $sql = 'SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,';
322  $sql.= ' fy.tms';
323  $sql.= ' FROM '.MAIN_DB_PREFIX.'accounting_fiscalyear as fy';
324  $sql.= ' WHERE fy.rowid = '.$id;
325 
326  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
327  $result = $this->db->query($sql);
328 
329  if ($result)
330  {
331  if ($this->db->num_rows($result))
332  {
333  $obj = $this->db->fetch_object($result);
334  $this->id = $obj->rowid;
335  if ($obj->fk_user_author)
336  {
337  $cuser = new User($this->db);
338  $cuser->fetch($obj->fk_user_author);
339  $this->user_creation = $cuser;
340  }
341  if ($obj->fk_user_modif)
342  {
343  $muser = new User($this->db);
344  $muser->fetch($obj->fk_user_modif);
345  $this->user_modification = $muser;
346  }
347  $this->date_creation = $this->db->jdate($obj->datec);
348  $this->date_modification = $this->db->jdate($obj->tms);
349  }
350  $this->db->free($result);
351  }
352  else
353  {
354  dol_print_error($this->db);
355  }
356  }
357 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
update($user)
Update record.
getLibStatut($mode=0)
Give a label from a status.
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
LibStatut($statut, $mode=0)
Give a label from a status.
info($id)
Information on record.
Class to manage fiscal year.
dol_now($mode='gmt')
Return date for now.
create($user)
Create object in database.
__construct($db)
Constructor.
fetch($id)
Load an object from database.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)