subaccount.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2020 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/subaccount.php
  21. * \ingroup Accountancy (Double entries)
  22. * \brief List of accounting sub-account (auxiliary accounts)
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("compta", "bills", "admin", "accountancy", "salaries", "hrm", "errors"));
  30. $mesg = '';
  31. $action = GETPOST('action', 'aZ09');
  32. $cancel = GETPOST('cancel', 'alpha');
  33. $id = GETPOST('id', 'int');
  34. $rowid = GETPOST('rowid', 'int');
  35. $massaction = GETPOST('massaction', 'aZ09');
  36. $optioncss = GETPOST('optioncss', 'alpha');
  37. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'accountingsubaccountlist'; // To manage different context of search
  38. $search_subaccount = GETPOST('search_subaccount', 'alpha');
  39. $search_label = GETPOST('search_label', 'alpha');
  40. $search_type = GETPOST('search_type', 'int');
  41. // Security check
  42. if ($user->socid > 0) {
  43. accessforbidden();
  44. }
  45. if (!$user->hasRight('accounting', 'chartofaccount')) {
  46. accessforbidden();
  47. }
  48. // Load variable for pagination
  49. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  50. $sortfield = GETPOST('sortfield', 'aZ09comma');
  51. $sortorder = GETPOST('sortorder', 'aZ09comma');
  52. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  53. if (empty($page) || $page == -1) {
  54. $page = 0;
  55. } // If $page is not defined, or '' or -1
  56. $offset = $limit * $page;
  57. $pageprev = $page - 1;
  58. $pagenext = $page + 1;
  59. if (!$sortfield) {
  60. $sortfield = "label";
  61. }
  62. if (!$sortorder) {
  63. $sortorder = "ASC";
  64. }
  65. $arrayfields = array(
  66. 'subaccount'=>array('label'=>$langs->trans("AccountNumber"), 'checked'=>1),
  67. 'label'=>array('label'=>$langs->trans("Label"), 'checked'=>1),
  68. 'type'=>array('label'=>$langs->trans("Type"), 'checked'=>1),
  69. 'reconcilable'=>array('label'=>$langs->trans("Reconcilable"), 'checked'=>1)
  70. );
  71. if ($conf->global->MAIN_FEATURES_LEVEL < 2) {
  72. unset($arrayfields['reconcilable']);
  73. }
  74. /*
  75. * Actions
  76. */
  77. if (GETPOST('cancel', 'alpha')) {
  78. $action = 'list'; $massaction = '';
  79. }
  80. if (!GETPOST('confirmmassaction', 'alpha')) {
  81. $massaction = '';
  82. }
  83. $parameters = array();
  84. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  85. if ($reshook < 0) {
  86. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  87. }
  88. if (empty($reshook)) {
  89. if (!empty($cancel)) {
  90. $action = '';
  91. }
  92. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  93. 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
  94. $search_subaccount = "";
  95. $search_label = "";
  96. $search_type = "";
  97. $search_array_options = array();
  98. }
  99. }
  100. /*
  101. * View
  102. */
  103. $form = new Form($db);
  104. $help_url = '';
  105. $title = $langs->trans('ChartOfIndividualAccountsOfSubsidiaryLedger');
  106. llxHeader('', $title, $help_url);
  107. // Customer
  108. $sql = "SELECT sa.rowid, sa.nom as label, sa.code_compta as subaccount, '1' as type, sa.entity";
  109. $sql .= " FROM ".MAIN_DB_PREFIX."societe sa";
  110. $sql .= " WHERE sa.entity IN (".getEntity('societe').")";
  111. $sql .= " AND sa.code_compta <> ''";
  112. //print $sql;
  113. if (strlen(trim($search_subaccount))) {
  114. $lengthpaddingaccount = 0;
  115. if ($conf->global->ACCOUNTING_LENGTH_AACCOUNT) {
  116. $lengthpaddingaccount = max($conf->global->ACCOUNTING_LENGTH_AACCOUNT);
  117. }
  118. $search_subaccount_tmp = $search_subaccount;
  119. $weremovedsomezero = 0;
  120. if (strlen($search_subaccount_tmp) <= $lengthpaddingaccount) {
  121. for ($i = 0; $i < $lengthpaddingaccount; $i++) {
  122. if (preg_match('/0$/', $search_subaccount_tmp)) {
  123. $weremovedsomezero++;
  124. $search_subaccount_tmp = preg_replace('/0$/', '', $search_subaccount_tmp);
  125. }
  126. }
  127. }
  128. //var_dump($search_subaccount); exit;
  129. if ($search_subaccount_tmp) {
  130. if ($weremovedsomezero) {
  131. $search_subaccount_tmp_clean = $search_subaccount_tmp;
  132. $search_subaccount_clean = $search_subaccount;
  133. $startchar = '%';
  134. if (strpos($search_subaccount_tmp, '^') === 0) {
  135. $startchar = '';
  136. $search_subaccount_tmp_clean = preg_replace('/^\^/', '', $search_subaccount_tmp);
  137. $search_subaccount_clean = preg_replace('/^\^/', '', $search_subaccount);
  138. }
  139. $sql .= " AND (sa.code_compta LIKE '".$db->escape($startchar.$search_subaccount_tmp_clean)."'";
  140. $sql .= " OR sa.code_compta LIKE '".$db->escape($startchar.$search_subaccount_clean)."%')";
  141. } else {
  142. $sql .= natural_search("sa.code_compta", $search_subaccount_tmp);
  143. }
  144. }
  145. }
  146. if (strlen(trim($search_label))) {
  147. $sql .= natural_search("sa.nom", $search_label);
  148. }
  149. if (!empty($search_type) && $search_type >= 0) {
  150. $sql .= " HAVING type LIKE '".$db->escape($search_type)."'";
  151. }
  152. // Supplier
  153. $sql .= " UNION ";
  154. $sql .= " SELECT sa.rowid, sa.nom as label, sa.code_compta_fournisseur as subaccount, '2' as type, sa.entity FROM ".MAIN_DB_PREFIX."societe sa";
  155. $sql .= " WHERE sa.entity IN (".getEntity('societe').")";
  156. $sql .= " AND sa.code_compta_fournisseur <> ''";
  157. //print $sql;
  158. if (strlen(trim($search_subaccount))) {
  159. $lengthpaddingaccount = 0;
  160. if ($conf->global->ACCOUNTING_LENGTH_AACCOUNT) {
  161. $lengthpaddingaccount = max($conf->global->ACCOUNTING_LENGTH_AACCOUNT);
  162. }
  163. $search_subaccount_tmp = $search_subaccount;
  164. $weremovedsomezero = 0;
  165. if (strlen($search_subaccount_tmp) <= $lengthpaddingaccount) {
  166. for ($i = 0; $i < $lengthpaddingaccount; $i++) {
  167. if (preg_match('/0$/', $search_subaccount_tmp)) {
  168. $weremovedsomezero++;
  169. $search_subaccount_tmp = preg_replace('/0$/', '', $search_subaccount_tmp);
  170. }
  171. }
  172. }
  173. //var_dump($search_subaccount); exit;
  174. if ($search_subaccount_tmp) {
  175. if ($weremovedsomezero) {
  176. $search_subaccount_tmp_clean = $search_subaccount_tmp;
  177. $search_subaccount_clean = $search_subaccount;
  178. $startchar = '%';
  179. if (strpos($search_subaccount_tmp, '^') === 0) {
  180. $startchar = '';
  181. $search_subaccount_tmp_clean = preg_replace('/^\^/', '', $search_subaccount_tmp);
  182. $search_subaccount_clean = preg_replace('/^\^/', '', $search_subaccount);
  183. }
  184. $sql .= " AND (sa.code_compta_fournisseur LIKE '".$db->escape($startchar.$search_subaccount_tmp_clean)."'";
  185. $sql .= " OR sa.code_compta_fournisseur LIKE '".$db->escape($startchar.$search_subaccount_clean)."%')";
  186. } else {
  187. $sql .= natural_search("sa.code_compta_fournisseur", $search_subaccount_tmp);
  188. }
  189. }
  190. }
  191. if (strlen(trim($search_label))) {
  192. $sql .= natural_search("sa.nom", $search_label);
  193. }
  194. if (!empty($search_type) && $search_type >= 0) {
  195. $sql .= " HAVING type LIKE '".$db->escape($search_type)."'";
  196. }
  197. // User
  198. $sql .= " UNION ";
  199. $sql .= " SELECT u.rowid, u.lastname as label, u.accountancy_code as subaccount, '3' as type, u.entity FROM ".MAIN_DB_PREFIX."user u";
  200. $sql .= " WHERE u.entity IN (".getEntity('user').")";
  201. $sql .= " AND u.accountancy_code <> ''";
  202. //print $sql;
  203. if (strlen(trim($search_subaccount))) {
  204. $lengthpaddingaccount = 0;
  205. if ($conf->global->ACCOUNTING_LENGTH_AACCOUNT) {
  206. $lengthpaddingaccount = max($conf->global->ACCOUNTING_LENGTH_AACCOUNT);
  207. }
  208. $search_subaccount_tmp = $search_subaccount;
  209. $weremovedsomezero = 0;
  210. if (strlen($search_subaccount_tmp) <= $lengthpaddingaccount) {
  211. for ($i = 0; $i < $lengthpaddingaccount; $i++) {
  212. if (preg_match('/0$/', $search_subaccount_tmp)) {
  213. $weremovedsomezero++;
  214. $search_subaccount_tmp = preg_replace('/0$/', '', $search_subaccount_tmp);
  215. }
  216. }
  217. }
  218. //var_dump($search_subaccount); exit;
  219. if ($search_subaccount_tmp) {
  220. if ($weremovedsomezero) {
  221. $search_subaccount_tmp_clean = $search_subaccount_tmp;
  222. $search_subaccount_clean = $search_subaccount;
  223. $startchar = '%';
  224. if (strpos($search_subaccount_tmp, '^') === 0) {
  225. $startchar = '';
  226. $search_subaccount_tmp_clean = preg_replace('/^\^/', '', $search_subaccount_tmp);
  227. $search_subaccount_clean = preg_replace('/^\^/', '', $search_subaccount);
  228. }
  229. $sql .= " AND (u.accountancy_code LIKE '".$db->escape($startchar.$search_subaccount_tmp_clean)."'";
  230. $sql .= " OR u.accountancy_code LIKE '".$db->escape($startchar.$search_subaccount_clean)."%')";
  231. } else {
  232. $sql .= natural_search("u.accountancy_code", $search_subaccount_tmp);
  233. }
  234. }
  235. }
  236. if (strlen(trim($search_label))) {
  237. $sql .= natural_search("u.lastname", $search_label);
  238. }
  239. if (!empty($search_type) && $search_type >= 0) {
  240. $sql .= " HAVING type LIKE '".$db->escape($search_type)."'";
  241. }
  242. $sql .= $db->order($sortfield, $sortorder);
  243. // Count total nb of records
  244. $nbtotalofrecords = '';
  245. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  246. $resql = $db->query($sql);
  247. $nbtotalofrecords = $db->num_rows($resql);
  248. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  249. $page = 0;
  250. $offset = 0;
  251. }
  252. }
  253. $sql .= $db->plimit($limit + 1, $offset);
  254. dol_syslog('accountancy/admin/subaccount.php:: $sql='.$sql);
  255. $resql = $db->query($sql);
  256. if ($resql) {
  257. $num = $db->num_rows($resql);
  258. $param = '';
  259. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  260. $param .= '&contextpage='.urlencode($contextpage);
  261. }
  262. if ($limit > 0 && $limit != $conf->liste_limit) {
  263. $param .= '&limit='.urlencode($limit);
  264. }
  265. if ($search_subaccount) {
  266. $param .= '&search_subaccount='.urlencode($search_subaccount);
  267. }
  268. if ($search_label) {
  269. $param .= '&search_label='.urlencode($search_label);
  270. }
  271. if ($optioncss != '') {
  272. $param .= '&optioncss='.urlencode($optioncss);
  273. }
  274. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  275. if ($optioncss != '') {
  276. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  277. }
  278. print '<input type="hidden" name="token" value="'.newToken().'">';
  279. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  280. print '<input type="hidden" name="action" value="list">';
  281. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  282. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  283. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  284. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit, 0, 0, 1);
  285. print '<div class="info">'.$langs->trans("WarningCreateSubAccounts").'</div>';
  286. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  287. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  288. $moreforfilter = '';
  289. $massactionbutton = '';
  290. print '<div class="div-table-responsive">';
  291. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  292. // Line for search fields
  293. print '<tr class="liste_titre_filter">';
  294. if (!empty($arrayfields['subaccount']['checked'])) {
  295. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_subaccount" value="'.$search_subaccount.'"></td>';
  296. }
  297. if (!empty($arrayfields['label']['checked'])) {
  298. print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="'.$search_label.'"></td>';
  299. }
  300. if (!empty($arrayfields['type']['checked'])) {
  301. print '<td class="liste_titre center">'.$form->selectarray('search_type', array('1'=>$langs->trans('Customer'), '2'=>$langs->trans('Supplier'), '3'=>$langs->trans('Employee')), $search_type, 1).'</td>';
  302. }
  303. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  304. if (!empty($arrayfields['reconcilable']['checked'])) {
  305. print '<td class="liste_titre">&nbsp;</td>';
  306. }
  307. }
  308. print '<td class="liste_titre maxwidthsearch">';
  309. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  310. print $searchpicto;
  311. print '</td>';
  312. print '</tr>';
  313. print '<tr class="liste_titre">';
  314. if (!empty($arrayfields['subaccount']['checked'])) {
  315. print_liste_field_titre($arrayfields['subaccount']['label'], $_SERVER["PHP_SELF"], "subaccount", "", $param, '', $sortfield, $sortorder);
  316. }
  317. if (!empty($arrayfields['label']['checked'])) {
  318. print_liste_field_titre($arrayfields['label']['label'], $_SERVER["PHP_SELF"], "label", "", $param, '', $sortfield, $sortorder);
  319. }
  320. if (!empty($arrayfields['type']['checked'])) {
  321. print_liste_field_titre($arrayfields['type']['label'], $_SERVER["PHP_SELF"], "type", "", $param, '', $sortfield, $sortorder, 'center ');
  322. }
  323. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  324. if (!empty($arrayfields['reconcilable']['checked'])) {
  325. print_liste_field_titre($arrayfields['reconcilable']['label'], $_SERVER["PHP_SELF"], 'reconcilable', '', $param, '', $sortfield, $sortorder, 'center ');
  326. }
  327. }
  328. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  329. print "</tr>\n";
  330. $totalarray = array();
  331. $totalarray['nbfield'] = 0;
  332. $i = 0;
  333. while ($i < min($num, $limit)) {
  334. $obj = $db->fetch_object($resql);
  335. print '<tr class="oddeven">';
  336. // Account number
  337. if (!empty($arrayfields['subaccount']['checked'])) {
  338. print "<td>";
  339. print length_accounta($obj->subaccount);
  340. print "</td>\n";
  341. if (!$i) {
  342. $totalarray['nbfield']++;
  343. }
  344. }
  345. // Subaccount label
  346. if (!empty($arrayfields['label']['checked'])) {
  347. print "<td>";
  348. print $obj->label;
  349. print "</td>\n";
  350. if (!$i) {
  351. $totalarray['nbfield']++;
  352. }
  353. }
  354. // Type
  355. if (!empty($arrayfields['type']['checked'])) {
  356. print '<td class="center">';
  357. $s = '';
  358. // Customer
  359. if ($obj->type == 1) {
  360. $s .= '<a class="customer-back" style="padding-left: 6px; padding-right: 6px" title="'.$langs->trans("Customer").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$obj->rowid.'">'.$langs->trans("Customer").'</a>';
  361. } elseif ($obj->type == 2) {
  362. // Supplier
  363. $s .= '<a class="vendor-back" style="padding-left: 6px; padding-right: 6px" title="'.$langs->trans("Supplier").'" href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$obj->rowid.'">'.$langs->trans("Supplier").'</a>';
  364. } elseif ($obj->type == 3) {
  365. // User
  366. $s .= '<a class="user-back" style="padding-left: 6px; padding-right: 6px" title="'.$langs->trans("Employee").'" href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->rowid.'">'.$langs->trans("Employee").'</a>';
  367. }
  368. print $s;
  369. print '</td>';
  370. if (!$i) {
  371. $totalarray['nbfield']++;
  372. }
  373. }
  374. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  375. // Activated or not reconciliation on accounting account
  376. if (!empty($arrayfields['reconcilable']['checked'])) {
  377. print '<td class="center">';
  378. if (empty($obj->reconcilable)) {
  379. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$obj->rowid.'&action=enable&mode=1&token='.newToken().'">';
  380. print img_picto($langs->trans("Disabled"), 'switch_off');
  381. print '</a>';
  382. } else {
  383. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$obj->rowid.'&action=disable&mode=1&token='.newToken().'">';
  384. print img_picto($langs->trans("Activated"), 'switch_on');
  385. print '</a>';
  386. }
  387. print '</td>';
  388. if (!$i) {
  389. $totalarray['nbfield']++;
  390. }
  391. }
  392. }
  393. // Action
  394. print '<td class="center">';
  395. $e = '';
  396. // Customer
  397. if ($obj->type == 1) {
  398. $e .= '<a class="editfielda" title="'.$langs->trans("Customer").'" href="'.DOL_URL_ROOT.'/societe/card.php?action=edit&token='.newToken().'&socid='.$obj->rowid.'&backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.img_edit().'</a>';
  399. } elseif ($obj->type == 2) {
  400. // Supplier
  401. $e .= '<a class="editfielda" title="'.$langs->trans("Supplier").'" href="'.DOL_URL_ROOT.'/societe/card.php?action=edit&token='.newToken().'&socid='.$obj->rowid.'&backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.img_edit().'</a>';
  402. } elseif ($obj->type == 3) {
  403. // User
  404. $e .= '<a class="editfielda" title="'.$langs->trans("Employee").'" href="'.DOL_URL_ROOT.'/user/card.php?action=edit&token='.newToken().'&id='.$obj->rowid.'&backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.img_edit().'</a>';
  405. }
  406. print $e;
  407. print '</td>'."\n";
  408. if (!$i) {
  409. $totalarray['nbfield']++;
  410. }
  411. print '</tr>'."\n";
  412. $i++;
  413. }
  414. $db->free($resql);
  415. $parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
  416. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters); // Note that $action and $object may have been modified by hook
  417. print $hookmanager->resPrint;
  418. print "</table>";
  419. print "</div>";
  420. print '</form>';
  421. } else {
  422. dol_print_error($db);
  423. }
  424. // End of page
  425. llxFooter();
  426. $db->close();