expensereport_rules.php 11 KB

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