From 078b015db8014d3df8f5285c13b017031c3ca6c4 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 3 Nov 2012 16:32:49 +0100
Subject: [PATCH] Work on multitax (vat). Need your test. Qual: Because params
 were often missing in calculate_price(), params are now mandatory.

---
 htdocs/admin/limits.php                       | 18 ++---
 htdocs/cashdesk/class/Facturation.class.php   |  2 +-
 htdocs/comm/propal/class/propal.class.php     |  2 +-
 htdocs/commande/class/commande.class.php      |  2 +-
 htdocs/contrat/class/contrat.class.php        |  7 +-
 htdocs/core/lib/price.lib.php                 | 54 ++++++++-----
 htdocs/expedition/class/expedition.class.php  |  2 +-
 .../class/fournisseur.commande.class.php      | 29 ++-----
 .../fourn/class/fournisseur.facture.class.php | 10 +--
 htdocs/install/upgrade2.php                   | 10 +--
 htdocs/margin/lib/margins.lib.php             | 81 ++++++++++---------
 11 files changed, 110 insertions(+), 107 deletions(-)

diff --git a/htdocs/admin/limits.php b/htdocs/admin/limits.php
index 8f7369de21b..1f4233a540f 100644
--- a/htdocs/admin/limits.php
+++ b/htdocs/admin/limits.php
@@ -173,21 +173,21 @@ print '<b>'.$langs->trans("ExamplesWithCurrentSetup").":</b><br>\n";
 
 // Always show vat rates with vat 0
 $s=2/7;$qty=1;$vat=0;
-$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0,0);
 print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
 print " x ".$langs->trans("Quantity").": ".$qty;
 print " - ".$langs->trans("VAT").": ".$vat.'%';
 print " &nbsp; -> &nbsp; ".$langs->trans("TotalPriceAfterRounding").": ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
 
 $s=10/3;$qty=1;$vat=0;
-$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0,0);
 print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
 print " x ".$langs->trans("Quantity").": ".$qty;
 print " - ".$langs->trans("VAT").": ".$vat.'%';
 print " &nbsp; -> &nbsp; ".$langs->trans("TotalPriceAfterRounding").": ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
 
 $s=10/3;$qty=2;$vat=0;
-$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+$tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0, 0);
 print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
 print " x ".$langs->trans("Quantity").": ".$qty;
 print " - ".$langs->trans("VAT").": ".$vat.'%';
@@ -222,7 +222,7 @@ if (count($vat_rates))
         for ($qty=1; $qty<=2; $qty++)
         {
             $s=10/3;
-            $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+            $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0, 0);
             print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
             print " x ".$langs->trans("Quantity").": ".$qty;
             print " - ".$langs->trans("VAT").": ".$vat.'%';
@@ -237,14 +237,14 @@ else
     // were calculated to show all possible cases of rounding. If we change this, examples becomes useless or show the same rounding rule.
 
     $s=10/3;$qty=1;$vat=10;
-    $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+    $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0, 0);
     print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
     print " x ".$langs->trans("Quantity").": ".$qty;
     print " - ".$langs->trans("VAT").": ".$vat.'%';
     print " &nbsp; -> &nbsp; ".$langs->trans("TotalPriceAfterRounding").": ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
 
     $s=10/3;$qty=2;$vat=10;
-    $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0);
+    $tmparray=calcul_price_total(1,$qty*price2num($s,'MU'),0,$vat,0,0,0,'HT',0, 0);
     print $langs->trans("UnitPriceOfProduct").": ".price2num($s,'MU');
     print " x ".$langs->trans("Quantity").": ".$qty;
     print " - ".$langs->trans("VAT").": ".$vat.'%';
@@ -264,8 +264,8 @@ foreach($vat_rates as $vat)
 		$s2=2/7;
 
 		// Round by line
-		$tmparray1=calcul_price_total(1,$qty*price2num($s1,'MU'),0,$vat,0,0,0,'HT',0);
-		$tmparray2=calcul_price_total(1,$qty*price2num($s2,'MU'),0,$vat,0,0,0,'HT',0);
+		$tmparray1=calcul_price_total(1,$qty*price2num($s1,'MU'),0,$vat,0,0,0,'HT',0, 0);
+		$tmparray2=calcul_price_total(1,$qty*price2num($s2,'MU'),0,$vat,0,0,0,'HT',0, 0);
 		$total_ht = $tmparray1[0] + $tmparray2[0];
 		$total_tva = $tmparray1[1] + $tmparray2[1];
 		$total_ttc = $tmparray1[2] + $tmparray2[2];
