From 253f7e252d9b16ef6d720466192a3bc795184924 Mon Sep 17 00:00:00 2001
From: Laurent Dinclaux <laurent@gecka.nc>
Date: Mon, 10 Jul 2017 13:15:14 +1100
Subject: [PATCH] Fixes local taxes repports by rate

---
 htdocs/compta/localtax/quadri_detail.php | 16 ++++++------
 htdocs/core/lib/tax.lib.php              | 31 ++++++++++++++++--------
 2 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/htdocs/compta/localtax/quadri_detail.php b/htdocs/compta/localtax/quadri_detail.php
index 175ff6cf98f..b7d8b1d6bad 100644
--- a/htdocs/compta/localtax/quadri_detail.php
+++ b/htdocs/compta/localtax/quadri_detail.php
@@ -54,8 +54,9 @@ if (empty($year))
 	$year_current = $year;
 	$year_start = $year;
 }
-$date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
-$date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
+
+$date_start = dol_mktime( 0, 0, 0, GETPOST( "date_startmonth" ), GETPOST( "date_startday" ), GETPOST( "date_startyear" ) );
+$date_end   = dol_mktime( 23, 59, 59, GETPOST( "date_endmonth" ), GETPOST( "date_endday" ), GETPOST( "date_endyear" ) );
 // Quarter
 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
 {
@@ -92,12 +93,9 @@ $socid = GETPOST('socid','int');
 if ($user->societe_id) $socid=$user->societe_id;
 $result = restrictedArea($user, 'tax', '', '', 'charges');
 
-
-
-/*
+/**
  * View
  */
-
 $morequerystring='';
 $listofparams=array('date_startmonth','date_startyear','date_startday','date_endmonth','date_endyear','date_endday');
 foreach($listofparams as $param)
@@ -118,6 +116,7 @@ $paymentfourn_static=new PaiementFourn($db);
 
 $fsearch.='  <input type="hidden" name="year" value="'.$year.'">';
 $fsearch.='  <input type="hidden" name="modetax" value="'.$modetax.'">';
+$fsearch.='  <input type="hidden" name="localTaxType" value="'.$local.'">';
 
 $calc=$conf->global->MAIN_INFO_LOCALTAX_CALC.$local;
 
@@ -196,9 +195,8 @@ $total = 0;
 $i=0;
 
 // Load arrays of datas
-$x_coll = vat_by_date($db, 0, 0, $date_start, $date_end, $modetax, 'sell');
-$x_paye = vat_by_date($db, 0, 0, $date_start, $date_end, $modetax, 'buy');
-
+$x_coll = tax_by_date('localtax' . $local, $db, 0, 0, $date_start, $date_end, $modetax, 'sell');
+$x_paye = tax_by_date('localtax' . $local, $db, 0, 0, $date_start, $date_end, $modetax, 'buy');
 
 echo '<table class="noborder" width="100%">';
 
diff --git a/htdocs/core/lib/tax.lib.php b/htdocs/core/lib/tax.lib.php
index 0fc401f0280..38ff0ed73f7 100644
--- a/htdocs/core/lib/tax.lib.php
+++ b/htdocs/core/lib/tax.lib.php
@@ -186,6 +186,7 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
  *  to report the amounts for different VAT rates as different lines.
  *  This function also accounts recurrent invoices.
  *
+ *  @param	string	$type          	Tax type, either vat, 'localtax1' or 'localtax2'. Default to 'vat'
  *  @param	DoliDB	$db          	Database handler object
  *  @param  int		$y           	Year
  *  @param  int		$q           	Quarter
@@ -196,7 +197,7 @@ function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction
  *  @param  int		$m           	Month
  *  @return array       			List of quarters with vat
  */
-function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
+function tax_by_date($type='vat', $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
 {
     global $conf;
 
@@ -210,8 +211,6 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
         $fk_facture2='fk_facture';
         $fk_payment='fk_paiement';
         $total_tva='total_tva';
-        $total_localtax1='total_localtax1';
-        $total_localtax2='total_localtax2';
         $paymenttable='paiement';
         $paymentfacturetable='paiement_facture';
         $invoicefieldref='facnumber';
@@ -224,13 +223,20 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
         $fk_facture2='fk_facturefourn';
         $fk_payment='fk_paiementfourn';
         $total_tva='tva';
-        $total_localtax1='total_localtax1';
-        $total_localtax2='total_localtax2';
         $paymenttable='paiementfourn';
         $paymentfacturetable='paiementfourn_facturefourn';
         $invoicefieldref='ref';
     }
 
+	if ( strpos( $type, 'localtax' ) === 0 ) {
+		$f_rate = $type . '_tx';
+	} else {
+		$f_rate = 'tva_tx';
+	}
+
+	$total_localtax1='total_localtax1';
+	$total_localtax2='total_localtax2';
+    
     // CAS DES BIENS
 
     // Define sql request
@@ -238,7 +244,7 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
     if ($modetax == 1)  // Option vat on delivery for goods (payment) and debit invoice for services
     {
         // Count on delivery date (use invoice date as delivery is unknown)
-        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
+        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
         $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
         $sql.= " d.date_start as date_start, d.date_end as date_end,";
         $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
@@ -273,7 +279,7 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
     else    // Option vat on delivery for goods (payments) and payments for services
     {
         // Count on delivery date (use invoice date as delivery is unknown)
-        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
+        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
         $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
         $sql.= " d.date_start as date_start, d.date_end as date_end,";
         $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef as date_f, s.nom as company_name, s.rowid as company_id,";
@@ -378,7 +384,7 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
     if ($modetax == 1)  // Option vat on delivery for goods (payment) and debit invoice for services
     {
         // Count on invoice date
-        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
+        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
         $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
         $sql.= " d.date_start as date_start, d.date_end as date_end,";
         $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
@@ -413,7 +419,7 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
     else    // Option vat on delivery for goods (payments) and payments for services
     {
         // Count on payments date
-        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
+        $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
         $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
         $sql.= " d.date_start as date_start, d.date_end as date_end,";
         $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
@@ -522,7 +528,7 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
 		$sql='';
 
 		// Count on payments date
-		$sql = "SELECT e.rowid, d.product_type as dtype, e.rowid as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
+		$sql = "SELECT e.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
 		$sql .=" d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
 		$sql.= " e.date_debut as date_start, e.date_fin as date_end,";
 		$sql.= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, s.nom as company_name, s.rowid as company_id, d.fk_c_type_fees as type,";
@@ -622,3 +628,8 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
 	return $list;
 }
 
+function vat_by_date ($db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
+{
+	return tax_by_date('vat', $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m);
+}
+
-- 
GitLab