establishment.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/hrm/class/establishment.class.php
  19. * \ingroup HRM
  20. * \brief File of class to manage establishments
  21. */
  22. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  23. /**
  24. * Class to manage establishments
  25. */
  26. class Establishment extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element='establishment';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element='establishment';
  36. /**
  37. * @var int Name of subtable line
  38. */
  39. public $table_element_line = '';
  40. /**
  41. * @var int Field with ID of parent key if this field has a parent
  42. */
  43. public $fk_element = 'fk_establishment';
  44. public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. public $picto='building';
  46. /**
  47. * @var int ID
  48. */
  49. public $id;
  50. /**
  51. * @var string Ref
  52. */
  53. public $ref;
  54. /**
  55. * @var int ID
  56. */
  57. public $rowid;
  58. public $name;
  59. public $address;
  60. public $zip;
  61. public $town;
  62. public $status; // 0=open, 1=closed
  63. /**
  64. * @var int Entity
  65. */
  66. public $entity;
  67. public $country_id;
  68. public $statuts=array();
  69. public $statuts_short=array();
  70. /**
  71. * Constructor
  72. *
  73. * @param DoliDB $db Database handler
  74. */
  75. function __construct($db)
  76. {
  77. $this->db = $db;
  78. $this->statuts_short = array(0 => 'Closed', 1 => 'Opened');
  79. $this->statuts = array(0 => 'Closed', 1 => 'Opened');
  80. }
  81. /**
  82. * Create object in database
  83. *
  84. * @param User $user User making creation
  85. * @return int <0 if KO, >0 if OK
  86. */
  87. function create($user)
  88. {
  89. global $conf, $langs;
  90. $error = 0;
  91. $ret = 0;
  92. $now=dol_now();
  93. // Clean parameters
  94. $this->address=($this->address>0?$this->address:$this->address);
  95. $this->zip=($this->zip>0?$this->zip:$this->zip);
  96. $this->town=($this->town>0?$this->town:$this->town);
  97. $this->country_id=($this->country_id>0?$this->country_id:$this->country_id);
  98. $this->db->begin();
  99. $sql = "INSERT INTO ".MAIN_DB_PREFIX."establishment (";
  100. $sql.= "name";
  101. $sql.= ", address";
  102. $sql.= ", zip";
  103. $sql.= ", town";
  104. $sql.= ", status";
  105. $sql.= ", fk_country";
  106. $sql.= ", entity";
  107. $sql.= ", datec";
  108. $sql.= ", fk_user_author";
  109. $sql.= ", fk_user_mod";
  110. $sql.= ") VALUES (";
  111. $sql.= " '".$this->db->escape($this->name)."'";
  112. $sql.= ", '".$this->db->escape($this->address)."'";
  113. $sql.= ", '".$this->db->escape($this->zip)."'";
  114. $sql.= ", '".$this->db->escape($this->town)."'";
  115. $sql.= ", ".$this->country_id;
  116. $sql.= ", ".$this->status;
  117. $sql.= ", ".$conf->entity;
  118. $sql.= ", '".$this->db->idate($now)."'";
  119. $sql.= ", ". $user->id;
  120. $sql.= ", ". $user->id;
  121. $sql.= ")";
  122. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  123. $resql = $this->db->query($sql);
  124. if (! $resql) {
  125. $error ++;
  126. $this->errors[] = "Error " . $this->db->lasterror();
  127. }
  128. if (! $error) {
  129. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "establishment");
  130. }
  131. // Commit or rollback
  132. if ($error) {
  133. foreach ( $this->errors as $errmsg ) {
  134. dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
  135. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  136. }
  137. $this->db->rollback();
  138. return - 1 * $error;
  139. } else {
  140. $this->db->commit();
  141. return $this->id;
  142. }
  143. }
  144. /**
  145. * Update record
  146. *
  147. * @param User $user User making update
  148. * @return int <0 if KO, >0 if OK
  149. */
  150. function update($user)
  151. {
  152. global $langs;
  153. // Check parameters
  154. if (empty($this->name))
  155. {
  156. $this->error='ErrorBadParameter';
  157. return -1;
  158. }
  159. $this->db->begin();
  160. $sql = "UPDATE ".MAIN_DB_PREFIX."establishment";
  161. $sql .= " SET name = '".$this->db->escape($this->name)."'";
  162. $sql .= ", address = '".$this->db->escape($this->address)."'";
  163. $sql .= ", zip = '".$this->db->escape($this->zip)."'";
  164. $sql .= ", town = '".$this->db->escape($this->town)."'";
  165. $sql .= ", fk_country = ".($this->country_id > 0 ? $this->country_id : 'null');
  166. $sql .= ", status = ".$this->db->escape($this->status);
  167. $sql .= ", fk_user_mod = " . $user->id;
  168. $sql .= " WHERE rowid = ".$this->id;
  169. dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  170. $result = $this->db->query($sql);
  171. if ($result) {
  172. $this->db->commit();
  173. return 1;
  174. } else {
  175. $this->error = $this->db->lasterror();
  176. $this->db->rollback();
  177. return - 1;
  178. }
  179. }
  180. /**
  181. * Load an object from database
  182. *
  183. * @param int $id Id of record to load
  184. * @return int <0 if KO, >0 if OK
  185. */
  186. function fetch($id)
  187. {
  188. $sql = "SELECT e.rowid, e.name, e.address, e.zip, e.town, e.status, e.fk_country as country_id,";
  189. $sql.= ' c.code as country_code, c.label as country';
  190. $sql.= " FROM ".MAIN_DB_PREFIX."establishment as e";
  191. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON e.fk_country = c.rowid';
  192. $sql.= " WHERE e.rowid = ".$id;
  193. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  194. $result = $this->db->query($sql);
  195. if ( $result )
  196. {
  197. $obj = $this->db->fetch_object($result);
  198. $this->id = $obj->rowid;
  199. $this->ref = $obj->rowid;
  200. $this->name = $obj->name;
  201. $this->address = $obj->address;
  202. $this->zip = $obj->zip;
  203. $this->town = $obj->town;
  204. $this->status = $obj->status;
  205. $this->country_id = $obj->country_id;
  206. $this->country_code = $obj->country_code;
  207. $this->country = $obj->country;
  208. return 1;
  209. }
  210. else
  211. {
  212. $this->error=$this->db->lasterror();
  213. return -1;
  214. }
  215. }
  216. /**
  217. * Delete record
  218. *
  219. * @param int $id Id of record to delete
  220. * @return int <0 if KO, >0 if OK
  221. */
  222. function delete($id)
  223. {
  224. $this->db->begin();
  225. $sql = "DELETE FROM ".MAIN_DB_PREFIX."establishment WHERE rowid = ".$id;
  226. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  227. $result = $this->db->query($sql);
  228. if ($result)
  229. {
  230. $this->db->commit();
  231. return 1;
  232. }
  233. else
  234. {
  235. $this->error=$this->db->lasterror();
  236. $this->db->rollback();
  237. return -1;
  238. }
  239. }
  240. /**
  241. * Give a label from a status
  242. *
  243. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  244. * @return string Label
  245. */
  246. function getLibStatut($mode=0)
  247. {
  248. return $this->LibStatut($this->status,$mode);
  249. }
  250. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  251. /**
  252. * Give a label from a status
  253. *
  254. * @param int $status Id status
  255. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  256. * @return string Label
  257. */
  258. function LibStatut($status,$mode=0)
  259. {
  260. // phpcs:enable
  261. global $langs;
  262. if ($mode == 0)
  263. {
  264. return $langs->trans($this->statuts[$status]);
  265. }
  266. elseif ($mode == 1)
  267. {
  268. return $langs->trans($this->statuts_short[$status]);
  269. }
  270. elseif ($mode == 2)
  271. {
  272. if ($status==0) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts_short[$status]);
  273. if ($status==1) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts_short[$status]);
  274. }
  275. elseif ($mode == 3)
  276. {
  277. if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5');
  278. if ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4');
  279. }
  280. elseif ($mode == 4)
  281. {
  282. if ($status==0 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut5').' '.$langs->trans($this->statuts[$status]);
  283. if ($status==1 && ! empty($this->statuts_short[$status])) return img_picto($langs->trans($this->statuts_short[$status]),'statut4').' '.$langs->trans($this->statuts[$status]);
  284. }
  285. elseif ($mode == 5)
  286. {
  287. if ($status==0 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
  288. if ($status==1 && ! empty($this->statuts_short[$status])) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut4');
  289. }
  290. }
  291. /**
  292. * Information on record
  293. *
  294. * @param int $id Id of record
  295. * @return void
  296. */
  297. function info($id)
  298. {
  299. $sql = 'SELECT e.rowid, e.datec, e.fk_user_author, e.tms, e.fk_user_mod';
  300. $sql.= ' FROM '.MAIN_DB_PREFIX.'establishment as e';
  301. $sql.= ' WHERE e.rowid = '.$id;
  302. dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
  303. $result = $this->db->query($sql);
  304. if ($result)
  305. {
  306. if ($this->db->num_rows($result))
  307. {
  308. $obj = $this->db->fetch_object($result);
  309. $this->id = $obj->rowid;
  310. $this->date_creation = $this->db->jdate($obj->datec);
  311. if ($obj->fk_user_author)
  312. {
  313. $cuser = new User($this->db);
  314. $cuser->fetch($obj->fk_user_author);
  315. $this->user_creation = $cuser;
  316. }
  317. if ($obj->fk_user_mod)
  318. {
  319. $muser = new User($this->db);
  320. $muser->fetch($obj->fk_user_mod);
  321. $this->user_modification = $muser;
  322. $this->date_modification = $this->db->jdate($obj->tms);
  323. }
  324. }
  325. $this->db->free($result);
  326. }
  327. else
  328. {
  329. dol_print_error($this->db);
  330. }
  331. }
  332. /**
  333. * Return clicable name (with picto eventually)
  334. *
  335. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  336. * @return string String with URL
  337. */
  338. function getNomUrl($withpicto=0)
  339. {
  340. global $langs;
  341. $result='';
  342. $link = '<a href="'.DOL_URL_ROOT.'/hrm/establishment/card.php?id='.$this->id.'">';
  343. $linkend='</a>';
  344. $picto='building';
  345. $label=$langs->trans("Show").': '.$this->name;
  346. if ($withpicto) $result.=($link.img_object($label,$picto).$linkend);
  347. if ($withpicto && $withpicto != 2) $result.=' ';
  348. if ($withpicto != 2) $result.=$link.$this->name.$linkend;
  349. return $result;
  350. }
  351. /**
  352. * Return account country code
  353. *
  354. * @return string country code
  355. */
  356. function getCountryCode()
  357. {
  358. global $mysoc;
  359. // We return country code of bank account
  360. if (! empty($this->country_code)) return $this->country_code;
  361. // We return country code of managed company
  362. if (! empty($mysoc->country_code)) return $mysoc->country_code;
  363. return '';
  364. }
  365. /**
  366. * Initialise object with example values
  367. * Id must be 0 if object instance is a specimen
  368. *
  369. * @return void
  370. */
  371. public function initAsSpecimen()
  372. {
  373. $this->id = 0;
  374. $this->ref = 'DEAAA';
  375. }
  376. }