dolibarr  13.0.2
expensereport_rules.php
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 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 <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 if (!$user->admin) accessforbidden();
38 
39 //Init error
40 $error = false;
41 $message = false;
42 
43 $action = GETPOST('action', 'aZ09');
44 $id = GETPOST('id', 'int');
45 
46 $apply_to = GETPOST('apply_to');
47 $fk_user = GETPOST('fk_user', 'int');
48 $fk_usergroup = GETPOST('fk_usergroup', 'int');
49 
50 $fk_c_type_fees = GETPOST('fk_c_type_fees');
51 $code_expense_rules_type = GETPOST('code_expense_rules_type');
52 $dates = dol_mktime(12, 0, 0, GETPOST('startmonth'), GETPOST('startday'), GETPOST('startyear'));
53 $datee = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
54 $amount = GETPOST('amount');
55 $restrictive = GETPOST('restrictive');
56 
57 $object = new ExpenseReportRule($db);
58 if (!empty($id))
59 {
60  $result = $object->fetch($id);
61  if ($result < 0) dol_print_error('', $object->error, $object->errors);
62 }
63 
64 // TODO do action
65 if ($action == 'save')
66 {
67  $error = 0;
68 
69  // check parameters
70  if (empty($apply_to)) {
71  $error++;
72  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportApplyTo")), null, 'errors');
73  }
74  if (empty($fk_c_type_fees)) {
75  $error++;
76  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDomain")), null, 'errors');
77  }
78  if (empty($code_expense_rules_type)) {
79  $error++;
80  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitOn")), null, 'errors');
81  }
82  if (empty($dates)) {
83  $error++;
84  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateStart")), null, 'errors');
85  }
86  if (empty($datee)) {
87  $error++;
88  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateEnd")), null, 'errors');
89  }
90  if (empty($amount)) {
91  $error++;
92  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitAmount")), null, 'errors');
93  }
94 
95  if (empty($error))
96  {
97  $object->setValues($_POST);
98 
99  if ($apply_to == 'U') {
100  $object->fk_user = (int) $fk_user;
101  $object->fk_usergroup = 0;
102  $object->is_for_all = 0;
103  } elseif ($apply_to == 'G') {
104  $object->fk_usergroup = (int) $fk_usergroup;
105  $object->fk_user = 0;
106  $object->is_for_all = 0;
107  } elseif ($apply_to == 'A') {
108  $object->is_for_all = 1;
109  $object->fk_user = 0;
110  $object->fk_usergroup = 0;
111  }
112 
113  $object->dates = $dates;
114  $object->datee = $datee;
115 
116  $object->entity = $conf->entity;
117 
118  $res = $object->create($user);
119  if ($res > 0) setEventMessages($langs->trans('ExpenseReportRuleSave'), null);
120  else dol_print_error($object->db);
121 
122  header('Location: '.$_SERVER['PHP_SELF']);
123  exit;
124  }
125 } elseif ($action == 'delete')
126 {
127  // TODO add confirm
128  $res = $object->delete($user);
129 
130  if ($res < 0) dol_print_error($object->db);
131 
132  header('Location: '.$_SERVER['PHP_SELF']);
133  exit;
134 }
135 
137 
138 $tab_apply = array('A' => $langs->trans('All'), 'G' => $langs->trans('Group'), 'U' => $langs->trans('User'));
139 $tab_rules_type = array('EX_DAY' => $langs->trans('Day'), 'EX_MON' => $langs->trans('Month'), 'EX_YEA' => $langs->trans('Year'), 'EX_EXP' => $langs->trans('OnExpense'));
140 
141 
142 /*
143  * View
144  */
145 
146 llxHeader('', $langs->trans("ExpenseReportsSetup"));
147 
148 $form = new Form($db);
149 
150 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
151 print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
152 
154 print dol_get_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
155 
156 echo '<span class="opacitymedium">'.$langs->trans('ExpenseReportRulesDesc').'</span>';
157 print '<br><br>';
158 
159 if ($action != 'edit')
160 {
161  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
162  echo '<input type="hidden" name="token" value="'.newToken().'" />';
163  echo '<input type="hidden" name="action" value="save" />';
164 
165  echo '<table class="noborder centpercent">';
166 
167  echo '<tr class="liste_titre">';
168  echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
169  echo '<th>'.$langs->trans('Type').'</th>';
170  echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
171  echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
172  echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
173  echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
174  echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
175  echo '<th>&nbsp;</th>';
176  echo '</tr>';
177 
178  echo '<tr class="oddeven">';
179  echo '<td>';
180  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, '', 0).'</div>';
181  echo '<div id="user" class="float">'.$form->select_dolusers('', 'fk_user').'</div>';
182  echo '<div id="group" class="float">'.$form->select_dolgroups('', 'fk_usergroup').'</div>';
183  echo '</td>';
184 
185  echo '<td>'.$form->selectExpense('', 'fk_c_type_fees', 0, 1, 1).'</td>';
186  echo '<td>'.$form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0).'</td>';
187  echo '<td>'.$form->selectDate(strtotime(date('Y-m-01', dol_now())), 'start', '', '', 0, '', 1, 0).'</td>';
188  echo '<td>'.$form->selectDate(strtotime(date('Y-m-t', dol_now())), 'end', '', '', 0, '', 1, 0).'</td>';
189  echo '<td><input type="text" value="" class="maxwidth100" name="amount" class="amount" /> '.$conf->currency.'</td>';
190  echo '<td>'.$form->selectyesno('restrictive', 0, 1).'</td>';
191  echo '<td class="right"><input type="submit" class="button" value="'.$langs->trans('Add').'" /></td>';
192  echo '</tr>';
193 
194  echo '</table>';
195  echo '</form>';
196 }
197 
198 
199 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
200 echo '<input type="hidden" name="token" value="'.newToken().'" />';
201 
202 if ($action == 'edit')
203 {
204  echo '<input type="hidden" name="id" value="'.$object->id.'" />';
205  echo '<input type="hidden" name="action" value="save" />';
206 }
207 
208 echo '<table class="noborder centpercent">';
209 
210 echo '<tr class="liste_titre">';
211 echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
212 echo '<th>'.$langs->trans('Type').'</th>';
213 echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
214 echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
215 echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
216 echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
217 echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
218 echo '<th>&nbsp;</th>';
219 echo '</tr>';
220 
221 foreach ($rules as $rule)
222 {
223  echo '<tr class="oddeven">';
224 
225  echo '<td>';
226  if ($action == 'edit' && $object->id == $rule->id)
227  {
228  $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
229  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, $selected, 0).'</div>';
230  echo '<div id="user" class="float">'.$form->select_dolusers($object->fk_user, 'fk_user').'</div>';
231  echo '<div id="group" class="float">'.$form->select_dolgroups($object->fk_usergroup, 'fk_usergroup').'</div>';
232  } else {
233  if ($rule->is_for_all > 0) echo $tab_apply['A'];
234  elseif ($rule->fk_usergroup > 0) echo $tab_apply['G'].' ('.$rule->getGroupLabel().')';
235  elseif ($rule->fk_user > 0) echo $tab_apply['U'].' ('.$rule->getUserName().')';
236  }
237  echo '</td>';
238 
239 
240  echo '<td>';
241  if ($action == 'edit' && $object->id == $rule->id)
242  {
243  echo $form->selectExpense($object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
244  } else {
245  if ($rule->fk_c_type_fees == -1) echo $langs->trans('AllExpenseReport');
246  else {
247  $key = getDictvalue(MAIN_DB_PREFIX.'c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
248  if ($key != $langs->trans($key)) echo $langs->trans($key);
249  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'
250  }
251  }
252  echo '</td>';
253 
254 
255  echo '<td>';
256  if ($action == 'edit' && $object->id == $rule->id)
257  {
258  echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
259  } else {
260  echo $tab_rules_type[$rule->code_expense_rules_type];
261  }
262  echo '</td>';
263 
264 
265  echo '<td>';
266  if ($action == 'edit' && $object->id == $rule->id)
267  {
268  print $form->selectDate(strtotime(date('Y-m-d', $object->dates)), 'start', '', '', 0, '', 1, 0);
269  } else {
270  echo dol_print_date($rule->dates, 'day');
271  }
272  echo '</td>';
273 
274 
275  echo '<td>';
276  if ($action == 'edit' && $object->id == $rule->id)
277  {
278  print $form->selectDate(strtotime(date('Y-m-d', $object->datee)), 'end', '', '', 0, '', 1, 0);
279  } else {
280  echo dol_print_date($rule->datee, 'day');
281  }
282  echo '</td>';
283 
284 
285  echo '<td>';
286  if ($action == 'edit' && $object->id == $rule->id)
287  {
288  echo '<input type="text" value="'.price2num($object->amount).'" name="amount" class="amount" />'.$conf->currency;
289  } else {
290  echo price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency);
291  }
292  echo '</td>';
293 
294 
295  echo '<td>';
296  if ($action == 'edit' && $object->id == $rule->id)
297  {
298  echo $form->selectyesno('restrictive', $object->restrictive, 1);
299  } else {
300  echo yn($rule->restrictive, 1, 1);
301  }
302  echo '</td>';
303 
304 
305  echo '<td class="center">';
306  if ($object->id != $rule->id)
307  {
308  echo '<a class="editfielda paddingright paddingleft" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$rule->id.'">'.img_edit().'</a>&nbsp;';
309  echo '<a class="paddingright paddingleft" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$rule->id.'">'.img_delete().'</a>';
310  } else {
311  echo '<input type="submit" class="button" value="'.$langs->trans('Update').'" />&nbsp;';
312  echo '<a href="'.$_SERVER['PHP_SELF'].'" class="button button-cancel">'.$langs->trans("Cancel").'</a>';
313  }
314  echo '</td>';
315 
316  echo '</tr>';
317 }
318 
319 
320 echo '</table>';
321 echo '</form>';
322 
323 echo '<script type="text/javascript"> $(function() {
324  $("#apply_to").change(function() {
325  var value = $(this).val();
326  if (value == "A") {
327  $("#group").hide(); $("#user").hide();
328  } else if (value == "U") {
329  $("#user").show();
330  $("#group").hide();
331  } else if (value == "G") {
332  $("#group").show();
333  $("#user").hide();
334  }
335  });
336 
337  $("#apply_to").change();
338 
339 }); </script>';
340 
342 
343 // End of page
344 llxFooter();
345 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDictvalue($tablename, $field, $id, $checkentity=false, $rowidfield= 'rowid')
Return value from dictionary.
img_edit($titlealt= 'default', $float=0, $other= '')
Show logo editer/modifier fiche.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
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...
dol_now($mode= 'auto')
Return date for now.
Class to manage inventories.
expensereport_admin_prepare_head()
Return array head with list of tabs to view object informations.
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
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 ...
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
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_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
static getAllRule($fk_c_type_fees= '', $date= '', $fk_user= '')
Return all rules or filtered by something.
llxFooter()
Empty footer.
Definition: wrapper.php:59
img_delete($titlealt= 'default', $other= 'class="pictodelete"', $morecss= '')
Show delete logo.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.