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	$socid = $user->socid;
38}
39
40$backtopage = GETPOST('backtopage', 'alpha');
41
42
43/*
44 * Actions
45 */
46
47if (GETPOST('cancel', 'alpha') && !empty($backtopage)) {
48	 header("Location: ".$backtopage);
49	 exit;
50}
51
52if (GETPOST('action', 'aZ09') == 'setremise') {
53	$object = new Societe($db);
54	$object->fetch($id);
55
56	$discount_type = GETPOST('discount_type', 'int');
57
58	if (!empty($discount_type)) {
59		$result = $object->set_remise_supplier(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
60	} else {
61		$result = $object->set_remise_client(price2num(GETPOST("remise")), GETPOST("note", "alphanohtml"), $user);
62	}
63
64	if ($result > 0) {
65		if (!empty($backtopage)) {
66			header("Location: ".$backtopage);
67			exit;
68		} else {
69			header("Location: remise.php?id=".GETPOST("id", 'int'));
70			exit;
71		}
72	} else {
73		setEventMessages($object->error, $object->errors, 'errors');
74	}
75}
76
77// Security check
78if ($user->socid > 0) {
79	$id = $user->socid;
80}
81$result = restrictedArea($user, 'societe', $id, '&societe', '', 'fk_soc', 'rowid', 0);
82
83
84/*
85 * View
86 */
87
88$form = new Form($db);
89
90llxHeader();
91
92/*********************************************************************************
93 *
94 * Mode fiche
95 *
96 *********************************************************************************/
97if ($socid > 0) {
98	// On recupere les donnees societes par l'objet
99	$object = new Societe($db);
100	$object->fetch($socid);
101
102	$head = societe_prepare_head($object);
103
104	$isCustomer = ($object->client == 1 || $object->client == 3);
105	$isSupplier = $object->fournisseur == 1;
106
107	print '<form method="POST" action="remise.php?id='.$object->id.'">';
108	print '<input type="hidden" name="token" value="'.newToken().'">';
109	print '<input type="hidden" name="action" value="setremise">';
110	print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
111
112	print dol_get_fiche_head($head, 'relativediscount', $langs->trans("ThirdParty"), -1, 'company');
113
114	dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
115
116	print '<div class="fichecenter">';
117
118	print '<div class="underbanner clearboth"></div>';
119
120	if (!$isCustomer && !$isSupplier) {
121		print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
122
123		print dol_get_fiche_end();
124
125		print '</form>';
126
127		// End of page
128		llxFooter();
129		$db->close();
130		exit;
131	}
132
133	print '<table class="border centpercent">';
134
135	if ($isCustomer) {
136		// Customer discount
137		print '<tr><td class="titlefield">';
138		print $langs->trans("CustomerRelativeDiscount").'</td><td>'.price2num($object->remise_percent)."%</td></tr>";
139	}
140
141	if ($isSupplier) {
142		// Supplier discount
143		print '<tr><td class="titlefield">';
144		print $langs->trans("SupplierRelativeDiscount").'</td><td>'.price2num($object->remise_supplier_percent)."%</td></tr>";
145	}
146
147	print '</table>';
148	print '<br>';
149
150	print load_fiche_titre($langs->trans("NewRelativeDiscount"), '', '');
151
152	print '<div class="underbanner clearboth"></div>';
153
154	/*if (! ($isCustomer && $isSupplier))
155	{
156		if ($isCustomer && ! $isSupplier) {
157			print '<input type="hidden" name="discount_type" value="0" />';
158		}
159		if (! $isCustomer && $isSupplier) {
160			print '<input type="hidden" name="discount_type" value="1" />';
161		}
162	}*/
163
164	print '<table class="border centpercent">';
165
166	if ($isCustomer || $isSupplier) {
167		// Discount type
168		print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td><td>';
169		if ($isCustomer) {
170			print '<input type="radio" name="discount_type" id="discount_type_0" '.(GETPOSTISSET('discount_type') ? (GETPOST('discount_type', 'int') == 0 ? ' checked' : '') : ' checked').' value="0"> <label for="discount_type_0">'.$langs->trans('Customer').'</label>';
171		}
172		if ($isSupplier) {
173			print ' <input type="radio" name="discount_type" id="discount_type_1"'.(GETPOSTISSET('discount_type') ? (GETPOST('discount_type', 'int') ? ' checked' : '') : ($isCustomer ? '' : ' checked')).' value="1"> <label for="discount_type_1">'.$langs->trans('Supplier').'</label>';
174		}
175		print '</td></tr>';
176	}
177
178	// New value
179	print '<tr><td class="titlefield fieldrequired">';
180	print $langs->trans("NewValue").'</td><td><input type="text" size="5" name="remise" value="'.dol_escape_htmltag(GETPOST("remise")).'">%</td></tr>';
181
182	// Motif/Note
183	print '<tr><td class="fieldrequired">';
184	print $langs->trans("NoteReason").'</td><td><input type="text" size="60" name="note" value="'.dol_escape_htmltag(GETPOST("note", "alphanohtml")).'"></td></tr>';
185
186	print "</table>";
187
188	print '</div>';
189
190	print dol_get_fiche_end();
191
192	print '<div class="center">';
193	print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
194	if (!empty($backtopage)) {
195		print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
196		print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
197	}
198	print '</div>';
199
200	print "</form>";
201
202	print '<br>';
203
204	if ($isCustomer) {
205		if ($isSupplier) {
206			print '<div class="fichecenter">';
207			print '<div class="fichehalfleft">';
208			print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
209		}
210
211		/*
212		 * List log of all customer percent discounts
213		 */
214		$sql = "SELECT rc.rowid, rc.remise_client as remise_percent, rc.note, rc.datec as dc,";
215		$sql .= " u.login, u.rowid as user_id";
216		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise as rc, ".MAIN_DB_PREFIX."user as u";
217		$sql .= " WHERE rc.fk_soc = ".$object->id;
218		$sql .= " AND rc.entity IN (".getEntity('discount').")";
219		$sql .= " AND u.rowid = rc.fk_user_author";
220		$sql .= " ORDER BY rc.datec DESC";
221
222		$resql = $db->query($sql);
223		if ($resql) {
224			print '<table class="noborder centpercent">';
225			$tag = !$tag;
226			print '<tr class="liste_titre">';
227			print '<td width="160">'.$langs->trans("Date").'</td>';
228			print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
229			print '<td class="left">'.$langs->trans("NoteReason").'</td>';
230			print '<td class="center">'.$langs->trans("User").'</td>';
231			print '</tr>';
232			$num = $db->num_rows($resql);
233			if ($num > 0) {
234				$i = 0;
235				while ($i < $num) {
236					$obj = $db->fetch_object($resql);
237					print '<tr class="oddeven">';
238					print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
239					print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
240					print '<td class="left">'.$obj->note.'</td>';
241					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>';
242					print '</tr>';
243					$i++;
244				}
245			} else {
246				print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
247			}
248			$db->free($resql);
249			print "</table>";
250		} else {
251			dol_print_error($db);
252		}
253	}
254
255	if ($isSupplier) {
256		if ($isCustomer) {
257			print '</div>'; // class="fichehalfleft"
258			print '<div class="fichehalfright">';
259			print '<div class="ficheaddleft">';
260			print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
261		}
262
263		/*
264		 * List log of all supplier percent discounts
265		 */
266		$sql = "SELECT rc.rowid, rc.remise_supplier as remise_percent, rc.note, rc.datec as dc,";
267		$sql .= " u.login, u.rowid as user_id";
268		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_supplier as rc, ".MAIN_DB_PREFIX."user as u";
269		$sql .= " WHERE rc.fk_soc = ".$object->id;
270		$sql .= " AND rc.entity IN (".getEntity('discount').")";
271		$sql .= " AND u.rowid = rc.fk_user_author";
272		$sql .= " ORDER BY rc.datec DESC";
273
274		$resql = $db->query($sql);
275		if ($resql) {
276			print '<table class="noborder centpercent">';
277			$tag = !$tag;
278			print '<tr class="liste_titre">';
279			print '<td width="160">'.$langs->trans("Date").'</td>';
280			print '<td width="160" align="center">'.$langs->trans("CustomerRelativeDiscountShort").'</td>';
281			print '<td class="left">'.$langs->trans("NoteReason").'</td>';
282			print '<td class="center">'.$langs->trans("User").'</td>';
283			print '</tr>';
284			$num = $db->num_rows($resql);
285			if ($num > 0) {
286				$i = 0;
287				while ($i < $num) {
288					$obj = $db->fetch_object($resql);
289					print '<tr class="oddeven">';
290					print '<td>'.dol_print_date($db->jdate($obj->dc), "dayhour").'</td>';
291					print '<td class="center">'.price2num($obj->remise_percent).'%</td>';
292					print '<td class="left">'.$obj->note.'</td>';
293					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>';
294					print '</tr>';
295					$i++;
296				}
297			} else {
298				print '<tr><td colspan="8" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
299			}
300			$db->free($resql);
301			print "</table>";
302		} else {
303			dol_print_error($db);
304		}
305
306		if ($isCustomer) {
307			print '</div>'; // class="ficheaddleft"
308			print '</div>'; // class="fichehalfright"
309			print '</div>'; // class="fichecenter"
310		}
311	}
312}
313
314// End of page
315llxFooter();
316$db->close();
317