fiscalyear.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. /* Copyright (C) 2014-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2020 OScss-Shop <support@oscss-shop.fr>
  4. *
  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. /**
  20. * \file htdocs/core/class/fiscalyear.class.php
  21. * \ingroup fiscal year
  22. * \brief File of class to manage fiscal years
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. /**
  26. * Class to manage fiscal year
  27. */
  28. class Fiscalyear extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element = 'fiscalyear';
  34. /**
  35. * @var string picto
  36. */
  37. public $picto = 'technic';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'accounting_fiscalyear';
  42. /**
  43. * @var string Name of subtable line
  44. */
  45. public $table_element_line = '';
  46. /**
  47. * @var string Field with ID of parent key if this field has a parent
  48. */
  49. public $fk_element = '';
  50. /**
  51. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  52. * @var int
  53. */
  54. public $ismultientitymanaged = 1;
  55. /**
  56. * @var int ID
  57. */
  58. public $rowid;
  59. /**
  60. * @var string fiscal year label
  61. */
  62. public $label;
  63. /**
  64. * Date start (date_start)
  65. *
  66. * @var integer
  67. */
  68. public $date_start;
  69. /**
  70. * Date end (date_end)
  71. *
  72. * @var integer
  73. */
  74. public $date_end;
  75. /**
  76. * Date creation record (datec)
  77. *
  78. * @var integer
  79. */
  80. public $datec;
  81. public $statut; // 0=open, 1=closed
  82. /**
  83. * @var int Entity
  84. */
  85. public $entity;
  86. public $statuts = array();
  87. public $statuts_short = array();
  88. /**
  89. * Constructor
  90. *
  91. * @param DoliDB $db Database handler
  92. */
  93. public function __construct(DoliDB $db)
  94. {
  95. global $langs;
  96. $this->db = $db;
  97. $this->statuts_short = array(0 => 'Opened', 1 => 'Closed');
  98. $this->statuts = array(0 => 'Opened', 1 => 'Closed');
  99. }
  100. /**
  101. * Create object in database
  102. *
  103. * @param User $user User making creation
  104. * @return int <0 if KO, >0 if OK
  105. */
  106. public function create($user)
  107. {
  108. global $conf;
  109. $error = 0;
  110. $now = dol_now();
  111. $this->db->begin();
  112. $sql = "INSERT INTO ".$this->db->prefix()."accounting_fiscalyear (";
  113. $sql .= "label";
  114. $sql .= ", date_start";
  115. $sql .= ", date_end";
  116. $sql .= ", statut";
  117. $sql .= ", entity";
  118. $sql .= ", datec";
  119. $sql .= ", fk_user_author";
  120. $sql .= ") VALUES (";
  121. $sql .= " '".$this->db->escape($this->label)."'";
  122. $sql .= ", '".$this->db->idate($this->date_start)."'";
  123. $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
  124. $sql .= ", 0";
  125. $sql .= ", ".((int) $conf->entity);
  126. $sql .= ", '".$this->db->idate($now)."'";
  127. $sql .= ", ".((int) $user->id);
  128. $sql .= ")";
  129. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  130. $result = $this->db->query($sql);
  131. if ($result) {
  132. $this->id = $this->db->last_insert_id($this->db->prefix()."accounting_fiscalyear");
  133. $result = $this->update($user);
  134. if ($result > 0) {
  135. $this->db->commit();
  136. return $this->id;
  137. } else {
  138. $this->error = $this->db->lasterror();
  139. $this->db->rollback();
  140. return $result;
  141. }
  142. } else {
  143. $this->error = $this->db->lasterror()." sql=".$sql;
  144. $this->db->rollback();
  145. return -1;
  146. }
  147. }
  148. /**
  149. * Update record
  150. *
  151. * @param User $user User making update
  152. * @return int <0 if KO, >0 if OK
  153. */
  154. public function update($user)
  155. {
  156. global $langs;
  157. // Check parameters
  158. if (empty($this->date_start) && empty($this->date_end)) {
  159. $this->error = 'ErrorBadParameter';
  160. return -1;
  161. }
  162. $this->db->begin();
  163. $sql = "UPDATE ".$this->db->prefix()."accounting_fiscalyear";
  164. $sql .= " SET label = '".$this->db->escape($this->label)."'";
  165. $sql .= ", date_start = '".$this->db->idate($this->date_start)."'";
  166. $sql .= ", date_end = ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
  167. $sql .= ", statut = '".$this->db->escape($this->statut ? $this->statut : 0)."'";
  168. $sql .= ", fk_user_modif = ".((int) $user->id);
  169. $sql .= " WHERE rowid = ".((int) $this->id);
  170. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  171. $result = $this->db->query($sql);
  172. if ($result) {
  173. $this->db->commit();
  174. return 1;
  175. } else {
  176. $this->error = $this->db->lasterror();
  177. dol_syslog($this->error, LOG_ERR);
  178. $this->db->rollback();
  179. return -1;
  180. }
  181. }
  182. /**
  183. * Load an object from database
  184. *
  185. * @param int $id Id of record to load
  186. * @return int <0 if KO, >0 if OK
  187. */
  188. public function fetch($id)
  189. {
  190. $sql = "SELECT rowid, label, date_start, date_end, statut";
  191. $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear";
  192. $sql .= " WHERE rowid = ".((int) $id);
  193. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  194. $result = $this->db->query($sql);
  195. if ($result) {
  196. $obj = $this->db->fetch_object($result);
  197. $this->id = $obj->rowid;
  198. $this->ref = $obj->rowid;
  199. $this->date_start = $this->db->jdate($obj->date_start);
  200. $this->date_end = $this->db->jdate($obj->date_end);
  201. $this->label = $obj->label;
  202. $this->statut = $obj->statut;
  203. return 1;
  204. } else {
  205. $this->error = $this->db->lasterror();
  206. return -1;
  207. }
  208. }
  209. /**
  210. * Delete record
  211. *
  212. * @param int $id Id of record to delete
  213. * @return int <0 if KO, >0 if OK
  214. */
  215. public function delete($id)
  216. {
  217. $this->db->begin();
  218. $sql = "DELETE FROM ".$this->db->prefix()."accounting_fiscalyear WHERE rowid = ".((int) $id);
  219. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  220. $result = $this->db->query($sql);
  221. if ($result) {
  222. $this->db->commit();
  223. return 1;
  224. } else {
  225. $this->error = $this->db->lasterror();
  226. $this->db->rollback();
  227. return -1;
  228. }
  229. }
  230. /**
  231. * Return clicable link of object (with eventually picto)
  232. *
  233. * @param int $withpicto Add picto into link
  234. * @param int $notooltip 1=Disable tooltip
  235. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  236. * @return string String with URL
  237. */
  238. public function getNomUrl($withpicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
  239. {
  240. global $conf, $langs, $user;
  241. if (empty($this->ref)) {
  242. $this->ref = $this->id;
  243. }
  244. if (!empty($conf->dol_no_mouse_hover)) {
  245. $notooltip = 1; // Force disable tooltips
  246. }
  247. $result = '';
  248. $url = DOL_URL_ROOT.'/accountancy/admin/fiscalyear_card.php?id='.$this->id;
  249. if (empty($user->rights->accounting->fiscalyear->write)) {
  250. $option = 'nolink';
  251. }
  252. if ($option !== 'nolink') {
  253. // Add param to save lastsearch_values or not
  254. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  255. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  256. $add_save_lastsearch_values = 1;
  257. }
  258. if ($add_save_lastsearch_values) {
  259. $url .= '&save_lastsearch_values=1';
  260. }
  261. }
  262. if ($short) {
  263. return $url;
  264. }
  265. $label = '';
  266. if ($user->rights->accounting->fiscalyear->write) {
  267. $label = '<u>'.$langs->trans("FiscalPeriod").'</u>';
  268. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
  269. if (isset($this->statut)) {
  270. $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
  271. }
  272. }
  273. $linkclose = '';
  274. if (empty($notooltip) && $user->rights->accounting->fiscalyear->write) {
  275. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  276. $label = $langs->trans("FiscalYear");
  277. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  278. }
  279. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  280. $linkclose .= ' class="classfortooltip"';
  281. }
  282. $linkstart = '<a href="'.$url.'"';
  283. $linkstart .= $linkclose.'>';
  284. $linkend = '</a>';
  285. if ($option === 'nolink') {
  286. $linkstart = '';
  287. $linkend = '';
  288. }
  289. $result .= $linkstart;
  290. if ($withpicto) {
  291. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  292. }
  293. if ($withpicto != 2) {
  294. $result .= $this->ref;
  295. }
  296. $result .= $linkend;
  297. return $result;
  298. }
  299. /**
  300. * Give a label from a status
  301. *
  302. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  303. * @return string Label
  304. */
  305. public function getLibStatut($mode = 0)
  306. {
  307. return $this->LibStatut($this->statut, $mode);
  308. }
  309. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  310. /**
  311. * Give a label from a status
  312. *
  313. * @param int $status Id status
  314. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  315. * @return string Label
  316. */
  317. public function LibStatut($status, $mode = 0)
  318. {
  319. // phpcs:enable
  320. global $langs;
  321. if ($mode == 0) {
  322. return $langs->trans($this->statuts[$status]);
  323. } elseif ($mode == 1) {
  324. return $langs->trans($this->statuts_short[$status]);
  325. } elseif ($mode == 2) {
  326. if ($status == 0) {
  327. return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts_short[$status]);
  328. } elseif ($status == 1) {
  329. return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts_short[$status]);
  330. }
  331. } elseif ($mode == 3) {
  332. if ($status == 0 && !empty($this->statuts_short[$status])) {
  333. return img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
  334. } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
  335. return img_picto($langs->trans($this->statuts_short[$status]), 'statut8');
  336. }
  337. } elseif ($mode == 4) {
  338. if ($status == 0 && !empty($this->statuts_short[$status])) {
  339. return img_picto($langs->trans($this->statuts_short[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
  340. } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
  341. return img_picto($langs->trans($this->statuts_short[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
  342. }
  343. } elseif ($mode == 5) {
  344. if ($status == 0 && !empty($this->statuts_short[$status])) {
  345. return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut4');
  346. } elseif ($status == 1 && !empty($this->statuts_short[$status])) {
  347. return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
  348. }
  349. }
  350. }
  351. /**
  352. * Information on record
  353. *
  354. * @param int $id Id of record
  355. * @return void
  356. */
  357. public function info($id)
  358. {
  359. $sql = "SELECT fy.rowid, fy.datec, fy.fk_user_author, fy.fk_user_modif,";
  360. $sql .= " fy.tms as datem";
  361. $sql .= " FROM ".$this->db->prefix()."accounting_fiscalyear as fy";
  362. $sql .= " WHERE fy.rowid = ".((int) $id);
  363. dol_syslog(get_class($this)."::fetch info", LOG_DEBUG);
  364. $result = $this->db->query($sql);
  365. if ($result) {
  366. if ($this->db->num_rows($result)) {
  367. $obj = $this->db->fetch_object($result);
  368. $this->id = $obj->rowid;
  369. $this->user_creation_id = $obj->fk_user_author;
  370. $this->user_modification_id = $obj->fk_user_modif;
  371. $this->date_creation = $this->db->jdate($obj->datec);
  372. $this->date_modification = $this->db->jdate($obj->datem);
  373. }
  374. $this->db->free($result);
  375. } else {
  376. dol_print_error($this->db);
  377. }
  378. }
  379. /**
  380. * Return the number of entries by fiscal year
  381. *
  382. * @param int $datestart Date start to scan
  383. * @param int $dateend Date end to scan
  384. * @return string Number of entries
  385. */
  386. public function getAccountancyEntriesByFiscalYear($datestart = '', $dateend = '')
  387. {
  388. global $conf;
  389. if (empty($datestart)) {
  390. $datestart = $this->date_start;
  391. }
  392. if (empty($dateend)) {
  393. $dateend = $this->date_end;
  394. }
  395. $sql = "SELECT count(DISTINCT piece_num) as nb";
  396. $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
  397. $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
  398. $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
  399. $resql = $this->db->query($sql);
  400. if ($resql) {
  401. $obj = $this->db->fetch_object($resql);
  402. $nb = $obj->nb;
  403. } else {
  404. dol_print_error($this->db);
  405. }
  406. return $nb;
  407. }
  408. /**
  409. * Return the number of movements by fiscal year
  410. *
  411. * @param int $datestart Date start to scan
  412. * @param int $dateend Date end to scan
  413. * @return string Number of movements
  414. */
  415. public function getAccountancyMovementsByFiscalYear($datestart = '', $dateend = '')
  416. {
  417. global $conf;
  418. if (empty($datestart)) {
  419. $datestart = $this->date_start;
  420. }
  421. if (empty($dateend)) {
  422. $dateend = $this->date_end;
  423. }
  424. $sql = "SELECT count(rowid) as nb";
  425. $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping ";
  426. $sql .= " WHERE entity IN (".getEntity('bookkeeping', 0).")";
  427. $sql .= " AND doc_date >= '".$this->db->idate($datestart)."' and doc_date <= '".$this->db->idate($dateend)."'";
  428. $resql = $this->db->query($sql);
  429. if ($resql) {
  430. $obj = $this->db->fetch_object($resql);
  431. $nb = $obj->nb;
  432. } else {
  433. dol_print_error($this->db);
  434. }
  435. return $nb;
  436. }
  437. }