From b8b9df7b953e6e8ba5226cd7096433c3e57a1813 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?=
 <marcosgdf@gmail.com>
Date: Fri, 6 Feb 2015 12:36:09 +0100
Subject: [PATCH] Fix: [ bug #1827 ] Tax reports gives incorrect amounts when
 using external modules that create lines with special codes

---
 ChangeLog                   |  1 +
 htdocs/core/lib/tax.lib.php | 26 +++++++++-----------------
 2 files changed, 10 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ace6a13363d..f5faea819fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -18,6 +18,7 @@ English Dolibarr ChangeLog
 - Fix: [ bug #1717 ] Sorting unpaid invoices by amount received brings due amount
 - Fix: [ bug #1784 ] MOTD doesn't show up in Amarok theme
 - Fix: Tracking number not visible on shipment pdf
+- Fix: [ bug #1827 ] Tax reports gives incorrect amounts when using external modules that create lines with special codes
 
 ***** ChangeLog for 3.6.2 compared to 3.6.1 *****
 - Fix: fix ErrorBadValueForParamNotAString error message in price customer multiprice.
diff --git a/htdocs/core/lib/tax.lib.php b/htdocs/core/lib/tax.lib.php
index a52aeb37345..3b7e4044227 100644
--- a/htdocs/core/lib/tax.lib.php
+++ b/htdocs/core/lib/tax.lib.php
@@ -3,6 +3,7 @@
  * Copyright (C) 2006-2007	Yannick Warnier		<ywarnier@beeznest.org>
  * Copyright (C) 2011		Regis Houssin		<regis.houssin@capnetworks.com>
  * Copyright (C) 2012		Juanjo Menent		<jmenent@2byte.es>
+ * Copyright (C) 2015       Marcos GarcĂ­a       <marcosgdf@gmail.com>
  *
  * 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
@@ -86,26 +87,18 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
     global $conf;
 
     $list=array();
-    //print "xx".$conf->global->MAIN_MODULE_ACCOUNTING;
-    //print "xx".$conf->global->MAIN_MODULE_COMPTABILITE;
 
     if ($direction == 'sell')
     {
         $invoicetable='facture';
-        $invoicedettable='facturedet';
-        $fk_facture='fk_facture';
-        $total_tva='total_tva';
-        $total_localtax1='total_localtax1';
-        $total_localtax2='total_localtax2';
+        $total_ht='total';
+        $total_tva='tva';
     }
     if ($direction == 'buy')
     {
         $invoicetable='facture_fourn';
-        $invoicedettable='facture_fourn_det';
-        $fk_facture='fk_facture_fourn';
-        $total_tva='tva';
-        $total_localtax1='total_localtax1';
-        $total_localtax2='total_localtax2';
+        $total_ht='total_ht';
+        $total_tva='total_tva';
     }
 
     // Define sql request
@@ -125,11 +118,10 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
         if (! empty($conf->global->MAIN_MODULE_COMPTABILITE))
         {
             $sql = "SELECT s.rowid as socid, s.nom as nom, s.siren as tva_intra, s.tva_assuj as assuj,";
-            $sql.= " sum(fd.total_ht) as amount, sum(fd.".$total_tva.") as tva,";
-            $sql.= " sum(fd.".$total_localtax1.") as localtax1,";
-            $sql.= " sum(fd.".$total_localtax2.") as localtax2";
+            $sql.= " sum(f.$total_ht) as amount, sum(f.".$total_tva.") as tva,";
+            $sql.= " sum(f.localtax1) as localtax1,";
+            $sql.= " sum(f.localtax2) as localtax2";
             $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
-            $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as fd,";
             $sql.= " ".MAIN_DB_PREFIX."societe as s";
             $sql.= " WHERE f.entity = " . $conf->entity;
             $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
@@ -146,7 +138,7 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
                 $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
             }
             if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
-            $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
+            $sql.= " AND s.rowid = f.fk_soc";
             $sql.= " GROUP BY s.rowid, s.nom, s.tva_intra, s.tva_assuj";
         }
     }
-- 
GitLab