From be97066a74f94aa8f566873972edd9620f9d3895 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 10 Mar 2014 23:57:19 +0100
Subject: [PATCH] Fix: Bug into sql request because of incomplete group by

---
 htdocs/margin/agentMargins.php           |  6 ++----
 htdocs/margin/customerMargins.php        | 16 ++++++----------
 htdocs/margin/productMargins.php         | 15 +++++----------
 htdocs/margin/tabs/productMargins.php    | 16 ++++++++--------
 htdocs/margin/tabs/thirdpartyMargins.php | 20 +++++++++-----------
 5 files changed, 30 insertions(+), 43 deletions(-)

diff --git a/htdocs/margin/agentMargins.php b/htdocs/margin/agentMargins.php
index 41deb207e1c..5bfa61cc7b6 100644
--- a/htdocs/margin/agentMargins.php
+++ b/htdocs/margin/agentMargins.php
@@ -130,10 +130,8 @@ if ($agentid > 0) {
 	else
 	    $sql .= " AND sc.fk_user = ".$agentid;
 }
-if (!empty($startdate))
-  $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
-if (!empty($enddate))
-  $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
+if (!empty($startdate)) $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
+if (!empty($enddate))   $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
 $sql .= " AND d.buy_price_ht IS NOT NULL";
 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
 $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname";
diff --git a/htdocs/margin/customerMargins.php b/htdocs/margin/customerMargins.php
index eeff645a6d0..e9a0bbfe102 100644
--- a/htdocs/margin/customerMargins.php
+++ b/htdocs/margin/customerMargins.php
@@ -172,17 +172,13 @@ $sql.= " WHERE f.fk_soc = s.rowid";
 $sql.= " AND f.fk_statut > 0";
 $sql.= " AND s.entity = ".$conf->entity;
 $sql.= " AND d.fk_facture = f.rowid";
-if ($client)
-  $sql.= " AND f.fk_soc = ".$socid;
-if (!empty($startdate))
-  $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
-if (!empty($enddate))
-  $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
+if ($client) $sql.= " AND f.fk_soc = ".$socid;
+if (!empty($startdate)) $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
+if (!empty($enddate)) $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
 $sql .= " AND d.buy_price_ht IS NOT NULL";
-if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
-	$sql .= " AND d.buy_price_ht <> 0";
-if ($client) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
-$sql.= " ORDER BY $sortfield $sortorder ";
+if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
+$sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
+$sql.= " ORDER BY ".$sortfield." ".$sortorder;
 // TODO: calculate total to display then restore pagination
 //$sql.= $db->plimit($conf->liste_limit +1, $offset);
 
diff --git a/htdocs/margin/productMargins.php b/htdocs/margin/productMargins.php
index 8e129e58b6e..c4ba7b54c20 100644
--- a/htdocs/margin/productMargins.php
+++ b/htdocs/margin/productMargins.php
@@ -178,17 +178,12 @@ $sql.= " AND f.fk_soc = s.rowid";
 $sql.= " AND d.fk_product = p.rowid";
 $sql.= " AND f.fk_statut > 0";
 $sql.= " AND d.fk_facture = f.rowid";
-if ($id > 0)
-	$sql.= " AND d.fk_product =".$id;
-if (!empty($startdate))
-  $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
-if (!empty($enddate))
-  $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
+if ($id > 0) $sql.= " AND d.fk_product =".$id;
+if (!empty($startdate)) $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
+if (!empty($enddate)) $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
 $sql .= " AND d.buy_price_ht IS NOT NULL";
-if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
-	$sql .= " AND d.buy_price_ht <> 0";
-if ($id > 0)
-  $sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, d.fk_product, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
+if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
+$sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, d.fk_product, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
 $sql.= " ORDER BY ".$sortfield." ".$sortorder;
 // TODO: calculate total to display then restore pagination
 //$sql.= $db->plimit($conf->liste_limit +1, $offset);
diff --git a/htdocs/margin/tabs/productMargins.php b/htdocs/margin/tabs/productMargins.php
index 214cf72b86a..f215a3870eb 100644
--- a/htdocs/margin/tabs/productMargins.php
+++ b/htdocs/margin/tabs/productMargins.php
@@ -129,13 +129,13 @@ if ($id > 0 || ! empty($ref))
 
 
 		$sql = "SELECT s.nom, s.rowid as socid, s.code_client,";
