dolibarr  7.0.0-beta
establishment.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.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 
30 {
31  public $element='establishment';
32  public $table_element='establishment';
33  public $table_element_line = '';
34  public $fk_element = 'fk_establishment';
35  public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
36  public $picto='building';
37 
38  public $id;
39  public $ref;
40  public $rowid;
41 
42  public $name;
43  public $address;
44  public $zip;
45  public $town;
46  public $status; // 0=open, 1=closed
47  public $entity;
48 
49  public $country_id;
50 
51  public $statuts=array();
52  public $statuts_short=array();
53 
59  function __construct($db)
60  {
61  $this->db = $db;
62 
63  $this->statuts_short = array(0 => 'Closed', 1 => 'Opened');
64  $this->statuts = array(0 => 'Closed', 1 => 'Opened');
65 
66  return 1;
67  }
68 
75  function create($user)
76  {
77  global $conf, $langs;
78 
79  $error = 0;
80  $ret = 0;
81  $now=dol_now();
82 
83  // Clean parameters
84  $this->address=($this->address>0?$this->address:$this->address);
85  $this->zip=($this->zip>0?$this->zip:$this->zip);
86  $this->town=($this->town>0?$this->town:$this->town);
87  $this->country_id=($this->country_id>0?$this->country_id:$this->country_id);
88 
89  $this->db->begin();
90 
91  $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
92  $sql.= "name";
93  $sql.= ", address";
94  $sql.= ", zip";
95  $sql.= ", town";
96  $sql.= ", status";
97  $sql.= ", fk_country";
98  $sql.= ", entity";
99  $sql.= ", datec";
100  $sql.= ", fk_user_author";
101  $sql.= ", fk_user_mod";
102  $sql.= ") VALUES (";
103  $sql.= " '".$this->db->escape($this->name)."'";
104  $sql.= ", '".$this->db->escape($this->address)."'";
105  $sql.= ", '".$this->db->escape($this->zip)."'";
106  $sql.= ", '".$this->db->escape($this->town)."'";
107  $sql.= ", ".$this->country_id;
108  $sql.= ", ".$this->status;
109  $sql.= ", ".$conf->entity;
110  $sql.= ", '".$this->db->idate($now)."'";
111  $sql.= ", ". $user->id;
112  $sql.= ", ". $user->id;
113  $sql.= ")";
114 
115  dol_syslog(get_class($this)."::create", LOG_DEBUG);
116  $resql = $this->db->query($sql);
117  if (! $resql) {
118  $error ++;
119  $this->errors[] = "Error " . $this->db->lasterror();
120  }
121 
122  if (! $error) {
123  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "establishment");
124  }
125 
126  // Commit or rollback
127  if ($error) {
128  foreach ( $this->errors as $errmsg ) {
129  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
130  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
131  }
132  $this->db->rollback();
133  return - 1 * $error;
134  } else {
135  $this->db->commit();
136  return $this->id;
137  }
138  }
139 
146  function update($user)
147  {
148  global $langs;
149 
150  // Check parameters
151  if (empty($this->name))
152  {
153  $this->error='ErrorBadParameter';
154  return -1;
155  }
156 
157  $this->db->begin();
158 
159  $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
160  $sql .= " SET name = '".$this->db->escape($this->name)."'";
161  $sql .= ", address = '".$this->db->escape($this->address)."'";
162  $sql .= ", zip = '".$this->db->escape($this->zip)."'";
163  $sql .= ", town = '".$this->db->escape($this->town)."'";
164  $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
165  $sql .= ", status = ".$this->db->escape($this->status);
166  $sql .= ", fk_user_mod = " . $user->id;
167  $sql .= " WHERE rowid = ".$this->id;
168 
169  dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
170  $result = $this->db->query($sql);
171  if ($result) {
172  $this->db->commit();
173  return 1;
174  } else {
175  $this->error = $this->db->lasterror();
176  $this->db->rollback();
177  return - 1;
178  }
179  }
180 
187  function fetch($id)
188  {
189  $sql = "SELECT e.rowid, e.name, e.address, e.zip, e.town, e.status, e.fk_country as country_id,";
190  $sql.= ' c.code as country_code, c.label as country';
191  $sql.= " FROM ".MAIN_DB_PREFIX."establishment as e";
192  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
193  $sql.= " WHERE e.rowid = ".$id;
194 
195  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
196  $result = $this->db->query($sql);
197  if ( $result )
198  {
199  $obj = $this->db->fetch_object($result);
200 
201  $this->id = $obj->rowid;
202  $this->ref = $obj->rowid;
203  $this->name = $obj->name;
204  $this->address = $obj->address;
205  $this->zip = $obj->zip;
206  $this->town = $obj->town;
207  $this->status = $obj->status;
208 
209  $this->country_id = $obj->country_id;
210  $this->country_code = $obj->country_code;
211  $this->country = $obj->country;
212 
213  return 1;
214  }
215  else
216  {
217  $this->error=$this->db->lasterror();
218  return -1;
219  }
220  }
221 
228  function delete($id)
229  {
230  $this->db->begin();
231 
232  $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".$id;
233 
234  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
235  $result = $this->db->query($sql);
236  if ($result)
237  {
238  $this->db->commit();
239  return 1;
240  }
241  else
242  {
243  $this->error=$this->db->lasterror();
244  $this->db->rollback();
245  return -1;
246  }
247  }
248 
255  function getLibStatut($mode=0)
256  {
257  return $this->LibStatut($this->status,$mode);
258  }
259 
267  function LibStatut($status,$mode=0)
268  {
269  global $langs;
270 
271  if ($mode == 0)
272  {
273  return $langs->trans($this->statuts[$status]);
274  }
275  if ($mode == 1)
276  {
277  return $langs->trans($this->statuts_short[$status]);
278  }
279  if ($mode == 2)
280  {
281  if ($status==0) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts_short[$status]);
282  if ($status==1) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts_short[$status]);
283  }
284  if ($mode == 3)
285  {
286  if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5');
287  if ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4');
288  }
289  if ($mode == 4)
290  {
291  if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts[$status]);
292  if ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts[$status]);
293  }
294  if ($mode == 5)
295  {
296  if ($status==0 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
297  if ($status==1 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut4');
298  }
299  }
300 
307  function info($id)
308  {
309  $sql = 'SELECT e.rowid, e.datec, e.fk_user_author, e.tms, e.fk_user_mod';
310  $sql.= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
311  $sql.= ' WHERE e.rowid = '.$id;
312 
313  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
314  $result = $this->db->query($sql);
315 
316  if ($result)
317  {
318  if ($this->db->num_rows($result))
319  {
320  $obj = $this->db->fetch_object($result);
321  $this->id = $obj->rowid;
322 
323  $this->date_creation = $this->db->jdate($obj->datec);
324  if ($obj->fk_user_author)
325  {
326  $cuser = new User($this->db);
327  $cuser->fetch($obj->fk_user_author);
328  $this->user_creation = $cuser;
329  }
330  if ($obj->fk_user_mod)
331  {
332  $muser = new User($this->db);
333  $muser->fetch($obj->fk_user_mod);
334  $this->user_modification = $muser;
335 
336  $this->date_modification = $this->db->jdate($obj->tms);
337  }
338  }
339  $this->db->free($result);
340  }
341  else
342  {
343  dol_print_error($this->db);
344  }
345  }
346 
353  function getNomUrl($withpicto=0)
354  {
355  global $langs;
356 
357  $result='';
358 
359  $link = '<a href="'.DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id.'">';
360  $linkend='</a>';
361 
362  $picto='building';
363 
364  $label=$langs->trans("Show").': '.$this->name;
365 
366  if ($withpicto) $result.=($link.img_object($label,$picto).$linkend);
367  if ($withpicto && $withpicto != 2) $result.=' ';
368  if ($withpicto != 2) $result.=$link.$this->name.$linkend;
369  return $result;
370  }
371 
377  function getCountryCode()
378  {
379  global $mysoc;
380 
381  // We return country code of bank account
382  if (! empty($this->country_code)) return $this->country_code;
383 
384  // We return country code of managed company
385  if (! empty($mysoc->country_code)) return $mysoc->country_code;
386 
387  return '';
388  }
389 
396  public function initAsSpecimen()
397  {
398  $this->id = 0;
399  $this->ref = 'DEAAA';
400  }
401 }
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
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...
info($id)
Information on record.
getCountryCode()
Return account country code.
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage establishments.
LibStatut($status, $mode=0)
Give a label from a status.
dol_now($mode='gmt')
Return date for now.
getLibStatut($mode=0)
Give a label from a status.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
update($user)
Update record.
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->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)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetch($id)
Load an object from database.
create($user)
Create object in database.