@@ -288,7 +288,7 @@ foreach($vat_rates as $vat)
 
 		// Global round
 		$subtotal_ht = (($qty*price2num($s1,'MU')) + ($qty*price2num($s2,'MU')));
-		$tmparray3=calcul_price_total(1,$subtotal_ht,0,$vat,0,0,0,'HT',0);
+		$tmparray3=calcul_price_total(1,$subtotal_ht,0,$vat,0,0,0,'HT',0, 0);
 		$total_ht = $tmparray3[0];
 		$total_tva = $tmparray3[1];
 		$total_ttc = $tmparray3[2];
diff --git a/htdocs/cashdesk/class/Facturation.class.php b/htdocs/cashdesk/class/Facturation.class.php
index 2c5d1b6df0d..9eda4232281 100644
--- a/htdocs/cashdesk/class/Facturation.class.php
+++ b/htdocs/cashdesk/class/Facturation.class.php
@@ -118,7 +118,7 @@ class Facturation
         }
 
         // Define part of HT, VAT, TTC
-        $resultarray=calcul_price_total($this->qte,$this->prix(),$this->remisePercent(),$vat_rate,0,0,$product->type,'HT',0);
+        $resultarray=calcul_price_total($this->qte,$this->prix(),$this->remisePercent(),$vat_rate,0,0,0,'HT',$product->type);
 
         // Calcul du total ht sans remise
         $total_ht = $resultarray[0];
diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php
index d1d3cf69423..dd840e1729e 100644
--- a/htdocs/comm/propal/class/propal.class.php
+++ b/htdocs/comm/propal/class/propal.class.php
@@ -352,7 +352,7 @@ class Propal extends CommonObject
             // qty, pu, remise_percent et txtva
             // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
             // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-            $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits,$type);
+            $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type);
             $total_ht  = $tabprice[0];
             $total_tva = $tabprice[1];
             $total_ttc = $tabprice[2];
diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php
index fe3c9bfd424..07050f41a01 100644
--- a/htdocs/commande/class/commande.class.php
+++ b/htdocs/commande/class/commande.class.php
@@ -1055,7 +1055,7 @@ class Commande extends CommonOrder
             // qty, pu, remise_percent et txtva
             // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
             // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-            $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits,$type);
+            $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type);
             $total_ht  = $tabprice[0];
             $total_tva = $tabprice[1];
             $total_ttc = $tabprice[2];
diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php
index c51c3f1a85f..19e0130cf29 100644
--- a/htdocs/contrat/class/contrat.class.php
+++ b/htdocs/contrat/class/contrat.class.php
@@ -892,7 +892,7 @@ class Contrat extends CommonObject
 			// qty, pu, remise_percent et txtva
 			// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
 			// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-			$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
+			$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1);
 			$total_ht  = $tabprice[0];
 			$total_tva = $tabprice[1];
 			$total_ttc = $tabprice[2];
@@ -995,6 +995,7 @@ class Contrat extends CommonObject
 		$tvatx = price2num($tvatx);
 		$localtax1tx = price2num($localtax1tx);
 		$localtax2tx = price2num($localtax2tx);
+
 		$subprice = $price;
 		$remise = 0;
 		if (dol_strlen($remise_percent) > 0)
@@ -1015,7 +1016,7 @@ class Contrat extends CommonObject
 		// qty, pu, remise_percent et txtva
 		// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
 		// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-		$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $localtaxtx1, $txlocaltaxtx2, 0, $price_base_type, $info_bits);
+		$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $localtaxtx1, $txlocaltaxtx2, 0, $price_base_type, $info_bits, 1);
 		$total_ht  = $tabprice[0];
 		$total_tva = $tabprice[1];
 		$total_ttc = $tabprice[2];
@@ -1879,7 +1880,7 @@ class ContratLigne
 		// qty, pu, remise_percent et txtva
 		// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
 		// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-		$tabprice=calcul_price_total($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, 'HT', 0);
+		$tabprice=calcul_price_total($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, 'HT', 0, 1);
 		$this->total_ht  = $tabprice[0];
 		$this->total_tva = $tabprice[1];
 		$this->total_ttc = $tabprice[2];
diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php
index d6188ec75a0..e69c15a3a95 100644
--- a/htdocs/core/lib/price.lib.php
+++ b/htdocs/core/lib/price.lib.php
@@ -39,33 +39,46 @@
  * 		@param 	float	$pu                         Unit price (HT or TTC selon price_base_type)
  *		@param 	float	$remise_percent_ligne       Discount for line
  *		@param 	float	$txtva                      Vat rate
- *		@param  float	$localtax1_rate             Localtax1 rate (used for some countries only, like spain). Can also be negative
- *		@param  float	$localtax2_rate             Localtax2 rate (used for some countries only, like spain). Can also be negative
+ *		@param  float	$localtax1_rate             Localtax1 rate (used for some countries only, like spain). Can also be negative. TODO: Remove this param that is not used.
+ *		@param  float	$localtax2_rate             Localtax2 rate (used for some countries only, like spain). Can also be negative. TODO: Remove this param that is not used.
  *		@param 	float	$remise_percent_global		0
  *		@param	string	$price_base_type 			HT=on calcule sur le HT, TTC=on calcule sur le TTC
  *		@param	int		$info_bits					Miscellanous informations on line
  *		@param	int		$type						0/1=Product/service
- *		@param  string	$localtax1_type				Localtax1 type (used for some countries only, like spain)
- *		@param  string	$localtax2_type				Localtax2 type (used for some countries only, like spain)
+ *		@param  string	$seller						Thirdparty seller (we need $seller->country_code property). Provided only if seller is the supplier.
  *		@return result[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]	(total_ht, total_vat, total_ttc, pu_ht, pu_tva, pu_ttc, total_ht_without_discount, total_vat_without_discount, total_ttc_without_discount, ...)
  */
