account.php 16 KB

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