fiscalyear.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /* Copyright (C) 2014-2017 Alexandre Spangaro <aspangaro@zendsi.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/core/class/fiscalyear.class.php
  19. * \ingroup fiscal year
  20. * \brief File of class to manage fiscal years
  21. */
  22. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  23. /**
  24. * Class to manage fiscal year
  25. */
  26. class Fiscalyear extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element='fiscalyear';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element='accounting_fiscalyear';
  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 = '';
  44. public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. /**
  46. * @var int ID
  47. */
  48. public $rowid;
  49. /**
  50. * @var string fiscal year label
  51. */
  52. public $label;
  53. public $date_start;
  54. public $date_end;
  55. public $datec;
  56. public $statut; // 0=open, 1=closed
  57. public $entity;
  58. public $statuts=array();
  59. public $statuts_short=array();
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDB $db Database handler
  64. */
  65. function __construct($db)
  66. {
  67. $this->db = $db;
  68. $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
  69. $this->statuts = array(0 => 'Opened', 1 => 'Closed');
  70. }
  71. /**
  72. * Create object in database
  73. *
  74. * @param User $user User making creation
  75. * @return int <0 if KO, >0 if OK
  76. */
  77. function create($user)
  78. {
  79. global $conf;
  80. $error = 0;
  81. $now=dol_now();
  82. $this->db->begin();
  83. $sql = "INSERT INTO ".MAIN_DB_PREFIX."accounting_fiscalyear (";
  84. $sql.= "label";
  85. $sql.= ", date_start";
  86. $sql.= ", date_end";
  87. $sql.= ", statut";
  88. $sql.= ", entity";
  89. $sql.= ", datec";
  90. $sql.= ", fk_user_author";
  91. $sql.= ") VALUES (";
  92. $sql.= " '".$this->db->escape($this->label)."'";
  93. $sql.= ", '".$this->db->idate($this->date_start)."'";
  94. $sql.= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'":"null");
  95. $sql.= ", 0";
  96. $sql.= ", ".$conf->entity;
  97. $sql.= ", '".$this->db->idate($now)."'";
  98. $sql.= ", ". $user->id;
  99. $sql.= ")";
  100. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  101. $result = $this->db->query($sql);
  102. if ($result)
  103. {
  104. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."accounting_fiscalyear");
  105. $result=$this->update($user);
  106. if ($result > 0)
  107. {
  108. $this->db->commit();
  109. return $this->id;
  110. }
  111. else
  112. {
  113. $this->error=$this->db->lasterror();
  114. $this->db->rollback();
  115. return $result;
  116. }
  117. }
  118. else
  119. {
  120. $this->error=$this->db->lasterror()." sql=".$sql;
  121. $this->db->rollback();
  122. return -1;
  123. }
  124. }
  125. /**
  126. * Update record
  127. *
  128. * @param User $user User making update
  129. * @return int <0 if KO, >0 if OK
  130. */
  131. function update($user)
  132. {
  133. global $langs;
  134. // Check parameters
  135. if (empty($this->date_start) && empty($this->date_end))
  136. {
  137. $this->error='ErrorBadParameter';
  138. return -1;
  139. }
  140. $this->db->begin();
  141. $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_fiscalyear";
  142. $sql .= " SET label = '".$this->db->escape($this->label)."'";
  143. $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
  144. $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
  145. $sql .= ", statut = '".$this->db->escape($this->statut?$this->statut:0)."'";
  146. $sql .= ", datec = " . ($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
  147. $sql .= ", fk_user_modif = " . $user->id;
  148. $sql .= " WHERE rowid = ".$this->id;
  149. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  150. $result = $this->db->query($sql);
  151. if ($result)
  152. {
  153. $this->db->commit();
  154. return 1;
  155. }
  156. else
  157. {
  158. $this->error=$this->db->lasterror();
  159. dol_syslog($this->error, LOG_ERR);
  160. $this->db->rollback();
  161. return -1;
  162. }
  163. }
  164. /**
  165. * Load an object from database
  166. *
  167. * @param int $id Id of record to load
  168. * @return int <0 if KO, >0 if OK
  169. */
  170. function fetch($id)
  171. {
  172. $sql = "SELECT rowid, label, date_start, date_end, statut";
  173. $sql.= " FROM ".MAIN_DB_PREFIX."accounting_fiscalyear";
  174. $sql.= " WHERE rowid = ".$id;
  175. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  176. $result = $this->db->query($sql);
  177. if ( $result )
  178. {
  179. $obj = $this->db->fetch_object($result);
  180. $this->id = $obj->rowid;
  181. $this->ref = $obj->rowid;
  182. $this->date_start = $this->db->jdate($obj->date_start);
  183. $this->date_end = $this->db->jdate($obj->date_end);
  184. $this->label = $obj->label;
  185. $this->statut = $obj->statut;
  186. return 1;
  187. }
  188. else
  189. {
  190. $this->error=$this->db->lasterror();
  191. return -1;
  192. }
  193. }
  194. /**
  195. * Delete record
  196. *
  197. * @param int $id Id of record to delete
  198. * @return int <0 if KO, >0 if OK
  199. */
  200. function delete($id)
  201. {
  202. $this->db->begin();
  203. $sql = "DELETE FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE rowid = ".$id;
  204. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  205. $result = $this->db->query($sql);
  206. if ($result)
  207. {
  208. $this->db->commit();
  209. return 1;
  210. }
  211. else
  212. {
  213. $this->error=$this->db->lasterror();
  214. $this->db->rollback();
  215. return -1;
  216. }
  217. }
  218. /**
  219. * Give a label from a status
  220. *
  221. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  222. * @return string Label
  223. */
  224. function getLibStatut($mode=0)
  225. {
  226. return $this->LibStatut($this->statut,$mode);
  227. }
  228. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  229. /**
  230. * Give a label from a status
  231. *
  232. * @param int $statut Id status
  233. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  234. * @return string Label
  235. */
  236. function LibStatut($statut,$mode=0)
  237. {
  238. // phpcs:enable
  239. global $langs;
  240. if ($mode == 0)
  241. {
  242. return $langs->trans($this->statuts[$statut]);
  243. }
  244. elseif ($mode == 1)
  245. {
  246. return $langs->trans($this->statuts_short[$statut]);
  247. }
  248. elseif ($mode == 2)
  249. {
  250. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
  251. elseif ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts_short[$statut]);
  252. }
  253. elseif ($mode == 3)
  254. {
  255. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  256. elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8');
  257. }
  258. elseif ($mode == 4)
  259. {
  260. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
  261. elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut8').' '.$langs->trans($this->statuts[$statut]);
  262. }
  263. elseif ($mode == 5)
  264. {
  265. if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  266. elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
  267. }
  268. }
  269. /**
  270. * Information on record
  271. *
  272. * @param int $id Id of record
  273. * @return void
  274. */
  275. function info($id)
  276. {
  277. $sql = 'SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,';
  278. $sql.= ' fy.tms';
  279. $sql.= ' FROM '.MAIN_DB_PREFIX.'accounting_fiscalyear as fy';
  280. $sql.= ' WHERE fy.rowid = '.$id;
  281. dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
  282. $result = $this->db->query($sql);
  283. if ($result)
  284. {
  285. if ($this->db->num_rows($result))
  286. {
  287. $obj = $this->db->fetch_object($result);
  288. $this->id = $obj->rowid;
  289. if ($obj->fk_user_author)
  290. {
  291. $cuser = new User($this->db);
  292. $cuser->fetch($obj->fk_user_author);
  293. $this->user_creation = $cuser;
  294. }
  295. if ($obj->fk_user_modif)
  296. {
  297. $muser = new User($this->db);
  298. $muser->fetch($obj->fk_user_modif);
  299. $this->user_modification = $muser;
  300. }
  301. $this->date_creation = $this->db->jdate($obj->datec);
  302. $this->date_modification = $this->db->jdate($obj->tms);
  303. }
  304. $this->db->free($result);
  305. }
  306. else
  307. {
  308. dol_print_error($this->db);
  309. }
  310. }
  311. }