-function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1_rate=0, $localtax2_rate=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0, $localtax1_type = '?', $localtax2_type = '?')
+function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1_rate, $localtax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller = '')
 {
-	global $conf,$mysoc;
+	global $conf,$mysoc,$db;
 
 	$result=array();
 
-	// TODO Remove this code. Added for backward compatibility. To remove once localtaxX_type is provided by caller.
-	if ($localtax1_type == '?')
-	{
-		if ($mysoc->country_code=='ES') $localtax1_type='3';
-		else $localtax1_type='0';
-	}
-	if ($localtax2_type == '?')
-	{
-		if ($mysoc->country_code=='ES') $localtax2_type='1';
-		else $localtax2_type='0';
+	$countryid=$mysoc->country_id;	// By default, seller is user company.
+	if (! empty($seller) && is_object($seller)) $countryid=$seller->country_id;	// If seller is a supplier, $seller is provided
+
+	// Now we search localtaxes information ourself (rates and types).
+	$sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
+	$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as cv";
+	//$sql.= ", ".MAIN_DB_PREFIX."c_pays as cc";
+	$sql.= " WHERE cv.taux = ".$txtva;
+	//$sql.= " AND cv.fk_pays = cc.rowid and cc.code = '".$mysoc->country_code."'";
+	$sql.= " AND cv.fk_pays = ".$countryid;
+	dol_syslog("search vat information sql=".$sql);
+	$resql = $db->query($sql);
+	if ($resql)
+	{
+		$obj = $db->fetch_object($resql);
+		if ($obj)
+		{
+			$localtax1_rate=$obj->localtax1;
+			$localtax2_rate=$obj->localtax2;
+			$localtax1_type=$obj->localtax1_type;
+			$localtax2_type=$obj->localtax2_type;
+			//var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);exit;
+		}
 	}
+	else dol_print_error($db);
 
 	// initialize total (may be HT or TTC depending on price_base_type)
 	$tot_sans_remise = $pu * $qty;
@@ -73,8 +86,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
 	$tot_avec_remise       = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
 
 	// initialize result
-	for ($i=0; $i <= 15; $i++)
-	 $result[$i] = 0;
+	for ($i=0; $i <= 15; $i++) $result[$i] = 0;
 
 	// if there's some localtax including vat, we calculate localtaxes (we will add later)
 	$localtaxes = array(0,0,0);
@@ -164,14 +176,14 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
 
 	// if there's some localtax without vat, we calculate localtaxes (we will add them at end)
     $apply_tax = false;
-    
+
     //If price is 'TTC' we need to have the totals without VAT for a correct calculation
     if ($price_base_type=='TTC')
     {
     	$tot_sans_remise= price2num($tot_sans_remise / (1 + ($txtva / 100)),'MU');
     	$tot_avec_remise= price2num($tot_avec_remise / (1 + ($txtva / 100)),'MU');
     }
-    
+
   	switch($localtax1_type) {
       case '1':     // localtax on product or service
         $apply_tax = true;
@@ -184,7 +196,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
         break;
     }
     if ($apply_tax) {
-    	
+
   		$result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT');	// amount tax1 for total_ht_without_discount
   		$result[8] += $result[14];																				// total_ttc_without_discount + tax1
 
diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php
index 0b2bee2a022..00dde07cb10 100644
--- a/htdocs/expedition/class/expedition.class.php
+++ b/htdocs/expedition/class/expedition.class.php
@@ -918,7 +918,7 @@ class Expedition extends CommonObject
 				$line->subprice			= $obj->subprice;
 				$line->remise_percent	= $obj->remise_percent;
 
-				$tabprice = calcul_price_total($obj->qty_shipped, $obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, 0, 'HT', $info_bits);
+				$tabprice = calcul_price_total($obj->qty_shipped, $obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, 0, 'HT', $info_bits, $obj->fk_product_type);	// We force type to 0
 				$this->total_ht+= $tabprice[0];
 				$this->total_tva+= $tabprice[1];
 				$this->total_ttc+= $tabprice[2];
diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php
index 6b598b1a8b5..d8eaef777ad 100644
--- a/htdocs/fourn/class/fournisseur.commande.class.php
+++ b/htdocs/fourn/class/fournisseur.commande.class.php
@@ -1101,7 +1101,7 @@ class CommandeFournisseur extends CommonOrder
             // qty, pu, remise_percent et txtva
             // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
             // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-            $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
+            $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty);
             $total_ht  = $tabprice[0];
             $total_tva = $tabprice[1];
             $total_ttc = $tabprice[2];
@@ -1110,25 +1110,17 @@ class CommandeFournisseur extends CommonOrder
 
             $subprice = price2num($pu,'MU');
 
-            // TODO A virer
-            // Anciens indicateurs: $price, $remise (a ne plus utiliser)
-            $remise = 0;
-            if ($remise_percent > 0)
-            {
-                $remise = round(($pu * $remise_percent / 100), 2);
-            }
-
             $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
             $sql.= " (fk_commande,label, description,";
             $sql.= " fk_product, product_type,";
-            $sql.= " qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref,";
+            $sql.= " qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, ref,";
             $sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc";
             $sql.= ")";
             $sql.= " VALUES (".$this->id.", '" . $this->db->escape($label) . "','" . $this->db->escape($desc) . "',";
             if ($fk_product) { $sql.= $fk_product.","; }
             else { $sql.= "null,"; }
             $sql.= "'".$product_type."',";
-            $sql.= "'".$qty."', ".$txtva.", ".$txlocaltax1.", ".$txlocaltax2.", ".$remise_percent.",'".price2num($subprice,'MU')."','".price2num($remise)."','".$ref."',";
+            $sql.= "'".$qty."', ".$txtva.", ".$txlocaltax1.", ".$txlocaltax2.", ".$remise_percent.",'".price2num($subprice,'MU')."','".$ref."',";
             $sql.= "'".price2num($total_ht)."',";
             $sql.= "'".price2num($total_tva)."',";
             $sql.= "'".price2num($total_localtax1)."',";
@@ -1597,7 +1589,7 @@ class CommandeFournisseur extends CommonOrder
      *  @param     	double	$price_base_type 	Type of price base
      *	@param		int		$info_bits			Miscellanous informations
      *	@param		int		$type				Type of line (0=product, 1=service)
-     *  @param		int		$notrigger				Disable triggers
+     *  @param		int		$notrigger			Disable triggers
      *	@return    	int             			< 0 if error, > 0 if ok
      */
     function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type=0, $notrigger=false)
@@ -1633,27 +1625,20 @@ class CommandeFournisseur extends CommonOrder
             // qty, pu, remise_percent et txtva
             // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
             // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-            $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
+            $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty);
             $total_ht  = $tabprice[0];
             $total_tva = $tabprice[1];
             $total_ttc = $tabprice[2];
             $total_localtax1 = $tabprice[9];
             $total_localtax2 = $tabprice[10];
 
