dolibarr 24.0.0-beta
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-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 $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 ? $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 $this->db->begin();
327
328 $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".((int) $user->id);
329
330 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
331
332 $result = $this->db->query($sql);
333 if ($result) {
334 $this->db->commit();
335 return 1;
336 } else {
337 $this->error = $this->db->lasterror();
338 $this->db->rollback();
339 return -1;
340 }
341 }
342
349 public function getLibStatut($mode = 0)
350 {
351 return $this->LibStatut($this->status, $mode);
352 }
353
354 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
362 public function LibStatut($status, $mode = 0)
363 {
364 // phpcs:enable
365 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
366 global $langs;
367 //$langs->load("mymodule");
368 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
369 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
370 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
371 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
372 }
373
374 $statusType = 'status'.$status;
375 if ($status == self::STATUS_OPEN) {
376 $statusType = 'status4';
377 }
378 if ($status == self::STATUS_CLOSED) {
379 $statusType = 'status6';
380 }
381
382 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
383 }
384
385
392 public function info($id)
393 {
394 $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
395 $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
396 $sql .= ' WHERE e.rowid = '.((int) $id);
397
398 dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
399 $result = $this->db->query($sql);
400
401 if ($result) {
402 if ($this->db->num_rows($result)) {
403 $obj = $this->db->fetch_object($result);
404
405 $this->id = $obj->rowid;
406
407 $this->user_creation_id = $obj->fk_user_author;
408 $this->user_modification_id = $obj->fk_user_mod;
409 $this->date_creation = $this->db->jdate($obj->datec);
410 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
411 }
412 $this->db->free($result);
413 } else {
414 dol_print_error($this->db);
415 }
416 }
417
428 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
429 {
430 global $conf, $langs, $hookmanager;
431
432 if (!empty($conf->dol_no_mouse_hover)) {
433 $notooltip = 1; // Force disable tooltips
434 }
435
436 $result = '';
437
438 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
439 if (isset($this->status)) {
440 $label .= ' '.$this->getLibStatut(5);
441 }
442 $label .= '<br>';
443 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
444
445 $label .= '<br>';
446 $label .= '<b>'.$langs->trans('Residence').':</b> '.$this->address.', '.$this->zip.' '.$this->town;
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 && isset($_SERVER["PHP_SELF"]) && 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 (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
464 $label = $langs->trans("Establishment");
465 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
466 }
467 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
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->label;
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 return 1;
546 }
547}
$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:168
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)
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