From 1f8ce55a4346249ee6b1f9da68ac93331075e2f6 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Mon, 27 Dec 2010 18:46:45 +0000
Subject: [PATCH] Fix: Payment amount was lost or with wrong sign

---
 .../compta/paiement/class/paiement.class.php  | 40 ++++----
 htdocs/compta/paiement_charge.php             | 96 ++++++++++---------
 htdocs/compta/sociales/charges.php            |  5 +-
 .../class/paymentsocialcontribution.class.php | 68 +++++++------
 htdocs/fourn/class/paiementfourn.class.php    | 20 ++--
 htdocs/lib/functions.lib.php                  |  4 +-
 6 files changed, 128 insertions(+), 105 deletions(-)

diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php
index 39c1138371e..57229ba81ee 100644
--- a/htdocs/compta/paiement/class/paiement.class.php
+++ b/htdocs/compta/paiement/class/paiement.class.php
@@ -21,8 +21,7 @@
 /**
  *	\file       htdocs/compta/paiement/class/paiement.class.php
  *	\ingroup    facture
- *	\brief      Fichier de la classe des paiement de factures clients
- *	\remarks	Cette classe est presque identique a paiementfourn.class.php
+ *	\brief      File of class to manage payments of customers invoices
  *	\version    $Id$
  */
 
@@ -41,8 +40,9 @@ class Paiement
 	var $ref;
 	var $facid;
 	var $datepaye;
-	var $amount;
-	var $total;
+    var $total;             // deprecated
+	var $amount;            // Total amount of payment
+	var $amounts=array();   // Array of amounts
 	var $author;
 	var $paiementid;	// Type de paiement. Stocke dans fk_paiement
 	// de llx_paiement qui est lie aux types de
@@ -93,7 +93,8 @@ class Paiement
 				$this->date           = $this->db->jdate($obj->dp);
 				$this->datepaye       = $this->db->jdate($obj->dp);
 				$this->numero         = $obj->num_paiement;
-				$this->montant        = $obj->amount;
+				$this->montant        = $obj->amount;   // deprecated
+				$this->amount         = $obj->amount;
 				$this->note           = $obj->note;
 				$this->type_libelle   = $obj->type_libelle;
 				$this->type_code      = $obj->type_code;
@@ -130,26 +131,26 @@ class Paiement
 
 		$error = 0;
 
-		// Clean parameters
-		$now=dol_now();
+        $now=dol_now();
 
-		$this->total = 0;
+        // Clean parameters
+        $totalamount = 0;
 		foreach ($this->amounts as $key => $value)	// How payment is dispatch
 		{
-			$value = price2num($value,'MT');
-			$this->amounts[$key] = $value;
-			$this->total += $value;
+			$newvalue = price2num($value,'MT');
+			$this->amounts[$key] = $newvalue;
+			$totalamount += $newvalue;
 		}
-		$this->total = price2num($this->total);
+		$totalamount = price2num($totalamount);
 
 		// Check parameters
-		if ($this->total == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
+        if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
 
 
 		$this->db->begin();
 
 		$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_creat)";
-		$sql.= " VALUES ('".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$this->total."', ".$this->paiementid.", '".$this->num_paiement."', '".addslashes($this->note)."', ".$user->id.")";
+		$sql.= " VALUES ('".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$totalamount."', ".$this->paiementid.", '".$this->num_paiement."', '".addslashes($this->note)."', ".$user->id.")";
 
 		dol_syslog(get_class($this)."::Create insert paiement sql=".$sql);
 		$resql = $this->db->query($sql);
@@ -201,6 +202,8 @@ class Paiement
 
 		if (! $error)
 		{
+		    $this->amount=$totalamount;
+		    $this->total=$totalamount;    // deprecated
 			$this->db->commit();
 			return $this->id;
 		}
@@ -325,15 +328,16 @@ class Paiement
             $acc = new Account($this->db);
             $acc->fetch($accountid);
 
