categories.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* Copyright (C) 2016 Jamal Elbaz <jamelbaz@gmail.pro>
  3. * Copyright (C) 2017-2024 Alexandre Spangaro <aspangaro@easya.solutions>
  4. * Copyright (C) 2022 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/categories.php
  21. * \ingroup Accountancy (Double entries)
  22. * \brief Page to assign mass categories to accounts
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  29. $error = 0;
  30. // Load translation files required by the page
  31. $langs->loadLangs(array("bills", "accountancy", "compta"));
  32. $id = GETPOST('id', 'int');
  33. $cancel = GETPOST('cancel', 'alpha');
  34. $action = GETPOST('action', 'aZ09');
  35. $cat_id = GETPOST('account_category', 'int');
  36. $selectcpt = GETPOST('cpt_bk', 'array');
  37. $cpt_id = GETPOST('cptid', 'int');
  38. if ($cat_id == 0) {
  39. $cat_id = null;
  40. }
  41. // Load variable for pagination
  42. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  43. $sortfield = GETPOST('sortfield', 'aZ09comma');
  44. $sortorder = GETPOST('sortorder', 'aZ09comma');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. if (empty($page) || $page < 0 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  47. // If $page is not defined, or '' or -1 or if we click on clear filters
  48. $page = 0;
  49. }
  50. $offset = $limit * $page;
  51. $pageprev = $page - 1;
  52. $pagenext = $page + 1;
  53. if (empty($sortfield)) {
  54. $sortfield = 'account_number';
  55. }
  56. if (empty($sortorder)) {
  57. $sortorder = 'ASC';
  58. }
  59. // Security check
  60. if (!$user->hasRight('accounting', 'chartofaccount')) {
  61. accessforbidden();
  62. }
  63. $accountingcategory = new AccountancyCategory($db);
  64. /*
  65. * Actions
  66. */
  67. // If we add account
  68. if (!empty($selectcpt)) {
  69. $cpts = array();
  70. foreach ($selectcpt as $selectedoption) {
  71. if (!array_key_exists($selectedoption, $cpts)) {
  72. $cpts[$selectedoption] = "'".$selectedoption."'";
  73. }
  74. }
  75. $return = $accountingcategory->updateAccAcc($cat_id, $cpts);
  76. if ($return < 0) {
  77. setEventMessages($langs->trans('errors'), $accountingcategory->errors, 'errors');
  78. } else {
  79. setEventMessages($langs->trans('RecordModifiedSuccessfully'), null, 'mesgs');
  80. }
  81. }
  82. if ($action == 'delete') {
  83. if ($cpt_id) {
  84. if ($accountingcategory->deleteCptCat($cpt_id)) {
  85. setEventMessages($langs->trans('AccountRemovedFromGroup'), null, 'mesgs');
  86. } else {
  87. setEventMessages($langs->trans('errors'), null, 'errors');
  88. }
  89. }
  90. }
  91. /*
  92. * View
  93. */
  94. $form = new Form($db);
  95. $formaccounting = new FormAccounting($db);
  96. $help_url = 'EN:Module_Double_Entry_Accounting#Setup|FR:Module_Comptabilit&eacute;_en_Partie_Double#Configuration';
  97. llxHeader('', $langs->trans('AccountingCategory'), $help_url);
  98. $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/categories_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  99. $titlepicto = 'setup';
  100. print load_fiche_titre($langs->trans('AccountingCategory'), $linkback, $titlepicto);
  101. print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  102. print '<input type="hidden" name="token" value="'.newToken().'">';
  103. print '<input type="hidden" name="action" value="display">';
  104. print dol_get_fiche_head();
  105. print '<table class="border centpercent">';
  106. // Select the category
  107. print '<tr><td class="titlefield">'.$langs->trans("AccountingCategory").'</td>';
  108. print '<td>';
  109. $s = $formaccounting->select_accounting_category($cat_id, 'account_category', 1, 0, 0, 0);
  110. if ($formaccounting->nbaccounts_category <= 0) {
  111. print '<span class="opacitymedium">'.$s.'</span>';
  112. } else {
  113. print $s;
  114. print '<input type="submit" class="button small" value="'.$langs->trans("Select").'">';
  115. }
  116. print '</td></tr>';
  117. print '</table>';
  118. print dol_get_fiche_end();
  119. // Select the accounts
  120. if (!empty($cat_id)) {
  121. $return = $accountingcategory->getAccountsWithNoCategory($cat_id);
  122. if ($return < 0) {
  123. setEventMessages(null, $accountingcategory->errors, 'errors');
  124. }
  125. print '<br>';
  126. $arraykeyvalue = array();
  127. foreach ($accountingcategory->lines_cptbk as $key => $val) {
  128. $doc_ref = !empty($val->doc_ref) ? $val->doc_ref : '';
  129. $arraykeyvalue[length_accountg($val->numero_compte)] = length_accountg($val->numero_compte) . ' - ' . $val->label_compte . ($doc_ref ? ' '.$doc_ref : '');
  130. }
  131. if (is_array($accountingcategory->lines_cptbk) && count($accountingcategory->lines_cptbk) > 0) {
  132. print img_picto($langs->trans("AccountingAccount"), 'accounting_account', 'class="pictofixedwith"');
  133. print $form->multiselectarray('cpt_bk', $arraykeyvalue, GETPOST('cpt_bk', 'array'), null, null, '', 0, "80%", '', '', $langs->transnoentitiesnoconv("AddAccountFromBookKeepingWithNoCategories"));
  134. print '<input type="submit" class="button button-add small" id="" class="action-delete" value="'.$langs->trans("Add").'"> ';
  135. }
  136. }
  137. print '</form>';
  138. if ((empty($action) || $action == 'display' || $action == 'delete') && $cat_id > 0) {
  139. $param = 'account_category='.((int) $cat_id);
  140. print '<br>';
  141. print '<table class="noborder centpercent">'."\n";
  142. print '<tr class="liste_titre">';
  143. print getTitleFieldOfList('AccountAccounting', 0, $_SERVER['PHP_SELF'], 'account_number', '', $param, '', $sortfield, $sortorder, '')."\n";
  144. print getTitleFieldOfList('Label', 0, $_SERVER['PHP_SELF'], 'label', '', $param, '', $sortfield, $sortorder, '')."\n";
  145. print getTitleFieldOfList('', 0, $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, '')."\n";
  146. print '</tr>'."\n";
  147. if (!empty($cat_id)) {
  148. $return = $accountingcategory->display($cat_id); // This load ->lines_display
  149. if ($return < 0) {
  150. setEventMessages(null, $accountingcategory->errors, 'errors');
  151. }
  152. if (is_array($accountingcategory->lines_display) && count($accountingcategory->lines_display) > 0) {
  153. $accountingcategory->lines_display = dol_sort_array($accountingcategory->lines_display, $sortfield, $sortorder, -1, 0, 1);
  154. foreach ($accountingcategory->lines_display as $cpt) {
  155. print '<tr class="oddeven">';
  156. print '<td>'.length_accountg($cpt->account_number).'</td>';
  157. print '<td>'.$cpt->label.'</td>';
  158. print '<td class="right">';
  159. print '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&account_category='.$cat_id.'&cptid='.$cpt->rowid.'">';
  160. print $langs->trans("DeleteFromCat");
  161. print img_picto($langs->trans("DeleteFromCat"), 'unlink', 'class="paddingleft"');
  162. print "</a>";
  163. print "</td>";
  164. print "</tr>\n";
  165. }
  166. } else {
  167. print '<tr><td colspan="3"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  168. }
  169. }
  170. print "</table>";
  171. }
  172. // End of page
  173. llxFooter();
  174. $db->close();