From fd4cda819c4f5e6215321e7935000d58d7f3d723 Mon Sep 17 00:00:00 2001
From: phf <phf@atm-consulting.fr>
Date: Sun, 21 Feb 2016 22:47:52 +0100
Subject: [PATCH] New add supplier payment

---
 .../compta/paiement/class/paiement.class.php  |  2 +-
 htdocs/fourn/class/paiementfourn.class.php    | 86 ++++++++++++++++---
 htdocs/fourn/facture/card.php                 |  5 +-
 htdocs/fourn/facture/paiement.php             |  2 +
 .../install/mysql/migration/3.9.0-4.0.0.sql   |  1 +
 .../mysql/tables/llx_paiementfourn.sql        |  1 +
 6 files changed, 80 insertions(+), 17 deletions(-)

diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php
index 7b6c7dd2f5b..daf43ec9d64 100644
--- a/htdocs/compta/paiement/class/paiement.class.php
+++ b/htdocs/compta/paiement/class/paiement.class.php
@@ -150,7 +150,7 @@ class Paiement extends CommonObject
 		global $conf, $langs;
 
 		$error = 0;
-		$way = $this->getWay();  
+		$way = $this->getWay();
 		
 		$now=dol_now();
 		
diff --git a/htdocs/fourn/class/paiementfourn.class.php b/htdocs/fourn/class/paiementfourn.class.php
index bb84f8ca917..940de150765 100644
--- a/htdocs/fourn/class/paiementfourn.class.php
+++ b/htdocs/fourn/class/paiementfourn.class.php
@@ -27,6 +27,7 @@
  */
 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
+require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
 
 /**
  *	Class to manage payments for supplier invoices
@@ -134,29 +135,58 @@ class PaiementFourn extends Paiement
 		global $langs,$conf;
 
 		$error = 0;
+		$way = $this->getWay();
 
 		// Clean parameters
-		$this->total = 0;
-		foreach ($this->amounts as $key => $value)
+		$totalamount = 0;
+		$totalamount_converted = 0;
+		
+		if ($way == 'dolibarr')
 		{
-			$newvalue = price2num($value, 'MT');
-			$this->amounts[$key] = $newvalue;
-			$this->total += $newvalue;
+			$amounts = &$this->amounts;
+			$amounts_to_update = &$this->multicurrency_amounts;
 		}
-		$this->total = price2num($this->total);
-
+		else
+		{
+			$amounts = &$this->multicurrency_amounts;
+			$amounts_to_update = &$this->amounts;
+		}
+		
+		foreach ($amounts as $key => $value)
+		{
+			$value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value, $way, 'facture_fourn');
+			$totalamount_converted += $value_converted;
+			$amounts_to_update[$key] = price2num($value_converted, 'MT');
+			
+			$newvalue = price2num($value,'MT');
+			$amounts[$key] = $newvalue;
+			$totalamount += $newvalue;
+		}
+		$totalamount = price2num($totalamount);
+		$totalamount_converted = price2num($totalamount_converted);
 
 		$this->db->begin();
 
-		if ($this->total <> 0) // On accepte les montants negatifs
+		if ($totalamount <> 0) // On accepte les montants negatifs
 		{
 			$ref = $this->getNextNumRef('');
 			$now=dol_now();
-
+			
+			if ($way == 'dolibarr')
+			{
+				$total = $totalamount;
+				$mtotal = $totalamount_converted; // Maybe use price2num with MT for the converted value
+			}
+			else
+			{
+				$total = $totalamount_converted; // Maybe use price2num with MT for the converted value
+				$mtotal = $totalamount;
+			}
+		
 			$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn (';
-			$sql.= 'ref, entity, datec, datep, amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
+			$sql.= 'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
 			$sql.= " VALUES ('".$this->db->escape($ref)."', ".$conf->entity.", '".$this->db->idate($now)."',";
-			$sql.= " '".$this->db->idate($this->datepaye)."', '".$this->total."', ".$this->paiementid.", '".$this->num_paiement."', '".$this->db->escape($this->note)."', ".$user->id.", 0)";
+			$sql.= " '".$this->db->idate($this->datepaye)."', '".$total."', '".$mtotal."', ".$this->paiementid.", '".$this->num_paiement."', '".$this->db->escape($this->note)."', ".$user->id.", 0)";
 
 			dol_syslog("PaiementFourn::create", LOG_DEBUG);
 			$resql = $this->db->query($sql);
@@ -171,8 +201,8 @@ class PaiementFourn extends Paiement
 					if (is_numeric($amount) && $amount <> 0)
 					{
 						$amount = price2num($amount);
-						$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount)';
-						$sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\')';
+						$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount)';
+						$sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\', \''.$this->multicurrency_amounts[$key].'\')';
 						$resql=$this->db->query($sql);
 						if ($resql)
 						{
@@ -229,8 +259,11 @@ class PaiementFourn extends Paiement
 			$error++;
 		}
 
-		if ($this->total <> 0 && $error == 0) // On accepte les montants negatifs
+		if ($totalamount <> 0 && $error == 0) // On accepte les montants negatifs
 		{
+			$this->amount=$total;
+		    $this->total=$total;
+			$this->multicurrency_amount=$mtotal;
 			$this->db->commit();
 			dol_syslog('PaiementFourn::Create Ok Total = '.$this->total);
 			return $this->id;
@@ -625,4 +658,29 @@ class PaiementFourn extends Paiement
 			return "";
 		}
 	}
+
+	/**
+	 * 	get the right way of payment
+	 * 
+	 * 	@return 	string 	'dolibarr' if standard comportment or paid in dolibarr currency, 'customer' if payment received from multicurrency inputs
+	 */
+	function getWay()
+	{
+		global $conf;
+		
+		$way = 'dolibarr';
+		if (!empty($conf->multicurrency->enabled))
+		{
+			foreach ($this->multicurrency_amounts as $value)
+			{
+				if (!empty($value)) // one value found then payment is in invoice currency
+				{
+					$way = 'customer';
+					break;
+				}
+			}
+		}
+		
+		return $way;
+	}
 }
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index 6860d5b6bb6..73def71adc1 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -1269,14 +1269,15 @@ if ($action == 'create')
 
     dol_htmloutput_events();
 
