dolibarr  7.0.0-beta
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
32 
33 $langs->load('admin');
34 $langs->load('other');
35 $langs->load('trips');
36 $langs->load('errors');
37 $langs->load('dict');
38 
39 if (!$user->admin) accessforbidden();
40 
41 //Init error
42 $error = false;
43 $message = false;
44 
45 $action = GETPOST('action','alpha');
46 $id = GETPOST('id','int');
47 
48 $apply_to = GETPOST('apply_to');
49 $fk_user = GETPOST('fk_user');
50 $fk_usergroup = GETPOST('fk_usergroup');
51 
52 $fk_c_type_fees = GETPOST('fk_c_type_fees');
53 $code_expense_rules_type = GETPOST('code_expense_rules_type');
54 $dates = dol_mktime(12, 0, 0, GETPOST('startmonth'), GETPOST('startday'), GETPOST('startyear'));
55 $datee = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
56 $amount = GETPOST('amount');
57 $restrictive = GETPOST('restrictive');
58 
59 $object = new ExpenseReportRule($db);
60 if (!empty($id))
61 {
62  $result = $object->fetch($id);
63  if ($result < 0) dol_print_error('', $object->error, $object->errors);
64 }
65 
66 // TODO do action
67 if ($action == 'save')
68 {
69  $error = 0;
70 
71  // check parameters
72  if (empty($apply_to)) {
73  $error++;
74  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportApplyTo")), null, 'errors');
75  }
76  if (empty($fk_c_type_fees)) {
77  $error++;
78  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDomain")), null, 'errors');
79  }
80  if (empty($code_expense_rules_type)) {
81  $error++;
82  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitOn")), null, 'errors');
83  }
84  if (empty($dates)) {
85  $error++;
86  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateStart")), null, 'errors');
87  }
88  if (empty($datee)) {
89  $error++;
90  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateEnd")), null, 'errors');
91  }
92  if (empty($amount)) {
93  $error++;
94  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitAmount")), null, 'errors');
95  }
96 
97  if (empty($error))
98  {
99  $object->setValues($_POST);
100 
101  if($apply_to=='U'){
102  $object->fk_user=$fk_user;
103  $object->fk_usergroup=0;
104  $object->is_for_all=0;
105  }elseif($apply_to=='G'){
106  $object->fk_usergroup=$fk_usergroup;
107  $object->fk_user=0;
108  $object->is_for_all=0;
109  }elseif($apply_to=='A'){
110  $object->is_for_all=1;
111  $object->fk_user=0;
112  $object->fk_usergroup=0;
113  }
114 
115  $object->dates = $dates;
116  $object->datee = $datee;
117 
118  $object->entity = $conf->entity;
119 
120  $res = $object->create($user);
121  if ($res > 0) setEventMessages($langs->trans('ExpenseReportRuleSave'), null);
122  else dol_print_error($object->db);
123 
124  header('Location: '.$_SERVER['PHP_SELF']);
125  exit;
126  }
127 }
128 elseif ($action == 'delete')
129 {
130  // TODO add confirm
131  $res = $object->delete($user);
132 
133  if ($res < 0) dol_print_error($object->db);
134 
135  header('Location: '.$_SERVER['PHP_SELF']);
136  exit;
137 }
138 
140 
141 $tab_apply = array('A' => $langs->trans('All'), 'G' => $langs->trans('Group'), 'U' => $langs->trans('User'));
142 $tab_rules_type = array('EX_DAY' => $langs->trans('Day'), 'EX_MON' => $langs->trans('Month'), 'EX_YEA' => $langs->trans('Year'), 'EX_EXP' => $langs->trans('OnExpense'));
143 
144 /*
145  * View
146  */
147 
148 llxHeader();
149 
150 $form=new Form($db);
151 
152 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
153 print load_fiche_titre($langs->trans("ExpenseReportsRulesSetup"),$linkback,'title_setup');
154 
156 dol_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
157 
158 echo $langs->trans('ExpenseReportRulesDesc');
159 
160 if ($action != 'edit')
161 {
162  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
163  echo '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
164  echo '<input type="hidden" name="action" value="save" />';
165 
166  echo '<table class="noborder" width="100%">';
167 
168  echo '<tr class="liste_titre">';
169  echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
170  echo '<th>'.$langs->trans('ExpenseReportDomain').'</th>';
171  echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
172  echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
173  echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
174  echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
175  echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
176  echo '<th>&nbsp;</th>';
177  echo '</tr>';
178 
179  $var=true;
180  echo '<tr '.$bc[$var].'>';
181  echo '<td>';
182  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, '', 0).'</div>';
183  echo '<div id="user" class="float">'.$form->select_dolusers('', 'fk_user').'</div>';
184  echo '<div id="group" class="float">'.$form->select_dolgroups('', 'fk_usergroup').'</div>';
185  echo '</td>';
186 
187  echo '<td>'.$form->selectExpense('', 'fk_c_type_fees', 0, 1, 1).'</td>';
188  echo '<td>'.$form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0).'</td>';
189  echo '<td>'.$form->select_date(strtotime(date('Y-m-01', dol_now())), 'start', '', '', 0, '', 1, 0, 1).'</td>';
190  echo '<td>'.$form->select_date(strtotime(date('Y-m-t', dol_now())), 'end', '', '', 0, '', 1, 0, 1).'</td>';
191  echo '<td><input type="text" value="" name="amount" class="amount" />'.$conf->currency.'</td>';
192  echo '<td>'.$form->selectyesno('restrictive', 0, 1).'</td>';
193  echo '<td align="right"><input type="submit" class="button" value="'.$langs->trans('Add').'" /></td>';
194  echo '</tr>';
195 
196  echo '</table>';
197  echo '</form>';
198 }
199 
200 
201 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
202 echo '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
203 
204 if ($action == 'edit')
205 {
206  echo '<input type="hidden" name="id" value="'.$object->id.'" />';
207  echo '<input type="hidden" name="action" value="save" />';
208 }
209 
210 echo '<table class="noborder" width="100%">';
211 
212 echo '<tr class="liste_titre">';
213 echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
214 echo '<th>'.$langs->trans('ExpenseReportDomain').'</th>';
215 echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
216 echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
217 echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
218 echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
219 echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
220 echo '<th>&nbsp;</th>';
221 echo '</tr>';
222 
223 $var=true;
224 foreach ($rules as $rule)
225 {
226  echo '<tr '.$bc[$var].'>';
227 
228  echo '<td>';
229  if ($action == 'edit' && $object->id == $rule->id)
230  {
231  $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
232  echo '<div class="float">'.$form->selectarray('apply_to', $tab_apply, $selected, 0).'</div>';
233  echo '<div id="user" class="float">'.$form->select_dolusers($object->fk_user, 'fk_user').'</div>';
234  echo '<div id="group" class="float">'.$form->select_dolgroups($object->fk_usergroup, 'fk_usergroup').'</div>';
235  }
236  else
237  {
238  if ($rule->is_for_all > 0) echo $tab_apply['A'];
239  elseif ($rule->fk_usergroup > 0) echo $tab_apply['G'].' ('.$rule->getGroupLabel().')';
240  elseif ($rule->fk_user > 0) echo $tab_apply['U'].' ('.$rule->getUserName().')';
241  }
242  echo '</td>';
243 
244 
245  echo '<td>';
246  if ($action == 'edit' && $object->id == $rule->id)
247  {
248  echo $form->selectExpense($object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
249  }
250  else
251  {
252  if ($rule->fk_c_type_fees == -1) echo $langs->trans('AllExpenseReport');
253  else
254  {
255  $key = getDictvalue(MAIN_DB_PREFIX.'c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
256  if ($key != $langs->trans($key)) echo $langs->trans($key);
257  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'
258  }
259  }
260  echo '</td>';
261 
262 
263 
264  echo '<td>';
265  if ($action == 'edit' && $object->id == $rule->id)
266  {
267  echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
268  }
269  else
270  {
271  echo $tab_rules_type[$rule->code_expense_rules_type];
272  }
273  echo '</td>';
274 
275 
276  echo '<td>';
277  if ($action == 'edit' && $object->id == $rule->id)
278  {
279  echo $form->select_date(strtotime(date('Y-m-d', $object->dates)), 'start', '', '', 0, '', 1, 0, 1);
280  }
281  else
282  {
283  echo dol_print_date($rule->dates, 'day');
284  }
285  echo '</td>';
286 
287 
288  echo '<td>';
289  if ($action == 'edit' && $object->id == $rule->id)
290  {
291  echo $form->select_date(strtotime(date('Y-m-d', $object->datee)), 'end', '', '', 0, '', 1, 0, 1);
292  }
293  else
294  {
295  echo dol_print_date($rule->datee, 'day');
296  }
297  echo '</td>';
298 
299 
300  echo '<td>';
301  if ($action == 'edit' && $object->id == $rule->id)
302  {
303  echo '<input type="text" value="'.price2num($object->amount).'" name="amount" class="amount" />'.$conf->currency;
304  }
305  else
306  {
307  echo price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency);
308  }
309  echo '</td>';
310 
311 
312  echo '<td>';
313  if ($action == 'edit' && $object->id == $rule->id)
314  {
315  echo $form->selectyesno('restrictive', $object->restrictive, 1);
316  }
317  else
318  {
319  echo yn($rule->restrictive, 1, 1);
320  }
321  echo '</td>';
322 
323 
324  echo '<td>';
325  if ($object->id != $rule->id)
326  {
327  echo '<a href="'.$_SERVER['PHP_SELF'].'?action=edit&id='.$rule->id.'">'.img_edit().'</a>&nbsp;';
328  echo '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$rule->id.'">'.img_delete().'</a>';
329  }
330  else
331  {
332  echo '<input type="submit" class="button" value="'.$langs->trans('Update').'" />&nbsp;';
333  echo '<a href="'.$_SERVER['PHP_SELF'].'" class="button">'.$langs->trans('Cancel').'</a>';
334  }
335  echo '</td>';
336 
337  echo '</tr>';
338  $var=!$var;
339 }
340 
341 
342 echo '</table>';
343 echo '</form>';
344 
345 echo '<script type="text/javascript"> $(function() {
346  $("#apply_to").change(function() {
347  var value = $(this).val();
348  if (value == "A") {
349  $("#group").hide(); $("#user").hide();
350  } else if (value == "U") {
351  $("#user").show();
352  $("#group").hide();
353  } else if (value == "G") {
354  $("#group").show();
355  $("#user").hide();
356  }
357  });
358 
359  $("#apply_to").change();
360 
361 }); </script>';
362 
363 dol_fiche_end();
364 llxFooter();
365 
366 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
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...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
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...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
dol_now($mode='gmt')
Return date for now.
img_delete($titlealt= 'default', $other= 'class="pictodelete"')
Show delete logo.
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).
print
Draft customers invoices.
Definition: index.php:91
static getAllRule($fk_c_type_fees='', $date='', $fk_user='')
Return all rules or filtered by something.
img_edit($titlealt= 'default', $float=0, $other= 'class="pictoedit"')
Show logo editer/modifier fiche.