-		$sql.= " f.facnumber, f.total as total_ht,";
+		$sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
+		$sql.= " f.datef, f.paye, f.fk_statut as statut,";
+		if (!$user->rights->societe->client->voir && !$socid) $sql.= " sc.fk_soc, sc.fk_user,";
 		$sql.= " sum(d.total_ht) as selling_price,";
-        $sql.= $db->ifsql('f.type =2','sum(d.qty *-1)','sum(d.qty)')." as qty,";
-		$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid,";
+		$sql.= $db->ifsql('f.type =2','sum(d.qty *-1)','sum(d.qty)')." as qty,";
 		$sql.= $db->ifsql('f.type =2','sum(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,";
         $sql.= $db->ifsql('f.type =2','sum(-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty)))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge" ;
-		if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
 		$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 		$sql.= ", ".MAIN_DB_PREFIX."facture as f";
 		$sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
@@ -148,10 +148,10 @@ if ($id > 0 || ! empty($ref))
 		if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
 		if (! empty($socid)) $sql.= " AND f.fk_soc = $socid";
 		$sql .= " AND d.buy_price_ht IS NOT NULL";
-		if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
-			$sql .= " AND d.buy_price_ht <> 0";
-		$sql.= " GROUP BY f.rowid";
-		$sql.= " ORDER BY $sortfield $sortorder ";
+		if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
+		$sql.= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
+		if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user";
+		$sql.= " ORDER BY ".$sortfield." ".$sortorder;
 		// TODO: calculate total to display then restore pagination
 		//$sql.= $db->plimit($conf->liste_limit +1, $offset);
 		dol_syslog('margin:tabs:productMargins.php sql='.$sql,LOG_DEBUG);
diff --git a/htdocs/margin/tabs/thirdpartyMargins.php b/htdocs/margin/tabs/thirdpartyMargins.php
index d33911f56e5..533ade010a3 100644
--- a/htdocs/margin/tabs/thirdpartyMargins.php
+++ b/htdocs/margin/tabs/thirdpartyMargins.php
@@ -127,12 +127,11 @@ if ($socid > 0)
 
 
 		$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
-		$sql.= " f.facnumber, f.total as total_ht,";
+		$sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
+		$sql.= " f.datef, f.paye, f.fk_statut as statut,";
 		$sql.= " sum(d.total_ht) as selling_price,";
-
-		$sql.= $db->ifsql('f.type =2','sum(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,";
-        $sql.= $db->ifsql('f.type =2','sum(-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty)))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge," ;
-		$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
+		$sql.= " ".$db->ifsql('f.type =2','sum(d.qty * d.buy_price_ht *-1)','sum(d.qty * d.buy_price_ht)')." as buying_price,";
+        $sql.= " ".$db->ifsql('f.type =2','sum(-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty)))','sum(d.total_ht - (d.buy_price_ht * d.qty))')." as marge";
 		$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 		$sql.= ", ".MAIN_DB_PREFIX."facture as f";
 		$sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
@@ -142,10 +141,9 @@ if ($socid > 0)
 		$sql.= " AND d.fk_facture = f.rowid";
 		$sql.= " AND f.fk_soc = $socid";
 		$sql .= " AND d.buy_price_ht IS NOT NULL";
-		if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
-			$sql .= " AND d.buy_price_ht <> 0";
-		$sql.= " GROUP BY f.rowid";
-		$sql.= " ORDER BY $sortfield $sortorder ";
+		if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
+		$sql.= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
+		$sql.= " ORDER BY ".$sortfield." ".$sortorder;
 		// TODO: calculate total to display then restore pagination
 		//$sql.= $db->plimit($conf->liste_limit +1, $offset);
 
@@ -175,7 +173,7 @@ if ($socid > 0)
 
 			$cumul_achat = 0;
 			$cumul_vente = 0;
-			
+
 			$rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
 
 			if ($num > 0)
@@ -195,7 +193,7 @@ if ($socid > 0)
 						$marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
 						$markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
 					}
-					
+
 					$var=!$var;
 
 					print "<tr ".$bc[$var].">";
-- 
GitLab