-            $total=$this->total;
-            if ($mode == 'payment') $total=$this->total;
-            if ($mode == 'payment_supplier') $total=-$total;
+            $totalamount=$this->amount;
+            if (empty($totalamount)) $totalamount=$this->total; // For backward compatibility
+            if ($mode == 'payment') $totalamount=$totalamount;
+            if ($mode == 'payment_supplier') $totalamount=-$totalamount;
 
             // Insert payment into llx_bank
             $bank_line_id = $acc->addline($this->datepaye,
             $this->paiementid,  // Payment mode id or code ("CHQ or VIR for example")
             $label,
-            $total,
+            $totalamount,
             $this->num_paiement,
             '',
             $user,
diff --git a/htdocs/compta/paiement_charge.php b/htdocs/compta/paiement_charge.php
index abe7eb24555..f265d751e83 100755
--- a/htdocs/compta/paiement_charge.php
+++ b/htdocs/compta/paiement_charge.php
@@ -42,12 +42,12 @@ if ($user->societe_id > 0)
 
 
 /*
- * Actions ajoute paiement
+ * Actions add payment
  */
-if ($_POST["action"] == 'add_paiement')
+if ($_POST["action"] == 'add_payment')
 {
 	$error=0;
-	
+
 	if ($_POST["cancel"])
 	{
 		$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
@@ -78,7 +78,6 @@ if ($_POST["action"] == 'add_paiement')
 		$paymentid = 0;
 
 		// Read possible payments
-		// FIXME add error message if no payment is defined
 		foreach ($_POST as $key => $value)
 		{
 			if (substr($key,0,7) == 'amount_')
@@ -88,47 +87,56 @@ if ($_POST["action"] == 'add_paiement')
 			}
 		}
 
-		$db->begin();
-
-		// Create a line of payments
-		$paiement = new PaymentSocialContribution($db);
-		$paiement->chid         = $chid;
-		$paiement->datepaye     = $datepaye;
-		$paiement->amounts      = $amounts;   // Tableau de montant
-		$paiement->paiementtype = $_POST["paiementtype"];
-		$paiement->num_paiement = $_POST["num_paiement"];
-		$paiement->note         = $_POST["note"];
-
-		if (! $error)
-		{
-		    $paymentid = $paiement->create($user);
-            if ($paymentid < 0)
-            {
-                $errmsg=$paiement->error;
-                $error++;
-            }
-		}
+        if (sizeof($amounts) <= 0)
+        {
+            $error++;
+            $errmsg='ErrorNoPaymentDefined';
+        }
 
         if (! $error)
         {
-            $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'','');
-            if (! $result > 0)
+    		$db->begin();
+
+    		// Create a line of payments
+    		$paiement = new PaymentSocialContribution($db);
+    		$paiement->chid         = $chid;
+    		$paiement->datepaye     = $datepaye;
+    		$paiement->amounts      = $amounts;   // Tableau de montant
+    		$paiement->paiementtype = $_POST["paiementtype"];
+    		$paiement->num_paiement = $_POST["num_paiement"];
+    		$paiement->note         = $_POST["note"];
+
+    		if (! $error)
+    		{
+    		    $paymentid = $paiement->create($user);
+                if ($paymentid < 0)
+                {
+                    $errmsg=$paiement->error;
+                    $error++;
+                }
+    		}
+
+            if (! $error)
             {
-                $errmsg=$paiement->error;
-                $error++;
+                $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'','');
+                if (! $result > 0)
+                {
+                    $errmsg=$paiement->error;
+                    $error++;
+                }
             }
-        }
 
-	    if (! $error)
-        {
-            $db->commit();
-            $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
-            Header('Location: '.$loc);
-            exit;
-        }
-        else
-        {
-            $db->rollback();
+    	    if (! $error)
+            {
+                $db->commit();
+                $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
+                Header('Location: '.$loc);
+                exit;
+            }
+            else
+            {
+                $db->rollback();
+            }
         }
 	}
 
@@ -164,10 +172,10 @@ if ($_GET["action"] == 'create')
 		print "<div class=\"error\">$mesg</div>";
 	}
 
-	print '<form name="add_paiement" action="paiement_charge.php" method="post">';
+	print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
 	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
-	print "<input type=\"hidden\" name=\"id\" value=\"$charge->id\">";
-	print '<input type="hidden" name="action" value="add_paiement">';
+	print '<input type="hidden" name="id" value="'.$charge->id.'">';
+	print '<input type="hidden" name="action" value="add_payment">';
 
 	print '<table cellspacing="0" class="border" width="100%" cellpadding="2">';
 
@@ -202,7 +210,7 @@ if ($_GET["action"] == 'create')
 	print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
 	$datepaye = dol_mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
 	$datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaye):0;
-	$html->select_date($datepayment,'','','','',"add_paiement",1,1);
+	$html->select_date($datepayment,'','','','',"add_payment",1,1);
 	print "</td>";
 	print '<td>'.$langs->trans("Comments").'</td></tr>';
 
@@ -213,7 +221,7 @@ if ($_GET["action"] == 'create')
 	print '<td rowspan="3" valign="top"><textarea name="comment" wrap="soft" cols="40" rows="'.ROWS_3.'"></textarea></td></tr>';
 
 	print '<tr>';
-	print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
+	print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
 	print '<td>';
 	$html->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$charge->accountid, "accountid", 0, "courant=1",1);  // Affiche liste des comptes courant
 	print '</td></tr>';
