account.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.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. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/html.formventilation.class.php';
  29. // Langs
  30. $langs->load("compta");
  31. $langs->load("bills");
  32. $langs->load("admin");
  33. $langs->load("accountancy");
  34. $langs->load("salaries");
  35. $mesg = '';
  36. $action = GETPOST('action');
  37. $cancel = GETPOST('cancel');
  38. $id = GETPOST('id', 'int');
  39. $rowid = GETPOST('rowid', 'int');
  40. $search_account = GETPOST("search_account");
  41. $search_label = GETPOST("search_label");
  42. $search_accountparent = GETPOST("search_accountparent");
  43. $search_pcgtype = GETPOST("search_pcgtype");
  44. $search_pcgsubtype = GETPOST("search_pcgsubtype");
  45. // Security check
  46. if ($user->societe_id > 0) accessforbidden();
  47. if (! $user->rights->accounting->chartofaccount) accessforbidden();
  48. // Load variable for pagination
  49. $limit = GETPOST("limit")?GETPOST("limit","int"):$conf->liste_limit;
  50. $sortfield = GETPOST("sortfield", 'alpha');
  51. $sortorder = GETPOST("sortorder", 'sortorder');
  52. $limit = GETPOST('limit') ? GETPOST('limit', 'int') : $conf->liste_limit;
  53. $page = GETPOST("page", 'int');
  54. if ($page == - 1) {
  55. $page = 0;
  56. }
  57. $offset = $limit * $page;
  58. $pageprev = $page - 1;
  59. $pagenext = $page + 1;
  60. if (! $sortfield)
  61. $sortfield = "aa.account_number";
  62. if (! $sortorder)
  63. $sortorder = "ASC";
  64. $accounting = new AccountingAccount($db);
  65. /*
  66. * Actions
  67. */
  68. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  69. if (! GETPOST('confirmmassaction')) { $massaction=''; }
  70. $parameters=array();
  71. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  72. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  73. if (empty($reshook))
  74. {
  75. if (! empty($cancel)) $action = '';
  76. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  77. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") ||GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  78. {
  79. $search_account = "";
  80. $search_label = "";
  81. $search_accountparent = "";
  82. $search_pcgtype = "";
  83. $search_pcgsubtype = "";
  84. }
  85. if (GETPOST('change_chart'))
  86. {
  87. $chartofaccounts = GETPOST('chartofaccounts', 'int');
  88. if (! empty($chartofaccounts)) {
  89. if (! dolibarr_set_const($db, 'CHARTOFACCOUNTS', $chartofaccounts, 'chaine', 0, '', $conf->entity)) {
  90. $error ++;
  91. }
  92. } else {
  93. $error ++;
  94. }
  95. }
  96. if ($action == 'disable') {
  97. if ($accounting->fetch($id)) {
  98. $result = $accounting->account_desactivate($id);
  99. }
  100. $action = 'update';
  101. if ($result < 0) {
  102. setEventMessages($accounting->error, $accounting->errors, 'errors');
  103. }
  104. } else if ($action == 'enable') {
  105. if ($accounting->fetch($id)) {
  106. $result = $accounting->account_activate($id);
  107. }
  108. $action = 'update';
  109. if ($result < 0) {
  110. setEventMessages($accounting->error, $accounting->errors, 'errors');
  111. }
  112. }
  113. }
  114. /*
  115. * View
  116. */
  117. llxHeader('', $langs->trans("ListAccounts"));
  118. if ($action == 'delete') {
  119. $formconfirm = $html->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id, $langs->trans('DeleteAccount'), $langs->trans('ConfirmDeleteAccount'), 'confirm_delete', '', 0, 1);
  120. print $formconfirm;
  121. }
  122. $pcgver = $conf->global->CHARTOFACCOUNTS;
  123. $sql = "SELECT aa.rowid, aa.fk_pcg_version, aa.pcg_type, aa.pcg_subtype, aa.account_number, aa.account_parent , aa.label, aa.active, ";
  124. $sql .= " a2.rowid as rowid2, a2.label as label2, a2.account_number as account_number2";
  125. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  126. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  127. // Dirty hack wainting that foreign key account_parent is an integer to be compared correctly with rowid
  128. if ($db->type == 'pgsql') $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = CAST(aa.account_parent AS INTEGER)";
  129. else $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = CAST(aa.account_parent AS UNSIGNED)";
  130. $sql .= " WHERE asy.rowid = " . $pcgver;
  131. if (strlen(trim($search_account))) {
  132. $sql .= natural_search("aa.account_number", $search_account);
  133. }
  134. if (strlen(trim($search_label))) {
  135. $sql .= natural_search("aa.label", $search_label);
  136. }
  137. if (strlen(trim($search_accountparent))) {
  138. $sql .= natural_search("aa.account_parent", $search_accountparent);
  139. }
  140. if (strlen(trim($search_pcgtype))) {
  141. $sql .= natural_search("aa.pcg_type", $search_pcgtype);
  142. }
  143. if (strlen(trim($search_pcgsubtype))) {
  144. $sql .= natural_search("aa.pcg_subtype", $search_pcgsubtype);
  145. }
  146. $sql .= $db->order($sortfield, $sortorder);
  147. // Count total nb of records
  148. $nbtotalofrecords = '';
  149. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  150. {
  151. $resql = $db->query($sql);
  152. $nbtotalofrecords = $db->num_rows($resql);
  153. }
  154. $sql .= $db->plimit($limit + 1, $offset);
  155. dol_syslog('accountancy/admin/account.php:: $sql=' . $sql);
  156. $resql = $db->query($sql);
  157. if ($resql) {
  158. $num = $db->num_rows($resql);
  159. $params='';
  160. if ($search_account != "") $params.= '&amp;search_account='.urlencode($search_account);
  161. if ($search_label != "") $params.= '&amp;search_label='.urlencode($search_label);
  162. if ($search_accountparent != "") $params.= '&amp;search_accountparent='.urlencode($search_accountparent);
  163. if ($search_pcgtype != "") $params.= '&amp;search_pcgtype='.urlencode($search_pcgtype);
  164. if ($search_pcgsubtype != "") $params.= '&amp;search_pcgsubtype='.urlencode($search_pcgsubtype);
  165. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  166. print '<form method="GET" action="' . $_SERVER["PHP_SELF"] . '">';
  167. $htmlbuttonadd = '<a class="butAction" href="./card.php?action=create">' . $langs->trans("Addanaccount") . '</a>';
  168. print_barre_liste($langs->trans('ListAccounts'), $page, $_SERVER["PHP_SELF"], $params, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy', 0, $htmlbuttonadd);
  169. // Box to select active chart of account
  170. $var = ! $var;
  171. print $langs->trans("Selectchartofaccounts") . " : ";
  172. print '<select class="flat" name="chartofaccounts" id="chartofaccounts">';
  173. $sql = "SELECT rowid, pcg_version, label, active";
  174. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_system";
  175. $sql .= " WHERE active = 1";
  176. dol_syslog('accountancy/admin/index.php:: $sql=' . $sql);
  177. $resqlchart = $db->query($sql);
  178. $var = true;
  179. if ($resqlchart) {
  180. $numbis = $db->num_rows($resqlchart);
  181. $i = 0;
  182. while ( $i < $numbis ) {
  183. $var = ! $var;
  184. $row = $db->fetch_row($resqlchart);
  185. print '<option value="' . $row[0] . '"';
  186. print $pcgver == $row[0] ? ' selected' : '';
  187. print '>' . $row[1] . ' - ' . $row[2] . '</option>';
  188. $i ++;
  189. }
  190. }
  191. print "</select>";
  192. print '<input type="submit" class="button" name="change_chart" value="'.dol_escape_htmltag($langs->trans("ChangeAndLoad")).'">';
  193. print '<br>';
  194. print '<br>';
  195. print '<table class="noborder" width="100%">';
  196. print '<tr class="liste_titre">';
  197. print_liste_field_titre($langs->trans("AccountNumber"), $_SERVER["PHP_SELF"], "aa.account_number", "", $params, "", $sortfield, $sortorder);
  198. print_liste_field_titre($langs->trans("Label"), $_SERVER["PHP_SELF"], "aa.label", "", $params, "", $sortfield, $sortorder);
  199. print_liste_field_titre($langs->trans("Accountparent"), $_SERVER["PHP_SELF"], "aa.account_parent", "", $params, "", $sortfield, $sortorder);
  200. print_liste_field_titre($langs->trans("Pcgtype"), $_SERVER["PHP_SELF"], "aa.pcg_type", "", $params, "", $sortfield, $sortorder);
  201. print_liste_field_titre($langs->trans("Pcgsubtype"), $_SERVER["PHP_SELF"], "aa.pcg_subtype", "", $params, "", $sortfield, $sortorder);
  202. print_liste_field_titre($langs->trans("Activated"), $_SERVER["PHP_SELF"], "aa.active", "", $params, "", $sortfield, $sortorder);
  203. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $params, "", 'width="60" align="center"', $sortfield, $sortorder);
  204. print '</tr>';
  205. print '<tr class="liste_titre">';
  206. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_account" value="' . $search_account . '"></td>';
  207. print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="' . $search_label . '"></td>';
  208. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_accountparent" value="' . $search_accountparent . '"></td>';
  209. print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgtype" value="' . $search_pcgtype . '"></td>';
  210. print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgsubtype" value="' . $search_pcgsubtype . '"></td>';
  211. print '<td class="liste_titre">&nbsp;</td>';
  212. print '<td align="right" colspan="2" class="liste_titre">';
  213. $searchpitco=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
  214. print $searchpitco;
  215. print '</td>';
  216. print '</tr>';
  217. $var = false;
  218. $accountstatic = new AccountingAccount($db);
  219. $accountparent = new AccountingAccount($db);
  220. $i = 0;
  221. while ( $i < min($num, $limit) )
  222. {
  223. $obj = $db->fetch_object($resql);
  224. $accountstatic->id = $obj->rowid;
  225. $accountstatic->label = $obj->label;
  226. $accountstatic->account_number = $obj->account_number;
  227. print '<tr ' . $bc[$var] . '>';
  228. print '<td>' . $accountstatic->getNomUrl(1) . '</td>';
  229. print '<td>' . $obj->label . '</td>';
  230. if (! empty($obj->account_parent))
  231. {
  232. $accountparent->id = $obj->rowid2;
  233. $accountparent->label = $obj->label2;
  234. $accountparent->account_number = $obj->account_number2;
  235. print '<td>' . $accountparent->getNomUrl(1) . '</td>';
  236. }
  237. else
  238. {
  239. print '<td>&nbsp;</td>';
  240. }
  241. print '<td>' . $obj->pcg_type . '</td>';
  242. print '<td>' . $obj->pcg_subtype . '</td>';
  243. print '<td>';
  244. if (empty($obj->active)) {
  245. print '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=enable">';
  246. print img_picto($langs->trans("Disabled"), 'switch_off');
  247. print '</a>';
  248. } else {
  249. print '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=disable">';
  250. print img_picto($langs->trans("Activated"), 'switch_on');
  251. print '</a>';
  252. }
  253. print '</td>';
  254. // Action
  255. print '<td align="center">';
  256. if ($user->rights->accounting->chartofaccount) {
  257. print '<a href="./card.php?action=update&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id).'">';
  258. print img_edit();
  259. print '</a>';
  260. print '&nbsp;';
  261. print '<a href="./card.php?action=delete&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id). '">';
  262. print img_delete();
  263. print '</a>';
  264. }
  265. print '</td>' . "\n";
  266. print "</tr>\n";
  267. $var = ! $var;
  268. $i++;
  269. }
  270. print "</table>";
  271. print '</form>';
  272. } else {
  273. dol_print_error($db);
  274. }
  275. llxFooter();
  276. $db->close();