account.php 14 KB

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