dolibarr 23.0.3
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-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22
29// Load Dolibarr environment
30require '../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
34require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
35
44// Load translation files required by the page
45$langs->loadLangs(array("admin", "other", "trips", "errors", "dict"));
46
47// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
48$hookmanager->initHooks(array('admin', 'dictionaryadmin','expensereport_rules'));
49
50$object = new ExpenseReportRule($db);
51
52if (!$user->admin) {
54}
55
56
57/*
58 * Action
59 */
60
61$parameters = array();
62$rules = array();
63$tab_apply = array();
64$tab_rules_type = array();
65$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
66if ($reshook < 0) {
67 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
68}
69
70if (empty($reshook)) {
71 //Init error
72 $error = false;
73
74 $action = GETPOST('action', 'aZ09');
75 $id = GETPOSTINT('id');
76
77 $apply_to = GETPOST('apply_to');
78 $fk_user = GETPOSTINT('fk_user');
79 $fk_usergroup = GETPOSTINT('fk_usergroup');
80 $restrictive = GETPOSTINT('restrictive');
81 $fk_c_type_fees = GETPOSTINT('fk_c_type_fees');
82 $code_expense_rules_type = GETPOST('code_expense_rules_type');
83 $dates = dol_mktime(12, 0, 0, GETPOSTINT('startmonth'), GETPOSTINT('startday'), GETPOSTINT('startyear'));
84 $datee = dol_mktime(12, 0, 0, GETPOSTINT('endmonth'), GETPOSTINT('endday'), GETPOSTINT('endyear'));
85 $amount = (float) price2num(GETPOST('amount'), 'MT', 2);
86
87 if (!empty($id)) {
88 $result = $object->fetch($id);
89 if ($result < 0) {
90 dol_print_error(null, $object->error, $object->errors);
91 }
92 }
93
94 if ($action == 'save') {
95 $error = 0;
96
97 // check parameters
98 if (empty($apply_to)) {
99 $error++;
100 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportApplyTo")), null, 'errors');
101 }
102 if (empty($fk_c_type_fees)) {
103 $error++;
104 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDomain")), null, 'errors');
105 }
106 if (empty($code_expense_rules_type)) {
107 $error++;
108 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitOn")), null, 'errors');
109 }
110 if (empty($dates)) {
111 $error++;
112 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateStart")), null, 'errors');
113 }
114 if (empty($datee)) {
115 $error++;
116 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportDateEnd")), null, 'errors');
117 }
118 if (empty($amount)) {
119 $error++;
120 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ExpenseReportLimitAmount")), null, 'errors');
121 }
122
123 if (empty($error)) {
124 if ($apply_to == 'U') {
125 $object->fk_user = (int) $fk_user;
126 $object->fk_usergroup = 0;
127 $object->is_for_all = 0;
128 } elseif ($apply_to == 'G') {
129 $object->fk_usergroup = (int) $fk_usergroup;
130 $object->fk_user = 0;
131 $object->is_for_all = 0;
132 } elseif ($apply_to == 'A') {
133 $object->is_for_all = 1;
134 $object->fk_user = 0;
135 $object->fk_usergroup = 0;
136 }
137
138 $object->dates = $dates;
139 $object->datee = $datee;
140 $object->restrictive = $restrictive;
141 $object->fk_c_type_fees = $fk_c_type_fees;
142 $object->code_expense_rules_type = $code_expense_rules_type;
143 $object->amount = $amount;
144 $object->entity = $conf->entity;
145
146 if ($object->id > 0) {
147 $res = $object->update($user);
148 } else {
149 $res = $object->create($user);
150 }
151 if ($res > 0) {
152 setEventMessages($langs->trans('ExpenseReportRuleSave'), null);
153 } else {
155 $error++;
156 }
157
158 if (!$error) {
159 header('Location: '.DOL_URL_ROOT.'/admin/expensereport_rules.php');
160 exit;
161 } else {
162 $action = '';
163 }
164 }
165 } elseif ($action == 'delete') {
166 // TODO add confirm
167 $res = $object->delete($user);
168
169 if ($res < 0) {
171 }
172
173 header('Location: ' . DOL_URL_ROOT.'/admin/expensereport_rules.php');
174 exit;
175 }
176
177 $rules = $object->getAllRule();
178
179 $tab_apply = array(
180 'A' => $langs->trans('All'),
181 'G' => $langs->trans('UserGroup'),
182 'U' => $langs->trans('User')
183 );
184 $tab_rules_type = array(
185 'EX_DAY' => $langs->trans('Day'),
186 'EX_MON' => $langs->trans('Month'),
187 'EX_YEA' => $langs->trans('Year'),
188 'EX_EXP' => $langs->trans('OnExpense')
189 );
190}
191
192
193/*
194 * View
195 */
196
197llxHeader('', $langs->trans("ExpenseReportsSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-expensereport_rules');
198
199$form = new Form($db);
200
201$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
202
203print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
204
206print dol_get_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
207
208echo '<span class="opacitymedium">' . $langs->trans('ExpenseReportRulesDesc') . '</span>';
209print '<br><br>';
210
211if ($action != 'edit') {
212 echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
213 echo '<input type="hidden" name="token" value="' . newToken() . '" />';
214 echo '<input type="hidden" name="action" value="save" />';
215
216 echo '<table class="noborder centpercent">';
217
218 echo '<tr class="liste_titre headerexpensereportrules">';
219 echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
220 echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
221 echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
222 echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
223 echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
224 echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
225 echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
226 echo '<th>&nbsp;</th>';
227 echo '</tr>';
228
229 echo '<tr class="oddeven">';
230 echo '<td>';
231 echo '<div class="float linecolapplyto">' . $form->selectarray('apply_to', $tab_apply, '', 0) . '</div>';
232 echo '<div id="user" class="float linecoluser">' . $form->select_dolusers('', 'fk_user') . '</div>';
233 echo '<div id="group" class="float linecolgroup">' . $form->select_dolgroups(0, 'fk_usergroup') . '</div>';
234 echo '</td>';
235
236 echo '<td class="linecoltype">' . $form->selectExpenseFees('', 'fk_c_type_fees', 0, 1, 1) . '</td>';
237 echo '<td class="linecoltyperule">' . $form->selectarray('code_expense_rules_type', $tab_rules_type, '', 0) . '</td>';
238 echo '<td class="linecoldatestart">' . $form->selectDate(strtotime(date('Y-m-01', dol_now())), 'start', 0, 0, 0, '', 1, 0) . '</td>';
239 echo '<td class="linecoldateend">' . $form->selectDate(strtotime(date('Y-m-t', dol_now())), 'end', 0, 0, 0, '', 1, 0) . '</td>';
240 echo '<td class="linecolamount"><input type="text" value="" class="maxwidth100" name="amount" class="amount right" /></td>';
241 echo '<td class="linecolrestrictive">' . $form->selectyesno('restrictive', 0, 1) . '</td>';
242 echo '<td class="right linecolbutton"><input type="submit" class="button button-add" value="' . $langs->trans('Add') . '" /></td>';
243 echo '</tr>';
244
245 echo '</table>';
246 echo '</form>';
247}
248
249
250echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
251echo '<input type="hidden" name="token" value="' . newToken() . '" />';
252
253if ($action == 'edit') {
254 echo '<input type="hidden" name="id" value="' . $object->id . '" />';
255 echo '<input type="hidden" name="action" value="save" />';
256}
257
258print dol_get_fiche_end();
259
260
261echo '<table class="noborder centpercent">';
262
263echo '<tr class="liste_titre expensereportrules">';
264echo '<th class="linecolapplyto">' . $langs->trans('ExpenseReportApplyTo') . '</th>';
265echo '<th class="linecoltype">' . $langs->trans('Type') . '</th>';
266echo '<th class="linecollimiton">' . $langs->trans('ExpenseReportLimitOn') . '</th>';
267echo '<th class="linecoldatestart">' . $langs->trans('ExpenseReportDateStart') . '</th>';
268echo '<th class="linecoldateend">' . $langs->trans('ExpenseReportDateEnd') . '</th>';
269echo '<th class="linecollimitamount">' . $langs->trans('ExpenseReportLimitAmount') . '</th>';
270echo '<th class="linecolrestrictive">' . $langs->trans('ExpenseReportRestrictive') . '</th>';
271echo '<th>&nbsp;</th>';
272echo '</tr>';
273
274foreach ($rules as $rule) {
275 echo '<tr class="oddeven linetrdata" id="'.$rule->id.'">';
276
277 echo '<td class="linecolusergroup">';
278 if ($action == 'edit' && $object->id == $rule->id) {
279 $selected = ($object->is_for_all > 0) ? 'A' : ($object->fk_usergroup > 0 ? 'G' : 'U');
280 echo '<div class="float">' . $form->selectarray('apply_to', $tab_apply, $selected, 0) . '</div>';
281 echo '<div id="user" class="float">' . $form->select_dolusers($object->fk_user, 'fk_user') . '</div>';
282 echo '<div id="group" class="float">' . $form->select_dolgroups($object->fk_usergroup, 'fk_usergroup') . '</div>';
283 } else {
284 if ($rule->is_for_all > 0) {
285 echo $tab_apply['A'];
286 } elseif ($rule->fk_usergroup > 0) {
287 echo $tab_apply['G'] . ' (' . $rule->getGroupLabel() . ')';
288 } elseif ($rule->fk_user > 0) {
289 echo $tab_apply['U'] . ' (' . $rule->getUserName() . ')';
290 }
291 }
292 echo '</td>';
293
294 echo '<td class="linecoltype">';
295 if ($action == 'edit' && $object->id == $rule->id) {
296 echo $form->selectExpenseFees((string) $object->fk_c_type_fees, 'fk_c_type_fees', 0, 1, 1);
297 } else {
298 if ($rule->fk_c_type_fees == -1) {
299 echo $langs->trans('AllExpenseReport');
300 } else {
301 $key = getDictionaryValue('c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
302 if ($key && $key != $langs->trans($key)) {
303 echo $langs->trans($key);
304 } else {
305 $value = getDictionaryValue('c_type_fees', 'label', $rule->fk_c_type_fees, false, 'id');
306 echo $langs->trans($value ? $value : 'Undefined'); // TODO check to return trans of 'code'
307 }
308 }
309 }
310 echo '</td>';
311
312
313 echo '<td class="linecoltyperule">';
314 if ($action == 'edit' && $object->id == $rule->id) {
315 echo $form->selectarray('code_expense_rules_type', $tab_rules_type, $object->code_expense_rules_type, 0);
316 } else {
317 echo $tab_rules_type[$rule->code_expense_rules_type];
318 }
319 echo '</td>';
320
321
322 echo '<td class="linecoldatestart">';
323 if ($action == 'edit' && $object->id == $rule->id) {
324 print $form->selectDate(strtotime(date('Y-m-d', $object->dates)), 'start', 0, 0, 0, '', 1, 0);
325 } else {
326 echo dol_print_date($rule->dates, 'day');
327 }
328 echo '</td>';
329
330
331 echo '<td class="linecoldateend">';
332 if ($action == 'edit' && $object->id == $rule->id) {
333 print $form->selectDate(strtotime(date('Y-m-d', $object->datee)), 'end', 0, 0, 0, '', 1, 0);
334 } else {
335 echo dol_print_date($rule->datee, 'day');
336 }
337 echo '</td>';
338
339 // Amount
340 echo '<td class="linecolamount">';
341 if ($action == 'edit' && $object->id == $rule->id) {
342 echo '<input type="text" value="' . price2num($object->amount) . '" name="amount" class="amount width50 right" />';
343 } else {
344 echo price($rule->amount, 0, $langs, 1, -1, -1, getDolCurrency());
345 }
346 echo '</td>';
347
348
349 echo '<td class="linecolrestrictive">';
350 if ($action == 'edit' && $object->id == $rule->id) {
351 echo $form->selectyesno('restrictive', $object->restrictive, 1);
352 } else {
353 echo yn($rule->restrictive, 1, 1);
354 }
355 echo '</td>';
356
357
358 echo '<td class="center">';
359 if ($object->id != $rule->id) {
360 echo '<a class="editfielda paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=edit&token=' . newToken() . '&id=' . $rule->id . '">' . img_edit() . '</a>&nbsp;';
361 echo '<a class="paddingright paddingleft" href="' . $_SERVER['PHP_SELF'] . '?action=delete&token=' . newToken() . '&id=' . $rule->id . '">' . img_delete() . '</a>';
362 } else {
363 echo '<input type="submit" class="button button-edit" value="' . $langs->trans('Update') . '" />&nbsp;';
364 echo '<a href="' . $_SERVER['PHP_SELF'] . '" class="button button-cancel">' . $langs->trans("Cancel") . '</a>';
365 }
366 echo '</td>';
367
368 echo '</tr>';
369}
370
371if (!is_array($rules) || count($rules) == 0) {
372 print '<tr class="none"><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
373}
374
375echo '</table>';
376echo '</form>';
377
378echo '<script type="text/javascript"> $(function() {
379 $("#apply_to").change(function() {
380 var value = $(this).val();
381 if (value == "A") {
382 $("#group").hide(); $("#user").hide();
383 } else if (value == "U") {
384 $("#user").show();
385 $("#group").hide();
386 } else if (value == "G") {
387 $("#group").show();
388 $("#user").hide();
389 }
390 });
391
392 $("#apply_to").change();
393
394}); </script>';
395
396
397print dol_get_fiche_end();
398
399// End of page
400llxFooter();
401$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage inventories.
Class to manage generation of HTML components Only common components must be here.
expensereport_admin_prepare_head()
Return array head with list of tabs to view object information.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
getDolCurrency()
Return the main currency ('EUR', 'USD', ...)
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
img_edit($titlealt='default', $float=0, $other='')
Show logo edit/modify fiche.
getDictionaryValue($tablename, $field, $id, $checkentity=false, $rowidfield='rowid')
Return the value of a filed into a dictionary for the record $id.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:464
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.