From bd061f42454a36ddc9d15dc23ae1778718b39bdd Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 13 May 2014 01:29:01 +0200
Subject: [PATCH] Fix: [ bug #1380 ] Customer invoices are not grouped in
 company results report

---
 ChangeLog                              |  1 +
 htdocs/compta/resultat/clientfourn.php | 57 +++++++++++++++-----------
 2 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 03f35cae0cd..3bb088b99f3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -25,6 +25,7 @@ Fix: [ bug #1372 ] Margin calculation does not work in proposals.
 Fix: [ bug #1381 ] PHP Warning when listing stock transactions page.
 Fix: [ bug #1367 ] "Show invoice" link after a POS sell throws an error.
 Fix: TCPDF error file not found in member card generation.
+Fix: [ bug #1380 ] Customer invoices are not grouped in company results report.
 
 ***** ChangeLog for 3.5.2 compared to 3.5.1 *****
 Fix: Can't add user for a task.
diff --git a/htdocs/compta/resultat/clientfourn.php b/htdocs/compta/resultat/clientfourn.php
index d28f457fb2b..9a08c76abe7 100644
--- a/htdocs/compta/resultat/clientfourn.php
+++ b/htdocs/compta/resultat/clientfourn.php
@@ -187,7 +187,7 @@ else
 $sql.= " AND f.entity = ".$conf->entity;
 if ($socid) $sql.= " AND f.fk_soc = ".$socid;
 $sql.= " GROUP BY s.nom, s.rowid";
-$sql.= " ORDER BY s.nom";
+$sql.= " ORDER BY s.nom, s.rowid";
 
 dol_syslog("get customer invoices sql=".$sql);
 $result = $db->query($sql);
@@ -281,11 +281,11 @@ print '</tr>';
 
 
 /*
- * Frais, factures fournisseurs.
+ * Suppliers invoices
  */
 if ($modecompta == 'CREANCES-DETTES')
 {
-    $sql = "SELECT s.nom, s.rowid as socid, date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
+    $sql = "SELECT s.nom, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
     $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
     $sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
     $sql.= " WHERE f.fk_soc = s.rowid";
@@ -299,7 +299,7 @@ if ($modecompta == 'CREANCES-DETTES')
 }
 else
 {
-    $sql = "SELECT s.nom, s.rowid as socid, date_format(p.datep,'%Y-%m') as dm, sum(pf.amount) as amount_ttc";
+    $sql = "SELECT s.nom, s.rowid as socid, sum(pf.amount) as amount_ttc";
     $sql.= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
     $sql.= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
     $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
@@ -312,7 +312,7 @@ else
 }
 $sql.= " AND f.entity = ".$conf->entity;
 if ($socid) $sql.= " AND f.fk_soc = ".$socid;
-$sql .= " GROUP BY s.nom, s.rowid, dm";
+$sql .= " GROUP BY s.nom, s.rowid";
 $sql .= " ORDER BY s.nom, s.rowid";
 
 print '<tr><td colspan="4">'.$langs->trans("SuppliersInvoices").'</td></tr>';
@@ -372,11 +372,11 @@ print '</tr>';
  * Charges sociales non deductibles
  */
 
-print '<tr><td colspan="4">'.$langs->trans("SocialContributions").'</td></tr>';
+print '<tr><td colspan="4">'.$langs->trans("SocialContributions").' ('.$langs->trans("Type").' 0)</td></tr>';
 
 if ($modecompta == 'CREANCES-DETTES')
 {
-    $sql = "SELECT c.libelle as nom, sum(cs.amount) as amount";
+    $sql = "SELECT c.id, c.libelle as nom, sum(cs.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
     $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
     $sql.= " WHERE cs.fk_type = c.id";
@@ -386,7 +386,7 @@ if ($modecompta == 'CREANCES-DETTES')
 }
 else
 {
-    $sql = "SELECT c.libelle as nom, sum(p.amount) as amount";
+    $sql = "SELECT c.id, c.libelle as nom, sum(p.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
     $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
     $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
@@ -397,8 +397,8 @@ else
     	$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
 }
 $sql.= " AND cs.entity = ".$conf->entity;
-$sql.= " GROUP BY c.libelle";
-$sql.= " ORDER BY c.libelle";
+$sql.= " GROUP BY c.libelle, c.id";
+$sql.= " ORDER BY c.libelle, c.id";
 
 dol_syslog("get social contributions deductible=0 sql=".$sql);
 $result=$db->query($sql);
@@ -446,11 +446,11 @@ print '</tr>';
  * Charges sociales deductibles
  */
 
-print '<tr><td colspan="4">'.$langs->trans("SocialContributions").'</td></tr>';
+print '<tr><td colspan="4">'.$langs->trans("SocialContributions").' ('.$langs->trans("Type").' 1)</td></tr>';
 
 if ($modecompta == 'CREANCES-DETTES')
 {
-    $sql = "SELECT c.libelle as nom, sum(cs.amount) as amount";
+    $sql = "SELECT c.id, c.libelle as nom, sum(cs.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
     $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
     $sql.= " WHERE cs.fk_type = c.id";
@@ -458,12 +458,12 @@ if ($modecompta == 'CREANCES-DETTES')
     if (! empty($date_start) && ! empty($date_end))
     	$sql.= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
     $sql.= " AND cs.entity = ".$conf->entity;
-    $sql.= " GROUP BY c.libelle";
-    $sql.= " ORDER BY c.libelle";
+    $sql.= " GROUP BY c.libelle, c.id";
+    $sql.= " ORDER BY c.libelle, c.id";
 }
 else
 {
-    $sql = "SELECT c.libelle as nom, sum(p.amount) as amount";
+    $sql = "SELECT c.id, c.libelle as nom, sum(p.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
     $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
     $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
@@ -473,8 +473,8 @@ else
     if (! empty($date_start) && ! empty($date_end))
     	$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
     $sql.= " AND cs.entity = ".$conf->entity;
-    $sql.= " GROUP BY c.libelle";
-    $sql.= " ORDER BY c.libelle";
+    $sql.= " GROUP BY c.libelle, c.id";
+    $sql.= " ORDER BY c.libelle, c.id";
 }
 
 dol_syslog("get social contributions deductible=1 sql=".$sql);
@@ -541,6 +541,7 @@ if ($mysoc->tva_assuj == 'franchise')	// Non assujeti
 /*
  * VAT
  */
+
 print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
 $subtotal_ht = 0;
 $subtotal_ttc = 0;
@@ -564,12 +565,15 @@ if ($modecompta == 'CREANCES-DETTES')
 
     dol_syslog("get vat to pay sql=".$sql);
     $result=$db->query($sql);
-    if ($result) {
+    if ($result)
+    {
         $num = $db->num_rows($result);
         $var=false;
         $i = 0;
-        if ($num) {
-            while ($i < $num) {
+        if ($num)
+        {
+            while ($i < $num)
+            {
                 $obj = $db->fetch_object($result);
 
                 $amount -= $obj->amount;
@@ -606,12 +610,15 @@ if ($modecompta == 'CREANCES-DETTES')
 
     dol_syslog("get vat received back sql=".$sql);
     $result=$db->query($sql);
-    if ($result) {
+    if ($result)
+    {
         $num = $db->num_rows($result);
         $var=true;
         $i = 0;
-        if ($num) {
-            while ($i < $num) {
+        if ($num)
+        {
+            while ($i < $num)
+            {
                 $obj = $db->fetch_object($result);
 
                 $amount += $obj->amount;
@@ -634,7 +641,7 @@ if ($modecompta == 'CREANCES-DETTES')
 }
 else
 {
-    // TVA reellement deja payee
+    // VAT really already paid
     $amount=0;
     $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
@@ -675,7 +682,7 @@ else
     print "<td align=\"right\">".price($amount)."</td>\n";
     print "</tr>\n";
 
-    // TVA recuperee
+    // VAT really received
     $amount=0;
     $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
     $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
-- 
GitLab