dolibarr  13.0.2
establishment.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  * Copyright (C) 2018-2020 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 <https://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 
61  public $picto = 'building';
62 
66  public $id;
67 
71  public $ref;
72 
76  public $rowid;
77 
81  public $label;
82 
86  public $address;
87 
91  public $zip;
92 
96  public $town;
97 
101  public $country_id;
102 
106  public $status;
107 
111  public $entity;
112 
116  public $fk_user_mod;
117 
121  public $fk_user_author;
122 
126  public $datec;
127 
128  const STATUS_OPEN = 1;
129  const STATUS_CLOSED = 0;
130 
131 
132  public $fields = array(
133  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
134  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
135  'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>20),
136  'label' =>array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'showoncombobox'=>1, 'position'=>22),
137  'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
138  'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
139  'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
140  'fk_state' =>array('type'=>'integer', 'label'=>'Fkstate', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
141  'fk_country' =>array('type'=>'integer', 'label'=>'Fkcountry', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
142  'profid1' =>array('type'=>'varchar(20)', 'label'=>'Profid1', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
143  'profid2' =>array('type'=>'varchar(20)', 'label'=>'Profid2', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
144  'profid3' =>array('type'=>'varchar(20)', 'label'=>'Profid3', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
145  'phone' =>array('type'=>'varchar(20)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
146  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fkuserauthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
147  'fk_user_mod' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fkusermod', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
148  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>80),
149  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>85),
150  'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
151  );
152 
153 
159  public function __construct($db)
160  {
161  $this->db = $db;
162  }
163 
170  public function create($user)
171  {
172  global $conf, $langs;
173 
174  $error = 0;
175  $now = dol_now();
176 
177  // Clean parameters
178  $this->label = trim($this->label);
179  $this->address = trim($this->address);
180  $this->zip = trim($this->zip);
181  $this->town = trim($this->town);
182 
183  if (empty($this->ref)) $this->ref = '(PROV)';
184 
185  $this->db->begin();
186 
187  $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
188  $sql .= "ref";
189  $sql .= ", label";
190  $sql .= ", address";
191  $sql .= ", zip";
192  $sql .= ", town";
193  $sql .= ", status";
194  $sql .= ", fk_country";
195  $sql .= ", entity";
196  $sql .= ", datec";
197  $sql .= ", fk_user_author";
198  $sql .= ", fk_user_mod";
199  $sql .= ") VALUES (";
200  $sql .= "'".$this->db->escape($this->ref)."'";
201  $sql .= ", '".$this->db->escape($this->label)."'";
202  $sql .= ", '".$this->db->escape($this->address)."'";
203  $sql .= ", '".$this->db->escape($this->zip)."'";
204  $sql .= ", '".$this->db->escape($this->town)."'";
205  $sql .= ", ".$this->country_id;
206  $sql .= ", ".$this->status;
207  $sql .= ", ".$conf->entity;
208  $sql .= ", '".$this->db->idate($now)."'";
209  $sql .= ", ".$user->id;
210  $sql .= ", ".$user->id;
211  $sql .= ")";
212 
213  dol_syslog(get_class($this)."::create", LOG_DEBUG);
214  $resql = $this->db->query($sql);
215  if (!$resql) {
216  $error++;
217  $this->errors[] = "Error ".$this->db->lasterror();
218  }
219 
220  // Commit or rollback
221  if ($error) {
222  foreach ($this->errors as $errmsg) {
223  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
224  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
225  }
226  $this->db->rollback();
227  return -1 * $error;
228  } else {
229  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'establishment');
230 
231  $sql = 'UPDATE '.MAIN_DB_PREFIX."establishment SET ref = '".$this->db->escape($this->id)."'";
232  $sql .= " WHERE rowid = ".$this->id;
233  $this->db->query($sql);
234 
235  $this->db->commit();
236  return $this->id;
237  }
238  }
239 
246  public function update($user)
247  {
248  global $langs;
249 
250  // Check parameters
251  if (empty($this->label))
252  {
253  $this->error = 'ErrorBadParameter';
254  return -1;
255  }
256 
257  $this->db->begin();
258 
259  $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
260  $sql .= " SET ref = '".$this->db->escape($this->ref)."'";
261  $sql .= ", label = '".$this->db->escape($this->label)."'";
262  $sql .= ", address = '".$this->db->escape($this->address)."'";
263  $sql .= ", zip = '".$this->db->escape($this->zip)."'";
264  $sql .= ", town = '".$this->db->escape($this->town)."'";
265  $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
266  $sql .= ", status = ".$this->db->escape($this->status);
267  $sql .= ", fk_user_mod = ".$user->id;
268  $sql .= ", entity = ".$this->entity;
269  $sql .= " WHERE rowid = ".$this->id;
270 
271  dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
272  $result = $this->db->query($sql);
273  if ($result) {
274  $this->db->commit();
275  return 1;
276  } else {
277  $this->error = $this->db->lasterror();
278  $this->db->rollback();
279  return -1;
280  }
281  }
282 
289  public function fetch($id)
290  {
291  $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
292  $sql .= ' c.code as country_code, c.label as country';
293  $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
294  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
295  $sql .= " WHERE e.rowid = ".$id;
296 
297  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
298  $result = $this->db->query($sql);
299  if ($result)
300  {
301  $obj = $this->db->fetch_object($result);
302 
303  $this->id = $obj->rowid;
304  $this->ref = $obj->ref;
305  $this->label = $obj->label;
306  $this->address = $obj->address;
307  $this->zip = $obj->zip;
308  $this->town = $obj->town;
309  $this->status = $obj->status;
310  $this->entity = $obj->entity;
311 
312  $this->country_id = $obj->country_id;
313  $this->country_code = $obj->country_code;
314  $this->country = $obj->country;
315 
316  return 1;
317  } else {
318  $this->error = $this->db->lasterror();
319  return -1;
320  }
321  }
322 
329  public function delete($id)
330  {
331  $this->db->begin();
332 
333  $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".$id;
334 
335  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
336  $result = $this->db->query($sql);
337  if ($result)
338  {
339  $this->db->commit();
340  return 1;
341  } else {
342  $this->error = $this->db->lasterror();
343  $this->db->rollback();
344  return -1;
345  }
346  }
347 
354  public function getLibStatut($mode = 0)
355  {
356  return $this->LibStatut($this->status, $mode);
357  }
358 
359  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
367  public function LibStatut($status, $mode = 0)
368  {
369  // phpcs:enable
370  if (empty($this->labelStatus) || empty($this->labelStatusShort))
371  {
372  global $langs;
373  //$langs->load("mymodule");
374  $this->labelStatus[self::STATUS_OPEN] = $langs->trans('Open');
375  $this->labelStatus[self::STATUS_CLOSED] = $langs->trans('Closed');
376  $this->labelStatusShort[self::STATUS_OPEN] = $langs->trans('Open');
377  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->trans('Closed');
378  }
379 
380  $statusType = 'status'.$status;
381  if ($status == self::STATUS_OPEN) $statusType = 'status4';
382  if ($status == self::STATUS_CLOSED) $statusType = 'status6';
383 
384  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
385  }
386 
387 
394  public function info($id)
395  {
396  $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms, e.fk_user_mod, e.entity';
397  $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
398  $sql .= ' WHERE e.rowid = '.$id;
399 
400  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
401  $result = $this->db->query($sql);
402 
403  if ($result)
404  {
405  if ($this->db->num_rows($result))
406  {
407  $obj = $this->db->fetch_object($result);
408  $this->id = $obj->rowid;
409 
410  $this->date_creation = $this->db->jdate($obj->datec);
411  if ($obj->fk_user_author)
412  {
413  $cuser = new User($this->db);
414  $cuser->fetch($obj->fk_user_author);
415  $this->user_creation = $cuser;
416  }
417  if ($obj->fk_user_mod)
418  {
419  $muser = new User($this->db);
420  $muser->fetch($obj->fk_user_mod);
421  $this->user_modification = $muser;
422 
423  $this->date_modification = $this->db->jdate($obj->tms);
424  }
425  }
426  $this->db->free($result);
427  } else {
428  dol_print_error($this->db);
429  }
430  }
431 
438  public function getNomUrl($withpicto = 0)
439  {
440  global $langs;
441 
442  $result = '';
443 
444  $link = '<a href="'.DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id.'">';
445  $linkend = '</a>';
446 
447  $picto = 'building';
448 
449  $label = '<u>'.$langs->trans("Establishment").'</u>';
450  $label .= '<br>'.$langs->trans("Label").': '.$this->label;
451 
452  if ($withpicto) $result .= ($link.img_object($label, $picto).$linkend);
453  if ($withpicto && $withpicto != 2) $result .= ' ';
454  if ($withpicto != 2) $result .= $link.$this->label.$linkend;
455  return $result;
456  }
457 
463  public function getCountryCode()
464  {
465  global $mysoc;
466 
467  // We return country code of bank account
468  if (!empty($this->country_code)) return $this->country_code;
469 
470  // We return country code of managed company
471  if (!empty($mysoc->country_code)) return $mysoc->country_code;
472 
473  return '';
474  }
475 
482  public function initAsSpecimen()
483  {
484  $this->id = 0;
485  $this->ref = '0';
486  $this->label = 'Department AAA';
487  }
488 }
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
info($id)
Information on record.
$conf db
API class for accounts.
Definition: inc.php:54
getCountryCode()
Return account country code.
__construct($db)
Constructor.
Class to manage establishments.
LibStatut($status, $mode=0)
Return the status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getLibStatut($mode=0)
Give a label from a status.
create($user)
Create object in database.
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)&&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...
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetch($id)
Load an object from database.