From 133657a8c7db8d18093ca35bea1eff8597cc4bed Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Wed, 21 Sep 2016 01:25:34 +0200
Subject: [PATCH] Uniformize code

---
 htdocs/compta/facture.php                     | 868 ++++++++++--------
 htdocs/compta/facture/class/facture.class.php |   2 +
 htdocs/core/class/html.formmargin.class.php   |   2 +-
 htdocs/core/lib/functions.lib.php             |   6 +
 htdocs/paypal/lib/paypal.lib.php              |   2 +-
 htdocs/theme/eldy/style.css.php               |   9 +
 6 files changed, 482 insertions(+), 407 deletions(-)

diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index 0ef7c830e68..c15d67e127b 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -395,10 +395,10 @@ if (empty($reshook))
 		}
 	}
 
-	else if ($action == 'set_ref_client' && $user->rights->facture->creer)
+	else if ($action == 'setref_client' && $user->rights->facture->creer)
 	{
 		$object->fetch($id);
-		$object->set_ref_client($_POST['ref_client']);
+		$object->set_ref_client(GETPOST('ref_client'));
 	}
 
 	// Classify to validated
@@ -2878,11 +2878,61 @@ else if ($id > 0 || ! empty($ref))
 
 	// Invoice content
 
-	print '<table class="border" width="100%">';
-
 	$linkback = '<a href="' . DOL_URL_ROOT . '/compta/facture/list.php' . (! empty($socid) ? '?socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
-
+	
+	
+	$morehtmlref='<div class="refidno">';
+	// Ref customer
+	$morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->facture->creer, 'string', '', 0, 1);
+	$morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->facture->creer, 'string', '', null, null, '', 1);
+	// Thirdparty
+	$morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1);
+	// Project
+	if (! empty($conf->projet->enabled))
+	{
+	    $langs->load("projects");
+	    $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
+	    if ($user->rights->facture->creer)
+	    {
+	        if ($action != 'classify')
+	            $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
+	            if ($action == 'classify') {
+	                //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
+	                $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
+	                $morehtmlref.='<input type="hidden" name="action" value="classin">';
+	                $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
+	                $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
+	                $morehtmlref.='<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
+	                $morehtmlref.='</form>';
+	            } else {
+	                $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
+	            }
+	    } else {
+	        if (! empty($object->fk_project)) {
+	            $proj = new Project($db);
+	            $proj->fetch($object->fk_project);
+	            $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $object->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
+	            $morehtmlref.=$proj->ref;
+	            $morehtmlref.='</a>';
+	        } else {
+	            $morehtmlref.='';
+	        }
+	    }
+	}
+	$morehtmlref.='</div>';
+	
+	$object->totalpaye = $totalpaye;   // To give a change to dol_banner_tab to use already paid amount to show correct status
+	
+	dol_banner_tab($object, 'ref', $linkback, 1, 'facnumber', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
+	
+	print '<div class="fichecenter">';
+	print '<div class="fichehalfleft">';
+	print '<div class="underbanner clearboth"></div>';
+	
+	print '<table class="border" width="100%">';
+	
 	// Ref
+	/*
 	print '<tr><td class="titlefield">' . $langs->trans('Ref') . '</td>';
 	print '<td colspan="5">';
 	$morehtmlright = '';
@@ -2946,10 +2996,10 @@ else if ($id > 0 || ! empty($ref))
 		}
 		print ')';
 	}
-	print '</tr>';
+	print '</tr>';*/
 
 	// Type
-	print '<tr><td>' . $langs->trans('Type') . '</td><td colspan="5">';
+	print '<tr><td class="titlefield">' . $langs->trans('Type') . '</td><td>';
 	print $object->getLibType();
 	if ($object->type == Facture::TYPE_REPLACEMENT) {
 		$facreplaced = new Facture($db);
@@ -2990,7 +3040,7 @@ else if ($id > 0 || ! empty($ref))
 	$addcreditnote = '<a href="' . DOL_URL_ROOT . '/compta/facture.php?action=create&socid=' . $soc->id . '&type=2&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("AddCreditNote") . '</a>';
 
 	print '<tr><td>' . $langs->trans('Discounts');
-	print '</td><td colspan="5">';
+	print '</td><td>';
 	if ($soc->remise_percent)
 		print $langs->trans("CompanyHasRelativeDiscount", $soc->remise_percent);
 	else
@@ -3071,7 +3121,7 @@ else if ($id > 0 || ! empty($ref))
 	if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editinvoicedate' && ! empty($object->brouillon) && $user->rights->facture->creer && empty($conf->global->FAC_FORCE_DATE_VALIDATION))
 		print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editinvoicedate&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetDate'), 1) . '</a></td>';
 	print '</tr></table>';
-	print '</td><td colspan="3">';
+	print '</td><td>';
 
 	if ($object->type != Facture::TYPE_CREDIT_NOTE) {
 		if ($action == 'editinvoicedate') {
@@ -3084,327 +3134,8 @@ else if ($id > 0 || ! empty($ref))
 	}
 	print '</td>';
 
-	// List of payments
-
-	$sign = 1;
-	if ($object->type == Facture::TYPE_CREDIT_NOTE)
-		$sign = - 1;
-
-	$nbrows = 8;
-	$nbcols = 3;
-	if (! empty($conf->projet->enabled))
-		$nbrows ++;
-	if (! empty($conf->banque->enabled)) {
-		$nbrows ++; $nbcols++;
-	}
-	if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0)
-		$nbrows ++;
-	if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0)
-		$nbrows ++;
-	if ($selleruserevenustamp)
-		$nbrows ++;
-	if (! empty($conf->multicurrency->enabled)) $nbrows+=5;
-	if (! empty($conf->incoterm->enabled)) $nbrows+=1;
-
-	print '<td rowspan="' . $nbrows . '" colspan="2" valign="top">';
-
-	if ($object->type == Facture::TYPE_SITUATION && !empty($conf->global->INVOICE_USE_SITUATION))
-	{
-		if (count($object->tab_previous_situation_invoice) > 0 || count($object->tab_next_situation_invoice) > 0) print '<table class="nobordernopadding paymenttable" width="100%">';
-
-		if (count($object->tab_previous_situation_invoice) > 0)
-		{
-			//List of previous invoices
-			print '<tr class="liste_titre">';
-			print '<td>' . $langs->trans('ListOfPreviousSituationInvoices') . '</td>';
-			print '<td></td>';
-			if (! empty($conf->banque->enabled))
-				print '<td align="right"></td>';
-			print '<td align="right">' . $langs->trans('AmountHT') . '</td>';
-			print '<td align="right">' . $langs->trans('AmountTTC') . '</td>';
-			print '<td width="18">&nbsp;</td>';
-			print '</tr>';
-
-			$total_prev_ht = $total_prev_ttc = 0;
-			$var = true;
-			foreach ($object->tab_previous_situation_invoice as $prev_invoice)
-			{
-				$totalpaye = $prev_invoice->getSommePaiement();
-				$total_prev_ht += $prev_invoice->total_ht;
-				$total_prev_ttc += $prev_invoice->total_ttc;
-				print '<tr '.$bc [$var].'>';
-				print '<td>'.$prev_invoice->getNomUrl(1).'</td>';
-				print '<td></td>';
-				if (! empty($conf->banque->enabled))
-					print '<td align="right"></td>';
-				print '<td align="right">' . price($prev_invoice->total_ht) . '</td>';
-				print '<td align="right">' . price($prev_invoice->total_ttc) . '</td>';
-				print '<td align="right">'.$prev_invoice->getLibStatut(3, $totalpaye).'</td>';
-				print '</tr>';
-
-				$var = !$var;
-			}
-
-			print '<tr '.$bc [$var].'>';
-			print '<td colspan="2" align="right"></td>';
-			print '<td align="right"><b>' . price($total_prev_ht) . '</b></td>';
-			print '<td align="right"><b>' . price($total_prev_ttc) . '</b></td>';
-			print '<td width="18">&nbsp;</td>';
-			print '</tr>';
-		}
-
-		if (count($object->tab_next_situation_invoice) > 0)
-		{
-			//List of next invoices
-			print '<tr class="liste_titre">';
-			print '<td>' . $langs->trans('ListOfNextSituationInvoices') . '</td>';
-			print '<td></td>';
-			if (! empty($conf->banque->enabled))
-				print '<td align="right"></td>';
-			print '<td align="right">' . $langs->trans('AmountHT') . '</td>';
-			print '<td align="right">' . $langs->trans('AmountTTC') . '</td>';
-			print '<td width="18">&nbsp;</td>';
-			print '</tr>';
-
-			$total_next_ht = $total_next_ttc = 0;
-
-			$var = true;
-			foreach ($object->tab_next_situation_invoice as $next_invoice)
-			{
-				$totalpaye = $next_invoice->getSommePaiement();
-				$total_next_ht += $next_invoice->total_ht;
-				$total_next_ttc += $next_invoice->total_ttc;
-				print '<tr '.$bc [$var].'>';
-				print '<td>'.$next_invoice->getNomUrl(1).'</td>';
-				print '<td></td>';
-				if (! empty($conf->banque->enabled))
-					print '<td align="right"></td>';
-				print '<td align="right">' . price($next_invoice->total_ht) . '</td>';
-				print '<td align="right">' . price($next_invoice->total_ttc) . '</td>';
-				print '<td align="right">'.$next_invoice->getLibStatut(3, $totalpaye).'</td>';
-				print '</tr>';
-
-				$var = !$var;
-			}
-
-			print '<tr '.$bc [$var].'>';
-			print '<td colspan="2" align="right"></td>';
-			print '<td align="right"><b>' . price($total_next_ht) . '</b></td>';
-			print '<td align="right"><b>' . price($total_next_ttc) . '</b></td>';
-			print '<td width="18">&nbsp;</td>';
-			print '</tr>';
-		}
-
-		if (count($object->tab_previous_situation_invoice) > 0 || count($object->tab_next_situation_invoice) > 0) print '</table>';
-	}
-
-	print '<table class="nobordernopadding paymenttable" width="100%">';
-
-	// List of payments already done
-	print '<tr class="liste_titre">';
-	print '<td class="liste_titre">' . ($object->type == Facture::TYPE_CREDIT_NOTE ? $langs->trans("PaymentsBack") : $langs->trans('Payments')) . '</td>';
-	print '<td class="liste_titre">' . $langs->trans('Date') . '</td>';
-	print '<td class="liste_titre">' . $langs->trans('Type') . '</td>';
-	if (! empty($conf->banque->enabled))
-		print '<td class="liste_titre" align="right">' . $langs->trans('BankAccount') . '</td>';
-	print '<td class="liste_titre" align="right">' . $langs->trans('Amount') . '</td>';
-	print '<td class="liste_titre" width="18">&nbsp;</td>';
 	print '</tr>';
 
-	$var = true;
-
-	// Payments already done (from payment on this invoice)
-	$sql = 'SELECT p.datep as dp, p.ref, p.num_paiement, p.rowid, p.fk_bank,';
-	$sql .= ' c.code as payment_code, c.libelle as payment_label,';
-	$sql .= ' pf.amount,';
-	$sql .= ' ba.rowid as baid, ba.ref as baref, ba.label';
-	$sql .= ' FROM ' . MAIN_DB_PREFIX . 'c_paiement as c, ' . MAIN_DB_PREFIX . 'paiement_facture as pf, ' . MAIN_DB_PREFIX . 'paiement as p';
-	$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON p.fk_bank = b.rowid';
-	$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank_account as ba ON b.fk_account = ba.rowid';
-	$sql .= ' WHERE pf.fk_facture = ' . $object->id . ' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid';
-	$sql .= ' ORDER BY p.datep, p.tms';
-
-	$result = $db->query($sql);
-	if ($result)
-	{
-		$num = $db->num_rows($result);
-		$i = 0;
-
-		// if ($object->type != 2)
-		// {
-		if ($num > 0)
-		{
-			while ($i < $num)
-			{
-				$objp = $db->fetch_object($result);
-				$var = ! $var;
-				print '<tr ' . $bc [$var] . '><td>';
-				$paymentstatic->id=$objp->rowid;
-				$paymentstatic->datepaye=$db->jdate($objp->dp);
-				$paymentstatic->ref=$objp->ref;
-				$paymentstatic->num_paiement=$objp->num_paiement;
-				$paymentstatic->payment_code=$objp->payment_code;
-				print $paymentstatic->getNomUrl(1);
-				print '</td>';
-				print '<td>'.dol_print_date($db->jdate($objp->dp), 'day') . '</td>';
-				$label = ($langs->trans("PaymentType" . $objp->payment_code) != ("PaymentType" . $objp->payment_code)) ? $langs->trans("PaymentType" . $objp->payment_code) : $objp->payment_label;
-				print '<td>' . $label . ' ' . $objp->num_paiement . '</td>';
-				if (! empty($conf->banque->enabled))
-				{
-					$bankaccountstatic->id = $objp->baid;
-					$bankaccountstatic->ref = $objp->baref;
-					$bankaccountstatic->label = $objp->baref;
-					print '<td align="right">';
-					if ($bankaccountstatic->id)
-						print $bankaccountstatic->getNomUrl(1, 'transactions');
-					print '</td>';
-				}
-				print '<td align="right">' . price($sign * $objp->amount) . '</td>';
-				print '<td>&nbsp;</td>';
-				print '</tr>';
-				$i ++;
-			}
-		} else {
-			print '<tr ' . $bc[false] . '><td colspan="' . $nbcols . '" class="opacitymedium">' . $langs->trans("None") . '</td><td></td><td></td></tr>';
-		}
-		// }
-		$db->free($result);
-	} else {
-		dol_print_error($db);
-	}
-
-	if ($object->type != Facture::TYPE_CREDIT_NOTE)
-	{
-		// Total already paid
-		print '<tr><td colspan="' . $nbcols . '" align="right">';
-		if ($object->type != Facture::TYPE_DEPOSIT)
-			print $langs->trans('AlreadyPaidNoCreditNotesNoDeposits');
-		else
-			print $langs->trans('AlreadyPaid');
-		print ' :</td><td align="right">' . price($totalpaye) . '</td><td>&nbsp;</td></tr>';
-
-		$resteapayeraffiche = $resteapayer;
-
-		// Loop on each credit note or deposit amount applied
-		$creditnoteamount = 0;
-		$depositamount = 0;
-		$sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
-		$sql .= " re.description, re.fk_facture_source";
-		$sql .= " FROM " . MAIN_DB_PREFIX . "societe_remise_except as re";
-		$sql .= " WHERE fk_facture = " . $object->id;
-		$resql = $db->query($sql);
-		if ($resql)
-		{
-			$num = $db->num_rows($resql);
-			$i = 0;
-			$invoice = new Facture($db);
-			while ($i < $num)
-			{
-				$obj = $db->fetch_object($resql);
-				$invoice->fetch($obj->fk_facture_source);
-				print '<tr><td colspan="' . $nbcols . '" align="right">';
-				if ($invoice->type == Facture::TYPE_CREDIT_NOTE)
-					print $langs->trans("CreditNote") . ' ';
-				if ($invoice->type == Facture::TYPE_DEPOSIT)
-					print $langs->trans("Deposit") . ' ';
-				print $invoice->getNomUrl(0);
-				print ' :</td>';
-				print '<td align="right">' . price($obj->amount_ttc) . '</td>';
-				print '<td align="right">';
-				print '<a href="' . $_SERVER["PHP_SELF"] . '?facid=' . $object->id . '&action=unlinkdiscount&discountid=' . $obj->rowid . '">' . img_delete() . '</a>';
-				print '</td></tr>';
-				$i ++;
-				if ($invoice->type == Facture::TYPE_CREDIT_NOTE)
-					$creditnoteamount += $obj->amount_ttc;
-				if ($invoice->type == Facture::TYPE_DEPOSIT)
-					$depositamount += $obj->amount_ttc;
-			}
-		} else {
-			dol_print_error($db);
-		}
-
-		// Paye partiellement 'escompte'
-		if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'discount_vat') {
-			print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
-			print $form->textwithpicto($langs->trans("Discount") . ':', $langs->trans("HelpEscompte"), - 1);
-			print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
-			$resteapayeraffiche = 0;
-		}
-		// Paye partiellement ou Abandon 'badcustomer'
-		if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'badcustomer') {
-			print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
-			print $form->textwithpicto($langs->trans("Abandoned") . ':', $langs->trans("HelpAbandonBadCustomer"), - 1);
-			print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
-			// $resteapayeraffiche=0;
-		}
-		// Paye partiellement ou Abandon 'product_returned'
-		if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'product_returned') {
-			print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
-			print $form->textwithpicto($langs->trans("ProductReturned") . ':', $langs->trans("HelpAbandonProductReturned"), - 1);
-			print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
-			$resteapayeraffiche = 0;
-		}
-		// Paye partiellement ou Abandon 'abandon'
-		if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'abandon') {
-			print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
-			$text = $langs->trans("HelpAbandonOther");
-			if ($object->close_note)
-				$text .= '<br><br><b>' . $langs->trans("Reason") . '</b>:' . $object->close_note;
-			print $form->textwithpicto($langs->trans("Abandoned") . ':', $text, - 1);
-			print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
-			$resteapayeraffiche = 0;
-		}
-
-		// Billed
-		print '<tr><td colspan="' . $nbcols . '" align="right">' . $langs->trans("Billed") . ' :</td><td align="right">' . price($object->total_ttc) . '</td><td>&nbsp;</td></tr>';
-
-		// Remainder to pay
-		print '<tr><td colspan="' . $nbcols . '" align="right">';
-		if ($resteapayeraffiche >= 0)
-			print $langs->trans('RemainderToPay');
-		else
-			print $langs->trans('ExcessReceived');
-		print ' :</td>';
-		print '<td align="right" bgcolor="#f0f0f0"><b>' . price($resteapayeraffiche) . '</b></td>';
-		print '<td class="nowrap">&nbsp;</td></tr>';
-	}
-	else 	// Credit note
-	{
-		// Total already paid back
-		print '<tr><td colspan="' . $nbcols . '" align="right">';
-		print $langs->trans('AlreadyPaidBack');
-		print ' :</td><td align="right">' . price($sign * $totalpaye) . '</td><td>&nbsp;</td></tr>';
-
-		// Billed
-		print '<tr><td colspan="' . $nbcols . '" align="right">' . $langs->trans("Billed") . ' :</td><td align="right">' . price($sign * $object->total_ttc) . '</td><td>&nbsp;</td></tr>';
-
-		// Remainder to pay back
-		print '<tr><td colspan="' . $nbcols . '" align="right">';
-		if ($resteapayeraffiche <= 0)
-			print $langs->trans('RemainderToPayBack');
-		else
-			print $langs->trans('ExcessPaydBack');
-		print ' :</td>';
-		print '<td align="right" bgcolor="#f0f0f0"><b>' . price($sign * $resteapayeraffiche) . '</b></td>';
-		print '<td class="nowrap">&nbsp;</td></tr>';
-
-		// Sold credit note
-		// print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans('TotalTTC').' :</td>';
-		// print '<td align="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($sign *
-	// $object->total_ttc).'</b></td><td>&nbsp;</td></tr>';
-	}
-
-	print '</table>';
-
-	// Margin Infos
-	if (! empty($conf->margin->enabled))
-	{
-		print '<br>';
-		$formmargin->displayMarginInfos($object, $object->statut > 0);
-	}
-
-	print '</td></tr>';
-
 	if (! empty($conf->global->INVOICE_POINTOFTAX_DATE))
 	{
 		// Date invoice
@@ -3414,7 +3145,7 @@ else if ($id > 0 || ! empty($ref))
 		print '</td>';
 		print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editdate_pointoftax&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetDate'), 1) . '</a></td>';
 		print '</tr></table>';
-		print '</td><td colspan="3">';
+		print '</td><td>';
 		if ($action == 'editdate_pointoftax') {
 			$form->form_date($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $object->date_pointoftax, 'date_pointoftax');
 		} else {
@@ -3431,7 +3162,7 @@ else if ($id > 0 || ! empty($ref))
 	if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editconditions' && ! empty($object->brouillon) && $user->rights->facture->creer)
 		print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editconditions&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetConditions'), 1) . '</a></td>';
 	print '</tr></table>';
-	print '</td><td colspan="3">';
+	print '</td><td>';
 	if ($object->type != Facture::TYPE_CREDIT_NOTE)
 	{
 		if ($action == 'editconditions') {
@@ -3452,7 +3183,7 @@ else if ($id > 0 || ! empty($ref))
 	if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editpaymentterm' && ! empty($object->brouillon) && $user->rights->facture->creer)
 		print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editpaymentterm&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetDate'), 1) . '</a></td>';
 	print '</tr></table>';
-	print '</td><td colspan="3">';
+	print '</td><td>';
 	if ($object->type != Facture::TYPE_CREDIT_NOTE)
 	{
 		if ($action == 'editpaymentterm') {
@@ -3476,7 +3207,7 @@ else if ($id > 0 || ! empty($ref))
 	if ($action != 'editmode' && ! empty($object->brouillon) && $user->rights->facture->creer)
 		print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmode&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetMode'), 1) . '</a></td>';
 	print '</tr></table>';
-	print '</td><td colspan="3">';
+	print '</td><td>';
 	if ($action == 'editmode')
 	{
 		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'CRDT');
@@ -3499,7 +3230,7 @@ else if ($id > 0 || ! empty($ref))
 		if ($action != 'editmulticurrencycode' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencycode&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
 		print '</tr></table>';
-		print '</td><td colspan="3">';
+		print '</td><td>';
 		if ($action == 'editmulticurrencycode') {
 			$form->form_multicurrency_code($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_code, 'multicurrency_code');
 		} else {
@@ -3516,7 +3247,7 @@ else if ($id > 0 || ! empty($ref))
 		if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
 		print '</tr></table>';
-		print '</td><td colspan="3">';
+		print '</td><td>';
 		if ($action == 'editmulticurrencyrate') {
 			$form->form_multicurrency_rate($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code);
 		} else {
@@ -3533,7 +3264,7 @@ else if ($id > 0 || ! empty($ref))
 	if (($action != 'editbankaccount') && $user->rights->facture->creer && ! empty($object->brouillon))
 	    print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&amp;id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'),1).'</a></td>';
 	print '</tr></table>';
-	print '</td><td colspan="3">';
+	print '</td><td>';
 	if ($action == 'editbankaccount')
 	{
 	    $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
@@ -3566,7 +3297,7 @@ else if ($id > 0 || ! empty($ref))
 			print $object->situation_counter;
 
 			print '</td>';
-			print '<td align="right" colspan="2" class="nowrap">';
+			print '<td align="right" class="nowrap">';
 
 			$prevsits_total_amount = 0;
 			foreach ($prevsits as $situation) {
@@ -3587,7 +3318,7 @@ else if ($id > 0 || ! empty($ref))
 				print $prevsits[$i]->situation_counter;
 				print '</a></td>';
 
-				print '<td align="right" colspan="2" class="nowrap">';
+				print '<td align="right" class="nowrap">';
 				print '- ' . price($prevsits[$i]->total_ht);
 				print '</td>';
 				print '<td>' . $langs->trans('Currency' . $conf->currency) . '</td></tr>';
@@ -3595,71 +3326,8 @@ else if ($id > 0 || ! empty($ref))
 		}
 	}
 
-	// Amount
-	print '<tr><td>' . $langs->trans('AmountHT') . '</td>';
-	print '<td colspan="3" class="nowrap">' . price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
-	print '<tr><td>' . $langs->trans('AmountVAT') . '</td><td colspan="3" class="nowrap">' . price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
-	print '</tr>';
-
-	// Amount Local Taxes
-	if (($mysoc->localtax1_assuj == "1" && $mysoc->useLocalTax(1)) || $object->total_localtax1 != 0) 	// Localtax1
-	{
-		print '<tr><td>' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '</td>';
-		print '<td colspan="3" class="nowrap">' . price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
-	}
-	if (($mysoc->localtax2_assuj == "1" && $mysoc->useLocalTax(2)) || $object->total_localtax2 != 0) 	// Localtax2
-	{
-		print '<tr><td>' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '</td>';
-		print '<td colspan="3" class=nowrap">' . price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
-	}
-
-	// Revenue stamp
-	if ($selleruserevenustamp) 	// Test company use revenue stamp
-	{
-		print '<tr><td>';
-		print '<table class="nobordernopadding" width="100%"><tr><td>';
-		print $langs->trans('RevenueStamp');
-		print '</td>';
-		if ($action != 'editrevenuestamp' && ! empty($object->brouillon) && $user->rights->facture->creer)
-			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editrevenuestamp&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetRevenuStamp'), 1) . '</a></td>';
-		print '</tr></table>';
-		print '</td><td colspan="3">';
-		if ($action == 'editrevenuestamp') {
-			print '<form action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="post">';
-			print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
-			print '<input type="hidden" name="action" value="setrevenuestamp">';
-			print $formother->select_revenue_stamp(GETPOST('revenuestamp'), 'revenuestamp', $mysoc->country_code);
-			// print '<input type="text" class="flat" size="4" name="revenuestamp" value="'.price2num($object->revenuestamp).'">';
-			print ' <input type="submit" class="button" value="' . $langs->trans('Modify') . '">';
-			print '</form>';
-		} else {
-			print price($object->revenuestamp, 1, '', 1, - 1, - 1, $conf->currency);
-		}
-		print '</td></tr>';
-	}
-
-	// Total with tax
-	print '<tr><td>' . $langs->trans('AmountTTC') . '</td><td colspan="3" class="nowrap">' . price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
-
-	if (!empty($conf->multicurrency->enabled))
-	{
-		// Multicurrency Amount HT
-		print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountHT','multicurrency_total_ht') . '</td>';
-		print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
-		print '</tr>';
-
-		// Multicurrency Amount VAT
-		print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountVAT','multicurrency_total_tva') . '</td>';
-		print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
-		print '</tr>';
-
-		// Multicurrency Amount TTC
-		print '<tr><td height="10">' . fieldLabel('MulticurrencyAmountTTC','multicurrency_total_ttc') . '</td>';
-		print '<td class="nowrap" colspan="2">' . price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
-		print '</tr>';
-	}
-
 	// Statut
+	/*
 	print '<tr><td>' . $langs->trans('Status') . '</td>';
 	print '<td colspan="3">' . ($object->getLibStatut(4, $totalpaye)) . '</td></tr>';
 
@@ -3687,7 +3355,7 @@ else if ($id > 0 || ! empty($ref))
 		}
 		print '</td>';
 		print '</tr>';
-	}
+	}*/
 
 	// Incoterms
 	if (!empty($conf->incoterm->enabled))
@@ -3700,7 +3368,7 @@ else if ($id > 0 || ! empty($ref))
         else print '&nbsp;';
         print '</td></tr></table>';
         print '</td>';
-        print '<td colspan="3">';
+        print '<td>';
 		if ($action != 'editincoterm')
 		{
 			print $form->textwithpicto($object->display_incoterms(), $object->libelle_incoterms, 1);
@@ -3713,11 +3381,401 @@ else if ($id > 0 || ! empty($ref))
 	}
 
 	// Other attributes
-	$cols = 5;
+	$cols = 2;
 	include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
 
-	print '</table><br>';
+	print '</table>';
+	
+	print '</div>';
+	print '<div class="fichehalfright">';
+	print '<div class="ficheaddleft">';
+	print '<div class="underbanner clearboth"></div>';
+	
+	print '<table class="border centpercent">';
+	
+	// Amount
+	print '<tr><td class="titlefield">' . $langs->trans('AmountHT') . '</td>';
+	print '<td class="nowrap">' . price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
+	print '<tr><td>' . $langs->trans('AmountVAT') . '</td><td colspan="3" class="nowrap">' . price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
+	print '</tr>';
+	
+	// Amount Local Taxes
+	if (($mysoc->localtax1_assuj == "1" && $mysoc->useLocalTax(1)) || $object->total_localtax1 != 0) 	// Localtax1
+	{
+	    print '<tr><td>' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '</td>';
+	    print '<td class="nowrap">' . price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
+	}
+	if (($mysoc->localtax2_assuj == "1" && $mysoc->useLocalTax(2)) || $object->total_localtax2 != 0) 	// Localtax2
+	{
+	    print '<tr><td>' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '</td>';
+	    print '<td class=nowrap">' . price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
+	}
+	
+	// Revenue stamp
+	if ($selleruserevenustamp) 	// Test company use revenue stamp
+	{
+	    print '<tr><td>';
+	    print '<table class="nobordernopadding" width="100%"><tr><td>';
+	    print $langs->trans('RevenueStamp');
+	    print '</td>';
+	    if ($action != 'editrevenuestamp' && ! empty($object->brouillon) && $user->rights->facture->creer)
+	    {
+	        print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editrevenuestamp&amp;facid=' . $object->id . '">' . img_edit($langs->trans('SetRevenuStamp'), 1) . '</a></td>';
+	    }
+        print '</tr></table>';
+        print '</td><td>';
+        if ($action == 'editrevenuestamp') {
+            print '<form action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="post">';
+            print '<input type="hidden" name="token" value="' . $_SESSION ['newtoken'] . '">';
+            print '<input type="hidden" name="action" value="setrevenuestamp">';
+            print $formother->select_revenue_stamp(GETPOST('revenuestamp'), 'revenuestamp', $mysoc->country_code);
+            // print '<input type="text" class="flat" size="4" name="revenuestamp" value="'.price2num($object->revenuestamp).'">';
+            print ' <input type="submit" class="button" value="' . $langs->trans('Modify') . '">';
+            print '</form>';
+        } else {
+            print price($object->revenuestamp, 1, '', 1, - 1, - 1, $conf->currency);
+        }
+        print '</td></tr>';
+	}
+	
+	// Total with tax
+	print '<tr><td>' . $langs->trans('AmountTTC') . '</td><td class="nowrap">' . price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency) . '</td></tr>';
+	
+	if (!empty($conf->multicurrency->enabled))
+	{
+	    // Multicurrency Amount HT
+	    print '<tr><td>' . fieldLabel('MulticurrencyAmountHT','multicurrency_total_ht') . '</td>';
+	    print '<td class="nowrap">' . price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
+	    print '</tr>';
+	
+	    // Multicurrency Amount VAT
+	    print '<tr><td>' . fieldLabel('MulticurrencyAmountVAT','multicurrency_total_tva') . '</td>';
+	    print '<td class="nowrap">' . price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
+	    print '</tr>';
+	
+	    // Multicurrency Amount TTC
+	    print '<tr><td>' . fieldLabel('MulticurrencyAmountTTC','multicurrency_total_ttc') . '</td>';
+	    print '<td class="nowrap">' . price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '</td>';
+	    print '</tr>';
+	}
+	
+	print '</table>';
+	
+	// List of payments
+	
+	$sign = 1;
+	if ($object->type == Facture::TYPE_CREDIT_NOTE) $sign = - 1;
+    $nbrows = 8;
+    $nbcols = 3;
+    if (! empty($conf->projet->enabled))
+        $nbrows ++;
+    if (! empty($conf->banque->enabled)) {
+        $nbrows ++;
+        $nbcols ++;
+    }
+    if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0)
+        $nbrows ++;
+    if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0)
+        $nbrows ++;
+    if ($selleruserevenustamp)
+        $nbrows ++;
+    if (! empty($conf->multicurrency->enabled))
+        $nbrows += 5;
+    if (! empty($conf->incoterm->enabled))
+        $nbrows += 1;
+    
+    if ($object->type == Facture::TYPE_SITUATION && ! empty($conf->global->INVOICE_USE_SITUATION)) 
+    {
+        if (count($object->tab_previous_situation_invoice) > 0 || count($object->tab_next_situation_invoice) > 0)
+            print '<table class="nobordernopadding paymenttable" width="100%">';
+        
+        if (count($object->tab_previous_situation_invoice) > 0) {
+            // List of previous invoices
+            print '<tr class="liste_titre">';
+            print '<td>' . $langs->trans('ListOfPreviousSituationInvoices') . '</td>';
+            print '<td></td>';
+            if (! empty($conf->banque->enabled))
+                print '<td align="right"></td>';
+            print '<td align="right">' . $langs->trans('AmountHT') . '</td>';
+            print '<td align="right">' . $langs->trans('AmountTTC') . '</td>';
+            print '<td width="18">&nbsp;</td>';
+            print '</tr>';
+            
+            $total_prev_ht = $total_prev_ttc = 0;
+            $var = true;
+            foreach ($object->tab_previous_situation_invoice as $prev_invoice) {
+                $totalpaye = $prev_invoice->getSommePaiement();
+                $total_prev_ht += $prev_invoice->total_ht;
+                $total_prev_ttc += $prev_invoice->total_ttc;
+                print '<tr ' . $bc[$var] . '>';
+                print '<td>' . $prev_invoice->getNomUrl(1) . '</td>';
+                print '<td></td>';
+                if (! empty($conf->banque->enabled))
+                    print '<td align="right"></td>';
+                print '<td align="right">' . price($prev_invoice->total_ht) . '</td>';
+                print '<td align="right">' . price($prev_invoice->total_ttc) . '</td>';
+                print '<td align="right">' . $prev_invoice->getLibStatut(3, $totalpaye) . '</td>';
+                print '</tr>';
+                
+                $var = ! $var;
+            }
+            
+            print '<tr ' . $bc[$var] . '>';
+            print '<td colspan="2" align="right"></td>';
+            print '<td align="right"><b>' . price($total_prev_ht) . '</b></td>';
+            print '<td align="right"><b>' . price($total_prev_ttc) . '</b></td>';
+            print '<td width="18">&nbsp;</td>';
+            print '</tr>';
+        }
+        
+        if (count($object->tab_next_situation_invoice) > 0) {
+            // List of next invoices
+            print '<tr class="liste_titre">';
+            print '<td>' . $langs->trans('ListOfNextSituationInvoices') . '</td>';
+            print '<td></td>';
+            if (! empty($conf->banque->enabled))
+                print '<td align="right"></td>';
+            print '<td align="right">' . $langs->trans('AmountHT') . '</td>';
+            print '<td align="right">' . $langs->trans('AmountTTC') . '</td>';
+            print '<td width="18">&nbsp;</td>';
+            print '</tr>';
+            
+            $total_next_ht = $total_next_ttc = 0;
+            
+            $var = true;
+            foreach ($object->tab_next_situation_invoice as $next_invoice) {
+                $totalpaye = $next_invoice->getSommePaiement();
+                $total_next_ht += $next_invoice->total_ht;
+                $total_next_ttc += $next_invoice->total_ttc;
+                print '<tr ' . $bc[$var] . '>';
+                print '<td>' . $next_invoice->getNomUrl(1) . '</td>';
+                print '<td></td>';
+                if (! empty($conf->banque->enabled))
+                    print '<td align="right"></td>';
+                print '<td align="right">' . price($next_invoice->total_ht) . '</td>';
+                print '<td align="right">' . price($next_invoice->total_ttc) . '</td>';
+                print '<td align="right">' . $next_invoice->getLibStatut(3, $totalpaye) . '</td>';
+                print '</tr>';
+                
+                $var = ! $var;
+            }
+            
+            print '<tr ' . $bc[$var] . '>';
+            print '<td colspan="2" align="right"></td>';
+            print '<td align="right"><b>' . price($total_next_ht) . '</b></td>';
+            print '<td align="right"><b>' . price($total_next_ttc) . '</b></td>';
+            print '<td width="18">&nbsp;</td>';
+            print '</tr>';
+        }
+        
+        if (count($object->tab_previous_situation_invoice) > 0 || count($object->tab_next_situation_invoice) > 0)
+            print '</table>';
+    }
+    
+    print '<table class="noborder paymenttable" width="100%">';
+    
+    // List of payments already done
+    print '<tr class="liste_titre">';
+    print '<td class="liste_titre">' . ($object->type == Facture::TYPE_CREDIT_NOTE ? $langs->trans("PaymentsBack") : $langs->trans('Payments')) . '</td>';
+    print '<td class="liste_titre">' . $langs->trans('Date') . '</td>';
+    print '<td class="liste_titre">' . $langs->trans('Type') . '</td>';
+    if (! empty($conf->banque->enabled)) {
+        print '<td class="liste_titre" align="right">' . $langs->trans('BankAccount') . '</td>';
+    }
+    print '<td class="liste_titre" align="right">' . $langs->trans('Amount') . '</td>';
+    print '<td class="liste_titre" width="18">&nbsp;</td>';
+    print '</tr>';
+    
+    $var = true;
+    
+    // Payments already done (from payment on this invoice)
+    $sql = 'SELECT p.datep as dp, p.ref, p.num_paiement, p.rowid, p.fk_bank,';
+    $sql .= ' c.code as payment_code, c.libelle as payment_label,';
+    $sql .= ' pf.amount,';
+    $sql .= ' ba.rowid as baid, ba.ref as baref, ba.label';
+    $sql .= ' FROM ' . MAIN_DB_PREFIX . 'c_paiement as c, ' . MAIN_DB_PREFIX . 'paiement_facture as pf, ' . MAIN_DB_PREFIX . 'paiement as p';
+    $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON p.fk_bank = b.rowid';
+    $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank_account as ba ON b.fk_account = ba.rowid';
+    $sql .= ' WHERE pf.fk_facture = ' . $object->id . ' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid';
+    $sql .= ' ORDER BY p.datep, p.tms';
+    
+    $result = $db->query($sql);
+    if ($result) {
+        $num = $db->num_rows($result);
+        $i = 0;
+        
+        // if ($object->type != 2)
+        // {
+        if ($num > 0) {
+            while ($i < $num) {
+                $objp = $db->fetch_object($result);
+                $var = ! $var;
+                print '<tr ' . $bc[$var] . '><td>';
+                $paymentstatic->id = $objp->rowid;
+                $paymentstatic->datepaye = $db->jdate($objp->dp);
+                $paymentstatic->ref = $objp->ref;
+                $paymentstatic->num_paiement = $objp->num_paiement;
+                $paymentstatic->payment_code = $objp->payment_code;
+                print $paymentstatic->getNomUrl(1);
+                print '</td>';
+                print '<td>' . dol_print_date($db->jdate($objp->dp), 'day') . '</td>';
+                $label = ($langs->trans("PaymentType" . $objp->payment_code) != ("PaymentType" . $objp->payment_code)) ? $langs->trans("PaymentType" . $objp->payment_code) : $objp->payment_label;
+                print '<td>' . $label . ' ' . $objp->num_paiement . '</td>';
+                if (! empty($conf->banque->enabled)) {
+                    $bankaccountstatic->id = $objp->baid;
+                    $bankaccountstatic->ref = $objp->baref;
+                    $bankaccountstatic->label = $objp->baref;
+                    print '<td align="right">';
+                    if ($bankaccountstatic->id)
+                        print $bankaccountstatic->getNomUrl(1, 'transactions');
+                    print '</td>';
+                }
+                print '<td align="right">' . price($sign * $objp->amount) . '</td>';
+                print '<td>&nbsp;</td>';
+                print '</tr>';
+                $i ++;
+            }
+        } else {
+            print '<tr ' . $bc[false] . '><td colspan="' . $nbcols . '" class="opacitymedium">' . $langs->trans("None") . '</td><td></td><td></td></tr>';
+        }
+        // }
+        $db->free($result);
+    } else {
+        dol_print_error($db);
+    }
+    
+    if ($object->type != Facture::TYPE_CREDIT_NOTE) {
+        // Total already paid
+        print '<tr><td colspan="' . $nbcols . '" align="right">';
+        if ($object->type != Facture::TYPE_DEPOSIT)
+            print $langs->trans('AlreadyPaidNoCreditNotesNoDeposits');
+        else
+            print $langs->trans('AlreadyPaid');
+        print ' :</td><td align="right" class="amountalreadypaid">' . price($totalpaye) . '</td><td>&nbsp;</td></tr>';
+        
+        $resteapayeraffiche = $resteapayer;
+        
+        // Loop on each credit note or deposit amount applied
+        $creditnoteamount = 0;
+        $depositamount = 0;
+        $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
+        $sql .= " re.description, re.fk_facture_source";
+        $sql .= " FROM " . MAIN_DB_PREFIX . "societe_remise_except as re";
+        $sql .= " WHERE fk_facture = " . $object->id;
+        $resql = $db->query($sql);
+        if ($resql) {
+            $num = $db->num_rows($resql);
+            $i = 0;
+            $invoice = new Facture($db);
+            while ($i < $num) {
+                $obj = $db->fetch_object($resql);
+                $invoice->fetch($obj->fk_facture_source);
+                print '<tr><td colspan="' . $nbcols . '" align="right">';
+                if ($invoice->type == Facture::TYPE_CREDIT_NOTE)
+                    print $langs->trans("CreditNote") . ' ';
+                if ($invoice->type == Facture::TYPE_DEPOSIT)
+                    print $langs->trans("Deposit") . ' ';
+                print $invoice->getNomUrl(0);
+                print ' :</td>';
+                print '<td align="right">' . price($obj->amount_ttc) . '</td>';
+                print '<td align="right">';
+                print '<a href="' . $_SERVER["PHP_SELF"] . '?facid=' . $object->id . '&action=unlinkdiscount&discountid=' . $obj->rowid . '">' . img_delete() . '</a>';
+                print '</td></tr>';
+                $i ++;
+                if ($invoice->type == Facture::TYPE_CREDIT_NOTE)
+                    $creditnoteamount += $obj->amount_ttc;
+                if ($invoice->type == Facture::TYPE_DEPOSIT)
+                    $depositamount += $obj->amount_ttc;
+            }
+        } else {
+            dol_print_error($db);
+        }
+        
+        // Paye partiellement 'escompte'
+        if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'discount_vat') {
+            print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
+            print $form->textwithpicto($langs->trans("Discount") . ':', $langs->trans("HelpEscompte"), - 1);
+            print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
+            $resteapayeraffiche = 0;
+        }
+        // Paye partiellement ou Abandon 'badcustomer'
+        if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'badcustomer') {
+            print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
+            print $form->textwithpicto($langs->trans("Abandoned") . ':', $langs->trans("HelpAbandonBadCustomer"), - 1);
+            print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
+            // $resteapayeraffiche=0;
+        }
+        // Paye partiellement ou Abandon 'product_returned'
+        if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'product_returned') {
+            print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
+            print $form->textwithpicto($langs->trans("ProductReturned") . ':', $langs->trans("HelpAbandonProductReturned"), - 1);
+            print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
+            $resteapayeraffiche = 0;
+        }
+        // Paye partiellement ou Abandon 'abandon'
+        if (($object->statut == 2 || $object->statut == 3) && $object->close_code == 'abandon') {
+            print '<tr><td colspan="' . $nbcols . '" align="right" class="nowrap">';
+            $text = $langs->trans("HelpAbandonOther");
+            if ($object->close_note)
+                $text .= '<br><br><b>' . $langs->trans("Reason") . '</b>:' . $object->close_note;
+            print $form->textwithpicto($langs->trans("Abandoned") . ':', $text, - 1);
+            print '</td><td align="right">' . price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaye) . '</td><td>&nbsp;</td></tr>';
+            $resteapayeraffiche = 0;
+        }
+        
+        // Billed
+        print '<tr><td colspan="' . $nbcols . '" align="right">' . $langs->trans("Billed") . ' :</td><td align="right">' . price($object->total_ttc) . '</td><td>&nbsp;</td></tr>';
+        
+        // Remainder to pay
+        print '<tr><td colspan="' . $nbcols . '" align="right">';
+        if ($resteapayeraffiche >= 0)
+            print $langs->trans('RemainderToPay');
+        else
+            print $langs->trans('ExcessReceived');
+        print ' :</td>';
+        print '<td align="right" class="amountremaintopay">' . price($resteapayeraffiche) . '</td>';
+        print '<td class="nowrap">&nbsp;</td></tr>';
+    } 
+    else // Credit note
+    {
+        // Total already paid back
+        print '<tr><td colspan="' . $nbcols . '" align="right">';
+        print $langs->trans('AlreadyPaidBack');
+        print ' :</td><td align="right">' . price($sign * $totalpaye) . '</td><td>&nbsp;</td></tr>';
+        
+        // Billed
+        print '<tr><td colspan="' . $nbcols . '" align="right">' . $langs->trans("Billed") . ' :</td><td align="right">' . price($sign * $object->total_ttc) . '</td><td>&nbsp;</td></tr>';
+        
+        // Remainder to pay back
+        print '<tr><td colspan="' . $nbcols . '" align="right">';
+        if ($resteapayeraffiche <= 0)
+            print $langs->trans('RemainderToPayBack');
+        else
+            print $langs->trans('ExcessPaydBack');
+        print ' :</td>';
+        print '<td align="right" bgcolor="#f0f0f0"><b>' . price($sign * $resteapayeraffiche) . '</b></td>';
+        print '<td class="nowrap">&nbsp;</td></tr>';
+        
+        // Sold credit note
+        // print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans('TotalTTC').' :</td>';
+        // print '<td align="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($sign *
+        // $object->total_ttc).'</b></td><td>&nbsp;</td></tr>';
+    }
+    
+    print '</table>';
+	                            
+	// Margin Infos
+	if (! empty($conf->margin->enabled)) {
+	    $formmargin->displayMarginInfos($object);
+	}
+	
 
+	print '</div>';
+	print '</div>';
+	print '</div>';
+	
+	print '<div class="clearboth"></div><br>';
+	
 	if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
 		$blocname = 'contacts';
 		$title = $langs->trans('ContactsAddresses');
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index 32e6ca242ca..e0ceb287ea9 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -1516,6 +1516,8 @@ class Facture extends CommonInvoice
 	 */
 	function set_ref_client($ref_client, $notrigger=0)
 	{
+	    global $user;
+	    
 		$error=0;
 
 		$this->db->begin();
diff --git a/htdocs/core/class/html.formmargin.class.php b/htdocs/core/class/html.formmargin.class.php
index 99159f62561..0ac6012f271 100644
--- a/htdocs/core/class/html.formmargin.class.php
+++ b/htdocs/core/class/html.formmargin.class.php
@@ -209,7 +209,7 @@ class FormMargin
     	    if (!empty($hidemargininfos)) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
 		}
 
-		print '<table class="border margintable" width="100%">';
+		print '<table class="noborder margintable centpercent">';
 		print '<tr class="liste_titre">';
 		print '<td class="liste_titre">'.$langs->trans('Margins').'</td>';
 		print '<td class="liste_titre" align="right">'.$langs->trans('SellingPrice').'</td>';
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 6bc46e1dfcf..7c4a19d729b 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -986,6 +986,12 @@ function dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='r
             $morehtmlright.=$object->getLibStatut(5,1);
         }
 	}
