html.formaccounting.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /* Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
  3. * Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  4. * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2016-2017 Alexandre Spangaro <aspangaro@zendsi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/core/class/html.formaccounting.class.php
  23. * \ingroup Advanced accountancy
  24. * \brief File of class with all html predefined components
  25. */
  26. /**
  27. * Class to manage generation of HTML components for accounting management
  28. */
  29. class FormAccounting extends Form
  30. {
  31. private $options_cache = array();
  32. var $db;
  33. var $error;
  34. /**
  35. * Constructor
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. public function __construct($db)
  40. {
  41. $this->db = $db;
  42. }
  43. /**
  44. * Return list of journals with label by nature
  45. *
  46. * @param string $selectid Preselected pcg_type
  47. * @param string $htmlname Name of field in html form
  48. * @param int $nature Limit the list to a particular type of journals (1:various operations / 2:sale / 3:purchase / 4:bank / 9: has-new)
  49. * @param int $showempty Add an empty field
  50. * @param array $event Event options
  51. * @param int $select_in 0=selectid value is the journal rowid (default) or 1=selectid is journal code
  52. * @param int $select_out Set value returned by select. 0=rowid (default), 1=code
  53. * @param string $morecss More css non HTML object
  54. * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache.
  55. *
  56. * @return string String with HTML select
  57. */
  58. function select_journal($selectid, $htmlname = 'journal', $nature=0, $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
  59. {
  60. global $conf;
  61. $out = '';
  62. $options = array();
  63. if ($usecache && ! empty($this->options_cache[$usecache]))
  64. {
  65. $options = $this->options_cache[$usecache];
  66. $selected=$selectid;
  67. }
  68. else
  69. {
  70. $sql = "SELECT rowid, code, label, nature, entity, active";
  71. $sql.= " FROM " . MAIN_DB_PREFIX . "accounting_journal";
  72. $sql.= " WHERE active = 1";
  73. $sql.= " AND entity = ".$conf->entity;
  74. //if ($nature && is_numeric($nature)) $sql .= " AND nature = ".$nature;
  75. $sql.= " ORDER BY code";
  76. dol_syslog(get_class($this) . "::select_journal", LOG_DEBUG);
  77. $resql = $this->db->query($sql);
  78. if (!$resql) {
  79. $this->error = "Error ".$this->db->lasterror();
  80. dol_syslog(get_class($this)."::select_journal ".$this->error, LOG_ERR);
  81. return -1;
  82. }
  83. $out = ajax_combobox($htmlname, $event);
  84. $selected = 0;
  85. while ($obj = $this->db->fetch_object($resql))
  86. {
  87. $label = $obj->code . ' - ' . $obj->label;
  88. $select_value_in = $obj->rowid;
  89. $select_value_out = $obj->rowid;
  90. // Try to guess if we have found default value
  91. if ($select_in == 1) {
  92. $select_value_in = $obj->code;
  93. }
  94. if ($select_out == 1) {
  95. $select_value_out = $obj->code;
  96. }
  97. // Remember guy's we store in database llx_accounting_bookkeeping the code of accounting_journal and not the rowid
  98. if ($selectid != '' && $selectid == $select_value_in) {
  99. //var_dump("Found ".$selectid." ".$select_value_in);
  100. $selected = $select_value_out;
  101. }
  102. $options[$select_value_out] = $label;
  103. }
  104. $this->db->free($resql);
  105. if ($usecache)
  106. {
  107. $this->options_cache[$usecache] = $options;
  108. }
  109. }
  110. $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
  111. return $out;
  112. }
  113. /**
  114. * Return list of accounting category.
  115. * Use mysoc->country_id or mysoc->country_code so they must be defined.
  116. *
  117. * @param string $selected Preselected type
  118. * @param string $htmlname Name of field in form
  119. * @param int $useempty Set to 1 if we want an empty value
  120. * @param int $maxlen Max length of text in combo box
  121. * @param int $help Add or not the admin help picto
  122. * @param int $allcountries All countries
  123. * @return void
  124. */
  125. function select_accounting_category($selected='',$htmlname='account_category', $useempty=0, $maxlen=0, $help=1, $allcountries=0)
  126. {
  127. global $db,$langs,$user,$mysoc;
  128. if (empty($mysoc->country_id) && empty($mysoc->country_code) && empty($allcountries))
  129. {
  130. dol_print_error('','Call to select_accounting_account with mysoc country not yet defined');
  131. exit;
  132. }
  133. if (! empty($mysoc->country_id))
  134. {
  135. $sql = "SELECT c.rowid, c.label as type, c.range_account";
  136. $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
  137. $sql.= " WHERE c.active = 1";
  138. $sql.= " AND c.category_type = 0";
  139. if (empty($allcountries)) $sql.= " AND c.fk_country = ".$mysoc->country_id;
  140. $sql.= " ORDER BY c.label ASC";
  141. }
  142. else
  143. {
  144. $sql = "SELECT c.rowid, c.label as type, c.range_account";
  145. $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co";
  146. $sql.= " WHERE c.active = 1";
  147. $sql.= " AND c.category_type = 0";
  148. $sql.= " AND c.fk_country = co.rowid";
  149. if (empty($allcountries)) $sql.= " AND co.code = '".$mysoc->country_code."'";
  150. $sql.= " ORDER BY c.label ASC";
  151. }
  152. dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
  153. $resql=$db->query($sql);
  154. if ($resql)
  155. {
  156. $num = $db->num_rows($resql);
  157. if ($num)
  158. {
  159. $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
  160. $i = 0;
  161. if ($useempty) $out.= '<option value="0">&nbsp;</option>';
  162. while ($i < $num)
  163. {
  164. $obj = $db->fetch_object($resql);
  165. $out .= '<option value="'.$obj->rowid.'"';
  166. if ($obj->rowid == $selected) $out .= ' selected';
  167. $out .= '>'.($maxlen ? dol_trunc($obj->type,$maxlen) : $obj->type);
  168. $out .= ' ('.$obj->range_account.')';
  169. $i++;
  170. }
  171. $out .= '</select>';
  172. //if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  173. }
  174. else
  175. {
  176. $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry",$mysoc->country_code);
  177. }
  178. }
  179. else
  180. {
  181. dol_print_error($db,$db->lasterror());
  182. }
  183. $out .= ajax_combobox($htmlname, $event);
  184. print $out;
  185. }
  186. /**
  187. * Return select filter with date of transaction
  188. *
  189. * @param string $htmlname Name of select field
  190. * @param string $selectedkey Value
  191. * @return string HTML edit field
  192. */
  193. function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') {
  194. $options = array();
  195. $sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
  196. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  197. $sql .= ' ORDER BY import_key DESC';
  198. dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
  199. $resql = $this->db->query($sql);
  200. if (!$resql) {
  201. $this->error = "Error " . $this->db->lasterror();
  202. dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
  203. return - 1;
  204. }
  205. while ($obj = $this->db->fetch_object($resql)) {
  206. $options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
  207. }
  208. return Form::selectarray($htmlname, $options, $selectedkey);
  209. }
  210. /**
  211. * Return list of accounts with label by chart of accounts
  212. *
  213. * @param string $selectid Preselected id or code of accounting accounts (depends on $select_in)
  214. * @param string $htmlname Name of field in html form
  215. * @param int $showempty Add an empty field
  216. * @param array $event Event options
  217. * @param int $select_in 0=selectid value is a aa.rowid (default) or 1=selectid is aa.account_number
  218. * @param int $select_out Set value returned by select. 0=rowid (default), 1=account_number
  219. * @param string $morecss More css non HTML object
  220. * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache.
  221. * @return string String with HTML select
  222. */
  223. function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
  224. {
  225. global $conf;
  226. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  227. $out = '';
  228. $options = array();
  229. if ($usecache && ! empty($this->options_cache[$usecache]))
  230. {
  231. $options = $this->options_cache[$usecache];
  232. $selected=$selectid;
  233. }
  234. else
  235. {
  236. $trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
  237. $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
  238. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  239. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  240. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  241. $sql .= " AND aa.active = 1";
  242. $sql .= " ORDER BY aa.account_number";
  243. dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
  244. $resql = $this->db->query($sql);
  245. if (!$resql) {
  246. $this->error = "Error " . $this->db->lasterror();
  247. dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
  248. return -1;
  249. }
  250. $out .= ajax_combobox($htmlname, $event);
  251. $selected = 0;
  252. while ($obj = $this->db->fetch_object($resql))
  253. {
  254. $label = length_accountg($obj->account_number) . ' - ' . $obj->label;
  255. $label = dol_trunc($label, $trunclength);
  256. $select_value_in = $obj->rowid;
  257. $select_value_out = $obj->rowid;
  258. // Try to guess if we have found default value
  259. if ($select_in == 1) {
  260. $select_value_in = $obj->account_number;
  261. }
  262. if ($select_out == 1) {
  263. $select_value_out = $obj->account_number;
  264. }
  265. // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
  266. // Because same account_number can be share between different accounting_system and do have the same meaning
  267. if ($selectid != '' && $selectid == $select_value_in) {
  268. //var_dump("Found ".$selectid." ".$select_value_in);
  269. $selected = $select_value_out;
  270. }
  271. $options[$select_value_out] = $label;
  272. }
  273. $this->db->free($resql);
  274. if ($usecache)
  275. {
  276. $this->options_cache[$usecache] = $options;
  277. }
  278. }
  279. $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
  280. return $out;
  281. }
  282. /**
  283. * Return list of auxilary thirdparty accounts
  284. *
  285. * @param string $selectid Preselected pcg_type
  286. * @param string $htmlname Name of field in html form
  287. * @param int $showempty Add an empty field
  288. * @param array $event Event options
  289. *
  290. * @return string String with HTML select
  291. */
  292. function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $event = array()) {
  293. $aux_account = array();
  294. // Auxiliary customer account
  295. $sql = "SELECT DISTINCT code_compta, nom ";
  296. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  297. $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
  298. $sql .= " ORDER BY code_compta";
  299. dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
  300. $resql = $this->db->query($sql);
  301. if ($resql) {
  302. while ($obj = $this->db->fetch_object($resql)) {
  303. if (!empty($obj->code_compta)) {
  304. $aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
  305. }
  306. }
  307. } else {
  308. $this->error = "Error ".$this->db->lasterror();
  309. dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
  310. return -1;
  311. }
  312. $this->db->free($resql);
  313. // Auxiliary supplier account
  314. $sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
  315. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  316. $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
  317. $sql .= " ORDER BY code_compta_fournisseur";
  318. dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
  319. $resql = $this->db->query($sql);
  320. if ($resql) {
  321. while ($obj = $this->db->fetch_object($resql)) {
  322. if (!empty($obj->code_compta_fournisseur)) {
  323. $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
  324. }
  325. }
  326. } else {
  327. $this->error = "Error ".$this->db->lasterror();
  328. dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
  329. return -1;
  330. }
  331. $this->db->free($resql);
  332. // Build select
  333. $out = ajax_combobox($htmlname, $event);
  334. $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
  335. return $out;
  336. }
  337. /**
  338. * Return HTML combo list of years existing into book keepping
  339. *
  340. * @param string $selected Preselected value
  341. * @param string $htmlname Name of HTML select object
  342. * @param int $useempty Affiche valeur vide dans liste
  343. * @param string $output_format (html/opton (for option html only)/array (to return options arrays
  344. * @return string/array
  345. */
  346. function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
  347. {
  348. global $conf;
  349. $out_array = array();
  350. $sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
  351. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
  352. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  353. $sql .= " ORDER BY date_format(doc_date,'%Y')";
  354. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  355. $resql = $this->db->query($sql);
  356. if (!$resql) {
  357. $this->error = "Error ".$this->db->lasterror();
  358. dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
  359. return -1;
  360. }
  361. while ($obj = $this->db->fetch_object($resql)) {
  362. $out_array[$obj->dtyear] = $obj->dtyear;
  363. }
  364. $this->db->free($resql);
  365. if ($output_format == 'html') {
  366. return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
  367. } else {
  368. return $out_array;
  369. }
  370. }
  371. }