expensereport_ik.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* Copyright (C) 2012 Mikael Carlavan <contact@mika-carl.fr>
  3. * Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
  4. * Copyright (C) 2017 Pierre-Henry Favre <phf@atm-consulting.fr>
  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/admin/expensereport_ik.php
  21. * \ingroup expensereport
  22. * \brief Page to display expense tax ik. Used when MAIN_USE_EXPENSE_IK is set.
  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/expensereport.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array("admin", "trips", "errors", "other", "dict"));
  32. $error = 0;
  33. $action = GETPOST('action', 'aZ09');
  34. $id = GETPOSTINT('id');
  35. $ikoffset = GETPOSTINT('ikoffset');
  36. $coef = (float) price2num(GETPOST('coef'));
  37. $fk_c_exp_tax_cat = GETPOSTINT('fk_c_exp_tax_cat');
  38. $fk_range = GETPOSTINT('fk_range');
  39. $expIk = new ExpenseReportIk($db);
  40. if (!$user->admin) {
  41. accessforbidden();
  42. }
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'updateik') {
  47. if ($id > 0) {
  48. $result = $expIk->fetch($id);
  49. if ($result < 0) {
  50. dol_print_error('', $expIk->error, $expIk->errors);
  51. }
  52. }
  53. $expIk->coef = $coef;
  54. $expIk->ikoffset = $ikoffset;
  55. $expIk->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
  56. $expIk->fk_range = $fk_range;
  57. if ($expIk->id > 0) {
  58. $result = $expIk->update($user);
  59. } else {
  60. $result = $expIk->create($user);
  61. }
  62. if ($result > 0) {
  63. setEventMessages('SetupSaved', null, 'mesgs');
  64. header('Location: '.$_SERVER['PHP_SELF']);
  65. exit;
  66. } else {
  67. setEventMessages($expIk->error, $expIk->errors, 'errors');
  68. }
  69. } elseif ($action == 'delete') { // TODO add confirm
  70. if ($id > 0) {
  71. $result = $expIk->fetch($id);
  72. if ($result < 0) {
  73. dol_print_error('', $expIk->error, $expIk->errors);
  74. }
  75. $expIk->delete($user);
  76. }
  77. header('Location: '.$_SERVER['PHP_SELF']);
  78. exit;
  79. }
  80. $rangesbycateg = $expIk->getAllRanges();
  81. /*
  82. * View
  83. */
  84. llxHeader('', $langs->trans("ExpenseReportsSetup"));
  85. $form = new Form($db);
  86. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  87. print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
  88. $head = expensereport_admin_prepare_head();
  89. print dol_get_fiche_head($head, 'expenseik', $langs->trans("ExpenseReportsIk"), -1, 'trip');
  90. echo '<span class="opacitymedium">'.$langs->trans('ExpenseReportIkDesc').'</span>';
  91. print '<br><br>';
  92. echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
  93. echo '<input type="hidden" name="token" value="'.newToken().'" />';
  94. if ($action == 'edit') {
  95. echo '<input type="hidden" name="id" value="'.$id.'" />';
  96. echo '<input type="hidden" name="fk_c_exp_tax_cat" value="'.$fk_c_exp_tax_cat.'" />';
  97. echo '<input type="hidden" name="fk_range" value="'.$fk_range.'" />';
  98. echo '<input type="hidden" name="action" value="updateik" />';
  99. }
  100. echo '<table class="noborder centpercent">';
  101. foreach ($rangesbycateg as $fk_c_exp_tax_cat => $Tab) {
  102. $title = ($Tab['active'] == 1) ? $langs->trans($Tab['label']) : $form->textwithpicto($langs->trans($Tab['label']), $langs->trans('expenseReportCatDisabled'), 1, 'help', '', 0, 3);
  103. echo '<tr class="liste_titre">';
  104. echo '<td>'.$title.'</td>';
  105. echo '<td>'.$langs->trans('expenseReportOffset').'</td>';
  106. echo '<td>'.$langs->trans('expenseReportCoef').'</td>';
  107. echo '<td>'.$langs->trans('expenseReportTotalForFive').'</td>';
  108. echo '<td>&nbsp;</td>';
  109. echo '</tr>';
  110. if ($Tab['active'] == 0) {
  111. continue;
  112. }
  113. $tranche = 1;
  114. foreach ($Tab['ranges'] as $k => $range) {
  115. if (isset($Tab['ranges'][$k + 1])) {
  116. $label = $langs->trans('expenseReportRangeFromTo', $range->range_ik, ($Tab['ranges'][$k + 1]->range_ik - 1));
  117. } else {
  118. $label = $langs->trans('expenseReportRangeMoreThan', $range->range_ik);
  119. }
  120. if ($range->range_active == 0) {
  121. $label = $form->textwithpicto($label, $langs->trans('expenseReportRangeDisabled'), 1, 'help', '', 0, 3);
  122. }
  123. echo '<tr class="oddeven">';
  124. // Label
  125. echo '<td class="nowraponall"><b>['.$langs->trans('RangeNum', $tranche++).']</b> - '.$label.'</td>';
  126. // Offset
  127. echo '<td class="nowraponall">';
  128. if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat) {
  129. echo '<input type="text" class="maxwidth100" name="ikoffset" value="'.$range->ik->ikoffset.'" />';
  130. } else {
  131. echo $range->ik->ikoffset;
  132. }
  133. echo '</td>';
  134. // Coef
  135. echo '<td class="nowraponall">';
  136. if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat) {
  137. echo '<input type="text" class="maxwidth100" name="coef" value="'.$range->ik->coef.'" />';
  138. } else {
  139. echo($range->ik->id > 0 ? $range->ik->coef : $langs->trans('expenseReportCoefUndefined'));
  140. }
  141. echo '</td>';
  142. // Total for one
  143. echo '<td class="nowraponall">'.$langs->trans('expenseReportPrintExample', price($range->ik->ikoffset + 5 * $range->ik->coef)).'</td>';
  144. // Action
  145. echo '<td class="right">';
  146. if ($range->range_active == 1) {
  147. if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat) {
  148. echo '<input id="" class="button button-save" name="save" value="'.$langs->trans("Save").'" type="submit" />';
  149. echo '<input class="button button-cancel" value="'.$langs->trans("Cancel").'" onclick="history.go(-1)" type="button" />';
  150. } else {
  151. echo '<a class="editfielda marginrightonly paddingleft paddingright" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$range->ik->id.'&fk_c_exp_tax_cat='.$range->fk_c_exp_tax_cat.'&fk_range='.$range->rowid.'">'.img_edit().'</a>';
  152. if (!empty($range->ik->id)) {
  153. echo '<a class="paddingleft paddingright" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$range->ik->id.'">'.img_delete().'</a>';
  154. }
  155. // TODO add delete link
  156. }
  157. }
  158. echo '</td>';
  159. echo '</tr>';
  160. }
  161. }
  162. echo '</table>';
  163. echo '</form>';
  164. print dol_get_fiche_end();
  165. // End of page
  166. llxFooter();
  167. $db->close();