dolibarr  9.0.0
establishment.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
3  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
26 
31 {
35  public $element='establishment';
36 
40  public $table_element='establishment';
41 
45  public $table_element_line = '';
46 
50  public $fk_element = 'fk_establishment';
51 
56  public $ismultientitymanaged = 1;
57 
58  public $picto='building';
59 
63  public $id;
64 
68  public $ref;
69 
73  public $rowid;
74 
75  public $name;
76 
80  public $address;
81 
82  public $zip;
83  public $town;
84 
88  public $status;
89 
93  public $entity;
94 
95  public $country_id;
96 
97  public $statuts=array();
98  public $statuts_short=array();
99 
105  function __construct($db)
106  {
107  $this->db = $db;
108 
109  $this->statuts_short = array(0 => 'Closed', 1 => 'Opened');
110  $this->statuts = array(0 => 'Closed', 1 => 'Opened');
111  }
112 
119  function create($user)
120  {
121  global $conf, $langs;
122 
123  $error = 0;
124  $ret = 0;
125  $now=dol_now();
126 
127  // Clean parameters
128  $this->address=($this->address>0?$this->address:$this->address);
129  $this->zip=($this->zip>0?$this->zip:$this->zip);
130  $this->town=($this->town>0?$this->town:$this->town);
131  $this->country_id=($this->country_id>0?$this->country_id:$this->country_id);
132 
133  $this->db->begin();
134 
135  $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
136  $sql.= "name";
137  $sql.= ", address";
138  $sql.= ", zip";
139  $sql.= ", town";
140  $sql.= ", status";
141  $sql.= ", fk_country";
142  $sql.= ", entity";
143  $sql.= ", datec";
144  $sql.= ", fk_user_author";
145  $sql.= ", fk_user_mod";
146  $sql.= ") VALUES (";
147  $sql.= " '".$this->db->escape($this->name)."'";
148  $sql.= ", '".$this->db->escape($this->address)."'";
149  $sql.= ", '".$this->db->escape($this->zip)."'";
150  $sql.= ", '".$this->db->escape($this->town)."'";
151  $sql.= ", ".$this->country_id;
152  $sql.= ", ".$this->status;
153  $sql.= ", ".$conf->entity;
154  $sql.= ", '".$this->db->idate($now)."'";
155  $sql.= ", ". $user->id;
156  $sql.= ", ". $user->id;
157  $sql.= ")";
158 
159  dol_syslog(get_class($this)."::create", LOG_DEBUG);
160  $resql = $this->db->query($sql);
161  if (! $resql) {
162  $error ++;
163  $this->errors[] = "Error " . $this->db->lasterror();
164  }
165 
166  if (! $error) {
167  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "establishment");
168  }
169 
170  // Commit or rollback
171  if ($error) {
172  foreach ( $this->errors as $errmsg ) {
173  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
174  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
175  }
176  $this->db->rollback();
177  return - 1 * $error;
178  } else {
179  $this->db->commit();
180  return $this->id;
181  }
182  }
183 
190  function update($user)
191  {
192  global $langs;
193 
194  // Check parameters
195  if (empty($this->name))
196  {
197  $this->error='ErrorBadParameter';
198  return -1;
199  }
200 
201  $this->db->begin();
202 
203  $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
204  $sql .= " SET name = '".$this->db->escape($this->name)."'";
205  $sql .= ", address = '".$this->db->escape($this->address)."'";
206  $sql .= ", zip = '".$this->db->escape($this->zip)."'";
207  $sql .= ", town = '".$this->db->escape($this->town)."'";
208  $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
209  $sql .= ", status = ".$this->db->escape($this->status);
210  $sql .= ", fk_user_mod = " . $user->id;
211  $sql .= " WHERE rowid = ".$this->id;
212 
213  dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
214  $result = $this->db->query($sql);
215  if ($result) {
216  $this->db->commit();
217  return 1;
218  } else {
219  $this->error = $this->db->lasterror();
220  $this->db->rollback();
221  return - 1;
222  }
223  }
224 
231  function fetch($id)
232  {
233  $sql = "SELECT e.rowid, e.name, e.address, e.zip, e.town, e.status, e.fk_country as country_id,";
234  $sql.= ' c.code as country_code, c.label as country';
235  $sql.= " FROM ".MAIN_DB_PREFIX."establishment as e";
236  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
237  $sql.= " WHERE e.rowid = ".$id;
238 
239  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
240  $result = $this->db->query($sql);
241  if ( $result )
242  {
243  $obj = $this->db->fetch_object($result);
244 
245  $this->id = $obj->rowid;
246  $this->ref = $obj->rowid;
247  $this->name = $obj->name;
248  $this->address = $obj->address;
249  $this->zip = $obj->zip;
250  $this->town = $obj->town;
251  $this->status = $obj->status;
252 
253  $this->country_id = $obj->country_id;
254  $this->country_code = $obj->country_code;
255  $this->country = $obj->country;
256 
257  return 1;
258  }
259  else
260  {
261  $this->error=$this->db->lasterror();
262  return -1;
263  }
264  }
265 
272  function delete($id)
273  {
274  $this->db->begin();
275 
276  $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".$id;
277 
278  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
279  $result = $this->db->query($sql);
280  if ($result)
281  {
282  $this->db->commit();
283  return 1;
284  }
285  else
286  {
287  $this->error=$this->db->lasterror();
288  $this->db->rollback();
289  return -1;
290  }
291  }
292 
299  function getLibStatut($mode=0)
300  {
301  return $this->LibStatut($this->status,$mode);
302  }
303 
304  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
312  function LibStatut($status,$mode=0)
313  {
314  // phpcs:enable
315  global $langs;
316 
317  if ($mode == 0)
318  {
319  return $langs->trans($this->statuts[$status]);
320  }
321  elseif ($mode == 1)
322  {
323  return $langs->trans($this->statuts_short[$status]);
324  }
325  elseif ($mode == 2)
326  {
327  if ($status==0) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts_short[$status]);
328  elseif ($status==1) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts_short[$status]);
329  }
330  elseif ($mode == 3)
331  {
332  if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5');
333  elseif ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4');
334  }
335  elseif ($mode == 4)
336  {
337  if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts[$status]);
338  elseif ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts[$status]);
339  }
340  elseif ($mode == 5)
341  {
342  if ($status==0 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
343  elseif ($status==1 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut4');
344  }
345  }
346 
353  function info($id)
354  {
355  $sql = 'SELECT e.rowid, e.datec, e.fk_user_author, e.tms, e.fk_user_mod';
356  $sql.= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
357  $sql.= ' WHERE e.rowid = '.$id;
358 
359  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
360  $result = $this->db->query($sql);
361 
362  if ($result)
363  {
364  if ($this->db->num_rows($result))
365  {
366  $obj = $this->db->fetch_object($result);
367  $this->id = $obj->rowid;
368 
369  $this->date_creation = $this->db->jdate($obj->datec);
370  if ($obj->fk_user_author)
371  {
372  $cuser = new User($this->db);
373  $cuser->fetch($obj->fk_user_author);
374  $this->user_creation = $cuser;
375  }
376  if ($obj->fk_user_mod)
377  {
378  $muser = new User($this->db);
379  $muser->fetch($obj->fk_user_mod);
380  $this->user_modification = $muser;
381 
382  $this->date_modification = $this->db->jdate($obj->tms);
383  }
384  }
385  $this->db->free($result);
386  }
387  else
388  {
389  dol_print_error($this->db);
390  }
391  }
392 
399  function getNomUrl($withpicto=0)
400  {
401  global $langs;
402 
403  $result='';
404 
405  $link = '<a href="'.DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id.'">';
406  $linkend='</a>';
407 
408  $picto='building';
409 
410  $label=$langs->trans("Show").': '.$this->name;
411 
412  if ($withpicto) $result.=($link.img_object($label,$picto).$linkend);
413  if ($withpicto && $withpicto != 2) $result.=' ';
414  if ($withpicto != 2) $result.=$link.$this->name.$linkend;
415  return $result;
416  }
417 
423  function getCountryCode()
424  {
425  global $mysoc;
426 
427  // We return country code of bank account
428  if (! empty($this->country_code)) return $this->country_code;
429 
430  // We return country code of managed company
431  if (! empty($mysoc->country_code)) return $mysoc->country_code;
432 
433  return '';
434  }
435 
442  public function initAsSpecimen()
443  {
444  $this->id = 0;
445  $this->ref = 'DEAAA';
446  }
447 }
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:1053
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:41
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
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.
update($user)
Update record.
getNomUrl($withpicto=0)
Return clicable name (with picto eventually)
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, ...)
fetch($id)
Load an object from database.
create($user)
Create object in database.