dolibarr 24.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-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2026 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 $fk_user_mod;
107
111 public $fk_user_author;
112
116 public $datec;
117
118 const STATUS_OPEN = 1;
119 const STATUS_CLOSED = 0;
120
121
122 public $fields = array(
123 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
124 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1),
125 'ref' => array('type' => 'varchar(30)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 20),
126 'label' => array('type' => 'varchar(128)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 2, 'position' => 22),
127 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 25),
128 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 30),
129 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 35),
130 'fk_state' => array('type' => 'integer', 'label' => 'Fkstate', 'enabled' => 1, 'visible' => -1, 'position' => 40),
131 'fk_country' => array('type' => 'integer', 'label' => 'Fkcountry', 'enabled' => 1, 'visible' => -1, 'position' => 45),
132 'profid1' => array('type' => 'varchar(20)', 'label' => 'Profid1', 'enabled' => 1, 'visible' => -1, 'position' => 50),
133 'profid2' => array('type' => 'varchar(20)', 'label' => 'Profid2', 'enabled' => 1, 'visible' => -1, 'position' => 55),
134 'profid3' => array('type' => 'varchar(20)', 'label' => 'Profid3', 'enabled' => 1, 'visible' => -1, 'position' => 60),
135 'phone' => array('type' => 'varchar(20)', 'label' => 'Phone', 'enabled' => 1, 'visible' => -1, 'position' => 65),
136 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkuserauthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
137 'fk_user_mod' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkusermod', 'enabled' => 1, 'visible' => -1, 'position' => 75),
138 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 80),
139 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 85),
140 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'position' => 500),
141 );
142
143
149 public function __construct($db)
150 {
151 $this->db = $db;
152
153 $this->ismultientitymanaged = 1;
154 }
155
162 public function create($user)
163 {
164 global $conf, $langs;
165
166 $error = 0;
167 $now = dol_now();
168
169 // Clean parameters
170 $this->label = trim($this->label);
171 $this->address = trim($this->address);
172 $this->zip = trim($this->zip);
173 $this->town = trim($this->town);
174
175 if (empty($this->ref)) {
176 $this->ref = '(PROV)';
177 }
178
179 $this->db->begin();
180
181 $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
182 $sql .= "ref";
183 $sql .= ", label";
184 $sql .= ", address";
185 $sql .= ", zip";
186 $sql .= ", town";
187 $sql .= ", fk_country";
188 $sql .= ", status";
189 $sql .= ", entity";
190 $sql .= ", datec";
191 $sql .= ", fk_user_author";
192 $sql .= ", fk_user_mod";
193 $sql .= ") VALUES (";
194 $sql .= "'".$this->db->escape($this->ref)."'";
195 $sql .= ", '".$this->db->escape($this->label)."'";
196 $sql .= ", '".$this->db->escape($this->address)."'";
197 $sql .= ", '".$this->db->escape($this->zip)."'";
198 $sql .= ", '".$this->db->escape($this->town)."'";
199 $sql .= ", ".((int) $this->country_id);
200 $sql .= ", ".((int) $this->status);
201 $sql .= ", ".((int) $conf->entity);
202 $sql .= ", '".$this->db->idate($now)."'";
203 $sql .= ", ".((int) $user->id);
204 $sql .= ", ".((int) $user->id);
205 $sql .= ")";
206
207 dol_syslog(get_class($this)."::create", LOG_DEBUG);
208 $resql = $this->db->query($sql);
209 if (!$resql) {
210 $error++;
211 $this->errors[] = "Error ".$this->db->lasterror();
212 }
213
214 // Commit or rollback
215 if ($error) {
216 foreach ($this->errors as $errmsg) {
217 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
218 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
219 }
220 $this->db->rollback();
221 return -1 * $error;
222 } else {
223 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'establishment');
224
225 $sql = 'UPDATE '.MAIN_DB_PREFIX."establishment SET ref = '".$this->db->escape((string) $this->id)."'";
226 $sql .= " WHERE rowid = ".((int) $this->id);
227 $this->db->query($sql);
228
229 $this->db->commit();
230 return $this->id;
231 }
232 }
233
240 public function update($user)
241 {
242 global $langs;
243
244 // Check parameters
245 if (empty($this->label)) {
246 $this->error = 'ErrorBadParameter';
247 return -1;
248 }
249
250 $this->db->begin();
251
252 $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
253 $sql .= " SET ref = '".$this->db->escape($this->ref)."'";
254 $sql .= ", label = '".$this->db->escape($this->label)."'";
255 $sql .= ", address = '".$this->db->escape($this->address)."'";
256 $sql .= ", zip = '".$this->db->escape($this->zip)."'";
257 $sql .= ", town = '".$this->db->escape($this->town)."'";
258 $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : 'null');
259 $sql .= ", status = ".((int) $this->status);
260 $sql .= ", fk_user_mod = ".((int) $user->id);
261 $sql .= ", entity = ".((int) $this->entity);
262 $sql .= " WHERE rowid = ".((int) $this->id);
263
264 dol_syslog(get_class($this)."::update", LOG_DEBUG);
265 $result = $this->db->query($sql);
266 if ($result) {
267 $this->db->commit();
268 return 1;
269 } else {
270 $this->error = $this->db->lasterror();
271 $this->db->rollback();
272 return -1;
273 }
274 }
275
282 public function fetch($id)
283 {
284 $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
285 $sql .= ' c.code as country_code, c.label as country';
286 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
287 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
288 $sql .= " WHERE e.rowid = ".((int) $id);
289
290 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
291 $result = $this->db->query($sql);
292 if ($result) {
293 $obj = $this->db->fetch_object($result);
294 if ($obj) {
295 $this->id = $obj->rowid;
296 $this->ref = $obj->ref;
297 $this->label = $obj->label;
298 $this->address = $obj->address;
299 $this->zip = $obj->zip;
300 $this->town = $obj->town;
301 $this->status = $obj->status;
302 $this->entity = $obj->entity;
303
304 $this->country_id = $obj->country_id;
305 $this->country_code = $obj->country_code;
306 $this->country = $obj->country;
307
308 return 1;
309 } else {
310 return 0;
311 }
312 } else {
313 $this->error = $this->db->lasterror();
314 return -1;
315 }
316 }
317
324 public function delete($user)
325 {
326 // An establishment still assigned to users must not be deleted
327 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."user WHERE fk_establishment = ".((int) $this->id);
328 $resql = $this->db->query($sql);
329 if (!$resql) {
330 $this->error = $this->db->lasterror();
331 return -1;
332 }
333 $obj = $this->db->fetch_object($resql);
334 if ($obj && $obj->nb > 0) {
335 $this->error = 'ErrorRecordHasChildren';
336 return -2;
337 }
338
339 $this->db->begin();
340
341 $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".((int) $this->id);
342
343 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
344
345 $result = $this->db->query($sql);
346 if ($result) {
347 $this->db->commit();
348 return 1;
349 } else {
350 $this->error = $this->db->lasterror();
351 $this->db->rollback();
352 return -1;
353 }
354 }
355
362 public function getLibStatut($mode = 0)
363 {
364 return $this->LibStatut($this->status, $mode);
365 }
366
367 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
375 public function LibStatut($status, $mode = 0)
376 {
377 // phpcs:enable
378 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
379 global $langs;
380 //$langs->load("mymodule");
381 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
382 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
383 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
384 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
385 }
386
387 $statusType = 'status'.$status;
388 if ($status == self::STATUS_OPEN) {
389 $statusType = 'status4';
390 }
391 if ($status == self::STATUS_CLOSED) {
392 $statusType = 'status6';
393 }
394
395 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
396 }
397
398
405 public function info($id)
406 {
407 $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
408 $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
409 $sql .= ' WHERE e.rowid = '.((int) $id);
410
411 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
412 $result = $this->db->query($sql);
413
414 if ($result) {
415 if ($this->db->num_rows($result)) {
416 $obj = $this->db->fetch_object($result);
417
418 $this->id = $obj->rowid;
419
420 $this->user_creation_id = $obj->fk_user_author;
421 $this->user_modification_id = $obj->fk_user_mod;
422 $this->date_creation = $this->db->jdate($obj->datec);
423 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
424 }
425 $this->db->free($result);
426 } else {
427 dol_print_error($this->db);
428 }
429 }
430
441 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
442 {
443 global $conf, $langs, $hookmanager;
444
445 if (!empty($conf->dol_no_mouse_hover)) {
446 $notooltip = 1; // Force disable tooltips
447 }
448
449 $result = '';
450
451 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
452 if (isset($this->status)) {
453 $label .= ' '.$this->getLibStatut(5);
454 }
455 $label .= '<br>';
456 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
457
458 $label .= '<br>';
459 $label .= '<b>'.$langs->trans('Residence').':</b> '.$this->address.', '.$this->zip.' '.$this->town;
460
461 $url = DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id;
462
463 if ($option != 'nolink') {
464 // Add param to save lastsearch_values or not
465 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
466 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
467 $add_save_lastsearch_values = 1;
468 }
469 if ($add_save_lastsearch_values) {
470 $url .= '&save_lastsearch_values=1';
471 }
472 }
473
474 $linkclose = '';
475 if (empty($notooltip)) {
476 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
477 $label = $langs->trans("Establishment");
478 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
479 }
480 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
481 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
482 } else {
483 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
484 }
485
486 if ($option == 'nolink') {
487 $linkstart = '<span';
488 } else {
489 $linkstart = '<a href="'.$url.'"';
490 }
491 $linkstart .= $linkclose.'>';
492 if ($option == 'nolink') {
493 $linkend = '</span>';
494 } else {
495 $linkend = '</a>';
496 }
497
498 $result .= $linkstart;
499
500 if ($withpicto) {
501 $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);
502 }
503
504 if ($withpicto != 2) {
505 $result .= $this->label;
506 }
507
508 $result .= $linkend;
509 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
510
511 global $action, $hookmanager;
512 $hookmanager->initHooks(array('establishmentdao'));
513 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
514 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
515 if ($reshook > 0) {
516 $result = $hookmanager->resPrint;
517 } else {
518 $result .= $hookmanager->resPrint;
519 }
520
521 return $result;
522 }
523
529 public function getCountryCode()
530 {
531 global $mysoc;
532
533 // We return country code of bank account
534 if (!empty($this->country_code)) {
535 return $this->country_code;
536 }
537
538 // We return country code of managed company
539 if (!empty($mysoc->country_code)) {
540 return $mysoc->country_code;
541 }
542
543 return '';
544 }
545
552 public function initAsSpecimen()
553 {
554 $this->id = 0;
555 $this->ref = '0';
556 $this->label = 'Department AAA';
557
558 return 1;
559 }
560}
$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:169
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
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)
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