expensereport_rules.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/expensereport_rules.php
  22. * \ingroup expensereport
  23. * \brief Page to display expense tax ik
  24. */
  25. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array("admin", "other", "trips", "errors", "dict"));
  33. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  34. $hookmanager->initHooks(array('admin', 'dictionaryadmin','expensereport_rules'));
  35. $object = new ExpenseReportRule($db);
  36. if (!$user->admin) {
  37. accessforbidden();
  38. }
  39. /*
  40. * Action
  41. */
  42. $parameters = array();
  43. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  44. if ($reshook < 0) {
  45. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  46. }
  47. if (empty($reshook)) {
  48. //Init error
  49. $error = false;
  50. $action = GETPOST('action', 'aZ09');
  51. $id = GETPOST('id', 'int');
  52. $apply_to = GETPOST('apply_to');
  53. $fk_user = GETPOST('fk_user', 'int');
  54. $fk_usergroup = GETPOST('fk_usergroup', 'int');
  55. $restrictive = GETPOST('restrictive', 'int');
  56. $fk_c_type_fees = GETPOST('fk_c_type_fees', 'int');
  57. $code_expense_rules_type = GETPOST('code_expense_rules_type');
  58. $dates = dol_mktime(12, 0, 0, GETPOST('startmonth'), GETPOST('startday'), GETPOST('startyear'));
  59. $datee = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
  60. $amount = price2num(GETPOST('amount'), 'MT', 2);
  61. if (!empty($id)) {
  62. $result = $object->fetch($id);
  63. if ($result < 0) {
  64. dol_print_error('', $object->error, $object->errors);
  65. }
  66. }
  67. if ($action == 'save') {
  68. $error = 0;
  69. // check parameters
  70. if (empty($apply_to)) {
  71. $error++;
  72. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportApplyTo")), null, 'errors');
  73. }
  74. if (empty($fk_c_type_fees)) {
  75. $error++;
  76. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDomain")), null, 'errors');
  77. }
  78. if (empty($code_expense_rules_type)) {
  79. $error++;
  80. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitOn")), null, 'errors');
  81. }
  82. if (empty($dates)) {
  83. $error++;
  84. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateStart")), null, 'errors');
  85. }
  86. if (empty($datee)) {
  87. $error++;
  88. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateEnd")), null, 'errors');
  89. }
  90. if (empty($amount)) {
  91. $error++;
  92. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitAmount")), null, 'errors');
  93. }
  94. if (empty($error)) {
  95. if ($apply_to == 'U') {
  96. $object->fk_user = (int) $fk_user;
  97. $object->fk_usergroup = 0;
  98. $object->is_for_all = 0;
  99. } elseif ($apply_to == 'G') {
  100. $object->fk_usergroup = (int) $fk_usergroup;
  101. $object->fk_user = 0;
  102. $object->is_for_all = 0;
  103. } elseif ($apply_to == 'A') {
  104. $object->is_for_all = 1;
  105. $object->fk_user = 0;
  106. $object->fk_usergroup = 0;
  107. }
  108. $object->dates = $dates;
  109. $object->datee = $datee;
  110. $object->restrictive = $restrictive;
  111. $object->fk_c_type_fees = $fk_c_type_fees;
  112. $object->code_expense_rules_type = $code_expense_rules_type;
  113. $object->amount = $amount;
  114. $object->entity = $conf->entity;
  115. if ($object->id > 0) {
  116. $res = $object->update($user);
  117. } else {
  118. $res = $object->create($user);
  119. }
  120. if ($res > 0) {
  121. setEventMessages($langs->trans('ExpenseReportRuleSave'), null);
  122. } else {
  123. dol_print_error($object->db);
  124. $error++;
  125. }
  126. if (!$error) {
  127. header('Location: ' . $_SERVER['PHP_SELF']);
  128. exit;
  129. } else {
  130. $action = '';
  131. }
  132. }
  133. } elseif ($action == 'delete') {
  134. // TODO add confirm
  135. $res = $object->delete($user);
  136. if ($res < 0) {
  137. dol_print_error($object->db);
  138. }
  139. header('Location: ' . $_SERVER['PHP_SELF']);
  140. exit;
  141. }
  142. $rules = $object->getAllRule();
  143. $tab_apply = array(
  144. 'A' => $langs->trans('All'),
  145. 'G' => $langs->trans('Group'),
  146. 'U' => $langs->trans('User')
  147. );
  148. $tab_rules_type = array(
  149. 'EX_DAY' => $langs->trans('Day'),
  150. 'EX_MON' => $langs->trans('Month'),
  151. 'EX_YEA' => $langs->trans('Year'),
  152. 'EX_EXP' => $langs->trans('OnExpense')
  153. );
  154. }
  155. /*
  156. * View
  157. */
  158. llxHeader('', $langs->trans("ExpenseReportsSetup"));
  159. $form = new Form($db);
  160. $linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1">' . $langs->trans("BackToModuleList") . '</a>';
  161. print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
  162. $head = expensereport_admin_prepare_head();
  163. print dol_get_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
  164. echo '<span class="opacitymedium">' . $langs->trans('ExpenseReportRulesDesc') . '</span>';
  165. print '<br><br>';
  166. if ($action != 'edit') {
  167. echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
  168. echo '<input type="hidden" name="token" value="' . newToken() . '" />';
  169. echo '<input type="hidden" name="action" value="save" />';
  170. echo '<table class="noborder centpercent">';
  171. echo '<tr class="liste_titre headerexpensereportrules">';
  172. echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
  173. echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
  174. echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
  175. echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
  176. echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
  177. echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
  178. echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
  179. echo '<th>&nbsp;</th>';
  180. echo '</tr>';
  181. echo '<tr class="oddeven">';
  182. echo '<td>';
  183. echo '<div class="float linecolapplyto">' . $form->selectarray('apply_to', $tab_apply, '', 0) . '</div>';
  184. echo '<div id="user" class="float linecoluser">' . $form->select_dolusers('', 'fk_user') . '</div>';
  185. echo '<div id="group" class="float linecolgroup">' . $form->select_dolgroups('', 'fk_usergroup') . '</div>';
  186. echo '</td>';
  187. echo '<td class="linecoltype">' . $form->selectExpense('', 'fk_c_type_fees', 0, 1, 1) . '</td>';
  188. echo '<td class="linecoltyperule">' . $form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0) . '</td>';
  189. echo '<td class="linecoldatestart">' . $form->selectDate(strtotime(date('Y-m-01', dol_now())), 'start', '', '', 0, '', 1, 0) . '</td>';
  190. echo '<td class="linecoldateend">' . $form->selectDate(strtotime(date('Y-m-t', dol_now())), 'end', '', '', 0, '', 1, 0) . '</td>';
  191. echo '<td class="linecolamount"><input type="text" value="" class="maxwidth100" name="amount" class="amount right" /></td>';
  192. echo '<td class="linecolrestrictive">' . $form->selectyesno('restrictive', 0, 1) . '</td>';
  193. echo '<td class="right linecolbutton"><input type="submit" class="button button-add" value="' . $langs->trans('Add') . '" /></td>';
  194. echo '</tr>';
  195. echo '</table>';
  196. echo '</form>';
  197. }
  198. echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
  199. echo '<input type="hidden" name="token" value="' . newToken() . '" />';
  200. if ($action == 'edit') {
  201. echo '<input type="hidden" name="id" value="' . $object->id . '" />';
  202. echo '<input type="hidden" name="action" value="save" />';
  203. }
  204. echo '<table class="noborder centpercent">';
  205. echo '<tr class="liste_titre expensereportrules">';
  206. echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
  207. echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
  208. echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
  209. echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
  210. echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
  211. echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
  212. echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
  213. echo '<th>&nbsp;</th>';
  214. echo '</tr>';
  215. foreach ($rules as $rule) {
  216. echo '<tr class="oddeven linetrdata" id="'.$rule->id.'">';
  217. echo '<td class="linecolusergroup">';
  218. if ($action == 'edit' && $object->id == $rule->id) {
  219. $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
  220. echo '<div class="float">' . $form->selectarray('apply_to', $tab_apply, $selected, 0) . '</div>';
  221. echo '<div id="user" class="float">' . $form->select_dolusers($object->fk_user, 'fk_user') . '</div>';
  222. echo '<div id="group" class="float">' . $form->select_dolgroups($object->fk_usergroup, 'fk_usergroup') . '</div>';
  223. } else {
  224. if ($rule->is_for_all > 0) {
  225. echo $tab_apply['A'];
  226. } elseif ($rule->fk_usergroup > 0) {
  227. echo $tab_apply['G'] . ' (' . $rule->getGroupLabel() . ')';
  228. } elseif ($rule->fk_user > 0) {
  229. echo $tab_apply['U'] . ' (' . $rule->getUserName() . ')';
  230. }
  231. }
  232. echo '</td>';
  233. echo '<td class="linecoltype">';
  234. if ($action == 'edit' && $object->id == $rule->id) {
  235. echo $form->selectExpense($object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
  236. } else {
  237. if ($rule->fk_c_type_fees == -1) {
  238. echo $langs->trans('AllExpenseReport');
  239. } else {
  240. $key = getDictionaryValue('c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
  241. if ($key && $key != $langs->trans($key)) {
  242. echo $langs->trans($key);
  243. } else {
  244. $value = getDictionaryValue('c_type_fees', 'label', $rule->fk_c_type_fees, false, 'id');
  245. echo $langs->trans($value ? $value : 'Undefined'); // TODO check to return trans of 'code'
  246. }
  247. }
  248. }
  249. echo '</td>';
  250. echo '<td class="linecoltyperule">';
  251. if ($action == 'edit' && $object->id == $rule->id) {
  252. echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
  253. } else {
  254. echo $tab_rules_type[$rule->code_expense_rules_type];
  255. }
  256. echo '</td>';
  257. echo '<td class="linecoldatestart">';
  258. if ($action == 'edit' && $object->id == $rule->id) {
  259. print $form->selectDate(strtotime(date('Y-m-d', $object->dates)), 'start', '', '', 0, '', 1, 0);
  260. } else {
  261. echo dol_print_date($rule->dates, 'day');
  262. }
  263. echo '</td>';
  264. echo '<td class="linecoldateend">';
  265. if ($action == 'edit' && $object->id == $rule->id) {
  266. print $form->selectDate(strtotime(date('Y-m-d', $object->datee)), 'end', '', '', 0, '', 1, 0);
  267. } else {
  268. echo dol_print_date($rule->datee, 'day');
  269. }
  270. echo '</td>';
  271. // Amount
  272. echo '<td class="linecolamount">';
  273. if ($action == 'edit' && $object->id == $rule->id) {
  274. echo '<input type="text" value="' . price2num($object->amount) . '" name="amount" class="amount width50 right" />';
  275. } else {
  276. echo price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency);
  277. }
  278. echo '</td>';
  279. echo '<td class="linecolrestrictive">';
  280. if ($action == 'edit' && $object->id == $rule->id) {
  281. echo $form->selectyesno('restrictive', $object->restrictive, 1);
  282. } else {
  283. echo yn($rule->restrictive, 1, 1);
  284. }
  285. echo '</td>';
  286. echo '<td class="center">';
  287. if ($object->id != $rule->id) {
  288. echo '<a class="editfielda paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=edit&token=' . newToken() . '&id=' . $rule->id . '">' . img_edit() . '</a>&nbsp;';
  289. echo '<a class="paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=delete&token=' . newToken() . '&id=' . $rule->id . '">' . img_delete() . '</a>';
  290. } else {
  291. echo '<input type="submit" class="button button-edit" value="' . $langs->trans('Update') . '" />&nbsp;';
  292. echo '<a href="' . $_SERVER['PHP_SELF'] . '" class="button button-cancel">' . $langs->trans("Cancel") . '</a>';
  293. }
  294. echo '</td>';
  295. echo '</tr>';
  296. }
  297. echo '</table>';
  298. echo '</form>';
  299. echo '<script type="text/javascript"> $(function() {
  300. $("#apply_to").change(function() {
  301. var value = $(this).val();
  302. if (value == "A") {
  303. $("#group").hide(); $("#user").hide();
  304. } else if (value == "U") {
  305. $("#user").show();
  306. $("#group").hide();
  307. } else if (value == "G") {
  308. $("#group").show();
  309. $("#user").hide();
  310. }
  311. });
  312. $("#apply_to").change();
  313. }); </script>';
  314. print dol_get_fiche_end();
  315. // End of page
  316. llxFooter();
  317. $db->close();