fiscalyear.class.php 8.5 KB

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