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