1<?php
2/* Copyright (C) 2001-2004 Rodolphe Quiedeville        <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2019 Laurent Destailleur         <eldy@users.sourceforge.net>
4 * Copyright (C) 2008      Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
5 * Copyright (C) 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 3 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 *	    \file       htdocs/comm/remx.php
23 *      \ingroup    societe
24 *		\brief      Page to edit absolute discounts for a customer
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
30require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('orders', 'bills', 'companies'));
35
36$id = GETPOST('id', 'int');
37
38$action = GETPOST('action', 'aZ09');
39$backtopage = GETPOST('backtopage', 'alpha');
40
41// Security check
42$socid = GETPOST('id', 'int') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
43if ($user->socid > 0)
44{
45	$socid = $user->socid;
46}
47
48
49/*
50 * Actions
51 */
52
53if (GETPOST('cancel', 'alpha') && !empty($backtopage))
54{
55	 header("Location: ".$backtopage);
56	 exit;
57}
58
59if ($action == 'confirm_split' && GETPOST("confirm", "alpha") == 'yes')
60{
61	//if ($user->rights->societe->creer)
62	//if ($user->rights->facture->creer)
63
64	$amount_ttc_1 = GETPOST('amount_ttc_1', 'alpha');
65	$amount_ttc_1 = price2num($amount_ttc_1);
66	$amount_ttc_2 = GETPOST('amount_ttc_2', 'alpha');
67	$amount_ttc_2 = price2num($amount_ttc_2);
68
69	$error = 0;
70	$remid = (GETPOST("remid", 'int') ? GETPOST("remid", 'int') : 0);
71	$discount = new DiscountAbsolute($db);
72	$res = $discount->fetch($remid);
73	if (!($res > 0))
74	{
75		$error++;
76		setEventMessages($langs->trans("ErrorFailedToLoadDiscount"), null, 'errors');
77	}
78	if (!$error && price2num($amount_ttc_1 + $amount_ttc_2) != $discount->amount_ttc)
79	{
80		$error++;
81		setEventMessages($langs->trans("TotalOfTwoDiscountMustEqualsOriginal"), null, 'errors');
82	}
83	if (!$error && $discount->fk_facture_line)
84	{
85		$error++;
86		setEventMessages($langs->trans("ErrorCantSplitAUsedDiscount"), null, 'errors');
87	}
88	if (!$error)
89	{
90		$newdiscount1 = new DiscountAbsolute($db);
91		$newdiscount2 = new DiscountAbsolute($db);
92		$newdiscount1->fk_facture_source = $discount->fk_facture_source;
93		$newdiscount2->fk_facture_source = $discount->fk_facture_source;
94		$newdiscount1->fk_facture = $discount->fk_facture;
95		$newdiscount2->fk_facture = $discount->fk_facture;
96		$newdiscount1->fk_facture_line = $discount->fk_facture_line;
97		$newdiscount2->fk_facture_line = $discount->fk_facture_line;
98		$newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
99		$newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
100		$newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
101		$newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
102		$newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
103		$newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
104		if ($discount->description == '(CREDIT_NOTE)' || $discount->description == '(DEPOSIT)')
105		{
106			$newdiscount1->description = $discount->description;
107			$newdiscount2->description = $discount->description;
108		} else {
109			$newdiscount1->description = $discount->description.' (1)';
110			$newdiscount2->description = $discount->description.' (2)';
111		}
112
113		$newdiscount1->fk_user = $discount->fk_user;
114		$newdiscount2->fk_user = $discount->fk_user;
115		$newdiscount1->fk_soc = $discount->fk_soc;
116		$newdiscount2->fk_soc = $discount->fk_soc;
117		$newdiscount1->discount_type = $discount->discount_type;
118		$newdiscount2->discount_type = $discount->discount_type;
119		$newdiscount1->datec = $discount->datec;
120		$newdiscount2->datec = $discount->datec;
121		$newdiscount1->tva_tx = $discount->tva_tx;
122		$newdiscount2->tva_tx = $discount->tva_tx;
123		$newdiscount1->vat_src_code = $discount->vat_src_code;
124		$newdiscount2->vat_src_code = $discount->vat_src_code;
125		$newdiscount1->amount_ttc = $amount_ttc_1;
126		$newdiscount2->amount_ttc = price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
127		$newdiscount1->amount_ht = price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
128		$newdiscount2->amount_ht = price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
129		$newdiscount1->amount_tva = price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
130		$newdiscount2->amount_tva = price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
131
132		$newdiscount1->multicurrency_amount_ttc = $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
133		$newdiscount2->multicurrency_amount_ttc = price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
134		$newdiscount1->multicurrency_amount_ht = price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
135		$newdiscount2->multicurrency_amount_ht = price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
136		$newdiscount1->multicurrency_amount_tva = price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
137		$newdiscount2->multicurrency_amount_tva = price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
138
139		$db->begin();
140		$discount->fk_facture_source = 0; // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_facture_source
141		// This is to delete only the require record (that we will recreate with two records) and not all family with same fk_invoice_supplier_source
142		$discount->fk_invoice_supplier_source = 0;
143		$res = $discount->delete($user);
144		$newid1 = $newdiscount1->create($user);
145		$newid2 = $newdiscount2->create($user);
146		if ($res > 0 && $newid1 > 0 && $newid2 > 0)
147		{
148			$db->commit();
149			header("Location: ".$_SERVER["PHP_SELF"].'?id='.$id.($backtopage ? '&backtopage='.urlencode($backtopage) : '')); // To avoid pb whith back
150			exit;
151		} else {
152			$db->rollback();
153		}
154	}
155}
156
157if ($action == 'setremise' && $user->rights->societe->creer)
158{
159	//if ($user->rights->societe->creer)
160	//if ($user->rights->facture->creer)
161
162	$amount_ht = price2num(GETPOST('amount_ht', 'alpha'));
163	$desc = GETPOST('desc', 'alpha');
164	$tva_tx = GETPOST('tva_tx', 'alpha');
165	$discount_type = !empty($_POST['discount_type']) ?GETPOST('discount_type', 'alpha') : 0;
166
167	if ($amount_ht > 0)
168	{
169		$error = 0;
170		if (empty($desc))
171		{
172			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ReasonDiscount")), null, 'errors');
173			$error++;
174		}
175
176		if (!$error)
177		{
178			$soc = new Societe($db);
179			$soc->fetch($id);
180			$discountid = $soc->set_remise_except($amount_ht, $user, $desc, $tva_tx, $discount_type);
181
182			if ($discountid > 0)
183			{
184				if (!empty($backtopage))
185				{
186					header("Location: ".$backtopage.'&discountid='.$discountid);
187					exit;
188				} else {
189					header("Location: remx.php?id=".$id);
190					exit;
191				}
192			} else {
193				$error++;
194				setEventMessages($soc->error, $soc->errors, 'errors');
195			}
196		}
197	} else {
198		setEventMessages($langs->trans("ErrorFieldFormat", $langs->transnoentitiesnoconv("AmountHT")), null, 'errors');
199	}
200}
201
202if (GETPOST('action', 'aZ09') == 'confirm_remove' && GETPOST("confirm") == 'yes')
203{
204	//if ($user->rights->societe->creer)
205	//if ($user->rights->facture->creer)
206
207	$db->begin();
208
209	$discount = new DiscountAbsolute($db);
210	$result = $discount->fetch(GETPOST("remid"));
211	$result = $discount->delete($user);
212	if ($result > 0)
213	{
214		$db->commit();
215		header("Location: ".$_SERVER["PHP_SELF"].'?id='.$id); // To avoid pb whith back
216		exit;
217	} else {
218		setEventMessages($discount->error, $discount->errors, 'errors');
219		$db->rollback();
220	}
221}
222
223
224/*
225 * View
226 */
227
228$form = new Form($db);
229$facturestatic = new Facture($db);
230$facturefournstatic = new FactureFournisseur($db);
231
232llxHeader('', $langs->trans("GlobalDiscount"));
233
234if ($socid > 0)
235{
236	// On recupere les donnees societes par l'objet
237	$object = new Societe($db);
238	$object->fetch($socid);
239
240	$isCustomer = $object->client == 1 || $object->client == 3;
241	$isSupplier = $object->fournisseur == 1;
242
243	/*
244	 * Display tabs
245	 */
246	$head = societe_prepare_head($object);
247
248	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
249	print '<input type="hidden" name="token" value="'.newToken().'">';
250	print '<input type="hidden" name="action" value="setremise">';
251	print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
252
253	print dol_get_fiche_head($head, 'absolutediscount', $langs->trans("ThirdParty"), -1, 'company');
254
255	dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
256
257	print '<div class="fichecenter">';
258
259	print '<div class="underbanner clearboth"></div>';
260
261	if (!$isCustomer && !$isSupplier) {
262		print '<p class="opacitymedium">'.$langs->trans('ThirdpartyIsNeitherCustomerNorClientSoCannotHaveDiscounts').'</p>';
263
264		print dol_get_fiche_end();
265
266		print '</form>';
267
268		llxFooter();
269		$db->close();
270		exit;
271	}
272
273
274	print '<table class="border centpercent tableforfield borderbottom">';
275
276	if ($isCustomer) {	// Calcul avoirs client en cours
277		$remise_all = $remise_user = 0;
278		$sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
279		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
280		$sql .= " WHERE rc.fk_soc = ".$object->id;
281		$sql .= " AND rc.entity = ".$conf->entity;
282		$sql .= " AND discount_type = 0"; // Exclude supplier discounts
283		$sql .= " AND (fk_facture_line IS NULL AND fk_facture IS NULL)";
284		$sql .= " GROUP BY rc.fk_user";
285		$resql = $db->query($sql);
286		if ($resql)
287		{
288			$obj = $db->fetch_object($resql);
289			$remise_all += $obj->amount;
290			if ($obj->fk_user == $user->id) $remise_user += $obj->amount;
291		} else {
292			dol_print_error($db);
293		}
294
295		print '<tr><td class="titlefield">'.$langs->trans("CustomerAbsoluteDiscountAllUsers").'</td>';
296		print '<td>'.$remise_all.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
297
298		if (!empty($user->fk_soc))    // No need to show this for external users
299		{
300			print '<tr><td>'.$langs->trans("CustomerAbsoluteDiscountMy").'</td>';
301			print '<td>'.$remise_user.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
302		}
303	}
304
305	if ($isSupplier) {
306		// Calcul avoirs fournisseur en cours
307		$remise_all = $remise_user = 0;
308		$sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user";
309		$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
310		$sql .= " WHERE rc.fk_soc = ".$object->id;
311		$sql .= " AND rc.entity = ".$conf->entity;
312		$sql .= " AND discount_type = 1"; // Exclude customer discounts
313		$sql .= " AND (fk_invoice_supplier_line IS NULL AND fk_invoice_supplier IS NULL)";
314		$sql .= " GROUP BY rc.fk_user";
315		$resql = $db->query($sql);
316		if ($resql)
317		{
318			$obj = $db->fetch_object($resql);
319			$remise_all += $obj->amount;
320			if ($obj->fk_user == $user->id) $remise_user += $obj->amount;
321		} else {
322			dol_print_error($db);
323		}
324
325		print '<tr><td class="titlefield">'.$langs->trans("SupplierAbsoluteDiscountAllUsers").'</td>';
326		print '<td>'.$remise_all.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
327
328		if (!empty($user->fk_soc))    // No need to show this for external users
329		{
330			print '<tr><td>'.$langs->trans("SupplierAbsoluteDiscountMy").'</td>';
331			print '<td>'.$remise_user.'&nbsp;'.$langs->trans("Currency".$conf->currency).' '.$langs->trans("HT").'</td></tr>';
332		}
333	}
334
335	print '</table>';
336
337	print '</div>';
338
339	print dol_get_fiche_end();
340
341
342	if ($user->rights->societe->creer)
343	{
344		print '<br>';
345
346		print load_fiche_titre($langs->trans("NewGlobalDiscount"), '', '');
347
348
349		if ($isCustomer && !$isSupplier) {
350			print '<input type="hidden" name="discount_type" value="0" />';
351		}
352
353		if (!$isCustomer && $isSupplier) {
354			print '<input type="hidden" name="discount_type" value="1" />';
355		}
356
357		print dol_get_fiche_head();
358
359		print '<table class="border centpercent">';
360		if ($isCustomer && $isSupplier) {
361			print '<tr><td class="titlefield fieldrequired">'.$langs->trans('DiscountType').'</td>';
362			print '<td><input type="radio" name="discount_type" id="discount_type_0" checked="checked" value="0"/> <label for="discount_type_0">'.$langs->trans('Customer').'</label>';
363			print ' &nbsp; <input type="radio" name="discount_type" id="discount_type_1" value="1"/> <label for="discount_type_1">'.$langs->trans('Supplier').'</label>';
364			print '</td></tr>';
365		}
366		print '<tr><td class="titlefield fieldrequired">'.$langs->trans("AmountHT").'</td>';
367		print '<td><input type="text" size="5" name="amount_ht" value="'.price2num(GETPOST("amount_ht")).'">';
368		print '<span class="hideonsmartphone">&nbsp;'.$langs->trans("Currency".$conf->currency).'</span></td></tr>';
369		print '<tr><td>'.$langs->trans("VAT").'</td>';
370		print '<td>';
371		print $form->load_tva('tva_tx', GETPOSTISSET('tva_tx') ? GETPOST('tva_tx', 'alpha') : 0, $mysoc, $object, 0, 0, '', 0, 1);
372		print '</td></tr>';
373		print '<tr><td class="fieldrequired" >'.$langs->trans("NoteReason").'</td>';
374		print '<td><input type="text" class="quatrevingtpercent" name="desc" value="'.GETPOST('desc', 'alphanohtml').'"></td></tr>';
375
376		print "</table>";
377
378		print dol_get_fiche_end();
379	}
380
381	if ($user->rights->societe->creer)
382	{
383		print '<div class="center">';
384		print '<input type="submit" class="button" name="submit" value="'.$langs->trans("AddGlobalDiscount").'">';
385		if (!empty($backtopage))
386		{
387			print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
388			print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
389		}
390		print '</div>';
391	}
392
393	print '</form>';
394
395
396	print '<br>';
397
398	if ($_GET['action'] == 'remove')
399	{
400		print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.GETPOST('remid'), $langs->trans('RemoveDiscount'), $langs->trans('ConfirmRemoveDiscount'), 'confirm_remove', '', 0, 1);
401	}
402
403
404	/*
405	 * List not consumed available credits (= linked to no invoice and no invoice line)
406	 */
407
408	print load_fiche_titre($langs->trans("DiscountStillRemaining"));
409
410	if ($isCustomer) {
411		if ($isSupplier) {
412			print '<div class="fichecenter">';
413			print '<div class="fichehalfleft fichehalfleft-lg">';
414			print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
415		}
416
417		$sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
418		$sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
419		$sql .= " rc.datec as dc, rc.description,";
420		$sql .= " rc.fk_facture_source,";
421		$sql .= " u.login, u.rowid as user_id,";
422		$sql .= " fa.ref as ref, fa.type as type";
423		$sql .= " FROM  ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
424		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
425		$sql .= " WHERE rc.fk_soc = ".$object->id;
426		$sql .= " AND rc.entity = ".$conf->entity;
427		$sql .= " AND u.rowid = rc.fk_user";
428		$sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
429		$sql .= " AND (rc.fk_facture_line IS NULL AND rc.fk_facture IS NULL)";
430		$sql .= " ORDER BY rc.datec DESC";
431
432		$resql = $db->query($sql);
433		if ($resql)
434		{
435			print '<div class="div-table-responsive-no-min">';
436			print '<table width="100%" class="noborder">';
437			print '<tr class="liste_titre">';
438			print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
439			print '<td>'.$langs->trans("ReasonDiscount").'</td>';
440			print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
441			print '<td class="right">'.$langs->trans("AmountHT").'</td>';
442			if (!empty($conf->multicurrency->enabled))
443			{
444				print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
445			}
446			print '<td class="right">'.$langs->trans("VATRate").'</td>';
447			print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
448			if (!empty($conf->multicurrency->enabled))
449			{
450				print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
451			}
452			print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
453			print '<td width="50">&nbsp;</td>';
454			print '</tr>';
455
456			$showconfirminfo = array();
457
458			$i = 0;
459			$num = $db->num_rows($resql);
460			if ($num > 0)
461			{
462				while ($i < $num)
463				{
464					$obj = $db->fetch_object($resql);
465
466					print '<tr class="oddeven">';
467					print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
468					if (preg_match('/\(CREDIT_NOTE\)/', $obj->description))
469					{
470						print '<td class="minwidth100">';
471						$facturestatic->id = $obj->fk_facture_source;
472						$facturestatic->ref = $obj->ref;
473						$facturestatic->type = $obj->type;
474						print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
475						print '</td>';
476					} elseif (preg_match('/\(DEPOSIT\)/', $obj->description))
477					{
478						print '<td class="minwidth100">';
479						$facturestatic->id = $obj->fk_facture_source;
480						$facturestatic->ref = $obj->ref;
481						$facturestatic->type = $obj->type;
482						print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
483						print '</td>';
484					} elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description))
485					{
486						print '<td class="minwidth100">';
487						$facturestatic->id = $obj->fk_facture_source;
488						$facturestatic->ref = $obj->ref;
489						$facturestatic->type = $obj->type;
490						print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $obj->description).' '.$facturestatic->getNomURl(1);
491						print '</td>';
492					} else {
493						print '<td class="minwidth100">';
494						print $obj->description;
495						print '</td>';
496					}
497					print '<td class="nowrap">'.$langs->trans("NotConsumed").'</td>';
498					print '<td class="right">'.price($obj->amount_ht).'</td>';
499					if (!empty($conf->multicurrency->enabled))
500					{
501						print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
502					}
503					print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
504					print '<td class="right">'.price($obj->amount_ttc).'</td>';
505					if (!empty($conf->multicurrency->enabled))
506					{
507						print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
508					}
509					print '<td class="center">';
510					print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
511					print '</td>';
512					if ($user->rights->societe->creer || $user->rights->facture->creer)
513					{
514						print '<td class="center nowrap">';
515						print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=split&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_split($langs->trans("SplitDiscount")).'</a>';
516						print '<a class="reposition marginleftonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=remove&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_delete($langs->trans("RemoveDiscount")).'</a>';
517						print '</td>';
518					} else print '<td>&nbsp;</td>';
519					print '</tr>';
520
521					if ($_GET["action"] == 'split' && GETPOST('remid') == $obj->rowid)
522					{
523						$showconfirminfo['rowid'] = $obj->rowid;
524						$showconfirminfo['amount_ttc'] = $obj->amount_ttc;
525					}
526					$i++;
527				}
528			} else {
529				$colspan = 8;
530				if (!empty($conf->multicurrency->enabled)) $colspan += 2;
531				print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
532			}
533			$db->free($resql);
534			print "</table>";
535			print '</div>';
536
537			if (count($showconfirminfo))
538			{
539				$amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
540				$amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
541				$formquestion = array(
542					'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
543					array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
544					array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
545				);
546				$langs->load("dict");
547				print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, '', 0);
548			}
549		} else {
550			dol_print_error($db);
551		}
552	}
553
554	if ($isSupplier) {
555		if ($isCustomer) {
556			print '</div>'; // class="fichehalfleft"
557			print '<div class="fichehalfright fichehalfright-lg">';
558			print '<div class="ficheaddleft">';
559			print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
560		}
561
562		/*
563		 * Liste remises fixes fournisseur restant en cours (= liees a aucune facture ni ligne de facture)
564		 */
565		$sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
566		$sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
567		$sql .= " rc.datec as dc, rc.description,";
568		$sql .= " rc.fk_invoice_supplier_source,";
569		$sql .= " u.login, u.rowid as user_id,";
570		$sql .= " fa.ref, fa.type as type";
571		$sql .= " FROM  ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc";
572		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
573		$sql .= " WHERE rc.fk_soc = ".$object->id;
574		$sql .= " AND rc.entity = ".$conf->entity;
575		$sql .= " AND u.rowid = rc.fk_user";
576		$sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
577		$sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
578		$sql .= " ORDER BY rc.datec DESC";
579
580		$resql = $db->query($sql);
581		if ($resql)
582		{
583			print '<div class="div-table-responsive-no-min">';
584			print '<table width="100%" class="noborder">';
585			print '<tr class="liste_titre">';
586			print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
587			print '<td>'.$langs->trans("ReasonDiscount").'</td>';
588			print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
589			print '<td class="right">'.$langs->trans("AmountHT").'</td>';
590			if (!empty($conf->multicurrency->enabled))
591			{
592				print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
593			}
594			print '<td class="right">'.$langs->trans("VATRate").'</td>';
595			print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
596			if (!empty($conf->multicurrency->enabled))
597			{
598				print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
599			}
600			print '<td width="100" class="center">'.$langs->trans("DiscountOfferedBy").'</td>';
601			print '<td width="50">&nbsp;</td>';
602			print '</tr>';
603
604			$showconfirminfo = array();
605
606			$i = 0;
607			$num = $db->num_rows($resql);
608			if ($num > 0)
609			{
610				while ($i < $num)
611				{
612					$obj = $db->fetch_object($resql);
613
614					print '<tr class="oddeven">';
615					print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
616					if (preg_match('/\(CREDIT_NOTE\)/', $obj->description))
617					{
618						print '<td class="minwidth100">';
619						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
620						$facturefournstatic->ref = $obj->ref;
621						$facturefournstatic->type = $obj->type;
622						print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
623						print '</td>';
624					} elseif (preg_match('/\(DEPOSIT\)/', $obj->description))
625					{
626						print '<td class="minwidth100">';
627						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
628						$facturefournstatic->ref = $obj->ref;
629						$facturefournstatic->type = $obj->type;
630						print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
631						print '</td>';
632					} elseif (preg_match('/\(EXCESS PAID\)/', $obj->description))
633					{
634						print '<td class="minwidth100">';
635						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
636						$facturefournstatic->ref = $obj->ref;
637						$facturefournstatic->type = $obj->type;
638						print preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $obj->description).' '.$facturefournstatic->getNomURl(1);
639						print '</td>';
640					} else {
641						print '<td class="minwidth100">';
642						print $obj->description;
643						print '</td>';
644					}
645					print '<td class="nowrap">'.$langs->trans("NotConsumed").'</td>';
646					print '<td class="right">'.price($obj->amount_ht).'</td>';
647					if (!empty($conf->multicurrency->enabled))
648					{
649						print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
650					}
651					print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
652					print '<td class="right">'.price($obj->amount_ttc).'</td>';
653					if (!empty($conf->multicurrency->enabled))
654					{
655						print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
656					}
657					print '<td class="center">';
658					print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
659					print '</td>';
660					if ($user->rights->societe->creer || $user->rights->facture->creer)
661					{
662						print '<td class="center nowrap">';
663						print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=split&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_split($langs->trans("SplitDiscount")).'</a>';
664						print '<a class="reposition marginleftonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=remove&remid='.$obj->rowid.($backtopage ? '&backtopage='.urlencode($backtopage) : '').'">'.img_delete($langs->trans("RemoveDiscount")).'</a>';
665						print '</td>';
666					} else print '<td>&nbsp;</td>';
667					print '</tr>';
668
669					if ($_GET["action"] == 'split' && GETPOST('remid') == $obj->rowid)
670					{
671						$showconfirminfo['rowid'] = $obj->rowid;
672						$showconfirminfo['amount_ttc'] = $obj->amount_ttc;
673					}
674					$i++;
675				}
676			} else {
677				$colspan = 8;
678				if (!empty($conf->multicurrency->enabled)) $colspan += 2;
679				print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
680			}
681			$db->free($resql);
682			print "</table>";
683			print '</div>';
684
685			if (count($showconfirminfo))
686			{
687				$amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT');
688				$amount2 = ($showconfirminfo['amount_ttc'] - $amount1);
689				$formquestion = array(
690					'text' => $langs->trans('TypeAmountOfEachNewDiscount'),
691					array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'),
692					array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5')
693				);
694				$langs->load("dict");
695				print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, 0, 0);
696			}
697		} else {
698			dol_print_error($db);
699		}
700
701		if ($isCustomer) {
702			print '</div>'; // class="ficheaddleft"
703			print '</div>'; // class="fichehalfright"
704			print '</div>'; // class="fichecenter"
705		}
706	}
707
708	print '<div class="clearboth"></div><br>';
709
710	/*
711	 * List discount consumed (=liees a une ligne de facture ou facture)
712	 */
713
714	print load_fiche_titre($langs->trans("DiscountAlreadyCounted"));
715
716	if ($isCustomer) {
717		if ($isSupplier) {
718			print '<div class="fichecenter">';
719			print '<div class="fichehalfleft fichehalfleft-lg">';
720			print load_fiche_titre($langs->trans("CustomerDiscounts"), '', '');
721		}
722
723		// Discount linked to invoice lines
724		$sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
725		$sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
726		$sql .= " rc.datec as dc, rc.description, rc.fk_facture_line, rc.fk_facture_source,";
727		$sql .= " u.login, u.rowid as user_id,";
728		$sql .= " f.rowid as invoiceid, f.ref,";
729		$sql .= " fa.ref as invoice_source_ref, fa.type as type";
730		$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
731		$sql .= " , ".MAIN_DB_PREFIX."user as u";
732		$sql .= " , ".MAIN_DB_PREFIX."facturedet as fc";
733		$sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
734		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
735		$sql .= " WHERE rc.fk_soc =".$object->id;
736		$sql .= " AND rc.fk_facture_line = fc.rowid";
737		$sql .= " AND fc.fk_facture = f.rowid";
738		$sql .= " AND rc.fk_user = u.rowid";
739		$sql .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
740		$sql .= " ORDER BY dc DESC";
741		//$sql.= " UNION ";
742		// Discount linked to invoices
743		$sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
744		$sql2 .= " rc.datec as dc, rc.description, rc.fk_facture_line, rc.fk_facture,";
745		$sql2 .= " rc.fk_facture_source,";
746		$sql2 .= " u.login, u.rowid as user_id,";
747		$sql2 .= " f.rowid as invoiceid, f.ref,";
748		$sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
749		$sql2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
750		$sql2 .= " , ".MAIN_DB_PREFIX."user as u";
751		$sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
752		$sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid";
753		$sql2 .= " WHERE rc.fk_soc =".$object->id;
754		$sql2 .= " AND rc.fk_facture = f.rowid";
755		$sql2 .= " AND rc.fk_user = u.rowid";
756		$sql2 .= " AND rc.discount_type = 0"; // Eliminate supplier discounts
757		$sql2 .= " ORDER BY dc DESC";
758
759		$resql = $db->query($sql);
760		$resql2 = null;
761		if ($resql) $resql2 = $db->query($sql2);
762		if ($resql2)
763		{
764			print '<div class="div-table-responsive-no-min">';
765			print '<table class="noborder centpercent">';
766			print '<tr class="liste_titre">';
767			print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
768			print '<td>'.$langs->trans("ReasonDiscount").'</td>';
769			print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
770			print '<td class="right">'.$langs->trans("AmountHT").'</td>';
771			if (!empty($conf->multicurrency->enabled))
772			{
773				print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
774			}
775			print '<td class="right">'.$langs->trans("VATRate").'</td>';
776			print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
777			if (!empty($conf->multicurrency->enabled))
778			{
779				print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
780			}
781			print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
782			print '<td width="50">&nbsp;</td>';
783			print '</tr>';
784
785			$tab_sqlobj = array();
786			$tab_sqlobjOrder = array();
787			$num = $db->num_rows($resql);
788			if ($num > 0)
789			{
790				for ($i = 0; $i < $num; $i++)
791				{
792					$sqlobj = $db->fetch_object($resql);
793					$tab_sqlobj[] = $sqlobj;
794					$tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
795				}
796			}
797			$db->free($resql);
798
799			$num = $db->num_rows($resql2);
800			for ($i = 0; $i < $num; $i++)
801			{
802				$sqlobj = $db->fetch_object($resql2);
803				$tab_sqlobj[] = $sqlobj;
804				$tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
805			}
806			$db->free($resql2);
807			array_multisort($tab_sqlobjOrder, SORT_DESC, $tab_sqlobj);
808
809			$num = count($tab_sqlobj);
810			if ($num > 0)
811			{
812				$i = 0;
813				while ($i < $num)
814				{
815					$obj = array_shift($tab_sqlobj);
816					print '<tr class="oddeven">';
817					print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
818					if (preg_match('/\(CREDIT_NOTE\)/', $obj->description))
819					{
820						print '<td class="minwidth100">';
821						$facturestatic->id = $obj->fk_facture_source;
822						$facturestatic->ref = $obj->invoice_source_ref;
823						$facturestatic->type = $obj->type;
824						print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturestatic->getNomURl(1);
825						print '</td>';
826					} elseif (preg_match('/\(DEPOSIT\)/', $obj->description))
827					{
828						print '<td class="minwidth100">';
829						$facturestatic->id = $obj->fk_facture_source;
830						$facturestatic->ref = $obj->invoice_source_ref;
831						$facturestatic->type = $obj->type;
832						print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturestatic->getNomURl(1);
833						print '</td>';
834					} elseif (preg_match('/\(EXCESS RECEIVED\)/', $obj->description))
835					{
836						print '<td class="minwidth100">';
837						$facturestatic->id = $obj->fk_facture_source;
838						$facturestatic->ref = $obj->invoice_source_ref;
839						$facturestatic->type = $obj->type;
840						print preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("Invoice"), $obj->description).' '.$facturestatic->getNomURl(1);
841						print '</td>';
842					} else {
843						print '<td class="minwidth100">';
844						print $obj->description;
845						print '</td>';
846					}
847					print '<td class="left nowrap">';
848					if ($obj->invoiceid)
849					{
850						print '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
851					}
852					print '</td>';
853					print '<td class="right">'.price($obj->amount_ht).'</td>';
854					if (!empty($conf->multicurrency->enabled))
855					{
856						print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
857					}
858					print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
859					print '<td class="right">'.price($obj->amount_ttc).'</td>';
860					if (!empty($conf->multicurrency->enabled))
861					{
862						print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
863					}
864					print '<td class="center">';
865					print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
866					print '</td>';
867					print '<td>&nbsp;</td>';
868					print '</tr>';
869					$i++;
870				}
871			} else {
872				$colspan = 8;
873				if (!empty($conf->multicurrency->enabled)) $colspan += 2;
874				print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
875			}
876
877			print "</table>";
878			print '</div>';
879		} else {
880			dol_print_error($db);
881		}
882	}
883
884	if ($isSupplier) {
885		if ($isCustomer) {
886			print '</div>'; // class="fichehalfleft"
887			print '<div class="fichehalfright fichehalfright-lg">';
888			print '<div class="ficheaddleft">';
889			print load_fiche_titre($langs->trans("SupplierDiscounts"), '', '');
890		}
891
892		// Discount linked to invoice lines
893		$sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
894		$sql .= " rc.multicurrency_amount_ht, rc.multicurrency_amount_tva, rc.multicurrency_amount_ttc,";
895		$sql .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier_line, rc.fk_invoice_supplier,";
896		$sql .= " rc.fk_invoice_supplier_source,";
897		$sql .= " u.login, u.rowid as user_id,";
898		$sql .= " f.rowid as invoiceid, f.ref as ref,";
899		$sql .= " fa.ref as invoice_source_ref, fa.type as type";
900		$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
901		$sql .= " , ".MAIN_DB_PREFIX."user as u";
902		$sql .= " , ".MAIN_DB_PREFIX."facture_fourn_det as fc";
903		$sql .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
904		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
905		$sql .= " WHERE rc.fk_soc =".$object->id;
906		$sql .= " AND rc.fk_invoice_supplier_line = fc.rowid";
907		$sql .= " AND fc.fk_facture_fourn = f.rowid";
908		$sql .= " AND rc.fk_user = u.rowid";
909		$sql .= " AND rc.discount_type = 1"; // Eliminate customer discounts
910		$sql .= " ORDER BY dc DESC";
911		//$sql.= " UNION ";
912		// Discount linked to invoices
913		$sql2 = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx, rc.vat_src_code,";
914		$sql2 .= " rc.datec as dc, rc.description, rc.fk_invoice_supplier_line, rc.fk_invoice_supplier,";
915		$sql2 .= " rc.fk_invoice_supplier_source,";
916		$sql2 .= " u.login, u.rowid as user_id,";
917		$sql2 .= " f.rowid as invoiceid, f.ref as ref,";
918		$sql2 .= " fa.ref as invoice_source_ref, fa.type as type";
919		$sql2 .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
920		$sql2 .= " , ".MAIN_DB_PREFIX."user as u";
921		$sql2 .= " , ".MAIN_DB_PREFIX."societe_remise_except as rc";
922		$sql2 .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as fa ON rc.fk_invoice_supplier_source = fa.rowid";
923		$sql2 .= " WHERE rc.fk_soc =".$object->id;
924		$sql2 .= " AND rc.fk_invoice_supplier = f.rowid";
925		$sql2 .= " AND rc.fk_user = u.rowid";
926		$sql2 .= " AND rc.discount_type = 1"; // Eliminate customer discounts
927		$sql2 .= " ORDER BY dc DESC";
928
929		$resql = $db->query($sql);
930		$resql2 = null;
931		if ($resql) $resql2 = $db->query($sql2);
932		if ($resql2)
933		{
934			print '<div class="div-table-responsive-no-min">';
935			print '<table class="noborder centpercent">';
936			print '<tr class="liste_titre">';
937			print '<td class="widthdate">'.$langs->trans("Date").'</td>'; // Need 120+ for format with AM/PM
938			print '<td>'.$langs->trans("ReasonDiscount").'</td>';
939			print '<td class="nowrap">'.$langs->trans("ConsumedBy").'</td>';
940			print '<td class="right">'.$langs->trans("AmountHT").'</td>';
941			if (!empty($conf->multicurrency->enabled))
942			{
943				print '<td class="right">'.$langs->trans("MulticurrencyAmountHT").'</td>';
944			}
945			print '<td class="right">'.$langs->trans("VATRate").'</td>';
946			print '<td class="right">'.$langs->trans("AmountTTC").'</td>';
947			if (!empty($conf->multicurrency->enabled))
948			{
949				print '<td class="right">'.$langs->trans("MulticurrencyAmountTTC").'</td>';
950			}
951			print '<td width="100" class="center">'.$langs->trans("Author").'</td>';
952			print '<td width="50">&nbsp;</td>';
953			print '</tr>';
954
955			$tab_sqlobj = array();
956			$tab_sqlobjOrder = array();
957			$num = $db->num_rows($resql);
958			if ($num > 0)
959			{
960				for ($i = 0; $i < $num; $i++)
961				{
962					$sqlobj = $db->fetch_object($resql);
963					$tab_sqlobj[] = $sqlobj;
964					$tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
965				}
966			}
967			$db->free($resql);
968
969			$num = $db->num_rows($resql2);
970			for ($i = 0; $i < $num; $i++)
971			{
972				$sqlobj = $db->fetch_object($resql2);
973				$tab_sqlobj[] = $sqlobj;
974				$tab_sqlobjOrder[] = $db->jdate($sqlobj->dc);
975			}
976			$db->free($resql2);
977			array_multisort($tab_sqlobjOrder, SORT_DESC, $tab_sqlobj);
978
979			$num = count($tab_sqlobj);
980			if ($num > 0)
981			{
982				$i = 0;
983				while ($i < $num)
984				{
985					$obj = array_shift($tab_sqlobj);
986					print '<tr class="oddeven">';
987					print '<td>'.dol_print_date($db->jdate($obj->dc), 'dayhour').'</td>';
988					if (preg_match('/\(CREDIT_NOTE\)/', $obj->description))
989					{
990						print '<td class="minwidth100">';
991						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
992						$facturefournstatic->ref = $obj->invoice_source_ref;
993						$facturefournstatic->type = $obj->type;
994						print preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $obj->description).' '.$facturefournstatic->getNomURl(1);
995						print '</td>';
996					} elseif (preg_match('/\(DEPOSIT\)/', $obj->description))
997					{
998						print '<td class="minwidth100">';
999						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
1000						$facturefournstatic->ref = $obj->invoice_source_ref;
1001						$facturefournstatic->type = $obj->type;
1002						print preg_replace('/\(DEPOSIT\)/', $langs->trans("InvoiceDeposit"), $obj->description).' '.$facturefournstatic->getNomURl(1);
1003						print '</td>';
1004					} elseif (preg_match('/\(EXCESS PAID\)/', $obj->description))
1005					{
1006						print '<td class="minwidth100">';
1007						$facturefournstatic->id = $obj->fk_invoice_supplier_source;
1008						$facturefournstatic->ref = $obj->invoice_source_ref;
1009						$facturefournstatic->type = $obj->type;
1010						print preg_replace('/\(EXCESS PAID\)/', $langs->trans("Invoice"), $obj->description).' '.$facturefournstatic->getNomURl(1);
1011						print '</td>';
1012					} else {
1013						print '<td class="minwidth100">';
1014						print $obj->description;
1015						print '</td>';
1016					}
1017					print '<td class="left nowrap">';
1018					if ($obj->invoiceid) {
1019						print '<a href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$obj->invoiceid.'">'.img_object($langs->trans("ShowBill"), 'bill').' '.$obj->ref.'</a>';
1020					}
1021					print '</td>';
1022					print '<td class="right">'.price($obj->amount_ht).'</td>';
1023					if (!empty($conf->multicurrency->enabled))
1024					{
1025						print '<td class="right">'.price($obj->multicurrency_amount_ht).'</td>';
1026					}
1027					print '<td class="right">'.vatrate($obj->tva_tx.($obj->vat_src_code ? ' ('.$obj->vat_src_code.')' : ''), true).'</td>';
1028					print '<td class="right">'.price($obj->amount_ttc).'</td>';
1029					if (!empty($conf->multicurrency->enabled))
1030					{
1031						print '<td class="right">'.price($obj->multicurrency_amount_ttc).'</td>';
1032					}
1033					print '<td class="center">';
1034					print '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$obj->login.'</a>';
1035					print '</td>';
1036					print '<td>&nbsp;</td>';
1037					print '</tr>';
1038					$i++;
1039				}
1040			} else {
1041				$colspan = 8;
1042				if (!empty($conf->multicurrency->enabled)) $colspan += 2;
1043				print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
1044			}
1045
1046			print "</table>";
1047			print '</div>';
1048		} else {
1049			dol_print_error($db);
1050		}
1051
1052		if ($isCustomer) {
1053			print '</div>'; // class="ficheaddleft"
1054			print '</div>'; // class="fichehalfright"
1055			print '</div>'; // class="fichecenter"
1056		}
1057	}
1058}
1059
1060// End of page
1061llxFooter();
1062$db->close();
1063