dolibarr  16.0.5
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 = 'establishment';
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'=>2, '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)) {
184  $this->ref = '(PROV)';
185  }
186 
187  $this->db->begin();
188 
189  $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
190  $sql .= "ref";
191  $sql .= ", label";
192  $sql .= ", address";
193  $sql .= ", zip";
194  $sql .= ", town";
195  $sql .= ", status";
196  $sql .= ", fk_country";
197  $sql .= ", entity";
198  $sql .= ", datec";
199  $sql .= ", fk_user_author";
200  $sql .= ", fk_user_mod";
201  $sql .= ") VALUES (";
202  $sql .= "'".$this->db->escape($this->ref)."'";
203  $sql .= ", '".$this->db->escape($this->label)."'";
204  $sql .= ", '".$this->db->escape($this->address)."'";
205  $sql .= ", '".$this->db->escape($this->zip)."'";
206  $sql .= ", '".$this->db->escape($this->town)."'";
207  $sql .= ", ".((int) $this->country_id);
208  $sql .= ", ".((int) $this->status);
209  $sql .= ", ".((int) $conf->entity);
210  $sql .= ", '".$this->db->idate($now)."'";
211  $sql .= ", ".((int) $user->id);
212  $sql .= ", ".((int) $user->id);
213  $sql .= ")";
214 
215  dol_syslog(get_class($this)."::create", LOG_DEBUG);
216  $resql = $this->db->query($sql);
217  if (!$resql) {
218  $error++;
219  $this->errors[] = "Error ".$this->db->lasterror();
220  }
221 
222  // Commit or rollback
223  if ($error) {
224  foreach ($this->errors as $errmsg) {
225  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
226  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
227  }
228  $this->db->rollback();
229  return -1 * $error;
230  } else {
231  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'establishment');
232 
233  $sql = 'UPDATE '.MAIN_DB_PREFIX."establishment SET ref = '".$this->db->escape($this->id)."'";
234  $sql .= " WHERE rowid = ".((int) $this->id);
235  $this->db->query($sql);
236 
237  $this->db->commit();
238  return $this->id;
239  }
240  }
241 
248  public function update($user)
249  {
250  global $langs;
251 
252  // Check parameters
253  if (empty($this->label)) {
254  $this->error = 'ErrorBadParameter';
255  return -1;
256  }
257 
258  $this->db->begin();
259 
260  $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
261  $sql .= " SET ref = '".$this->db->escape($this->ref)."'";
262  $sql .= ", label = '".$this->db->escape($this->label)."'";
263  $sql .= ", address = '".$this->db->escape($this->address)."'";
264  $sql .= ", zip = '".$this->db->escape($this->zip)."'";
265  $sql .= ", town = '".$this->db->escape($this->town)."'";
266  $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
267  $sql .= ", status = ".((int) $this->status);
268  $sql .= ", fk_user_mod = ".((int) $user->id);
269  $sql .= ", entity = ".((int) $this->entity);
270  $sql .= " WHERE rowid = ".((int) $this->id);
271 
272  dol_syslog(get_class($this)."::update", LOG_DEBUG);
273  $result = $this->db->query($sql);
274  if ($result) {
275  $this->db->commit();
276  return 1;
277  } else {
278  $this->error = $this->db->lasterror();
279  $this->db->rollback();
280  return -1;
281  }
282  }
283 
290  public function fetch($id)
291  {
292  $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
293  $sql .= ' c.code as country_code, c.label as country';
294  $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
295  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
296  $sql .= " WHERE e.rowid = ".((int) $id);
297 
298  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
299  $result = $this->db->query($sql);
300  if ($result) {
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 = ".((int) $id);
334 
335  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
336  $result = $this->db->query($sql);
337  if ($result) {
338  $this->db->commit();
339  return 1;
340  } else {
341  $this->error = $this->db->lasterror();
342  $this->db->rollback();
343  return -1;
344  }
345  }
346 
353  public function getLibStatut($mode = 0)
354  {
355  return $this->LibStatut($this->status, $mode);
356  }
357 
358  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
366  public function LibStatut($status, $mode = 0)
367  {
368  // phpcs:enable
369  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
370  global $langs;
371  //$langs->load("mymodule");
372  $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
373  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
374  $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
375  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
376  }
377 
378  $statusType = 'status'.$status;
379  if ($status == self::STATUS_OPEN) {
380  $statusType = 'status4';
381  }
382  if ($status == self::STATUS_CLOSED) {
383  $statusType = 'status6';
384  }
385 
386  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
387  }
388 
389 
396  public function info($id)
397  {
398  $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
399  $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
400  $sql .= ' WHERE e.rowid = '.((int) $id);
401 
402  dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
403  $result = $this->db->query($sql);
404 
405  if ($result) {
406  if ($this->db->num_rows($result)) {
407  $obj = $this->db->fetch_object($result);
408  $this->id = $obj->rowid;
409 
410  $this->user_creation_id = $obj->fk_user_author;
411  $this->user_modification_id = $obj->fk_user_mod;
412  $this->date_creation = $this->db->jdate($obj->datec);
413  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
414  }
415  $this->db->free($result);
416  } else {
417  dol_print_error($this->db);
418  }
419  }
420 
431  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
432  {
433  global $conf, $langs, $hookmanager;
434 
435  if (!empty($conf->dol_no_mouse_hover)) {
436  $notooltip = 1; // Force disable tooltips
437  }
438 
439  $result = '';
440 
441  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
442  if (isset($this->status)) {
443  $label .= ' '.$this->getLibStatut(5);
444  }
445  $label .= '<br>';
446  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
447 
448  $url = DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id;
449 
450  if ($option != 'nolink') {
451  // Add param to save lastsearch_values or not
452  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
453  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
454  $add_save_lastsearch_values = 1;
455  }
456  if ($add_save_lastsearch_values) {
457  $url .= '&save_lastsearch_values=1';
458  }
459  }
460 
461  $linkclose = '';
462  if (empty($notooltip)) {
463  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
464  $label = $langs->trans("Establishment");
465  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
466  }
467  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
468  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
469  } else {
470  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
471  }
472 
473  if ($option == 'nolink') {
474  $linkstart = '<span';
475  } else {
476  $linkstart = '<a href="'.$url.'"';
477  }
478  $linkstart .= $linkclose.'>';
479  if ($option == 'nolink') {
480  $linkend = '</span>';
481  } else {
482  $linkend = '</a>';
483  }
484 
485  $result .= $linkstart;
486 
487  if ($withpicto) {
488  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
489  }
490 
491  if ($withpicto != 2) {
492  $result .= $this->ref;
493  }
494 
495  $result .= $linkend;
496  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
497 
498  global $action, $hookmanager;
499  $hookmanager->initHooks(array('establishmentdao'));
500  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
501  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
502  if ($reshook > 0) {
503  $result = $hookmanager->resPrint;
504  } else {
505  $result .= $hookmanager->resPrint;
506  }
507 
508  return $result;
509  }
510 
516  public function getCountryCode()
517  {
518  global $mysoc;
519 
520  // We return country code of bank account
521  if (!empty($this->country_code)) {
522  return $this->country_code;
523  }
524 
525  // We return country code of managed company
526  if (!empty($mysoc->country_code)) {
527  return $mysoc->country_code;
528  }
529 
530  return '';
531  }
532 
539  public function initAsSpecimen()
540  {
541  $this->id = 0;
542  $this->ref = '0';
543  $this->label = 'Department AAA';
544  }
545 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Establishment\update
update($user)
Update record.
Definition: establishment.class.php:248
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
ref
$object ref
Definition: info.php:77
Establishment\create
create($user)
Create object in database.
Definition: establishment.class.php:170
Establishment\info
info($id)
Information on record.
Definition: establishment.class.php:396
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Establishment\fetch
fetch($id)
Load an object from database.
Definition: establishment.class.php:290
Establishment\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: establishment.class.php:431
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Establishment\getCountryCode
getCountryCode()
Return account country code.
Definition: establishment.class.php:516
Establishment\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: establishment.class.php:539
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Establishment\LibStatut
LibStatut($status, $mode=0)
Return the status.
Definition: establishment.class.php:366
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Establishment\getLibStatut
getLibStatut($mode=0)
Give a label from a status.
Definition: establishment.class.php:353
Establishment\__construct
__construct($db)
Constructor.
Definition: establishment.class.php:159
Establishment
Class to manage establishments.
Definition: establishment.class.php:30