dolibarr  16.0.5
expensereport_rules.php
Go to the documentation of this file.
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 
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array("admin", "other", "trips", "errors", "dict"));
36 
37 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
38 $hookmanager->initHooks(array('admin', 'dictionaryadmin','expensereport_rules'));
39 
40 $object = new ExpenseReportRule($db);
41 
42 if (!$user->admin) {
44 }
45 
46 
47 /*
48  * Action
49  */
50 
51 $parameters = array();
52 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
53 if ($reshook < 0) {
54  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
55 }
56 
57 if (empty($reshook)) {
58  //Init error
59  $error = false;
60 
61  $action = GETPOST('action', 'aZ09');
62  $id = GETPOST('id', 'int');
63 
64  $apply_to = GETPOST('apply_to');
65  $fk_user = GETPOST('fk_user', 'int');
66  $fk_usergroup = GETPOST('fk_usergroup', 'int');
67  $restrictive = GETPOST('restrictive', 'int');
68  $fk_c_type_fees = GETPOST('fk_c_type_fees', 'int');
69  $code_expense_rules_type = GETPOST('code_expense_rules_type');
70  $dates = dol_mktime(12, 0, 0, GETPOST('startmonth'), GETPOST('startday'), GETPOST('startyear'));
71  $datee = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
72  $amount = price2num(GETPOST('amount'), 'MT', 2);
73 
74  if (!empty($id)) {
75  $result = $object->fetch($id);
76  if ($result < 0) {
77  dol_print_error('', $object->error, $object->errors);
78  }
79  }
80 
81  if ($action == 'save') {
82  $error = 0;
83 
84  // check parameters
85  if (empty($apply_to)) {
86  $error++;
87  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportApplyTo")), null, 'errors');
88  }
89  if (empty($fk_c_type_fees)) {
90  $error++;
91  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDomain")), null, 'errors');
92  }
93  if (empty($code_expense_rules_type)) {
94  $error++;
95  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitOn")), null, 'errors');
96  }
97  if (empty($dates)) {
98  $error++;
99  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateStart")), null, 'errors');
100  }
101  if (empty($datee)) {
102  $error++;
103  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateEnd")), null, 'errors');
104  }
105  if (empty($amount)) {
106  $error++;
107  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitAmount")), null, 'errors');
108  }
109 
110  if (empty($error)) {
111  if ($apply_to == 'U') {
112  $object->fk_user = (int) $fk_user;
113  $object->fk_usergroup = 0;
114  $object->is_for_all = 0;
115  } elseif ($apply_to == 'G') {
116  $object->fk_usergroup = (int) $fk_usergroup;
117  $object->fk_user = 0;
118  $object->is_for_all = 0;
119  } elseif ($apply_to == 'A') {
120  $object->is_for_all = 1;
121  $object->fk_user = 0;
122  $object->fk_usergroup = 0;
123  }
124 
125  $object->dates = $dates;
126  $object->datee = $datee;
127  $object->restrictive = $restrictive;
128  $object->fk_c_type_fees = $fk_c_type_fees;
129  $object->code_expense_rules_type = $code_expense_rules_type;
130  $object->amount = $amount;
131  $object->entity = $conf->entity;
132 
133  if ($object->id > 0) {
134  $res = $object->update($user);
135  } else {
136  $res = $object->create($user);
137  }
138  if ($res > 0) {
139  setEventMessages($langs->trans('ExpenseReportRuleSave'), null);
140  } else {
141  dol_print_error($object->db);
142  $error++;
143  }
144 
145  if (!$error) {
146  header('Location: ' . $_SERVER['PHP_SELF']);
147  exit;
148  } else {
149  $action = '';
150  }
151  }
152  } elseif ($action == 'delete') {
153  // TODO add confirm
154  $res = $object->delete($user);
155 
156  if ($res < 0) {
157  dol_print_error($object->db);
158  }
159 
160  header('Location: ' . $_SERVER['PHP_SELF']);
161  exit;
162  }
163 
164  $rules = $object->getAllRule();
165 
166  $tab_apply = array(
167  'A' => $langs->trans('All'),
168  'G' => $langs->trans('Group'),
169  'U' => $langs->trans('User')
170  );
171  $tab_rules_type = array(
172  'EX_DAY' => $langs->trans('Day'),
173  'EX_MON' => $langs->trans('Month'),
174  'EX_YEA' => $langs->trans('Year'),
175  'EX_EXP' => $langs->trans('OnExpense')
176  );
177 }
178  /*
179  * View
180  */
181 
182  llxHeader('', $langs->trans("ExpenseReportsSetup"));
183 
184  $form = new Form($db);
185 
186  $linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1">' . $langs->trans("BackToModuleList") . '</a>';
187  print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
188 
190  print dol_get_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
191 
192  echo '<span class="opacitymedium">' . $langs->trans('ExpenseReportRulesDesc') . '</span>';
193  print '<br><br>';
194 
195 if ($action != 'edit') {
196  echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
197  echo '<input type="hidden" name="token" value="' . newToken() . '" />';
198  echo '<input type="hidden" name="action" value="save" />';
199 
200  echo '<table class="noborder centpercent">';
201 
202  echo '<tr class="liste_titre headerexpensereportrules">';
203  echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
204  echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
205  echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
206  echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
207  echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
208  echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
209  echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
210  echo '<th>&nbsp;</th>';
211  echo '</tr>';
212 
213  echo '<tr class="oddeven">';
214  echo '<td>';
215  echo '<div class="float linecolapplyto">' . $form->selectarray('apply_to', $tab_apply, '', 0) . '</div>';
216  echo '<div id="user" class="float linecoluser">' . $form->select_dolusers('', 'fk_user') . '</div>';
217  echo '<div id="group" class="float linecolgroup">' . $form->select_dolgroups('', 'fk_usergroup') . '</div>';
218  echo '</td>';
219 
220  echo '<td class="linecoltype">' . $form->selectExpense('', 'fk_c_type_fees', 0, 1, 1) . '</td>';
221  echo '<td class="linecoltyperule">' . $form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0) . '</td>';
222  echo '<td class="linecoldatestart">' . $form->selectDate(strtotime(date('Y-m-01', dol_now())), 'start', '', '', 0, '', 1, 0) . '</td>';
223  echo '<td class="linecoldateend>' . $form->selectDate(strtotime(date('Y-m-t', dol_now())), 'end', '', '', 0, '', 1, 0) . '</td>';
224  echo '<td class="linecolamount"><input type="text" value="" class="maxwidth100" name="amount" class="amount right" /></td>';
225  echo '<td class="linecolrestrictive">' . $form->selectyesno('restrictive', 0, 1) . '</td>';
226  echo '<td class="right linecolbutton"><input type="submit" class="button button-add" value="' . $langs->trans('Add') . '" /></td>';
227  echo '</tr>';
228 
229  echo '</table>';
230  echo '</form>';
231 }
232 
233 
234  echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
235  echo '<input type="hidden" name="token" value="' . newToken() . '" />';
236 
237 if ($action == 'edit') {
238  echo '<input type="hidden" name="id" value="' . $object->id . '" />';
239  echo '<input type="hidden" name="action" value="save" />';
240 }
241 
242  echo '<table class="noborder centpercent">';
243 
244  echo '<tr class="liste_titre expensereportrules">';
245  echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
246  echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
247  echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
248  echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
249  echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
250  echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
251  echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
252  echo '<th>&nbsp;</th>';
253  echo '</tr>';
254 
255 foreach ($rules as $rule) {
256  echo '<tr class="oddeven linetrdata" id="'.$rule->id.'">';
257 
258  echo '<td class="linecolusergroup">';
259  if ($action == 'edit' && $object->id == $rule->id) {
260  $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
261  echo '<div class="float">' . $form->selectarray('apply_to', $tab_apply, $selected, 0) . '</div>';
262  echo '<div id="user" class="float">' . $form->select_dolusers($object->fk_user, 'fk_user') . '</div>';
263  echo '<div id="group" class="float">' . $form->select_dolgroups($object->fk_usergroup, 'fk_usergroup') . '</div>';
264  } else {
265  if ($rule->is_for_all > 0) {
266  echo $tab_apply['A'];
267  } elseif ($rule->fk_usergroup > 0) {
268  echo $tab_apply['G'] . ' (' . $rule->getGroupLabel() . ')';
269  } elseif ($rule->fk_user > 0) {
270  echo $tab_apply['U'] . ' (' . $rule->getUserName() . ')';
271  }
272  }
273  echo '</td>';
274 
275 
276  echo '<td class="linecoltype">';
277  if ($action == 'edit' && $object->id == $rule->id) {
278  echo $form->selectExpense($object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
279  } else {
280  if ($rule->fk_c_type_fees == -1) {
281  echo $langs->trans('AllExpenseReport');
282  } else {
283  $key = getDictionaryValue('c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
284  if ($key && $key != $langs->trans($key)) {
285  echo $langs->trans($key);
286  } else {
287  $value = getDictionaryValue('c_type_fees', 'label', $rule->fk_c_type_fees, false, 'id');
288  echo $langs->trans($value ? $value : 'Undefined'); // TODO check to return trans of 'code'
289  }
290  }
291  }
292  echo '</td>';
293 
294 
295  echo '<td class="linecoltyperule">';
296  if ($action == 'edit' && $object->id == $rule->id) {
297  echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
298  } else {
299  echo $tab_rules_type[$rule->code_expense_rules_type];
300  }
301  echo '</td>';
302 
303 
304  echo '<td class="linecoldatestart">';
305  if ($action == 'edit' && $object->id == $rule->id) {
306  print $form->selectDate(strtotime(date('Y-m-d', $object->dates)), 'start', '', '', 0, '', 1, 0);
307  } else {
308  echo dol_print_date($rule->dates, 'day');
309  }
310  echo '</td>';
311 
312 
313  echo '<td class="linecoldateend">';
314  if ($action == 'edit' && $object->id == $rule->id) {
315  print $form->selectDate(strtotime(date('Y-m-d', $object->datee)), 'end', '', '', 0, '', 1, 0);
316  } else {
317  echo dol_print_date($rule->datee, 'day');
318  }
319  echo '</td>';
320 
321  // Amount
322  echo '<td class="linecolamount">';
323  if ($action == 'edit' && $object->id == $rule->id) {
324  echo '<input type="text" value="' . price2num($object->amount) . '" name="amount" class="amount width50 right" />';
325  } else {
326  echo price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency);
327  }
328  echo '</td>';
329 
330 
331  echo '<td class="linecolrestrictive">';
332  if ($action == 'edit' && $object->id == $rule->id) {
333  echo $form->selectyesno('restrictive', $object->restrictive, 1);
334  } else {
335  echo yn($rule->restrictive, 1, 1);
336  }
337  echo '</td>';
338 
339 
340  echo '<td class="center">';
341  if ($object->id != $rule->id) {
342  echo '<a class="editfielda paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=edit&token=' . newToken() . '&id=' . $rule->id . '">' . img_edit() . '</a>&nbsp;';
343  echo '<a class="paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=delete&token=' . newToken() . '&id=' . $rule->id . '">' . img_delete() . '</a>';
344  } else {
345  echo '<input type="submit" class="button button-edit" value="' . $langs->trans('Update') . '" />&nbsp;';
346  echo '<a href="' . $_SERVER['PHP_SELF'] . '" class="button button-cancel">' . $langs->trans("Cancel") . '</a>';
347  }
348  echo '</td>';
349 
350  echo '</tr>';
351 }
352 
353 
354  echo '</table>';
355  echo '</form>';
356 
357  echo '<script type="text/javascript"> $(function() {
358  $("#apply_to").change(function() {
359  var value = $(this).val();
360  if (value == "A") {
361  $("#group").hide(); $("#user").hide();
362  } else if (value == "U") {
363  $("#user").show();
364  $("#group").hide();
365  } else if (value == "G") {
366  $("#group").show();
367  $("#user").hide();
368  }
369  });
370 
371  $("#apply_to").change();
372 
373 }); </script>';
374 
375 
376 print dol_get_fiche_end();
377 
378 // End of page
379 llxFooter();
380 $db->close();
yn
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
Definition: functions.lib.php:6491
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
img_edit
img_edit($titlealt='default', $float=0, $other='')
Show logo editer/modifier fiche.
Definition: functions.lib.php:4389
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_delete
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
Definition: functions.lib.php:4429
getDictionaryValue
getDictionaryValue($tablename, $field, $id, $checkentity=false, $rowidfield='rowid')
Return the value of a filed into a dictionary for the record $id.
Definition: functions.lib.php:10138
expensereport_admin_prepare_head
expensereport_admin_prepare_head()
Return array head with list of tabs to view object informations.
Definition: expensereport.lib.php:129
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
ExpenseReportRule
Class to manage inventories.
Definition: expensereport_rule.class.php:30
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
dol_mktime
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Definition: functions.lib.php:2757
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59