html.formventilation.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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) 2013-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  5. * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  6. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.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/accountancy/class/html.formventilation.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 bank module
  28. */
  29. class FormVentilation extends Form
  30. {
  31. private $options_cache = array();
  32. /**
  33. * Return select filter with date of transaction
  34. *
  35. * @param string $htmlname Name of select field
  36. * @param string $selectedkey Value
  37. * @return string HTML edit field
  38. */
  39. function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') {
  40. $options = array();
  41. $sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
  42. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  43. $sql .= ' ORDER BY import_key DESC';
  44. dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
  45. $resql = $this->db->query($sql);
  46. if (!$resql) {
  47. $this->error = "Error " . $this->db->lasterror();
  48. dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
  49. return - 1;
  50. }
  51. while ($obj = $this->db->fetch_object($resql)) {
  52. $options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
  53. }
  54. return Form::selectarray($htmlname, $options, $selectedkey);
  55. }
  56. /**
  57. * Return list of accounts with label by chart of accounts
  58. *
  59. * @param string $selectid Preselected id or code of accounting accounts (depends on $select_in)
  60. * @param string $htmlname Name of field in html form
  61. * @param int $showempty Add an empty field
  62. * @param array $event Event options
  63. * @param int $select_in 0=selectid value is a aa.rowid (default) or 1=selectid is aa.account_number
  64. * @param int $select_out Set value returned by select. 0=rowid (default), 1=account_number
  65. * @param string $morecss More css non HTML object
  66. * @param string $usecache Key to use to store result into a cache. Next call with same key will reuse the cache.
  67. * @return string String with HTML select
  68. */
  69. function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone', $usecache='')
  70. {
  71. global $conf;
  72. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  73. $out = '';
  74. $options = array();
  75. if ($usecache && ! empty($this->options_cache[$usecache]))
  76. {
  77. $options = $this->options_cache[$usecache];
  78. $selected=$selectid;
  79. }
  80. else
  81. {
  82. $trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
  83. $sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
  84. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  85. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  86. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  87. $sql .= " AND aa.active = 1";
  88. $sql .= " ORDER BY aa.account_number";
  89. dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
  90. $resql = $this->db->query($sql);
  91. if (!$resql) {
  92. $this->error = "Error " . $this->db->lasterror();
  93. dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
  94. return -1;
  95. }
  96. $out .= ajax_combobox($htmlname, $event);
  97. $selected = 0;
  98. while ($obj = $this->db->fetch_object($resql))
  99. {
  100. $label = length_accountg($obj->account_number) . ' - ' . $obj->label;
  101. $label = dol_trunc($label, $trunclength);
  102. $select_value_in = $obj->rowid;
  103. $select_value_out = $obj->rowid;
  104. // Try to guess if we have found default value
  105. if ($select_in == 1) {
  106. $select_value_in = $obj->account_number;
  107. }
  108. if ($select_out == 1) {
  109. $select_value_out = $obj->account_number;
  110. }
  111. // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
  112. // Because same account_number can be share between different accounting_system and do have the same meaning
  113. if ($selectid != '' && $selectid == $select_value_in) {
  114. //var_dump("Found ".$selectid." ".$select_value_in);
  115. $selected = $select_value_out;
  116. }
  117. $options[$select_value_out] = $label;
  118. }
  119. $this->db->free($resql);
  120. if ($usecache)
  121. {
  122. $this->options_cache[$usecache] = $options;
  123. }
  124. }
  125. $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
  126. return $out;
  127. }
  128. /**
  129. * Return list of accounts with label by class of accounts
  130. *
  131. * @param string $selectid Preselected pcg_type
  132. * @param string $htmlname Name of field in html form
  133. * @param int $showempty Add an empty field
  134. * @param array $event Event options
  135. *
  136. * @return string String with HTML select
  137. */
  138. function select_pcgtype($selectid, $htmlname = 'pcg_type', $showempty = 0, $event = array()) {
  139. global $conf;
  140. $sql = "SELECT DISTINCT pcg_type ";
  141. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  142. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  143. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  144. $sql .= " ORDER BY pcg_type";
  145. dol_syslog(get_class($this) . "::select_pcgtype", LOG_DEBUG);
  146. $resql = $this->db->query($sql);
  147. if (!$resql) {
  148. $this->error = "Error ".$this->db->lasterror();
  149. dol_syslog(get_class($this)."::select_pcgtype ".$this->error, LOG_ERR);
  150. return -1;
  151. }
  152. $options = array();
  153. $out = ajax_combobox($htmlname, $event);
  154. while ($obj = $this->db->fetch_object($resql))
  155. {
  156. if ($obj->pcg_type != '-1')
  157. {
  158. $options[$obj->pcg_type] = $obj->pcg_type;
  159. }
  160. }
  161. $out .= Form::selectarray($htmlname, $options, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth200');
  162. $this->db->free($resql);
  163. return $out;
  164. }
  165. /**
  166. * Return list of accounts with label by sub_class of accounts
  167. *
  168. * @param string $selectid Preselected pcg_type
  169. * @param string $htmlname Name of field in html form
  170. * @param int $showempty Add an empty field
  171. * @param array $event Event options
  172. *
  173. * @return string String with HTML select
  174. */
  175. function select_pcgsubtype($selectid, $htmlname = 'pcg_subtype', $showempty = 0, $event = array())
  176. {
  177. global $conf;
  178. $sql = "SELECT DISTINCT pcg_subtype ";
  179. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  180. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  181. $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
  182. $sql .= " ORDER BY pcg_subtype";
  183. dol_syslog(get_class($this) . "::select_pcgsubtype", LOG_DEBUG);
  184. $resql = $this->db->query($sql);
  185. if (!$resql) {
  186. $this->error = "Error ".$this->db->lasterror();
  187. dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
  188. return -1;
  189. }
  190. $options = array();
  191. $out = ajax_combobox($htmlname, $event);
  192. while ($obj = $this->db->fetch_object($resql))
  193. {
  194. if ($obj->pcg_type != '-1')
  195. {
  196. $options[$obj->pcg_subtype] = $obj->pcg_subtype;
  197. }
  198. }
  199. $out .= Form::selectarray($htmlname, $options, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth200');
  200. $this->db->free($resql);
  201. return $out;
  202. }
  203. /**
  204. * Return list of auxilary thirdparty accounts
  205. *
  206. * @param string $selectid Preselected pcg_type
  207. * @param string $htmlname Name of field in html form
  208. * @param int $showempty Add an empty field
  209. * @param array $event Event options
  210. *
  211. * @return string String with HTML select
  212. */
  213. function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $event = array()) {
  214. $aux_account = array();
  215. // Auxiliary customer account
  216. $sql = "SELECT DISTINCT code_compta, nom ";
  217. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  218. $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
  219. $sql .= " ORDER BY code_compta";
  220. dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
  221. $resql = $this->db->query($sql);
  222. if ($resql) {
  223. while ($obj = $this->db->fetch_object($resql)) {
  224. if (!empty($obj->code_compta)) {
  225. $aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
  226. }
  227. }
  228. } else {
  229. $this->error = "Error ".$this->db->lasterror();
  230. dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
  231. return -1;
  232. }
  233. $this->db->free($resql);
  234. // Auxiliary supplier account
  235. $sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
  236. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  237. $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
  238. $sql .= " ORDER BY code_compta_fournisseur";
  239. dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
  240. $resql = $this->db->query($sql);
  241. if ($resql) {
  242. while ($obj = $this->db->fetch_object($resql)) {
  243. if (!empty($obj->code_compta_fournisseur)) {
  244. $aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
  245. }
  246. }
  247. } else {
  248. $this->error = "Error ".$this->db->lasterror();
  249. dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
  250. return -1;
  251. }
  252. $this->db->free($resql);
  253. // Build select
  254. $out = ajax_combobox($htmlname, $event);
  255. $out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
  256. return $out;
  257. }
  258. /**
  259. * Return HTML combo list of years existing into book keepping
  260. *
  261. * @param string $selected Preselected value
  262. * @param string $htmlname Name of HTML select object
  263. * @param int $useempty Affiche valeur vide dans liste
  264. * @param string $output_format (html/opton (for option html only)/array (to return options arrays
  265. * @return string/array
  266. */
  267. function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
  268. {
  269. global $conf;
  270. $out_array = array();
  271. $sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
  272. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
  273. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  274. $sql .= " ORDER BY date_format(doc_date,'%Y')";
  275. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  276. $resql = $this->db->query($sql);
  277. if (!$resql) {
  278. $this->error = "Error ".$this->db->lasterror();
  279. dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
  280. return -1;
  281. }
  282. while ($obj = $this->db->fetch_object($resql)) {
  283. $out_array[$obj->dtyear] = $obj->dtyear;
  284. }
  285. $this->db->free($resql);
  286. if ($output_format == 'html') {
  287. return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
  288. } else {
  289. return $out_array;
  290. }
  291. }
  292. /**
  293. * Return HTML combo list of years existing into book keepping
  294. *
  295. * @param string $selected Preselected value
  296. * @param string $htmlname Name of HTML select object
  297. * @param int $useempty Affiche valeur vide dans liste
  298. * @param string $output_format Html/option (for option html only)/array (to return options arrays
  299. * @return string/array
  300. */
  301. function selectjournal_accountancy_bookkepping($selected = '', $htmlname = 'journalid', $useempty = 0, $output_format = 'html')
  302. {
  303. global $conf,$langs;
  304. $out_array = array();
  305. $sql = "SELECT DISTINCT code_journal";
  306. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
  307. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  308. $sql .= " ORDER BY code_journal";
  309. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  310. $resql = $this->db->query($sql);
  311. if (!$resql) {
  312. $this->error = "Error ".$this->db->lasterror();
  313. dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
  314. return -1;
  315. }
  316. while ($obj = $this->db->fetch_object($resql)) {
  317. $out_array[$obj->code_journal] = $obj->code_journal?$obj->code_journal:$langs->trans("NotDefined"); // TODO Not defined is accepted ? We should avoid this, shouldn't we ?
  318. }
  319. $this->db->free($resql);
  320. if ($output_format == 'html') {
  321. return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
  322. } else {
  323. return $out_array;
  324. }
  325. }
  326. }