+	$currency_code = $conf->currency;
+	
     $societe='';
     if (GETPOST('socid') > 0)
     {
         $societe=new Societe($db);
         $societe->fetch(GETPOST('socid','int'));
+		if (!empty($conf->multicurrency->enabled) && !empty($societe->multicurrency_code)) $currency_code = $societe->multicurrency_code;
     }
-
-	$currency_code = $conf->currency;
 	
     if (GETPOST('origin') && GETPOST('originid'))
     {
diff --git a/htdocs/fourn/facture/paiement.php b/htdocs/fourn/facture/paiement.php
index 7aab308d2fa..dd9d8fae87f 100644
--- a/htdocs/fourn/facture/paiement.php
+++ b/htdocs/fourn/facture/paiement.php
@@ -497,7 +497,9 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
 	                        print '<tr class="liste_total">';
 	                        print '<td colspan="3" align="left">'.$langs->trans('TotalTTC').':</td>';
 	                        print '<td align="right"><b>'.price($total_ttc).'</b></td>';
+							if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
 	                        print '<td align="right"><b>'.price($totalrecu).'</b></td>';
+							if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
 	                        print '<td align="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
 	                        print '<td align="center">&nbsp;</td>';
 	                        print "</tr>\n";
diff --git a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
index 2b38f915869..c05af05a2d5 100644
--- a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
+++ b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
@@ -262,6 +262,7 @@ ALTER TABLE llx_contratdet ADD COLUMN multicurrency_total_ttc double(24,8) DEFAU
 
 ALTER TABLE llx_paiement ADD COLUMN multicurrency_amount double(24,8) DEFAULT 0;
 ALTER TABLE llx_paiement_facture ADD COLUMN multicurrency_amount double(24,8) DEFAULT 0;
+ALTER TABLE llx_paiementfourn ADD COLUMN multicurrency_amount double(24,8) DEFAULT 0;
 ALTER TABLE llx_paiementfourn_facturefourn ADD COLUMN multicurrency_amount double(24,8) DEFAULT 0;
 
 ALTER TABLE llx_societe_remise_except ADD COLUMN multicurrency_amount_ht double(24,8) NOT NULL;
diff --git a/htdocs/install/mysql/tables/llx_paiementfourn.sql b/htdocs/install/mysql/tables/llx_paiementfourn.sql
index cbcc93d9b0f..aba54627a09 100644
--- a/htdocs/install/mysql/tables/llx_paiementfourn.sql
+++ b/htdocs/install/mysql/tables/llx_paiementfourn.sql
@@ -26,6 +26,7 @@ create table llx_paiementfourn
   datec             datetime,          -- date de creation de l'enregistrement
   datep             datetime,          -- date de paiement
   amount            real DEFAULT 0,    -- montant
+  multicurrency_amount double(24,8) DEFAULT 0,    -- multicurrency amount
   fk_user_author    integer,           -- auteur
   fk_paiement       integer NOT NULL,  -- moyen de paiement
   num_paiement      varchar(50),       -- numero de paiement (cheque)
-- 
GitLab