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
22/**
23 *      \file       htdocs/admin/expensereport_ik.php
24 *		\ingroup    expensereport
25 *		\brief      Page to display expense tax ik
26 */
27
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
32require_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
37if (!$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);
58if (!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
65if ($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
136$rules = ExpenseReportRule::getAllRule();
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
146llxHeader('', $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>';
151print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
152
153$head = expensereport_admin_prepare_head();
154print dol_get_fiche_head($head, 'expenserules', $langs->trans("ExpenseReportsRules"), -1, 'trip');
155
156echo '<span class="opacitymedium">'.$langs->trans('ExpenseReportRulesDesc').'</span>';
157print '<br><br>';
158
159if ($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
199echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
200echo '<input type="hidden" name="token" value="'.newToken().'" />';
201
202if ($action == 'edit')
203{
204	echo '<input type="hidden" name="id" value="'.$object->id.'" />';
205	echo '<input type="hidden" name="action" value="save" />';
206}
207
208echo '<table class="noborder centpercent">';
209
210echo '<tr class="liste_titre">';
211echo '<th>'.$langs->trans('ExpenseReportApplyTo').'</th>';
212echo '<th>'.$langs->trans('Type').'</th>';
213echo '<th>'.$langs->trans('ExpenseReportLimitOn').'</th>';
214echo '<th>'.$langs->trans('ExpenseReportDateStart').'</th>';
215echo '<th>'.$langs->trans('ExpenseReportDateEnd').'</th>';
216echo '<th>'.$langs->trans('ExpenseReportLimitAmount').'</th>';
217echo '<th>'.$langs->trans('ExpenseReportRestrictive').'</th>';
218echo '<th>&nbsp;</th>';
219echo '</tr>';
220
221foreach ($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
320echo '</table>';
321echo '</form>';
322
323echo '<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
341print dol_get_fiche_end();
342
343// End of page
344llxFooter();
345$db->close();
346