establishment.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. /**
  19. * \file htdocs/hrm/class/establishment.class.php
  20. * \ingroup HRM
  21. * \brief File of class to manage establishments
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. /**
  25. * Class to manage establishments
  26. */
  27. class Establishment extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'establishment';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'establishment';
  37. /**
  38. * @var string Name of subtable line
  39. */
  40. public $table_element_line = '';
  41. /**
  42. * @var string Field with ID of parent key if this field has a parent
  43. */
  44. public $fk_element = 'fk_establishment';
  45. /**
  46. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  47. * @var int
  48. */
  49. public $ismultientitymanaged = 1;
  50. /**
  51. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  52. */
  53. public $picto = 'establishment';
  54. /**
  55. * @var int ID
  56. */
  57. public $id;
  58. /**
  59. * @var string Ref
  60. */
  61. public $ref;
  62. /**
  63. * @var int ID
  64. */
  65. public $rowid;
  66. /**
  67. * @var string Label
  68. */
  69. public $label;
  70. /**
  71. * @var string Address
  72. */
  73. public $address;
  74. /**
  75. * @var string Zip
  76. */
  77. public $zip;
  78. /**
  79. * @var string Town
  80. */
  81. public $town;
  82. /**
  83. * @var int country id
  84. */
  85. public $country_id;
  86. /**
  87. * @var int Status 0=open, 1=closed
  88. */
  89. public $status;
  90. /**
  91. * @var int Entity
  92. */
  93. public $entity;
  94. /**
  95. * @var int user mod id
  96. */
  97. public $fk_user_mod;
  98. /**
  99. * @var int user author id
  100. */
  101. public $fk_user_author;
  102. /**
  103. * @var int date create
  104. */
  105. public $datec;
  106. const STATUS_OPEN = 1;
  107. const STATUS_CLOSED = 0;
  108. public $fields = array(
  109. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  110. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
  111. 'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>20),
  112. 'label' =>array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'showoncombobox'=>2, 'position'=>22),
  113. 'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
  114. 'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
  115. 'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
  116. 'fk_state' =>array('type'=>'integer', 'label'=>'Fkstate', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
  117. 'fk_country' =>array('type'=>'integer', 'label'=>'Fkcountry', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
  118. 'profid1' =>array('type'=>'varchar(20)', 'label'=>'Profid1', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
  119. 'profid2' =>array('type'=>'varchar(20)', 'label'=>'Profid2', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  120. 'profid3' =>array('type'=>'varchar(20)', 'label'=>'Profid3', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  121. 'phone' =>array('type'=>'varchar(20)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  122. 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fkuserauthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
  123. 'fk_user_mod' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fkusermod', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  124. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>80),
  125. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>85),
  126. 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
  127. );
  128. /**
  129. * Constructor
  130. *
  131. * @param DoliDB $db Database handler
  132. */
  133. public function __construct($db)
  134. {
  135. $this->db = $db;
  136. }
  137. /**
  138. * Create object in database
  139. *
  140. * @param User $user User making creation
  141. * @return int <0 if KO, >0 if OK
  142. */
  143. public function create($user)
  144. {
  145. global $conf, $langs;
  146. $error = 0;
  147. $now = dol_now();
  148. // Clean parameters
  149. $this->label = trim($this->label);
  150. $this->address = trim($this->address);
  151. $this->zip = trim($this->zip);
  152. $this->town = trim($this->town);
  153. if (empty($this->ref)) {
  154. $this->ref = '(PROV)';
  155. }
  156. $this->db->begin();
  157. $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
  158. $sql .= "ref";
  159. $sql .= ", label";
  160. $sql .= ", address";
  161. $sql .= ", zip";
  162. $sql .= ", town";
  163. $sql .= ", status";
  164. $sql .= ", fk_country";
  165. $sql .= ", entity";
  166. $sql .= ", datec";
  167. $sql .= ", fk_user_author";
  168. $sql .= ", fk_user_mod";
  169. $sql .= ") VALUES (";
  170. $sql .= "'".$this->db->escape($this->ref)."'";
  171. $sql .= ", '".$this->db->escape($this->label)."'";
  172. $sql .= ", '".$this->db->escape($this->address)."'";
  173. $sql .= ", '".$this->db->escape($this->zip)."'";
  174. $sql .= ", '".$this->db->escape($this->town)."'";
  175. $sql .= ", ".((int) $this->country_id);
  176. $sql .= ", ".((int) $this->status);
  177. $sql .= ", ".((int) $conf->entity);
  178. $sql .= ", '".$this->db->idate($now)."'";
  179. $sql .= ", ".((int) $user->id);
  180. $sql .= ", ".((int) $user->id);
  181. $sql .= ")";
  182. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  183. $resql = $this->db->query($sql);
  184. if (!$resql) {
  185. $error++;
  186. $this->errors[] = "Error ".$this->db->lasterror();
  187. }
  188. // Commit or rollback
  189. if ($error) {
  190. foreach ($this->errors as $errmsg) {
  191. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  192. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  193. }
  194. $this->db->rollback();
  195. return -1 * $error;
  196. } else {
  197. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'establishment');
  198. $sql = 'UPDATE '.MAIN_DB_PREFIX."establishment SET ref = '".$this->db->escape($this->id)."'";
  199. $sql .= " WHERE rowid = ".((int) $this->id);
  200. $this->db->query($sql);
  201. $this->db->commit();
  202. return $this->id;
  203. }
  204. }
  205. /**
  206. * Update record
  207. *
  208. * @param User $user User making update
  209. * @return int <0 if KO, >0 if OK
  210. */
  211. public function update($user)
  212. {
  213. global $langs;
  214. // Check parameters
  215. if (empty($this->label)) {
  216. $this->error = 'ErrorBadParameter';
  217. return -1;
  218. }
  219. $this->db->begin();
  220. $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
  221. $sql .= " SET ref = '".$this->db->escape($this->ref)."'";
  222. $sql .= ", label = '".$this->db->escape($this->label)."'";
  223. $sql .= ", address = '".$this->db->escape($this->address)."'";
  224. $sql .= ", zip = '".$this->db->escape($this->zip)."'";
  225. $sql .= ", town = '".$this->db->escape($this->town)."'";
  226. $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
  227. $sql .= ", status = ".((int) $this->status);
  228. $sql .= ", fk_user_mod = ".((int) $user->id);
  229. $sql .= ", entity = ".((int) $this->entity);
  230. $sql .= " WHERE rowid = ".((int) $this->id);
  231. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  232. $result = $this->db->query($sql);
  233. if ($result) {
  234. $this->db->commit();
  235. return 1;
  236. } else {
  237. $this->error = $this->db->lasterror();
  238. $this->db->rollback();
  239. return -1;
  240. }
  241. }
  242. /**
  243. * Load an object from database
  244. *
  245. * @param int $id Id of record to load
  246. * @return int <0 if KO, >=0 if OK
  247. */
  248. public function fetch($id)
  249. {
  250. $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,";
  251. $sql .= ' c.code as country_code, c.label as country';
  252. $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e";
  253. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
  254. $sql .= " WHERE e.rowid = ".((int) $id);
  255. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  256. $result = $this->db->query($sql);
  257. if ($result) {
  258. $obj = $this->db->fetch_object($result);
  259. if ($obj) {
  260. $this->id = $obj->rowid;
  261. $this->ref = $obj->ref;
  262. $this->label = $obj->label;
  263. $this->address = $obj->address;
  264. $this->zip = $obj->zip;
  265. $this->town = $obj->town;
  266. $this->status = $obj->status;
  267. $this->entity = $obj->entity;
  268. $this->country_id = $obj->country_id;
  269. $this->country_code = $obj->country_code;
  270. $this->country = $obj->country;
  271. return 1;
  272. } else {
  273. return 0;
  274. }
  275. } else {
  276. $this->error = $this->db->lasterror();
  277. return -1;
  278. }
  279. }
  280. /**
  281. * Delete record
  282. *
  283. * @param int $id Id of record to delete
  284. * @return int <0 if KO, >0 if OK
  285. */
  286. public function delete($id)
  287. {
  288. $this->db->begin();
  289. $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".((int) $id);
  290. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  291. $result = $this->db->query($sql);
  292. if ($result) {
  293. $this->db->commit();
  294. return 1;
  295. } else {
  296. $this->error = $this->db->lasterror();
  297. $this->db->rollback();
  298. return -1;
  299. }
  300. }
  301. /**
  302. * Give a label from a status
  303. *
  304. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  305. * @return string Label of status
  306. */
  307. public function getLibStatut($mode = 0)
  308. {
  309. return $this->LibStatut($this->status, $mode);
  310. }
  311. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  312. /**
  313. * Return the status
  314. *
  315. * @param int $status Id status
  316. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  317. * @return string Label of status
  318. */
  319. public function LibStatut($status, $mode = 0)
  320. {
  321. // phpcs:enable
  322. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  323. global $langs;
  324. //$langs->load("mymodule");
  325. $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
  326. $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
  327. $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Open');
  328. $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
  329. }
  330. $statusType = 'status'.$status;
  331. if ($status == self::STATUS_OPEN) {
  332. $statusType = 'status4';
  333. }
  334. if ($status == self::STATUS_CLOSED) {
  335. $statusType = 'status6';
  336. }
  337. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  338. }
  339. /**
  340. * Information on record
  341. *
  342. * @param int $id Id of record
  343. * @return void
  344. */
  345. public function info($id)
  346. {
  347. $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity';
  348. $sql .= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
  349. $sql .= ' WHERE e.rowid = '.((int) $id);
  350. dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
  351. $result = $this->db->query($sql);
  352. if ($result) {
  353. if ($this->db->num_rows($result)) {
  354. $obj = $this->db->fetch_object($result);
  355. $this->id = $obj->rowid;
  356. $this->user_creation_id = $obj->fk_user_author;
  357. $this->user_modification_id = $obj->fk_user_mod;
  358. $this->date_creation = $this->db->jdate($obj->datec);
  359. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  360. }
  361. $this->db->free($result);
  362. } else {
  363. dol_print_error($this->db);
  364. }
  365. }
  366. /**
  367. * Return a link to the object card (with optionaly the picto)
  368. *
  369. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  370. * @param string $option On what the link point to ('nolink', ...)
  371. * @param int $notooltip 1=Disable tooltip
  372. * @param string $morecss Add more css on link
  373. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  374. * @return string String with URL
  375. */
  376. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  377. {
  378. global $conf, $langs, $hookmanager;
  379. if (!empty($conf->dol_no_mouse_hover)) {
  380. $notooltip = 1; // Force disable tooltips
  381. }
  382. $result = '';
  383. $label = img_picto('', $this->picto).' <u>'.$langs->trans("Establishment").'</u>';
  384. if (isset($this->status)) {
  385. $label .= ' '.$this->getLibStatut(5);
  386. }
  387. $label .= '<br>';
  388. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  389. $label .= '<br>';
  390. $label .= '<b>'.$langs->trans('Residence').':</b> '.$this->address.', '.$this->zip.' '.$this->town;
  391. $url = DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id;
  392. if ($option != 'nolink') {
  393. // Add param to save lastsearch_values or not
  394. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  395. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  396. $add_save_lastsearch_values = 1;
  397. }
  398. if ($add_save_lastsearch_values) {
  399. $url .= '&save_lastsearch_values=1';
  400. }
  401. }
  402. $linkclose = '';
  403. if (empty($notooltip)) {
  404. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  405. $label = $langs->trans("Establishment");
  406. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  407. }
  408. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  409. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  410. } else {
  411. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  412. }
  413. if ($option == 'nolink') {
  414. $linkstart = '<span';
  415. } else {
  416. $linkstart = '<a href="'.$url.'"';
  417. }
  418. $linkstart .= $linkclose.'>';
  419. if ($option == 'nolink') {
  420. $linkend = '</span>';
  421. } else {
  422. $linkend = '</a>';
  423. }
  424. $result .= $linkstart;
  425. if ($withpicto) {
  426. $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);
  427. }
  428. if ($withpicto != 2) {
  429. $result .= $this->label;
  430. }
  431. $result .= $linkend;
  432. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  433. global $action, $hookmanager;
  434. $hookmanager->initHooks(array('establishmentdao'));
  435. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  436. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  437. if ($reshook > 0) {
  438. $result = $hookmanager->resPrint;
  439. } else {
  440. $result .= $hookmanager->resPrint;
  441. }
  442. return $result;
  443. }
  444. /**
  445. * Return account country code
  446. *
  447. * @return string country code
  448. */
  449. public function getCountryCode()
  450. {
  451. global $mysoc;
  452. // We return country code of bank account
  453. if (!empty($this->country_code)) {
  454. return $this->country_code;
  455. }
  456. // We return country code of managed company
  457. if (!empty($mysoc->country_code)) {
  458. return $mysoc->country_code;
  459. }
  460. return '';
  461. }
  462. /**
  463. * Initialise object with example values
  464. * Id must be 0 if object instance is a specimen
  465. *
  466. * @return void
  467. */
  468. public function initAsSpecimen()
  469. {
  470. $this->id = 0;
  471. $this->ref = '0';
  472. $this->label = 'Department AAA';
  473. }
  474. }