categories.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /* Copyright (C) 2016 Jamal Elbaz <jamelbaz@gmail.pro>
  3. * Copyright (C) 2017-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  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. // Security check
  42. if (!$user->hasRight('accounting', 'chartofaccount')) {
  43. accessforbidden();
  44. }
  45. $accountingcategory = new AccountancyCategory($db);
  46. /*
  47. * Actions
  48. */
  49. // If we add account
  50. if (!empty($selectcpt)) {
  51. $cpts = array();
  52. foreach ($selectcpt as $selectedoption) {
  53. if (!array_key_exists($selectedoption, $cpts)) {
  54. $cpts[$selectedoption] = "'".$selectedoption."'";
  55. }
  56. }
  57. $return = $accountingcategory->updateAccAcc($cat_id, $cpts);
  58. if ($return < 0) {
  59. setEventMessages($langs->trans('errors'), $accountingcategory->errors, 'errors');
  60. } else {
  61. setEventMessages($langs->trans('RecordModifiedSuccessfully'), null, 'mesgs');
  62. }
  63. }
  64. if ($action == 'delete') {
  65. if ($cpt_id) {
  66. if ($accountingcategory->deleteCptCat($cpt_id)) {
  67. setEventMessages($langs->trans('AccountRemovedFromGroup'), null, 'mesgs');
  68. } else {
  69. setEventMessages($langs->trans('errors'), null, 'errors');
  70. }
  71. }
  72. }
  73. /*
  74. * View
  75. */
  76. $form = new Form($db);
  77. $formaccounting = new FormAccounting($db);
  78. llxheader('', $langs->trans('AccountingCategory'));
  79. $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/categories_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  80. $titlepicto = 'setup';
  81. print load_fiche_titre($langs->trans('AccountingCategory'), $linkback, $titlepicto);
  82. print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  83. print '<input type="hidden" name="token" value="'.newToken().'">';
  84. print '<input type="hidden" name="action" value="display">';
  85. print dol_get_fiche_head();
  86. print '<table class="border centpercent">';
  87. // Select the category
  88. print '<tr><td class="titlefield">'.$langs->trans("AccountingCategory").'</td>';
  89. print '<td>';
  90. $formaccounting->select_accounting_category($cat_id, 'account_category', 1, 0, 0, 0);
  91. print '<input type="submit" class="button small" value="'.$langs->trans("Select").'">';
  92. print '</td></tr>';
  93. print '</table>';
  94. print dol_get_fiche_end();
  95. // Select the accounts
  96. if (!empty($cat_id)) {
  97. $return = $accountingcategory->getAccountsWithNoCategory($cat_id);
  98. if ($return < 0) {
  99. setEventMessages(null, $accountingcategory->errors, 'errors');
  100. }
  101. print '<br>';
  102. $arraykeyvalue = array();
  103. foreach ($accountingcategory->lines_cptbk as $key => $val) {
  104. $doc_ref = !empty($val->doc_ref) ? $val->doc_ref : '';
  105. $arraykeyvalue[length_accountg($val->numero_compte)] = length_accountg($val->numero_compte) . ' - ' . $val->label_compte . ($doc_ref ? ' '.$doc_ref : '');
  106. }
  107. if (is_array($accountingcategory->lines_cptbk) && count($accountingcategory->lines_cptbk) > 0) {
  108. print img_picto($langs->trans("AccountingAccount"), 'accounting_account', 'class="pictofixedwith"');
  109. print $form->multiselectarray('cpt_bk', $arraykeyvalue, GETPOST('cpt_bk', 'array'), null, null, '', 0, "80%", '', '', $langs->transnoentitiesnoconv("AddAccountFromBookKeepingWithNoCategories"));
  110. //print '<br>';
  111. /*print '<select class="flat minwidth200" size="8" name="cpt_bk[]" multiple>';
  112. foreach ( $accountingcategory->lines_cptbk as $cpt ) {
  113. print '<option value="' . length_accountg($cpt->numero_compte) . '">' . length_accountg($cpt->numero_compte) . ' (' . $cpt->label_compte . ' ' . $cpt->doc_ref . ')</option>';
  114. }
  115. print '</select><br>';
  116. print ajax_combobox('cpt_bk');
  117. */
  118. print '<input type="submit" class="button button-add small" id="" class="action-delete" value="'.$langs->trans("Add").'"> ';
  119. }
  120. }
  121. print '</form>';
  122. if ($action == 'display' || $action == 'delete') {
  123. print '<br>';
  124. print '<table class="noborder centpercent">'."\n";
  125. print '<tr class="liste_titre">';
  126. print '<td class="liste_titre">'.$langs->trans("AccountAccounting")."</td>";
  127. print '<td class="liste_titre" colspan="2">'.$langs->trans("Label")."</td>";
  128. print "</tr>\n";
  129. if (!empty($cat_id)) {
  130. $return = $accountingcategory->display($cat_id); // This load ->lines_display
  131. if ($return < 0) {
  132. setEventMessages(null, $accountingcategory->errors, 'errors');
  133. }
  134. if (is_array($accountingcategory->lines_display) && count($accountingcategory->lines_display) > 0) {
  135. foreach ($accountingcategory->lines_display as $cpt) {
  136. print '<tr class="oddeven">';
  137. print '<td>'.length_accountg($cpt->account_number).'</td>';
  138. print '<td>'.$cpt->label.'</td>';
  139. print '<td class="right">';
  140. print '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&account_category='.$cat_id.'&cptid='.$cpt->rowid.'">';
  141. print $langs->trans("DeleteFromCat");
  142. print img_picto($langs->trans("DeleteFromCat"), 'unlink', 'class="paddingleft"');
  143. print "</a>";
  144. print "</td>";
  145. print "</tr>\n";
  146. }
  147. } else {
  148. print '<tr><td colspan="3"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  149. }
  150. }
  151. print "</table>";
  152. }
  153. // End of page
  154. llxFooter();
  155. $db->close();