From cfec85c2d648fe08f616adf4b5caaa358249bb1b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 16 Dec 2013 13:24:00 +0100
Subject: [PATCH] Fix: Rewrite content of getMarginInfos. This solve bug of
 rounding with total price when we need a unit price. This solve lso bug of
 rounding on non decimal basis that we don't want. Also this remove calling
 database when not usefull so make possible to call this function into fetch
 (this remove the FIXME not into comment so).

---
 htdocs/margin/lib/margins.lib.php | 51 +++++++++++++++----------------
 1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/htdocs/margin/lib/margins.lib.php b/htdocs/margin/lib/margins.lib.php
index 1ee6fd99e2a..8556548dcbc 100644
--- a/htdocs/margin/lib/margins.lib.php
+++ b/htdocs/margin/lib/margins.lib.php
@@ -81,50 +81,49 @@ function marges_prepare_head()
 }
 
 /**
- * getMarginInfos
+ * Return an array with margins information of a line
  *
- * @param 	float 	$pvht				Buying price with tax
- * @param 	float	$remise_percent		Discount percent
- * @param 	float	$tva_tx				Vat rate
- * @param 	float	$localtax1_tx		Vat rate special 1
- * @param 	float	$localtax2_tx		Vat rate special 2
- * @param 	int		$fk_pa				???
- * @param 	float	$paht				Buying price without tax
- * @param	int		$type				Type of line (product or service)
- * @param	Societe	$seller				Object of seller
- * @param	array	$localtaxes_array	Array of localtaxes
- * @return	array						Array of margin info
- * 
- * FIXME This function is called too frequently without type, seller and without localtaxes_array defined. This make vat rate detection wrong.
+ * @param 	float 	$pvht					Selling price without tax
+ * @param 	float	$remise_percent_ligne	Discount percent on line
+ * @param 	float	$tva_tx					Vat rate
+ * @param 	float	$localtax1_tx			Vat rate special 1
+ * @param 	float	$localtax2_tx			Vat rate special 2
+ * @param 	int		$fk_pa					Id of buying price (prefer set this to 0 and provide $paht instead. With id, buying price may have change)
+ * @param 	float	$paht					Buying price without tax
+ * @return	array							Array of margin info
  */
-function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht, $type=0, $seller='', $localtaxes_array='')
+function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
 {
-	global $db, $conf, $mysoc;
+	global $db, $conf;
 
 	$marge_tx_ret='';
 	$marque_tx_ret='';
 
-	if (empty($seller) || ! is_object($seller)) $seller=$mysoc;
-	if (empty($localtaxes_array) || ! is_array($localtaxes_array)) $localtaxes_array=array();
-	
-	if($fk_pa > 0) {
+	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)) {
+		if ($product->fetch_product_fournisseur_price($fk_pa)) 
+		{
 			$paht_ret = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
 			if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
 				$paht_ret += $product->fourn_unitcharges;
 		}
 		else
+		{
 			$paht_ret = $paht;
+		}
 	}
-	else
+	else 
+	{
 		$paht_ret	= $paht;
+	}
+	
+	// Calculate selling unit price including line discount
+	// We don't use calculate_price, because this function is dedicated to calculation of total with accuracy of total. We need an accuracy of a unit price.
+	// Also we must not apply rounding on non decimal rule defined by option MAIN_ROUNDING_RULE_TOT
+	$pu_ht_remise = $pvht * (1 - ($remise_percent / 100));
+	$pu_ht_remise = price2num($pu_ht_remise, 'MU');
 
-	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, $type, $seller, $localtaxes_array);
-	$pu_ht_remise = $tabprice[0];
 	// calcul marge
 	if ($pu_ht_remise < 0)
 		$marge = -1 * (abs($pu_ht_remise) - $paht_ret);
-- 
GitLab