From 638e90eb67daf18deeb8ef68ce0bfe62d5369bed Mon Sep 17 00:00:00 2001
From: Juanjo Menent <jmenent@2byte.es>
Date: Fri, 28 Mar 2014 18:22:44 +0100
Subject: [PATCH] Fix: Margin tabs bad data show

---
 ChangeLog                         |  1 +
 htdocs/margin/agentMargins.php    | 10 +++++++---
 htdocs/margin/customerMargins.php | 18 ++++++++++++------
 htdocs/margin/productMargins.php  | 18 +++++++++++++-----
 4 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e221e29f097..0a2c5118d95 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -116,6 +116,7 @@ Fix: [ bug #1280 ] service with not end of date was tagged as expired.
 Fix: [ bug #1295 ] Error when creating an agenda extrafield with a number as reference
 Fix: [ bug #1306 ] Fatal error when adding an external calendar
 New: Added es_CL language
+Fix: Margin tabs bad data show
 
 ***** ChangeLog for 3.5 compared to 3.4.* *****
 For users:
diff --git a/htdocs/margin/agentMargins.php b/htdocs/margin/agentMargins.php
index b7c5b8d4701..cc8f4d067eb 100644
--- a/htdocs/margin/agentMargins.php
+++ b/htdocs/margin/agentMargins.php
@@ -1,5 +1,6 @@
 <?php
 /* Copyright (C) 2012-2013	Christophe Battarel	<christophe.battarel@altairis.fr>
+ * Copyright (C) 2014		Ferran Marcet		<fmarcet@2byte.es>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -130,11 +131,14 @@ 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";
+if($agentid) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname";
+else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname";
 $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/customerMargins.php b/htdocs/margin/customerMargins.php
index e9a0bbfe102..e5226040794 100644
--- a/htdocs/margin/customerMargins.php
+++ b/htdocs/margin/customerMargins.php
@@ -1,5 +1,6 @@
 <?php
 /* Copyright (C) 2012-2013	Christophe Battarel	<christophe.battarel@altairis.fr>
+ * Copyright (C) 2014		Ferran Marcet		<fmarcet@2byte.es>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -172,13 +173,18 @@ $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";
-$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";
+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";
+else $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client";
+$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 c4ba7b54c20..3152b8387b6 100644
--- a/htdocs/margin/productMargins.php
+++ b/htdocs/margin/productMargins.php
@@ -1,5 +1,6 @@
 <?php
 /* Copyright (C) 2012-2013	Christophe Battarel	<christophe.battarel@altairis.fr>
+ * Copyright (C) 2014		Ferran Marcet		<fmarcet@2byte.es>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -178,12 +179,19 @@ $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";
-$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";
+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";
+else 
+	$sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref";
 $sql.= " ORDER BY ".$sortfield." ".$sortorder;
 // TODO: calculate total to display then restore pagination
 //$sql.= $db->plimit($conf->liste_limit +1, $offset);
-- 
GitLab