dolibarr 25.0.0-alpha
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-2026 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' => 'State', 'enabled' => 1, 'visible' => -1, 'position' => 40),
131 'fk_country' => array('type' => 'integer', 'label' => 'Country', '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' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
137 'fk_user_mod' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', '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 // Call trigger
230 $triggerres = $this->call_trigger('ESTABLISHMENT_CREATE', $user);
231 if ($triggerres < 0) {
232 $this->db->rollback();
233 return -1;
234 }
235 // End call triggers
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 ? ((int) $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 // Call trigger
276 $triggerres = $this->call_trigger('ESTABLISHMENT_MODIFY', $user);
277 if ($triggerres < 0) {
278 $this->db->rollback();
279 return -1;
280 }
281 // End call triggers
282
283 $this->db->commit();
284 return 1;
285 } else {
286 $this->error = $this->db->lasterror();
287 $this->db->rollback();
288 return -1;
289 }
290 }
291
298 public function fetch($id)
299 {
300 $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
301 $sql .= ' c.code as country_code, c.label as country';
302 $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
303 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
304 $sql .= " WHERE e.rowid = ".((int) $id);
305
306 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
307 $result = $this->db->query($sql);
308 if ($result) {
309 $obj = $this->db->fetch_object($result);
310 if ($obj) {
311 $this->id = $obj->rowid;
312 $this->ref = $obj->ref;
313 $this->label = $obj->label;
314 $this->address = $obj->address;
315 $this->zip = $obj->zip;
316 $this->town = $obj->town;
317 $this->status = $obj->status;
318 $this->entity = $obj->entity;
319
320 $this->country_id = $obj->country_id;
321 $this->country_code = $obj->country_code;
322 $this->country = $obj->country;
323
324 return 1;
325 } else {
326 return 0;
327 }
328 } else {
329 $this->error = $this->db->lasterror();
330 return -1;
331 }
332 }
333
340 public function delete($user)
341 {
342 $this->db->begin();
343
344 $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".((int) $this->id);
345
346 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
347
348 $result = $this->db->query($sql);
349 if ($result) {
350 // Call trigger
351 $triggerres = $this->call_trigger('ESTABLISHMENT_DELETE', $user);
352 if ($triggerres < 0) {
353 $this->db->rollback();
354 return -1;
355 }
356 // End call triggers
357
358 $this->db->commit();
359 return 1;
360 } else {
361 $this->error = $this->db->lasterror();
362 $this->db->rollback();
363 return -1;
364 }
365 }
366
373 public function getLibStatut($mode = 0)
374 {
375 return $this->LibStatut($this->status, $mode);
376 }
377
378 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
386 public function LibStatut($status, $mode = 0)
387 {
388 // phpcs:enable
389 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
390 global $langs;
391 //$langs->load("mymodule");
392 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
393 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
394 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
395 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
396 }
397
398 $statusType = 'status'.$status;
399 if ($status == self::STATUS_OPEN) {
400 $statusType = 'status4';
401 }
402 if ($status == self::STATUS_CLOSED) {
403 $statusType = 'status6';
404 }
405
406 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
407 }
408
409
416 public function info($id)
417 {
418 $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
419 $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
420 $sql .= ' WHERE e.rowid = '.((int) $id);
421
422 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
423 $result = $this->db->query($sql);
424
425 if ($result) {
426 if ($this->db->num_rows($result)) {
427 $obj = $this->db->fetch_object($result);
428
429 $this->id = $obj->rowid;
430
431 $this->user_creation_id = $obj->fk_user_author;
432 $this->user_modification_id = $obj->fk_user_mod;
433 $this->date_creation = $this->db->jdate($obj->datec);
434 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
435 }
436 $this->db->free($result);
437 } else {
438 dol_print_error($this->db);
439 }
440 }
441
452 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
453 {
454 global $conf, $langs, $hookmanager;
455
456 if (!empty($conf->dol_no_mouse_hover)) {
457 $notooltip = 1; // Force disable tooltips
458 }
459
460 $result = '';
461
462 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
463 if (isset($this->status)) {
464 $label .= ' '.$this->getLibStatut(5);
465 }
466 $label .= '<br>';
467 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
468
469 $label .= '<br>';
470 $label .= '<b>'.$langs->trans('Residence').':</b> '.$this->address.', '.$this->zip.' '.$this->town;
471
472 $url = DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id;
473
474 if ($option != 'nolink') {
475 // Add param to save lastsearch_values or not
476 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
477 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
478 $add_save_lastsearch_values = 1;
479 }
480 if ($add_save_lastsearch_values) {
481 $url .= '&save_lastsearch_values=1';
482 }
483 }
484
485 $linkclose = '';
486 if (empty($notooltip)) {
487 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
488 $label = $langs->trans("Establishment");
489 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
490 }
491 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
492 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
493 } else {
494 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
495 }
496
497 if ($option == 'nolink') {
498 $linkstart = '<span';
499 } else {
500 $linkstart = '<a href="'.$url.'"';
501 }
502 $linkstart .= $linkclose.'>';
503 if ($option == 'nolink') {
504 $linkend = '</span>';
505 } else {
506 $linkend = '</a>';
507 }
508
509 $result .= $linkstart;
510
511 if ($withpicto) {
512 $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);
513 }
514
515 if ($withpicto != 2) {
516 $result .= $this->label;
517 }
518
519 $result .= $linkend;
520 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
521
522 global $action, $hookmanager;
523 $hookmanager->initHooks(array('establishmentdao'));
524 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
525 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
526 if ($reshook > 0) {
527 $result = $hookmanager->resPrint;
528 } else {
529 $result .= $hookmanager->resPrint;
530 }
531
532 return $result;
533 }
534
540 public function getCountryCode()
541 {
542 global $mysoc;
543
544 // We return country code of bank account
545 if (!empty($this->country_code)) {
546 return $this->country_code;
547 }
548
549 // We return country code of managed company
550 if (!empty($mysoc->country_code)) {
551 return $mysoc->country_code;
552 }
553
554 return '';
555 }
556
563 public function initAsSpecimen()
564 {
565 $this->id = 0;
566 $this->ref = '0';
567 $this->label = 'Department AAA';
568
569 return 1;
570 }
571}
$object ref
Definition info.php:90
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.
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