expensereport_rules.php 13 KB

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