dolibarr  7.0.0-beta
fiscalyear.class.php
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 {
31  public $element='fiscalyear';
32  public $table_element='accounting_fiscalyear';
33  public $table_element_line = '';
34  public $fk_element = '';
35  public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
36 
37  var $rowid;
38 
39  var $label;
40  var $date_start;
41  var $date_end;
42  var $datec;
43  var $statut; // 0=open, 1=closed
44  var $entity;
45 
46  var $statuts=array();
47  var $statuts_short=array();
48 
54  function __construct($db)
55  {
56  $this->db = $db;
57 
58  $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
59  $this->statuts = array(0 => 'Opened', 1 => 'Closed');
60 
61  return 1;
62  }
63 
70  function create($user)
71  {
72  global $conf;
73 
74  $error = 0;
75 
76  $now=dol_now();
77 
78  $this->db->begin();
79 
80  $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_fiscalyear (";
81  $sql.= "label";
82  $sql.= ", date_start";
83  $sql.= ", date_end";
84  $sql.= ", statut";
85  $sql.= ", entity";
86  $sql.= ", datec";
87  $sql.= ", fk_user_author";
88  $sql.= ") VALUES (";
89  $sql.= " '".$this->db->escape($this->label)."'";
90  $sql.= ", '".$this->db->idate($this->date_start)."'";
91  $sql.= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'":"null");
92  $sql.= ", 0";
93  $sql.= ", ".$conf->entity;
94  $sql.= ", '".$this->db->idate($now)."'";
95  $sql.= ", ". $user->id;
96  $sql.= ")";
97 
98  dol_syslog(get_class($this)."::create", LOG_DEBUG);
99  $result = $this->db->query($sql);
100  if ($result)
101  {
102  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_fiscalyear");
103 
104  $result=$this->update($user);
105  if ($result > 0)
106  {
107  $this->db->commit();
108  return $this->id;
109  }
110  else
111  {
112  $this->error=$this->db->lasterror();
113  $this->db->rollback();
114  return $result;
115  }
116  }
117  else
118  {
119  $this->error=$this->db->lasterror()." sql=".$sql;
120  $this->db->rollback();
121  return -1;
122  }
123  }
124 
131  function update($user)
132  {
133  global $langs;
134 
135  // Check parameters
136  if (empty($this->date_start) && empty($this->date_end))
137  {
138  $this->error='ErrorBadParameter';
139  return -1;
140  }
141 
142  $this->db->begin();
143 
144  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_fiscalyear";
145  $sql .= " SET label = '".$this->db->escape($this->label)."'";
146  $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
147  $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
148  $sql .= ", statut = '".$this->db->escape($this->statut?$this->statut:0)."'";
149  $sql .= ", datec = " . ($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
150  $sql .= ", fk_user_modif = " . $user->id;
151  $sql .= " WHERE rowid = ".$this->id;
152 
153  dol_syslog(get_class($this)."::update", LOG_DEBUG);
154  $result = $this->db->query($sql);
155  if ($result)
156  {
157  $this->db->commit();
158  return 1;
159  }
160  else
161  {
162  $this->error=$this->db->lasterror();
163  dol_syslog($this->error, LOG_ERR);
164  $this->db->rollback();
165  return -1;
166  }
167  }
168 
175  function fetch($id)
176  {
177  $sql = "SELECT rowid, label, date_start, date_end, statut";
178  $sql.= " FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
179  $sql.= " WHERE rowid = ".$id;
180 
181  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
182  $result = $this->db->query($sql);
183  if ( $result )
184  {
185  $obj = $this->db->fetch_object($result);
186 
187  $this->id = $obj->rowid;
188  $this->ref = $obj->rowid;
189  $this->date_start = $this->db->jdate($obj->date_start);
190  $this->date_end = $this->db->jdate($obj->date_end);
191  $this->label = $obj->label;
192  $this->statut = $obj->statut;
193 
194  return 1;
195  }
196  else
197  {
198  $this->error=$this->db->lasterror();
199  return -1;
200  }
201  }
202 
209  function delete($id)
210  {
211  $this->db->begin();
212 
213  $sql = "DELETE FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE rowid = ".$id;
214 
215  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
216  $result = $this->db->query($sql);
217  if ($result)
218  {
219  $this->db->commit();
220  return 1;
221  }
222  else
223  {
224  $this->error=$this->db->lasterror();
225  $this->db->rollback();
226  return -1;
227  }
228  }
229 
236  function getLibStatut($mode=0)
237  {
238  return $this->LibStatut($this->statut,$mode);
239  }
240 
248  function LibStatut($statut,$mode=0)
249  {
250  global $langs;
251 
252  if ($mode == 0)
253  {
254  return $langs->trans($this->statuts[$statut]);
255  }
256  if ($mode == 1)
257  {
258  return $langs->trans($this->statuts_short[$statut]);
259  }
260  if ($mode == 2)
261  {
262  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
263  if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts_short[$statut]);
264  }
265  if ($mode == 3)
266  {
267  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
268  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8');
269  }
270  if ($mode == 4)
271  {
272  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
273  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts[$statut]);
274  }
275  if ($mode == 5)
276  {
277  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
278  if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
279  }
280  }
281 
288  function info($id)
289  {
290  $sql = 'SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,';
291  $sql.= ' fy.tms';
292  $sql.= ' FROM '.MAIN_DB_PREFIX.'accounting_fiscalyear as fy';
293  $sql.= ' WHERE fy.rowid = '.$id;
294 
295  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
296  $result = $this->db->query($sql);
297 
298  if ($result)
299  {
300  if ($this->db->num_rows($result))
301  {
302  $obj = $this->db->fetch_object($result);
303  $this->id = $obj->rowid;
304  if ($obj->fk_user_author)
305  {
306  $cuser = new User($this->db);
307  $cuser->fetch($obj->fk_user_author);
308  $this->user_creation = $cuser;
309  }
310  if ($obj->fk_user_modif)
311  {
312  $muser = new User($this->db);
313  $muser->fetch($obj->fk_user_modif);
314  $this->user_modification = $muser;
315  }
316  $this->date_creation = $this->db->jdate($obj->datec);
317  $this->date_modification = $this->db->jdate($obj->tms);
318  }
319  $this->db->free($result);
320  }
321  else
322  {
323  dol_print_error($this->db);
324  }
325  }
326 
327 }
update($user)
Update record.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
getLibStatut($mode=0)
Give a label from a status.
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)