+	elseif ($object->element == 'facture')
+	{
+	    $tmptxt=$object->getLibStatut(6, $object->totalpaye);
+	    if (empty($tmptxt) || $tmptxt == $object->getLibStatut(3)) $tmptxt=$object->getLibStatut(5, $object->totalpaye); 
+		$morehtmlright.=$tmptxt;
+	}
 	else {
 	    $tmptxt=$object->getLibStatut(6);
 	    if (empty($tmptxt) || $tmptxt == $object->getLibStatut(3)) $tmptxt=$object->getLibStatut(5); 
diff --git a/htdocs/paypal/lib/paypal.lib.php b/htdocs/paypal/lib/paypal.lib.php
index 6587e25ecb1..1496bb3552e 100644
--- a/htdocs/paypal/lib/paypal.lib.php
+++ b/htdocs/paypal/lib/paypal.lib.php
@@ -209,7 +209,7 @@ function showPaypalPaymentUrl($type,$ref)
     $out='<br><br>';
     $out.=img_picto('','object_globe.png').' '.$langs->trans("ToOfferALinkForOnlinePayment",$servicename).'<br>';
     $url=getPaypalPaymentUrl(0,$type,$ref);
-    $out.='<input type="text" id="paypalurl" value="'.$url.'" size="60"><br>';
+    $out.='<input type="text" id="paypalurl" class="quatrevingtpercent" value="'.$url.'"><br>';
     return $out;
 }
 
diff --git a/htdocs/theme/eldy/style.css.php b/htdocs/theme/eldy/style.css.php
index d2834a87669..f415b214742 100644
--- a/htdocs/theme/eldy/style.css.php
+++ b/htdocs/theme/eldy/style.css.php
@@ -580,6 +580,14 @@ div.myavailability {
 .tablelistofcalendars {
 	margin-top: 25px !important;
 }
+.amountalreadypaid {
+	color: #008800;
+	font-weight: bold;
+}
+.amountremaintopay {
+	color: #880000;
+	font-weight: bold;
+}
 
 /* DOL_XXX for future usage (when left menu has been removed). If we do not use datatable */
 .table-responsive {
@@ -2833,6 +2841,7 @@ tr.liste_sub_total, tr.liste_sub_total td {
 	border-top-width: <?php echo $borderwith ?>px !important;
 	border-top-color: rgb(<?php echo $colortopbordertitle1 ?>) !important;
 	border-top-style: solid !important;
+	margin: 0px 0px 0px 0px !important;
 }
 .paymenttable tr td:first-child, .margintable tr td:first-child
 {
-- 
GitLab