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 <https://www.gnu.org/licenses/>.
18 */
19
20
21/**
22 *      \file       htdocs/admin/expensereport_ik.php
23 *		\ingroup    expensereport
24 *		\brief      Page to display expense tax ik
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
31require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array("admin", "trips", "errors", "other", "dict"));
35
36if (!$user->admin) accessforbidden();
37
38$error = 0;
39
40$action = GETPOST('action', 'aZ09');
41$id = GETPOST('id', 'int');
42$ikoffset = GETPOST('ikoffset', 'int');
43$coef = GETPOST('coef', 'int');
44
45$fk_c_exp_tax_cat = GETPOST('fk_c_exp_tax_cat');
46$fk_range = GETPOST('fk_range');
47
48if ($action == 'updateik')
49{
50	$expIk = new ExpenseReportIk($db);
51	if ($id > 0)
52	{
53		$result = $expIk->fetch($id);
54		if ($result < 0) dol_print_error('', $expIk->error, $expIk->errors);
55	}
56
57	$expIk->setValues($_POST);
58	$result = $expIk->create($user);
59
60	if ($result > 0) {
61		setEventMessages('SetupSaved', null, 'mesgs');
62		header('Location: '.$_SERVER['PHP_SELF']);
63		exit;
64	} else {
65		setEventMessages($expIk->error, $expIk->errors, 'errors');
66	}
67} elseif ($action == 'delete') // TODO add confirm
68{
69	$expIk = new ExpenseReportIk($db);
70	if ($id > 0)
71	{
72		$result = $expIk->fetch($id);
73		if ($result < 0) dol_print_error('', $expIk->error, $expIk->errors);
74
75		$expIk->delete($user);
76	}
77
78
79	header('Location: '.$_SERVER['PHP_SELF']);
80	exit;
81}
82
83$rangesbycateg = ExpenseReportIk::getAllRanges();
84
85
86/*
87 * View
88 */
89
90llxHeader('', $langs->trans("ExpenseReportsSetup"));
91
92$form = new Form($db);
93
94$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
95print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
96
97$head = expensereport_admin_prepare_head();
98print dol_get_fiche_head($head, 'expenseik', $langs->trans("ExpenseReportsIk"), -1, 'trip');
99
100echo '<span class="opacitymedium">'.$langs->trans('ExpenseReportIkDesc').'</span>';
101print '<br><br>';
102
103echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
104
105if ($action == 'edit')
106{
107	echo '<input type="hidden" name="id" value="'.$id.'" />';
108	echo '<input type="hidden" name="fk_c_exp_tax_cat" value="'.$fk_c_exp_tax_cat.'" />';
109	echo '<input type="hidden" name="fk_range" value="'.$fk_range.'" />';
110	echo '<input type="hidden" name="action" value="updateik" />';
111}
112
113echo '<input type="hidden" name="token" value="'.newToken().'" />';
114
115echo '<table class="noborder centpercent">';
116
117foreach ($rangesbycateg as $fk_c_exp_tax_cat => $Tab)
118{
119	$title = ($Tab['active'] == 1) ? $langs->trans($Tab['label']) : $form->textwithpicto($langs->trans($Tab['label']), $langs->trans('expenseReportCatDisabled'), 1, 'help', '', 0, 3);
120	echo '<tr class="liste_titre">';
121	echo '<td>'.$title.'</td>';
122	echo '<td>'.$langs->trans('expenseReportOffset').'</td>';
123	echo '<td>'.$langs->trans('expenseReportCoef').'</td>';
124	echo '<td>'.$langs->trans('expenseReportTotalForFive').'</td>';
125	echo '<td>&nbsp;</td>';
126	echo '</tr>';
127
128	if ($Tab['active'] == 0) continue;
129
130	$tranche = 1;
131
132	foreach ($Tab['ranges'] as $k => $range)
133	{
134		if (isset($Tab['ranges'][$k + 1])) $label = $langs->trans('expenseReportRangeFromTo', $range->range_ik, ($Tab['ranges'][$k + 1]->range_ik - 1));
135		else $label = $langs->trans('expenseReportRangeMoreThan', $range->range_ik);
136
137		if ($range->range_active == 0) $label = $form->textwithpicto($label, $langs->trans('expenseReportRangeDisabled'), 1, 'help', '', 0, 3);
138
139		echo '<tr class="oddeven">';
140
141		// Label
142		echo '<td class="nowraponall"><b>['.$langs->trans('RangeNum', $tranche++).']</b> - '.$label.'</td>';
143
144		// Offset
145		echo '<td class="nowraponall">';
146		if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat) echo '<input type="text" class="maxwidth100" name="ikoffset" value="'.$range->ik->ikoffset.'" />';
147		else {
148			echo $range->ik->ikoffset;
149		}
150		echo '</td>';
151		// Coef
152		echo '<td class="nowraponall">';
153		if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat) echo '<input type="text" class="maxwidth100" name="coef" value="'.$range->ik->coef.'" />';
154		else echo ($range->ik->id > 0 ? $range->ik->coef : $langs->trans('expenseReportCoefUndefined'));
155		echo '</td>';
156
157		// Total for one
158		echo '<td class="nowraponall">'.$langs->trans('expenseReportPrintExample', price($range->ik->ikoffset + 5 * $range->ik->coef)).'</td>';
159
160		// Action
161		echo '<td class="right">';
162		if ($range->range_active == 1)
163		{
164			if ($action == 'edit' && $range->ik->id == $id && $range->rowid == $fk_range && $range->fk_c_exp_tax_cat == $fk_c_exp_tax_cat)
165			{
166				echo '<input id="" class="button button-save" name="save" value="'.$langs->trans("Save").'" type="submit" />';
167				echo '<input class="button button-cancel" value="'.$langs->trans("Cancel").'" onclick="javascript:history.go(-1)" type="button" />';
168			} else {
169				echo '<a class="editfielda marginrightonly paddingleft paddingright" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().'&id='.$range->ik->id.'&fk_c_exp_tax_cat='.$range->fk_c_exp_tax_cat.'&fk_range='.$range->rowid.'">'.img_edit().'</a>';
170				if (!empty($range->ik->id)) echo '<a class="paddingleft paddingright" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$range->ik->id.'">'.img_delete().'</a>';
171				// TODO add delete link
172			}
173		}
174		echo '</td>';
175
176		echo '</tr>';
177	}
178}
179
180echo '</table>';
181echo '</form>';
182
183print dol_get_fiche_end();
184
185// End of page
186llxFooter();
187$db->close();
188