diff --git a/htdocs/compta/sociales/charges.php b/htdocs/compta/sociales/charges.php
index 9b4d5da15ab..3b259927c6e 100644
--- a/htdocs/compta/sociales/charges.php
+++ b/htdocs/compta/sociales/charges.php
@@ -308,7 +308,7 @@ if ($chid > 0)
 		// Type
 		print "<tr><td>".$langs->trans("Type")."</td><td>".$cha->type_libelle."</td><td>".$langs->trans("Payments")."</td></tr>";
 
-		// Period en date
+		// Period end date
 		print "<tr><td>".$langs->trans("PeriodEndDate")."</td>";
 		print "<td>";
 		if ($_GET['action'] == 'edit')
@@ -399,9 +399,10 @@ if ($chid > 0)
 		// Amount
 		print '<tr><td>'.$langs->trans("AmountTTC").'</td><td>'.price($cha->amount).'</td></tr>';
 
+		// Status
 		print '<tr><td>'.$langs->trans("Status").'</td><td>'.$cha->getLibStatut(4).'</td></tr>';
 
-		print '<tr><td coslpan="2">&nbsp;</td></tr>';
+		print '<tr><td colspan="2">&nbsp;</td></tr>';
 
 		if ($_GET['action'] == 'edit')
 		{
diff --git a/htdocs/compta/sociales/class/paymentsocialcontribution.class.php b/htdocs/compta/sociales/class/paymentsocialcontribution.class.php
index 6b91c2bb69d..33280164094 100644
--- a/htdocs/compta/sociales/class/paymentsocialcontribution.class.php
+++ b/htdocs/compta/sociales/class/paymentsocialcontribution.class.php
@@ -18,9 +18,9 @@
  */
 
 /**
- *      \file       htdocs/compta/sociales/class/chargesociales.class.php
+ *      \file       htdocs/compta/sociales/class/paymentsocialcontribution.class.php
  *		\ingroup    facture
- *		\brief      Fichier de la classe des charges sociales
+ *		\brief      File of class to manage payment of social contributions
  *		\version    $Id$
  */
 
@@ -45,7 +45,8 @@ class PaymentSocialContribution extends CommonObject
 	var $datec='';
 	var $tms='';
 	var $datep='';
-	var $amount;
+    var $amount;            // Total amount of payment
+    var $amounts=array();   // Array of amounts
 	var $fk_typepaiement;
 	var $num_paiement;
 	var $note;
@@ -64,25 +65,26 @@ class PaymentSocialContribution extends CommonObject
 	}
 
 	/**
-	 *      \brief      Creation d'un paiement de charge sociale dans la base
-	 *      \param      user    Utilisateur qui cree le paiement
-	 *      \return     int     <0 si KO, id du paiement cree si OK
+	 *      Create payment of social contribution into database.
+     *      Use this->amounts to have list of lines for the payment
+	 *      @param      user    User making payment
+	 *      @return     int     <0 if KO, id of payment if OK
 	 */
 	function create($user)
 	{
 		global $conf, $langs;
-		
+
 		$error=0;
 
-		// Validation parametres
+        $now=dol_now();
+
+        // Validate parametres
 		if (! $this->datepaye)
 		{
 			$this->error='ErrorBadValueForParameters';
 			return -1;
 		}
 
-		$now=dol_now();
-
 		// Clean parameters
 		if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
 		if (isset($this->amount)) $this->amount=trim($this->amount);
@@ -93,24 +95,30 @@ class PaymentSocialContribution extends CommonObject
 		if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
 		if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
 
-		$this->db->begin();
+        $totalamount = 0;
+        foreach ($this->amounts as $key => $value)  // How payment is dispatch
+        {
+            $newvalue = price2num($value,'MT');
+            $this->amounts[$key] = $newvalue;
+            $totalamount += $newvalue;
+        }
+        $totalamount = price2num($totalamount);
 
-		$total=0;
-		foreach ($this->amounts as $key => $value)
-		{
-			$amount = price2num(trim($value), 'MT');
-			$total += $amount;
-		}
+        // Check parameters
+        if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
 
-		if ($total != 0)
+
+		$this->db->begin();
+
+		if ($totalamount != 0)
 		{
 			$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
 			$sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
-			$sql.= " VALUES ($this->chid, '".$this->db->idate($now)."', ";
-			$sql.= " '".$this->db->idate($this->datepaye)."', ";
-			$sql.= price2num($total);
-			$sql.= ", ".$this->paiementtype.", '".addslashes($this->num_paiement)."', '".addslashes($this->note)."', ".$user->id.",";
-			$sql.= "0)";
+			$sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
+			$sql.= " '".$this->db->idate($this->datepaye)."',";
+			$sql.= " ".$totalamount.",";
+			$sql.= " ".$this->paiementtype.", '".addslashes($this->num_paiement)."', '".addslashes($this->note)."', ".$user->id.",";
+			$sql.= " 0)";
 
 			dol_syslog(get_class($this)."::create sql=".$sql);
 			$resql=$this->db->query($sql);
@@ -125,9 +133,11 @@ class PaymentSocialContribution extends CommonObject
 
 		}
 
-		if ($total != 0 && ! $error)
+		if ($totalamount != 0 && ! $error)
 		{
-			$this->db->commit();
+		    $this->amount=$totalamount;
+            $this->total=$totalamount;    // deprecated
+		    $this->db->commit();
 			return $this->id;
 		}
 		else
@@ -442,7 +452,7 @@ class PaymentSocialContribution extends CommonObject
 
 
     /**
-     *      A record into bank for payment with links between this bank record and invoices of payment.
+     *      Add record into bank for payment with links between this bank record and invoices of payment.
      *      All payment properties must have been set first like after a call to create().
      *      @param      user                Object of user making payment
      *      @param      mode                'payment_sc'
@@ -569,19 +579,19 @@ class PaymentSocialContribution extends CommonObject
 		global $langs;
 
 		$result='';
-		
+
 		if (empty($this->ref)) $this->ref=$this->lib;
 
 		if (!empty($this->id))
 		{
 			$lien = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id='.$this->id.'">';
 			$lienfin='</a>';
-			
+
 			if ($withpicto) $result.=($lien.img_object($langs->trans("ShowPayment").': '.$this->ref,'payment').$lienfin.' ');
 			if ($withpicto && $withpicto != 2) $result.=' ';
 			if ($withpicto != 2) $result.=$lien.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$lienfin;
 		}
-		
+
 		return $result;
 	}
 }
diff --git a/htdocs/fourn/class/paiementfourn.class.php b/htdocs/fourn/class/paiementfourn.class.php
index a4f2dd76916..9f04b9b5e3a 100644
--- a/htdocs/fourn/class/paiementfourn.class.php
+++ b/htdocs/fourn/class/paiementfourn.class.php
@@ -20,19 +20,18 @@
  */
 
 /**
-		\file       htdocs/fourn/class/paiementfourn.class.php
-		\ingroup    fournisseur, facture
-		\brief      Classe paiement fournisseur
-        \remarks	Cette classe est presque identique a paiement.class.php
-		\version    $Id$
-*/
+ *		\file       htdocs/fourn/class/paiementfourn.class.php
+ *		\ingroup    fournisseur, facture
+ *		\brief      File of class to manage payments of suppliers invoices
+ *		\version    $Id$
+ */
 require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
 require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php');
 
 /**
-	\class      PaiementFourn
-	\brief      Classe permettant la gestion des paiements des factures fournisseurs
-*/
+ *	\class      PaiementFourn
+ *	\brief      Classe permettant la gestion des paiements des factures fournisseurs
+ */
 class PaiementFourn extends Paiement
 {
     var $db;
@@ -44,8 +43,9 @@ class PaiementFourn extends Paiement
 	var $ref;
 	var $facid;
 	var $datepaye;
-	var $amount;
 	var $total;
+    var $amount;            // Total amount of payment
+    var $amounts=array();   // Array of amounts
 	var $author;
 	var $paiementid;	// Type de paiement. Stocke dans fk_paiement
 						// de llx_paiement qui est lie aux types de
diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php
index 83034352d33..535f35cbcf6 100644
--- a/htdocs/lib/functions.lib.php
+++ b/htdocs/lib/functions.lib.php
@@ -1192,7 +1192,7 @@ function img_object($alt, $object, $cssclass='')
 {
 	global $conf,$langs;
 
-	$cssclass = (!empty($cssclass)?'class="'.$cssclass.'"':'');
+	$cssclass = (!empty($cssclass)?' class="'.$cssclass.'"':'');
 	$path =  'theme/'.$conf->theme;
 	$url = DOL_URL_ROOT;
 
@@ -1204,7 +1204,7 @@ function img_object($alt, $object, $cssclass='')
 		if (DOL_URL_ROOT_ALT && ! file_exists(DOL_DOCUMENT_ROOT.'/'.$path.'/img/object_'.$object.'.png')) $url = DOL_URL_ROOT_ALT;
 	}
 
-	return '<img src="'.$url.'/'.$path.'/img/object_'.$object.'.png" border="0" alt="'.dol_escape_htmltag($alt).'" title="'.dol_escape_htmltag($alt).'" '.$cssclass.'>';
+	return '<img src="'.$url.'/'.$path.'/img/object_'.$object.'.png" border="0" alt="'.dol_escape_htmltag($alt).'" title="'.dol_escape_htmltag($alt).'"'.$cssclass.'>';
 }
 
 /**
-- 
GitLab