expensereport_rules.php 11 KB

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