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