account.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2016-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  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/accountancy/admin/account.php
  21. * \ingroup Accountancy (Double entries)
  22. * \brief List accounting account
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  27. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("compta","bills","admin","accountancy","salaries"));
  30. $mesg = '';
  31. $action = GETPOST('action', 'aZ09');
  32. $cancel = GETPOST('cancel', 'alpha');
  33. $id = GETPOST('id', 'int');
  34. $rowid = GETPOST('rowid', 'int');
  35. $contextpage=GETPOST('contextpage', 'aZ')?GETPOST('contextpage', 'aZ'):'accountingaccountlist'; // To manage different context of search
  36. $search_account = GETPOST('search_account', 'alpha');
  37. $search_label = GETPOST('search_label', 'alpha');
  38. $search_accountparent = GETPOST('search_accountparent', 'alpha');
  39. $search_pcgtype = GETPOST('search_pcgtype', 'alpha');
  40. $search_pcgsubtype = GETPOST('search_pcgsubtype', 'alpha');
  41. // Security check
  42. if ($user->societe_id > 0) accessforbidden();
  43. if (! $user->rights->accounting->chartofaccount) accessforbidden();
  44. // Load variable for pagination
  45. $limit = GETPOST('limit', 'int')?GETPOST('limit', 'int'):$conf->liste_limit;
  46. $sortfield = GETPOST('sortfield', 'alpha');
  47. $sortorder = GETPOST('sortorder', 'alpha');
  48. $page = GETPOST('page', 'int');
  49. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  50. $offset = $limit * $page;
  51. $pageprev = $page - 1;
  52. $pagenext = $page + 1;
  53. if (! $sortfield) $sortfield = "aa.account_number";
  54. if (! $sortorder) $sortorder = "ASC";
  55. $arrayfields=array(
  56. 'aa.account_number'=>array('label'=>$langs->trans("AccountNumber"), 'checked'=>1),
  57. 'aa.label'=>array('label'=>$langs->trans("Label"), 'checked'=>1),
  58. 'aa.account_parent'=>array('label'=>$langs->trans("Accountparent"), 'checked'=>1),
  59. 'aa.pcg_type'=>array('label'=>$langs->trans("Pcgtype"), 'checked'=>1, 'help'=>'PcgtypeDesc'),
  60. 'aa.pcg_subtype'=>array('label'=>$langs->trans("Pcgsubtype"), 'checked'=>0, 'help'=>'PcgtypeDesc'),
  61. 'aa.active'=>array('label'=>$langs->trans("Activated"), 'checked'=>1)
  62. );
  63. $accounting = new AccountingAccount($db);
  64. /*
  65. * Actions
  66. */
  67. if (GETPOST('cancel', 'alpha')) { $action='list'; $massaction=''; }
  68. if (! GETPOST('confirmmassaction', 'alpha')) { $massaction=''; }
  69. $parameters=array();
  70. $reshook=$hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  71. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  72. if (empty($reshook))
  73. {
  74. if (! empty($cancel)) $action = '';
  75. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  76. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') ||GETPOST('button_removefilter', 'alpha')) // All test are required to be compatible with all browsers
  77. {
  78. $search_account = "";
  79. $search_label = "";
  80. $search_accountparent = "";
  81. $search_pcgtype = "";
  82. $search_pcgsubtype = "";
  83. $search_array_options=array();
  84. }
  85. if (GETPOST('change_chart', 'alpha'))
  86. {
  87. $chartofaccounts = GETPOST('chartofaccounts', 'int');
  88. if ($chartofaccounts > 0)
  89. {
  90. // Get language code for this $chartofaccounts
  91. $sql ='SELECT code FROM '.MAIN_DB_PREFIX.'c_country as c, '.MAIN_DB_PREFIX.'accounting_system as a';
  92. $sql.=' WHERE c.rowid = a.fk_country AND a.rowid = '.(int) $chartofaccounts;
  93. $resql = $db->query($sql);
  94. if ($resql)
  95. {
  96. $obj = $db->fetch_object($resql);
  97. $country_code = $obj->code;
  98. }
  99. else dol_print_error($db);
  100. // Try to load sql file
  101. if ($country_code)
  102. {
  103. $sqlfile = DOL_DOCUMENT_ROOT.'/install/mysql/data/llx_accounting_account_'.strtolower($country_code).'.sql';
  104. $offsetforchartofaccount = 0;
  105. // Get the comment line '-- ADD CCCNNNNN to rowid...' to find CCCNNNNN (CCC is country num, NNNNN is id of accounting account)
  106. // and pass CCCNNNNN + (num of company * 100 000 000) as offset to the run_sql as a new parameter to say to update sql on the fly to add offset to rowid and account_parent value.
  107. // This is to be sure there is no conflict for each chart of account, whatever is country, whatever is company when multicompany is used.
  108. $tmp = file_get_contents($sqlfile);
  109. if (preg_match('/-- ADD (\d+) to rowid/ims', $tmp, $reg))
  110. {
  111. $offsetforchartofaccount += $reg[1];
  112. }
  113. $offsetforchartofaccount+=($conf->entity * 100000000);
  114. $result = run_sql($sqlfile, 1, $conf->entity, 1, '', 'default', 32768, 0, $offsetforchartofaccount);
  115. if ($result > 0)
  116. {
  117. setEventMessages($langs->trans("ChartLoaded"), null);
  118. }
  119. else
  120. {
  121. setEventMessages($langs->trans("ErrorDuringChartLoad"), null, 'warnings');
  122. }
  123. }
  124. if (! dolibarr_set_const($db, 'CHARTOFACCOUNTS', $chartofaccounts, 'chaine', 0, '', $conf->entity)) {
  125. $error++;
  126. }
  127. } else {
  128. $error++;
  129. }
  130. }
  131. if ($action == 'disable') {
  132. if ($accounting->fetch($id)) {
  133. $result = $accounting->account_desactivate($id);
  134. }
  135. $action = 'update';
  136. if ($result < 0) {
  137. setEventMessages($accounting->error, $accounting->errors, 'errors');
  138. }
  139. } elseif ($action == 'enable') {
  140. if ($accounting->fetch($id)) {
  141. $result = $accounting->account_activate($id);
  142. }
  143. $action = 'update';
  144. if ($result < 0) {
  145. setEventMessages($accounting->error, $accounting->errors, 'errors');
  146. }
  147. }
  148. }
  149. /*
  150. * View
  151. */
  152. $form=new Form($db);
  153. llxHeader('', $langs->trans("ListAccounts"));
  154. if ($action == 'delete') {
  155. $formconfirm = $html->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id, $langs->trans('DeleteAccount'), $langs->trans('ConfirmDeleteAccount'), 'confirm_delete', '', 0, 1);
  156. print $formconfirm;
  157. }
  158. $pcgver = $conf->global->CHARTOFACCOUNTS;
  159. $sql = "SELECT aa.rowid, aa.fk_pcg_version, aa.pcg_type, aa.pcg_subtype, aa.account_number, aa.account_parent , aa.label, aa.active, ";
  160. $sql .= " a2.rowid as rowid2, a2.label as label2, a2.account_number as account_number2";
  161. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  162. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version AND aa.entity = " . $conf->entity;
  163. if ($db->type == 'pgsql') $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = aa.account_parent AND a2.entity = " . $conf->entity;
  164. else $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = aa.account_parent AND a2.entity = " . $conf->entity;
  165. $sql .= " WHERE asy.rowid = " . $pcgver;
  166. //print $sql;
  167. if (strlen(trim($search_account))) $sql .= natural_search("aa.account_number", $search_account);
  168. if (strlen(trim($search_label))) $sql .= natural_search("aa.label", $search_label);
  169. if (strlen(trim($search_accountparent))) $sql .= natural_search("aa.account_parent", $search_accountparent);
  170. if (strlen(trim($search_pcgtype))) $sql .= natural_search("aa.pcg_type", $search_pcgtype);
  171. if (strlen(trim($search_pcgsubtype))) $sql .= natural_search("aa.pcg_subtype", $search_pcgsubtype);
  172. $sql .= $db->order($sortfield, $sortorder);
  173. // Count total nb of records
  174. $nbtotalofrecords = '';
  175. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  176. {
  177. $resql = $db->query($sql);
  178. $nbtotalofrecords = $db->num_rows($resql);
  179. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  180. {
  181. $page = 0;
  182. $offset = 0;
  183. }
  184. }
  185. $sql .= $db->plimit($limit + 1, $offset);
  186. dol_syslog('accountancy/admin/account.php:: $sql=' . $sql);
  187. $resql = $db->query($sql);
  188. if ($resql)
  189. {
  190. $num = $db->num_rows($resql);
  191. $param='';
  192. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  193. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  194. if ($search_account) $param.= '&search_account='.urlencode($search_account);
  195. if ($search_label) $param.= '&search_label='.urlencode($search_label);
  196. if ($search_accountparent) $param.= '&search_accountparent='.urlencode($search_accountparent);
  197. if ($search_pcgtype) $param.= '&search_pcgtype='.urlencode($search_pcgtype);
  198. if ($search_pcgsubtype) $param.= '&search_pcgsubtype='.urlencode($search_pcgsubtype);
  199. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  200. print '<form method="POST" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
  201. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  202. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  203. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  204. print '<input type="hidden" name="action" value="list">';
  205. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  206. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  207. print '<input type="hidden" name="page" value="'.$page.'">';
  208. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  209. $newcardbutton.= dolGetButtonTitle($langs->trans("New"), $langs->trans("Addanaccount"), 'fa fa-plus-circle', './card.php?action=create');
  210. print_barre_liste($langs->trans('ListAccounts'), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy', 0, $newcardbutton, '', $limit);
  211. // Box to select active chart of account
  212. print $langs->trans("Selectchartofaccounts") . " : ";
  213. print '<select class="flat" name="chartofaccounts" id="chartofaccounts">';
  214. $sql = "SELECT a.rowid, a.pcg_version, a.label, a.active, c.code as country_code";
  215. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_system as a";
  216. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as c ON a.fk_country = c.rowid AND c.active = 1";
  217. $sql .= " WHERE a.active = 1";
  218. dol_syslog('accountancy/admin/account.php $sql='.$sql);
  219. print $sql;
  220. $resqlchart = $db->query($sql);
  221. if ($resqlchart) {
  222. $numbis = $db->num_rows($resqlchart);
  223. $i = 0;
  224. while ($i < $numbis) {
  225. $obj = $db->fetch_object($resqlchart);
  226. print '<option value="' . $obj->rowid . '"';
  227. print ($pcgver == $obj->rowid) ? ' selected' : '';
  228. print '>' . $obj->pcg_version . ' - ' . $obj->label . ' - (' . $obj->country_code . ')</option>';
  229. $i++;
  230. }
  231. }
  232. else dol_print_error($db);
  233. print "</select>";
  234. print ajax_combobox("chartofaccounts");
  235. print '<input type="submit" class="button" name="change_chart" tabindex="-1" value="'.dol_escape_htmltag($langs->trans("ChangeAndLoad")).'">';
  236. print '<br>';
  237. print '<br>';
  238. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  239. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  240. print '<div class="div-table-responsive">';
  241. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  242. // Line for search fields
  243. print '<tr class="liste_titre_filter">';
  244. if (! empty($arrayfields['aa.account_number']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_account" value="' . $search_account . '"></td>';
  245. if (! empty($arrayfields['aa.label']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="' . $search_label . '"></td>';
  246. if (! empty($arrayfields['aa.account_parent']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_accountparent" value="' . $search_accountparent . '"></td>';
  247. if (! empty($arrayfields['aa.pcg_type']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgtype" value="' . $search_pcgtype . '"></td>';
  248. if (! empty($arrayfields['aa.pcg_subtype']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgsubtype" value="' . $search_pcgsubtype . '"></td>';
  249. if (! empty($arrayfields['aa.active']['checked'])) print '<td class="liste_titre">&nbsp;</td>';
  250. print '<td class="liste_titre maxwidthsearch">';
  251. $searchpicto=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
  252. print $searchpicto;
  253. print '</td>';
  254. print '</tr>';
  255. print '<tr class="liste_titre">';
  256. if (! empty($arrayfields['aa.account_number']['checked'])) print_liste_field_titre($arrayfields['aa.account_number']['label'], $_SERVER["PHP_SELF"], "aa.account_number", "", $param, '', $sortfield, $sortorder);
  257. if (! empty($arrayfields['aa.label']['checked'])) print_liste_field_titre($arrayfields['aa.label']['label'], $_SERVER["PHP_SELF"], "aa.label", "", $param, '', $sortfield, $sortorder);
  258. if (! empty($arrayfields['aa.account_parent']['checked'])) print_liste_field_titre($arrayfields['aa.account_parent']['label'], $_SERVER["PHP_SELF"], "aa.account_parent", "", $param, '', $sortfield, $sortorder, 'left ');
  259. if (! empty($arrayfields['aa.pcg_type']['checked'])) print_liste_field_titre($arrayfields['aa.pcg_type']['label'], $_SERVER["PHP_SELF"], 'aa.pcg_type', '', $param, '', $sortfield, $sortorder, '', $arrayfields['aa.pcg_type']['help']);
  260. if (! empty($arrayfields['aa.pcg_subtype']['checked'])) print_liste_field_titre($arrayfields['aa.pcg_subtype']['label'], $_SERVER["PHP_SELF"], 'aa.pcg_subtype', '', $param, '', $sortfield, $sortorder, '', $arrayfields['aa.pcg_subtype']['help']);
  261. if (! empty($arrayfields['aa.active']['checked'])) print_liste_field_titre($arrayfields['aa.active']['label'], $_SERVER["PHP_SELF"], 'aa.active', '', $param, '', $sortfield, $sortorder);
  262. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  263. print "</tr>\n";
  264. $accountstatic = new AccountingAccount($db);
  265. $accountparent = new AccountingAccount($db);
  266. $i=0;
  267. while ($i < min($num, $limit))
  268. {
  269. $obj = $db->fetch_object($resql);
  270. $accountstatic->id = $obj->rowid;
  271. $accountstatic->label = $obj->label;
  272. $accountstatic->account_number = $obj->account_number;
  273. print '<tr class="oddeven">';
  274. // Account number
  275. if (! empty($arrayfields['aa.account_number']['checked']))
  276. {
  277. print "<td>";
  278. print $accountstatic->getNomUrl(1, 0, 0, '', 0, 1);
  279. print "</td>\n";
  280. if (! $i) $totalarray['nbfield']++;
  281. }
  282. // Account label
  283. if (! empty($arrayfields['aa.label']['checked']))
  284. {
  285. print "<td>";
  286. print $obj->label;
  287. print "</td>\n";
  288. if (! $i) $totalarray['nbfield']++;
  289. }
  290. // Account parent
  291. if (! empty($arrayfields['aa.account_parent']['checked']))
  292. {
  293. if (! empty($obj->account_parent))
  294. {
  295. $accountparent->id = $obj->rowid2;
  296. $accountparent->label = $obj->label2;
  297. $accountparent->account_number = $obj->account_number2;
  298. print "<td>";
  299. print $accountparent->getNomUrl(1);
  300. print "</td>\n";
  301. if (! $i) $totalarray['nbfield']++;
  302. }
  303. else
  304. {
  305. print '<td>&nbsp;</td>';
  306. if (! $i) $totalarray['nbfield']++;
  307. }
  308. }
  309. // Chart of accounts type
  310. if (! empty($arrayfields['aa.pcg_type']['checked']))
  311. {
  312. print "<td>";
  313. print $obj->pcg_type;
  314. print "</td>\n";
  315. if (! $i) $totalarray['nbfield']++;
  316. }
  317. // Chart of accounts subtype
  318. if (! empty($arrayfields['aa.pcg_subtype']['checked']))
  319. {
  320. print "<td>";
  321. print $obj->pcg_subtype;
  322. print "</td>\n";
  323. if (! $i) $totalarray['nbfield']++;
  324. }
  325. // Activated or not
  326. if (! empty($arrayfields['aa.active']['checked']))
  327. {
  328. print '<td>';
  329. if (empty($obj->active)) {
  330. print '<a class="reposition" href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=enable">';
  331. print img_picto($langs->trans("Disabled"), 'switch_off');
  332. print '</a>';
  333. } else {
  334. print '<a class="reposition" href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=disable">';
  335. print img_picto($langs->trans("Activated"), 'switch_on');
  336. print '</a>';
  337. }
  338. print '</td>';
  339. if (! $i) $totalarray['nbfield']++;
  340. }
  341. // Action
  342. print '<td class="center">';
  343. if ($user->rights->accounting->chartofaccount) {
  344. print '<a href="./card.php?action=update&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id).'">';
  345. print img_edit();
  346. print '</a>';
  347. print '&nbsp;';
  348. print '<a href="./card.php?action=delete&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id). '">';
  349. print img_delete();
  350. print '</a>';
  351. }
  352. print '</td>' . "\n";
  353. if (! $i) $totalarray['nbfield']++;
  354. print "</tr>\n";
  355. $i++;
  356. }
  357. print "</table>";
  358. print "</div>";
  359. print '</form>';
  360. } else {
  361. dol_print_error($db);
  362. }
  363. // End of page
  364. llxFooter();
  365. $db->close();