-            // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
-            $subprice = $pu;
-            $remise = 0;
-            if ($remise_percent > 0)
-            {
-                $remise = round(($pu * $remise_percent / 100),2);
-            }
-            $subprice  = price2num($subprice);
+            $subprice = price2num($pu,'MU');
 
             // Mise a jour ligne en base
             $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseurdet SET";
             $sql.= " description='".$this->db->escape($desc)."'";
             $sql.= ",subprice='".price2num($subprice)."'";
-            $sql.= ",remise='".price2num($remise)."'";
+            //$sql.= ",remise='".price2num($remise)."'";
             $sql.= ",remise_percent='".price2num($remise_percent)."'";
             $sql.= ",tva_tx='".price2num($txtva)."'";
             $sql.= ",localtax1_tx='".price2num($txlocaltax1)."'";
diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php
index 38b085f212f..0ac846810a9 100644
--- a/htdocs/fourn/class/fournisseur.facture.class.php
+++ b/htdocs/fourn/class/fournisseur.facture.class.php
@@ -1009,7 +1009,7 @@ class FactureFournisseur extends CommonInvoice
 
         $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)';
         $sql.= ' VALUES ('.$this->id.')';
-        dol_syslog("Fournisseur.facture::addline sql=".$sql);
+        dol_syslog(get_class($this)."::addline sql=".$sql);
 
         $resql = $this->db->query($sql);
         if ($resql)
@@ -1076,7 +1076,7 @@ class FactureFournisseur extends CommonInvoice
         $pu = price2num($pu);
         $qty  = price2num($qty);
 		$remise_percent=price2num($remise_percent);
-		
+
         // Check parameters
         if (! is_numeric($pu) || ! is_numeric($qty)) return -1;
         if ($type < 0) return -1;
@@ -1092,7 +1092,7 @@ class FactureFournisseur extends CommonInvoice
         // qty, pu, remise_percent et txtva
         // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
         // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
-        $tabprice = calcul_price_total($qty, $pu, $remise_percent, $vatrate, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
+        $tabprice = calcul_price_total($qty, $pu, $remise_percent, $vatrate, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty);
         $total_ht  = $tabprice[0];
         $total_tva = $tabprice[1];
         $total_ttc = $tabprice[2];
@@ -1132,7 +1132,7 @@ class FactureFournisseur extends CommonInvoice
         $sql.= ", product_type = ".$product_type;
         $sql.= " WHERE rowid = ".$id;
 
-        dol_syslog("Fournisseur.facture::updateline sql=".$sql);
+        dol_syslog(get_class($this)."::updateline sql=".$sql);
         $resql=$this->db->query($sql);
         if ($resql)
         {
@@ -1157,7 +1157,7 @@ class FactureFournisseur extends CommonInvoice
         else
         {
             $this->error=$this->db->lasterror();
-            dol_syslog("Fournisseur.facture::updateline error=".$this->error, LOG_ERR);
+            dol_syslog(get_class($this)."::updateline error=".$this->error, LOG_ERR);
             return -1;
         }
     }
diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index a26900abba9..f8ba6f4b0f9 100644
--- a/htdocs/install/upgrade2.php
+++ b/htdocs/install/upgrade2.php
@@ -1268,7 +1268,7 @@ function migrate_price_facture($db,$langs,$conf)
                 $facligne= new FactureLigne($db);
                 $facligne->fetch($rowid);
 
-                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva, 0, 0,$remise_percent_global,'HT',$info_bits);
+                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva, 0, 0,$remise_percent_global,'HT',$info_bits,0);
                 $total_ht  = $result[0];
                 $total_tva = $result[1];
                 $total_ttc = $result[2];
@@ -1380,7 +1380,7 @@ function migrate_price_propal($db,$langs,$conf)
                 $propalligne= new PropaleLigne($db);
                 $propalligne->fetch($rowid);
 
-                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits);
+                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
                 $total_ht  = $result[0];
                 $total_tva = $result[1];
                 $total_ttc = $result[2];
@@ -1485,7 +1485,7 @@ function migrate_price_contrat($db,$langs,$conf)
                 //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
                 $contratligne->rowid=$rowid;
 
