account.php 15 KB

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