1<?php
2/* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2015 Laurent Destailleur  <eldy@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *	    \file       htdocs/comm/remise.php
21 *      \ingroup    societe
22 *		\brief      Page to edit relative discount of a customer
23 */
24
25require '../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
28
29// Load translation files required by the page
30$langs->loadLangs(array('companies', 'orders', 'bills'));
31
32$id = GETPOST("id", 'int');
33
34$socid = GETPOST('id', 'int') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
35// Security check
36if ($user->socid > 0)
37{
38	$socid = $user->socid;
39}
40
41$backtopage = GETPOST('backtopage', 'alpha');
42
43
44/*
45 * Actions
46 */
47
48if (GETPOST('cancel', 'alpha') && !empty($backtopage))
49{
50	 header("Location: ".$backtopage);
51	 exit;
52}
53
54if (GETPOST('action', 'aZ09') == 'setremise')
55{
56	$object = new Societe($db);
57	$object->fetch($id);
58
59	$discount_type = GETPOST('discount_type', 'int');
60
61	if (!empty($discount_type)) {
62		$result = $object->set_remise_supplier(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
63	} else {
64		$result = $object->set_remise_client(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
65	}
66
67	if ($result > 0)
68	{
69		if (!empty($backtopage))
70		{
71			header("Location: ".$backtopage);
72			exit;
73		} else {
74			header("Location: remise.php?id=".$_GET["id"]);
75			exit;
76		}
77	} else {
78		setEventMessages($object->error, $object->errors, 'errors');
79	}
80}
81
82
83/*
84 * View
85 */
86
87$form = new Form($db);
88
89llxHeader();
90
91
92/*********************************************************************************
93 *
94 * Mode fiche
95 *
96 *********************************************************************************/
97if ($socid > 0)
98{
99	// On recupere les donnees societes par l'objet
100	$object = new Societe($db);
101	$object->fetch($socid);
102
103	$head = societe_prepare_head($object);
104
105	$isCustomer = ($object->client == 1 || $object->client == 3);
106	$isSupplier = $object->fournisseur == 1;
107
108	print '<form method="POST" action="remise.php?id='.$object->id.'">';
109	print '<input type="hidden" name="token" value="'.newToken().'">';
110	print '<input type="hidden" name="action" value="setremise">';
111	print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
112
113	print dol_get_fiche_head($head, 'relativediscount', $langs->trans("ThirdParty"), -1, 'company');
114
115	dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
116
117	print '<div class="fichecenter">';
118
119	print '<div class="underbanner clearboth"></div>';
120
121	if (!$isCustomer && !$isSupplier) {
122		print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
123
124		print dol_get_fiche_end();
125
126		print '</form>';
127
128		// End of page
129		llxFooter();
130		$db->close();
131		exit;
132	}
133
134	print '<table class="border centpercent">';
135
136	if ($isCustomer) {
137		// Customer discount
138		print '<tr><td class="titlefield">';
139		print $langs->trans("CustomerRelativeDiscount").'</td><td>'.price2num($object->remise_percent)."%</td></tr>";
140	}
141
142	if ($isSupplier) {
143		// Supplier discount
144		print '<tr><td class="titlefield">';
145		print $langs->trans("SupplierRelativeDiscount").'</td><td>'.price2num($object->remise_supplier_percent)."%</td></tr>";
146	}
147
148	print '</table>';
149	print '<br>';
150
151	print load_fiche_titre($langs->trans("NewRelativeDiscount"), '', '');
152
153	print '<div class="underbanner clearboth"></div>';
154
155	/*if (! ($isCustomer && $isSupplier))
156	{
157	    if ($isCustomer && ! $isSupplier) {
158	        print '<input type="hidden" name="discount_type" value="0" />';
159	    }
160	    if (! $isCustomer && $isSupplier) {
161	        print '<input type="hidden" name="discount_type" value="1" />';
162	    }
163	}*/
164
165	print '<table class="border centpercent">';
166
167	if ($isCustomer || $isSupplier)
168	{
169		// Discount type
170		print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td><td>';
171		if ($isCustomer) {
172			print '<input type="radio" name="discount_type" id="discount_type_0" checked value="0"/> <label for="discount_type_0">'.$langs->trans('Customer').'</label>';
173		}
174		if ($isSupplier) {
175			print ' <input type="radio" name="discount_type" id="discount_type_1"'.($isCustomer ? '' : ' checked').' value="1"/> <label for="discount_type_1">'.$langs->trans('Supplier').'</label>';
176		}
177		print '</td></tr>';
178	}
179
180	// New value
181	print '<tr><td class="titlefield fieldrequired">';
182	print $langs->trans("NewValue").'</td><td><input type="text" size="5" name="remise" value="'.dol_escape_htmltag(GETPOST("remise")).'">%</td></tr>';
183
184	// Motif/Note
185	print '<tr><td class="fieldrequired">';
186	print $langs->trans("NoteReason").'</td><td><input type="text" size="60" name="note" value="'.dol_escape_htmltag(GETPOST("note", "alphanohtml")).'"></td></tr>';
187
188	print "</table>";
189
190	print '</div>';
191
192	print dol_get_fiche_end();
193
194	print '<div class="center">';
195	print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
196	if (!empty($backtopage))
197	{
198		print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
199		print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
200	}
201	print '</div>';
202
203	print "</form>";
204
205	print '<br>';
206
207	if ($isCustomer) {
208		if ($isSupplier) {
209			print '<div class="fichecenter">';
210			print '<div class="fichehalfleft">';
211			print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
212		}
213
214		/*
215		 * List log of all customer percent discounts
216		 */
217		$sql = "SELECT rc.rowid, rc.remise_client as remise_percent, rc.note, rc.datec as dc,";
218		$sql .= " u.login, u.rowid as user_id";
219		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise as rc, ".MAIN_DB_PREFIX."user as u";
220		$sql .= " WHERE rc.fk_soc = ".$object->id;
221		$sql .= " AND rc.entity IN (".getEntity('discount').")";
222		$sql .= " AND u.rowid = rc.fk_user_author";
223		$sql .= " ORDER BY rc.datec DESC";
224
225		$resql = $db->query($sql);
226		if ($resql)
227		{
228			print '<table class="noborder centpercent">';
229			$tag = !$tag;
230			print '<tr class="liste_titre">';
231			print '<td width="160">'.$langs->trans("Date").'</td>';
232			print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
233			print '<td class="left">'.$langs->trans("NoteReason").'</td>';
234			print '<td class="center">'.$langs->trans("User").'</td>';
235			print '</tr>';
236			$num = $db->num_rows($resql);
237			if ($num > 0)
238			{
239				$i = 0;
240				while ($i < $num)
241				{
242					$obj = $db->fetch_object($resql);
243					print '<tr class="oddeven">';
244					print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
245					print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
246					print '<td class="left">'.$obj->note.'</td>';
247					print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a></td>';
248					print '</tr>';
249					$i++;
250				}
251			} else {
252				print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
253			}
254			$db->free($resql);
255			print "</table>";
256		} else {
257			dol_print_error($db);
258		}
259	}
260
261	if ($isSupplier) {
262		if ($isCustomer) {
263			print '</div>'; // class="fichehalfleft"
264			print '<div class="fichehalfright">';
265			print '<div class="ficheaddleft">';
266			print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
267		}
268
269		/*
270		 * List log of all supplier percent discounts
271		 */
272		$sql = "SELECT rc.rowid, rc.remise_supplier as remise_percent, rc.note, rc.datec as dc,";
273		$sql .= " u.login, u.rowid as user_id";
274		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_supplier as rc, ".MAIN_DB_PREFIX."user as u";
275		$sql .= " WHERE rc.fk_soc = ".$object->id;
276		$sql .= " AND rc.entity IN (".getEntity('discount').")";
277		$sql .= " AND u.rowid = rc.fk_user_author";
278		$sql .= " ORDER BY rc.datec DESC";
279
280		$resql = $db->query($sql);
281		if ($resql)
282		{
283			print '<table class="noborder centpercent">';
284			$tag = !$tag;
285			print '<tr class="liste_titre">';
286			print '<td width="160">'.$langs->trans("Date").'</td>';
287			print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
288			print '<td class="left">'.$langs->trans("NoteReason").'</td>';
289			print '<td class="center">'.$langs->trans("User").'</td>';
290			print '</tr>';
291			$num = $db->num_rows($resql);
292			if ($num > 0)
293			{
294				$i = 0;
295				while ($i < $num)
296				{
297					$obj = $db->fetch_object($resql);
298					print '<tr class="oddeven">';
299					print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
300					print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
301					print '<td class="left">'.$obj->note.'</td>';
302					print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a></td>';
303					print '</tr>';
304					$i++;
305				}
306			} else {
307				print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
308			}
309			$db->free($resql);
310			print "</table>";
311		} else {
312			dol_print_error($db);
313		}
314
315		if ($isCustomer) {
316			print '</div>'; // class="ficheaddleft"
317			print '</div>'; // class="fichehalfright"
318			print '</div>'; // class="fichecenter"
319		}
320	}
321}
322
323// End of page
324llxFooter();
325$db->close();
326