accountingjournal.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/accountancy/class/accountingjournal.class.php
  19. * \ingroup Accountancy (Double entries)
  20. * \brief File of class to manage accounting journals
  21. */
  22. /**
  23. * Class to manage accounting accounts
  24. */
  25. class AccountingJournal extends CommonObject
  26. {
  27. /**
  28. * @var string ID to identify managed object
  29. */
  30. public $element='accounting_journal';
  31. /**
  32. * @var string Name of table without prefix where object is stored
  33. */
  34. public $table_element='accounting_journal';
  35. /**
  36. * @var int Field with ID of parent key if this field has a parent
  37. */
  38. public $fk_element = '';
  39. /**
  40. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  41. * @var int
  42. */
  43. public $ismultientitymanaged = 0;
  44. /**
  45. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  46. */
  47. public $picto = 'generic';
  48. /**
  49. * @var int ID
  50. */
  51. public $rowid;
  52. public $code;
  53. /**
  54. * @var string Accounting Journal label
  55. */
  56. public $label;
  57. public $nature; // 1:various operations, 2:sale, 3:purchase, 4:bank, 5:expense-report, 8:inventory, 9: has-new
  58. public $active;
  59. public $lines;
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDB $db Database handle
  64. */
  65. public function __construct($db)
  66. {
  67. $this->db = $db;
  68. }
  69. /**
  70. * Load an object from database
  71. *
  72. * @param int $rowid Id of record to load
  73. * @param string $journal_code Journal code
  74. * @return int <0 if KO, Id of record if OK and found
  75. */
  76. public function fetch($rowid = null, $journal_code = null)
  77. {
  78. global $conf;
  79. if ($rowid || $journal_code)
  80. {
  81. $sql = "SELECT rowid, code, label, nature, active";
  82. $sql.= " FROM ".MAIN_DB_PREFIX."accounting_journal";
  83. $sql .= " WHERE";
  84. if ($rowid) {
  85. $sql .= " rowid = " . (int) $rowid;
  86. }
  87. elseif ($journal_code)
  88. {
  89. $sql .= " code = '" . $this->db->escape($journal_code) . "'";
  90. $sql .= " AND entity = " . $conf->entity;
  91. }
  92. dol_syslog(get_class($this)."::fetch sql=" . $sql, LOG_DEBUG);
  93. $result = $this->db->query($sql);
  94. if ($result)
  95. {
  96. $obj = $this->db->fetch_object($result);
  97. if ($obj) {
  98. $this->id = $obj->rowid;
  99. $this->rowid = $obj->rowid;
  100. $this->code = $obj->code;
  101. $this->ref = $obj->code;
  102. $this->label = $obj->label;
  103. $this->nature = $obj->nature;
  104. $this->active = $obj->active;
  105. return $this->id;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. else
  111. {
  112. $this->error = "Error " . $this->db->lasterror();
  113. $this->errors[] = "Error " . $this->db->lasterror();
  114. }
  115. }
  116. return -1;
  117. }
  118. /**
  119. * Load object in memory from the database
  120. *
  121. * @param string $sortorder Sort Order
  122. * @param string $sortfield Sort field
  123. * @param int $limit offset limit
  124. * @param int $offset offset limit
  125. * @param array $filter filter array
  126. * @param string $filtermode filter mode (AND or OR)
  127. *
  128. * @return int <0 if KO, >0 if OK
  129. */
  130. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  131. {
  132. $sql = "SELECT rowid, code, label, nature, active";
  133. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  134. // Manage filter
  135. $sqlwhere = array();
  136. if (count($filter) > 0) {
  137. foreach ($filter as $key => $value) {
  138. if ($key == 't.code' || $key == 't.label' || $key == 't.nature') {
  139. $sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
  140. } elseif ($key == 't.rowid' || $key == 't.active') {
  141. $sqlwhere[] = $key . '=' . $value;
  142. }
  143. }
  144. }
  145. $sql .= ' WHERE 1 = 1';
  146. $sql .= " AND entity IN (" . getEntity('accountancy') . ")";
  147. if (count($sqlwhere) > 0) {
  148. $sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
  149. }
  150. if (! empty($sortfield)) {
  151. $sql .= $this->db->order($sortfield, $sortorder);
  152. }
  153. if (! empty($limit)) {
  154. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  155. }
  156. $this->lines = array();
  157. dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
  158. $resql = $this->db->query($sql);
  159. if ($resql) {
  160. $num = $this->db->num_rows($resql);
  161. while ( $obj = $this->db->fetch_object($resql) ) {
  162. $line = new self($this->db);
  163. $line->id = $obj->rowid;
  164. $line->code = $obj->code;
  165. $line->label = $obj->label;
  166. $line->nature = $obj->nature;
  167. $line->active = $obj->active;
  168. $this->lines[] = $line;
  169. }
  170. $this->db->free($resql);
  171. return $num;
  172. } else {
  173. $this->errors[] = 'Error ' . $this->db->lasterror();
  174. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  175. return - 1;
  176. }
  177. }
  178. /**
  179. * Return clicable name (with picto eventually)
  180. *
  181. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  182. * @param int $withlabel 0=No label, 1=Include label of journal
  183. * @param int $nourl 1=Disable url
  184. * @param string $moretitle Add more text to title tooltip
  185. * @param int $notooltip 1=Disable tooltip
  186. * @return string String with URL
  187. */
  188. public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0)
  189. {
  190. global $langs, $conf, $user;
  191. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  192. $result = '';
  193. $url = DOL_URL_ROOT . '/accountancy/admin/journals_list.php?id=35';
  194. $label = '<u>' . $langs->trans("ShowAccountingJournal") . '</u>';
  195. if (! empty($this->code))
  196. $label .= '<br><b>'.$langs->trans('Code') . ':</b> ' . $this->code;
  197. if (! empty($this->label))
  198. $label .= '<br><b>'.$langs->trans('Label') . ':</b> ' . $langs->transnoentities($this->label);
  199. if ($moretitle) $label.=' - '.$moretitle;
  200. $linkclose='';
  201. if (empty($notooltip))
  202. {
  203. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  204. {
  205. $label=$langs->trans("ShowAccoutingJournal");
  206. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  207. }
  208. $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
  209. $linkclose.=' class="classfortooltip"';
  210. }
  211. $linkstart='<a href="'.$url.'"';
  212. $linkstart.=$linkclose.'>';
  213. $linkend='</a>';
  214. if ($nourl)
  215. {
  216. $linkstart = '';
  217. $linkclose = '';
  218. $linkend = '';
  219. }
  220. $label_link = $this->code;
  221. if ($withlabel) $label_link .= ' - ' . $langs->transnoentities($this->label);
  222. $result .= $linkstart;
  223. if ($withpicto) $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);
  224. if ($withpicto != 2) $result.= $label_link;
  225. $result .= $linkend;
  226. return $result;
  227. }
  228. /**
  229. * Retourne le libelle du statut d'un user (actif, inactif)
  230. *
  231. * @param int $mode 0=libelle long, 1=libelle court
  232. * @return string Label of type
  233. */
  234. public function getLibType($mode = 0)
  235. {
  236. return $this->LibType($this->nature, $mode);
  237. }
  238. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  239. /**
  240. * Return type of an accounting journal
  241. *
  242. * @param int $nature Id type
  243. * @param int $mode 0=libelle long, 1=libelle court
  244. * @return string Label of type
  245. */
  246. public function LibType($nature, $mode = 0)
  247. {
  248. // phpcs:enable
  249. global $langs;
  250. $langs->loadLangs(array("accountancy"));
  251. if ($mode == 0)
  252. {
  253. $prefix='';
  254. if ($nature == 9) return $langs->trans('AccountingJournalType9');
  255. elseif ($nature == 5) return $langs->trans('AccountingJournalType5');
  256. elseif ($nature == 4) return $langs->trans('AccountingJournalType4');
  257. elseif ($nature == 3) return $langs->trans('AccountingJournalType3');
  258. elseif ($nature == 2) return $langs->trans('AccountingJournalType2');
  259. elseif ($nature == 1) return $langs->trans('AccountingJournalType1');
  260. }
  261. elseif ($mode == 1)
  262. {
  263. if ($nature == 9) return $langs->trans('AccountingJournalType9');
  264. elseif ($nature == 5) return $langs->trans('AccountingJournalType5');
  265. elseif ($nature == 4) return $langs->trans('AccountingJournalType4');
  266. elseif ($nature == 3) return $langs->trans('AccountingJournalType3');
  267. elseif ($nature == 2) return $langs->trans('AccountingJournalType2');
  268. elseif ($nature == 1) return $langs->trans('AccountingJournalType1');
  269. }
  270. }
  271. }