-                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,0,'HT',$info_bits);
+                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,0,'HT',$info_bits,0);
                 $total_ht  = $result[0];
                 $total_tva = $result[1];
                 $total_ttc = $result[2];
@@ -1570,7 +1570,7 @@ function migrate_price_commande($db,$langs,$conf)
                 $commandeligne= new OrderLine($db);
                 $commandeligne->fetch($rowid);
 
-                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits);
+                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
                 $total_ht  = $result[0];
                 $total_tva = $result[1];
                 $total_ttc = $result[2];
@@ -1684,7 +1684,7 @@ function migrate_price_commande_fournisseur($db,$langs,$conf)
                 $commandeligne= new CommandeFournisseurLigne($db);
                 $commandeligne->fetch($rowid);
 
-                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits);
+                $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits,0);
                 $total_ht  = $result[0];
                 $total_tva = $result[1];
                 $total_ttc = $result[2];
diff --git a/htdocs/margin/lib/margins.lib.php b/htdocs/margin/lib/margins.lib.php
index 6609830ee60..99e46b1161c 100644
--- a/htdocs/margin/lib/margins.lib.php
+++ b/htdocs/margin/lib/margins.lib.php
@@ -38,16 +38,21 @@ function marges_admin_prepare_head()
 	$head[$h][2] = 'parameters';
 	$h++;
 
-    // Show more tabs from modules
-    // Entries must be declared in modules descriptor with line
-    // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
-    // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to remove a tab
-    complete_head_from_modules($conf,$langs,'',$head,$h,'margesadmin');
+	// Show more tabs from modules
+	// Entries must be declared in modules descriptor with line
+	// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
+	// $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to remove a tab
+	complete_head_from_modules($conf,$langs,'',$head,$h,'margesadmin');
 
-    return $head;
+	return $head;
 }
 
-function marges_prepare_head($user)
+/**
+ * Return array of tabs to used on pages for third parties cards.
+ *
+ * @return 	array				Array of tabs
+ */
+function marges_prepare_head()
 {
 	global $langs, $conf;
 	$langs->load("marges@marges");
@@ -87,36 +92,36 @@ function marges_prepare_head($user)
  */
 function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
 {
-  global $db, $conf;
-
-  $marge_tx_ret='';
-  $marque_tx_ret='';
-
-  if($fk_pa > 0) {
-  	require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
-  	$product = new ProductFournisseur($db);
-  	if ($product->fetch_product_fournisseur_price($fk_pa)) {
-  		$paht_ret = $product->fourn_unitprice;
-  		if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
-  			$paht_ret += $product->fourn_unitcharges;
-  	}
-  	else
-  		$paht_ret = $paht;
-  }
-  else
-  	$paht_ret	= $paht;
-
-  require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
-  // calcul pu_ht remisés
-  $tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT');
-  $pu_ht_remise = $tabprice[0];
-  // calcul taux marge
-  if ($paht_ret != 0)
-  	$marge_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $paht_ret, 3);
-  // calcul taux marque
-  if ($pu_ht_remise != 0)
-  	$marque_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $pu_ht_remise, 3);
-
-  return array($paht_ret, $marge_tx_ret, $marque_tx_ret);
+	global $db, $conf;
+
+	$marge_tx_ret='';
+	$marque_tx_ret='';
+
+	if($fk_pa > 0) {
+		require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
+		$product = new ProductFournisseur($db);
+		if ($product->fetch_product_fournisseur_price($fk_pa)) {
+			$paht_ret = $product->fourn_unitprice;
+			if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
+				$paht_ret += $product->fourn_unitcharges;
+		}
+		else
+			$paht_ret = $paht;
+	}
+	else
+		$paht_ret	= $paht;
+
+	require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
+	// calcul pu_ht remisés
+	$tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', 0, 0);	// FIXME Parameter type is missing, i put 0 to avoid blocking error
+	$pu_ht_remise = $tabprice[0];
+	// calcul taux marge
+	if ($paht_ret != 0)
+		$marge_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $paht_ret, 3);
+	// calcul taux marque
+	if ($pu_ht_remise != 0)
+		$marque_tx_ret = round((100 * ($pu_ht_remise - $paht_ret)) / $pu_ht_remise, 3);
+
+	return array($paht_ret, $marge_tx_ret, $marque_tx_ret);
 }
 ?>
\ No newline at end of file
-- 
GitLab