dolibarr 22.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-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
32{
36 public $element = 'establishment';
37
41 public $table_element = 'establishment';
42
46 public $table_element_line = '';
47
51 public $fk_element = 'fk_establishment';
52
56 public $picto = 'establishment';
57
61 public $id;
62
66 public $ref;
67
71 public $rowid;
72
76 public $label;
77
81 public $address;
82
86 public $zip;
87
91 public $town;
92
96 public $country_id;
97
101 public $status;
102
106 public $entity;
107
111 public $fk_user_mod;
112
116 public $fk_user_author;
117
121 public $datec;
122
123 const STATUS_OPEN = 1;
124 const STATUS_CLOSED = 0;
125
126
127 public $fields = array(
128 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
129 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1),
130 'ref' => array('type' => 'varchar(30)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 20),
131 'label' => array('type' => 'varchar(128)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 2, 'position' => 22),
132 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 25),
133 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 30),
134 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 35),
135 'fk_state' => array('type' => 'integer', 'label' => 'Fkstate', 'enabled' => 1, 'visible' => -1, 'position' => 40),
136 'fk_country' => array('type' => 'integer', 'label' => 'Fkcountry', 'enabled' => 1, 'visible' => -1, 'position' => 45),
137 'profid1' => array('type' => 'varchar(20)', 'label' => 'Profid1', 'enabled' => 1, 'visible' => -1, 'position' => 50),
138 'profid2' => array('type' => 'varchar(20)', 'label' => 'Profid2', 'enabled' => 1, 'visible' => -1, 'position' => 55),
139 'profid3' => array('type' => 'varchar(20)', 'label' => 'Profid3', 'enabled' => 1, 'visible' => -1, 'position' => 60),
140 'phone' => array('type' => 'varchar(20)', 'label' => 'Phone', 'enabled' => 1, 'visible' => -1, 'position' => 65),
141 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkuserauthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
142 'fk_user_mod' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkusermod', 'enabled' => 1, 'visible' => -1, 'position' => 75),
143 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 80),
144 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 85),
145 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'position' => 500),
146 );
147
148
154 public function __construct($db)
155 {
156 $this->db = $db;
157
158 $this->ismultientitymanaged = 1;
159 }
160
167 public function create($user)
168 {
169 global $conf, $langs;
170
171 $error = 0;
172 $now = dol_now();
173
174 // Clean parameters
175 $this->label = trim($this->label);
176 $this->address = trim($this->address);
177 $this->zip = trim($this->zip);
178 $this->town = trim($this->town);
179
180 if (empty($this->ref)) {
181 $this->ref = '(PROV)';
182 }
183
184 $this->db->begin();
185
186 $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
187 $sql .= "ref";
188 $sql .= ", label";
189 $sql .= ", address";
190 $sql .= ", zip";
191 $sql .= ", town";
192 $sql .= ", fk_country";
193 $sql .= ", status";
194 $sql .= ", entity";
195 $sql .= ", datec";
196 $sql .= ", fk_user_author";
197 $sql .= ", fk_user_mod";
198 $sql .= ") VALUES (";
199 $sql .= "'".$this->db->escape($this->ref)."'";
200 $sql .= ", '".$this->db->escape($this->label)."'";
201 $sql .= ", '".$this->db->escape($this->address)."'";
202 $sql .= ", '".$this->db->escape($this->zip)."'";
203 $sql .= ", '".$this->db->escape($this->town)."'";
204 $sql .= ", ".((int) $this->country_id);
205 $sql .= ", ".((int) $this->status);
206 $sql .= ", ".((int) $conf->entity);
207 $sql .= ", '".$this->db->idate($now)."'";
208 $sql .= ", ".((int) $user->id);
209 $sql .= ", ".((int) $user->id);
210 $sql .= ")";
211
212 dol_syslog(get_class($this)."::create", LOG_DEBUG);
213 $resql = $this->db->query($sql);
214 if (!$resql) {
215 $error++;
216 $this->errors[] = "Error ".$this->db->lasterror();
217 }
218
219 // Commit or rollback
220 if ($error) {
221 foreach ($this->errors as $errmsg) {
222 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
223 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
224 }
225 $this->db->rollback();
226 return -1 * $error;
227 } else {
228 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'establishment');
229
230 $sql = 'UPDATE '.MAIN_DB_PREFIX."establishment SET ref = '".$this->db->escape((string) $this->id)."'";
231 $sql .= " WHERE rowid = ".((int) $this->id);
232 $this->db->query($sql);
233
234 $this->db->commit();
235 return $this->id;
236 }
237 }
238
245 public function update($user)
246 {
247 global $langs;
248
249 // Check parameters
250 if (empty($this->label)) {
251 $this->error = 'ErrorBadParameter';
252 return -1;
253 }
254
255 $this->db->begin();
256
257 $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
258 $sql .= " SET ref = '".$this->db->escape($this->ref)."'";
259 $sql .= ", label = '".$this->db->escape($this->label)."'";
260 $sql .= ", address = '".$this->db->escape($this->address)."'";
261 $sql .= ", zip = '".$this->db->escape($this->zip)."'";
262 $sql .= ", town = '".$this->db->escape($this->town)."'";
263 $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
264 $sql .= ", status = ".((int) $this->status);
265 $sql .= ", fk_user_mod = ".((int) $user->id);
266 $sql .= ", entity = ".((int) $this->entity);
267 $sql .= " WHERE rowid = ".((int) $this->id);
268
269 dol_syslog(get_class($this)."::update", LOG_DEBUG);
270 $result = $this->db->query($sql);
271 if ($result) {
272 $this->db->commit();
273 return 1;
274 } else {
275 $this->error = $this->db->lasterror();
276 $this->db->rollback();
277 return -1;
278 }
279 }
280
287 public function fetch($id)
288 {
289 $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
290 $sql .= ' c.code as country_code, c.label as country';
291 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
292 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
293 $sql .= " WHERE e.rowid = ".((int) $id);
294
295 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
296 $result = $this->db->query($sql);
297 if ($result) {
298 $obj = $this->db->fetch_object($result);
299 if ($obj) {
300 $this->id = $obj->rowid;
301 $this->ref = $obj->ref;
302 $this->label = $obj->label;
303 $this->address = $obj->address;
304 $this->zip = $obj->zip;
305 $this->town = $obj->town;
306 $this->status = $obj->status;
307 $this->entity = $obj->entity;
308
309 $this->country_id = $obj->country_id;
310 $this->country_code = $obj->country_code;
311 $this->country = $obj->country;
312
313 return 1;
314 } else {
315 return 0;
316 }
317 } else {
318 $this->error = $this->db->lasterror();
319 return -1;
320 }
321 }
322
329 public function delete($user)
330 {
331 // An establishment still assigned to users must not be deleted
332 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."user WHERE fk_establishment = ".((int) $this->id);
333 $resql = $this->db->query($sql);
334 if (!$resql) {
335 $this->error = $this->db->lasterror();
336 return -1;
337 }
338 $obj = $this->db->fetch_object($resql);
339 if ($obj && $obj->nb > 0) {
340 $this->error = 'ErrorRecordHasChildren';
341 return -2;
342 }
343
344 $this->db->begin();
345
346 $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".((int) $this->id);
347
348 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
349
350 $result = $this->db->query($sql);
351 if ($result) {
352 $this->db->commit();
353 return 1;
354 } else {
355 $this->error = $this->db->lasterror();
356 $this->db->rollback();
357 return -1;
358 }
359 }
360
367 public function getLibStatut($mode = 0)
368 {
369 return $this->LibStatut($this->status, $mode);
370 }
371
372 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
380 public function LibStatut($status, $mode = 0)
381 {
382 // phpcs:enable
383 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
384 global $langs;
385 //$langs->load("mymodule");
386 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
387 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
388 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
389 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
390 }
391
392 $statusType = 'status'.$status;
393 if ($status == self::STATUS_OPEN) {
394 $statusType = 'status4';
395 }
396 if ($status == self::STATUS_CLOSED) {
397 $statusType = 'status6';
398 }
399
400 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
401 }
402
403
410 public function info($id)
411 {
412 $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
413 $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
414 $sql .= ' WHERE e.rowid = '.((int) $id);
415
416 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
417 $result = $this->db->query($sql);
418
419 if ($result) {
420 if ($this->db->num_rows($result)) {
421 $obj = $this->db->fetch_object($result);
422
423 $this->id = $obj->rowid;
424
425 $this->user_creation_id = $obj->fk_user_author;
426 $this->user_modification_id = $obj->fk_user_mod;
427 $this->date_creation = $this->db->jdate($obj->datec);
428 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
429 }
430 $this->db->free($result);
431 } else {
432 dol_print_error($this->db);
433 }
434 }
435
446 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
447 {
448 global $conf, $langs, $hookmanager;
449
450 if (!empty($conf->dol_no_mouse_hover)) {
451 $notooltip = 1; // Force disable tooltips
452 }
453
454 $result = '';
455
456 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
457 if (isset($this->status)) {
458 $label .= ' '.$this->getLibStatut(5);
459 }
460 $label .= '<br>';
461 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
462
463 $label .= '<br>';
464 $label .= '<b>'.$langs->trans('Residence').':</b> '.$this->address.', '.$this->zip.' '.$this->town;
465
466 $url = DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id;
467
468 if ($option != 'nolink') {
469 // Add param to save lastsearch_values or not
470 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
471 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
472 $add_save_lastsearch_values = 1;
473 }
474 if ($add_save_lastsearch_values) {
475 $url .= '&save_lastsearch_values=1';
476 }
477 }
478
479 $linkclose = '';
480 if (empty($notooltip)) {
481 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
482 $label = $langs->trans("Establishment");
483 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
484 }
485 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
486 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
487 } else {
488 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
489 }
490
491 if ($option == 'nolink') {
492 $linkstart = '<span';
493 } else {
494 $linkstart = '<a href="'.$url.'"';
495 }
496 $linkstart .= $linkclose.'>';
497 if ($option == 'nolink') {
498 $linkend = '</span>';
499 } else {
500 $linkend = '</a>';
501 }
502
503 $result .= $linkstart;
504
505 if ($withpicto) {
506 $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);
507 }
508
509 if ($withpicto != 2) {
510 $result .= $this->label;
511 }
512
513 $result .= $linkend;
514 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
515
516 global $action, $hookmanager;
517 $hookmanager->initHooks(array('establishmentdao'));
518 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
519 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
520 if ($reshook > 0) {
521 $result = $hookmanager->resPrint;
522 } else {
523 $result .= $hookmanager->resPrint;
524 }
525
526 return $result;
527 }
528
534 public function getCountryCode()
535 {
536 global $mysoc;
537
538 // We return country code of bank account
539 if (!empty($this->country_code)) {
540 return $this->country_code;
541 }
542
543 // We return country code of managed company
544 if (!empty($mysoc->country_code)) {
545 return $mysoc->country_code;
546 }
547
548 return '';
549 }
550
557 public function initAsSpecimen()
558 {
559 $this->id = 0;
560 $this->ref = '0';
561 $this->label = 'Department AAA';
562
563 return 1;
564 }
565}
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage establishments.
__construct($db)
Constructor.
getCountryCode()
Return account country code.
update($user)
Update record.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
LibStatut($status, $mode=0)
Return the status.
getLibStatut($mode=0)
Give a label from a status.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
fetch($id)
Load an object from database.
create($user)
Create object in database.
info($id)
Information on record.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div refaddress div address
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79