dolibarr  9.0.0
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 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 
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','alpha');
44 $id = GETPOST('id','int');
45 
46 $apply_to = GETPOST('apply_to');
47 $fk_user = GETPOST('fk_user');
48 $fk_usergroup = GETPOST('fk_usergroup');
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=$fk_user;
101  $object->fk_usergroup=0;
102  $object->is_for_all=0;
103  }elseif($apply_to=='G'){
104  $object->fk_usergroup=$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 }
126 elseif ($action == 'delete')
127 {
128  // TODO add confirm
129  $res = $object->delete($user);
130 
131  if ($res < 0) dol_print_error($object->db);
132 
133  header('Location: '.$_SERVER['PHP_SELF']);
134  exit;
135 }
136 
138 
139 $tab_apply = array('A' => $langs->trans('All'), 'G' => $langs->trans('Group'), 'U' => $langs->trans('User'));
140 $tab_rules_type = array('EX_DAY' => $langs->trans('Day'), 'EX_MON' => $langs->trans('Month'), 'EX_YEA' => $langs->trans('Year'), 'EX_EXP' => $langs->trans('OnExpense'));
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("ExpenseReportsRulesSetup"),$linkback,'title_setup');
152 
154 dol_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
155 
156 echo $langs->trans('ExpenseReportRulesDesc');
157 
158 if ($action != 'edit')
159 {
160  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
161  echo '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
162  echo '<input type="hidden" name="action" value="save" />';
163 
164  echo '<table class="noborder" width="100%">';
165 
166  echo '<tr class="liste_titre">';
167  echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
168  echo '<th>'.$langs->trans('ExpenseReportDomain').'</th>';
169  echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
170  echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
171  echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
172  echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
173  echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
174  echo '<th>&nbsp;</th>';
175  echo '</tr>';
176 
177  echo '<tr class="oddeven">';
178  echo '<td>';
179  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, '', 0).'</div>';
180  echo '<div id="user" class="float">'.$form->select_dolusers('', 'fk_user').'</div>';
181  echo '<div id="group" class="float">'.$form->select_dolgroups('', 'fk_usergroup').'</div>';
182  echo '</td>';
183 
184  echo '<td>'.$form->selectExpense('', 'fk_c_type_fees', 0, 1, 1).'</td>';
185  echo '<td>'.$form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0).'</td>';
186  echo '<td>'.$form->selectDate(strtotime(date('Y-m-01', dol_now())), 'start', '', '', 0, '', 1, 0).'</td>';
187  echo '<td>'.$form->selectDate(strtotime(date('Y-m-t', dol_now())), 'end', '', '', 0, '', 1, 0).'</td>';
188  echo '<td><input type="text" value="" name="amount" class="amount" />'.$conf->currency.'</td>';
189  echo '<td>'.$form->selectyesno('restrictive', 0, 1).'</td>';
190  echo '<td align="right"><input type="submit" class="button" value="'.$langs->trans('Add').'" /></td>';
191  echo '</tr>';
192 
193  echo '</table>';
194  echo '</form>';
195 }
196 
197 
198 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
199 echo '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
200 
201 if ($action == 'edit')
202 {
203  echo '<input type="hidden" name="id" value="'.$object->id.'" />';
204  echo '<input type="hidden" name="action" value="save" />';
205 }
206 
207 echo '<table class="noborder" width="100%">';
208 
209 echo '<tr class="liste_titre">';
210 echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
211 echo '<th>'.$langs->trans('ExpenseReportDomain').'</th>';
212 echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
213 echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
214 echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
215 echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
216 echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
217 echo '<th>&nbsp;</th>';
218 echo '</tr>';
219 
220 foreach ($rules as $rule)
221 {
222  echo '<tr class="oddeven">';
223 
224  echo '<td>';
225  if ($action == 'edit' && $object->id == $rule->id)
226  {
227  $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
228  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, $selected, 0).'</div>';
229  echo '<div id="user" class="float">'.$form->select_dolusers($object->fk_user, 'fk_user').'</div>';
230  echo '<div id="group" class="float">'.$form->select_dolgroups($object->fk_usergroup, 'fk_usergroup').'</div>';
231  }
232  else
233  {
234  if ($rule->is_for_all > 0) echo $tab_apply['A'];
235  elseif ($rule->fk_usergroup > 0) echo $tab_apply['G'].' ('.$rule->getGroupLabel().')';
236  elseif ($rule->fk_user > 0) echo $tab_apply['U'].' ('.$rule->getUserName().')';
237  }
238  echo '</td>';
239 
240 
241  echo '<td>';
242  if ($action == 'edit' && $object->id == $rule->id)
243  {
244  echo $form->selectExpense($object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
245  }
246  else
247  {
248  if ($rule->fk_c_type_fees == -1) echo $langs->trans('AllExpenseReport');
249  else
250  {
251  $key = getDictvalue(MAIN_DB_PREFIX.'c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
252  if ($key != $langs->trans($key)) echo $langs->trans($key);
253  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'
254  }
255  }
256  echo '</td>';
257 
258 
259  echo '<td>';
260  if ($action == 'edit' && $object->id == $rule->id)
261  {
262  echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
263  }
264  else
265  {
266  echo $tab_rules_type[$rule->code_expense_rules_type];
267  }
268  echo '</td>';
269 
270 
271  echo '<td>';
272  if ($action == 'edit' && $object->id == $rule->id)
273  {
274  print $form->selectDate(strtotime(date('Y-m-d', $object->dates)), 'start', '', '', 0, '', 1, 0);
275  }
276  else
277  {
278  echo dol_print_date($rule->dates, 'day');
279  }
280  echo '</td>';
281 
282 
283  echo '<td>';
284  if ($action == 'edit' && $object->id == $rule->id)
285  {
286  print $form->selectDate(strtotime(date('Y-m-d', $object->datee)), 'end', '', '', 0, '', 1, 0);
287  }
288  else
289  {
290  echo dol_print_date($rule->datee, 'day');
291  }
292  echo '</td>';
293 
294 
295  echo '<td>';
296  if ($action == 'edit' && $object->id == $rule->id)
297  {
298  echo '<input type="text" value="'.price2num($object->amount).'" name="amount" class="amount" />'.$conf->currency;
299  }
300  else
301  {
302  echo price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency);
303  }
304  echo '</td>';
305 
306 
307  echo '<td>';
308  if ($action == 'edit' && $object->id == $rule->id)
309  {
310  echo $form->selectyesno('restrictive', $object->restrictive, 1);
311  }
312  else
313  {
314  echo yn($rule->restrictive, 1, 1);
315  }
316  echo '</td>';
317 
318 
319  echo '<td>';
320  if ($object->id != $rule->id)
321  {
322  echo '<a href="'.$_SERVER['PHP_SELF'].'?action=edit&id='.$rule->id.'">'.img_edit().'</a>&nbsp;';
323  echo '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$rule->id.'">'.img_delete().'</a>';
324  }
325  else
326  {
327  echo '<input type="submit" class="button" value="'.$langs->trans('Update').'" />&nbsp;';
328  echo '<a href="'.$_SERVER['PHP_SELF'].'" class="button">'.$langs->trans('Cancel').'</a>';
329  }
330  echo '</td>';
331 
332  echo '</tr>';
333 }
334 
335 
336 echo '</table>';
337 echo '</form>';
338 
339 echo '<script type="text/javascript"> $(function() {
340  $("#apply_to").change(function() {
341  var value = $(this).val();
342  if (value == "A") {
343  $("#group").hide(); $("#user").hide();
344  } else if (value == "U") {
345  $("#user").show();
346  $("#group").hide();
347  } else if (value == "G") {
348  $("#group").show();
349  $("#user").hide();
350  }
351  });
352 
353  $("#apply_to").change();
354 
355 }); </script>';
356 
357 dol_fiche_end();
358 
359 // End of page
360 llxFooter();
361 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
img_edit($titlealt='default', $float=0, $other='class="pictoedit"')
Show logo editer/modifier fiche.
Class to manage inventories.
expensereport_admin_prepare_head()
Return array head with list of tabs to view object informations.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
img_delete($titlealt='default', $other='class="pictodelete"')
Show delete logo.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
getDictvalue($tablename, $field, $id, $checkentity=false, $rowidfield='rowid')
Return value from dictionary.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
dol_now($mode='gmt')
Return date for now.
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='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
static getAllRule($fk_c_type_fees='', $date='', $fk_user='')
Return all rules or filtered by something.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.