<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_TAXREP';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Nagaraj
// date_:	2021-08-01
// Title:	Sales Summary Report Filter by amount
// ----------------------------------------------------------------
$path_to_root="..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");

//------------------------------------------------------------------


print_sales_summary_report();

function getTaxTransactions($from, $to, $tax_id)
{
	$fromdate = date2sql($from);
	$todate = date2sql($to);

	$sql = "SELECT d.debtor_no, d.name AS cust_name, d.tax_id, d.curr_code,dt.type, dt.trans_no,  
			CASE WHEN dt.type=".ST_CUSTCREDIT." THEN (ov_amount+ov_freight+ov_discount)*-1 
			ELSE (ov_amount+ov_freight+ov_discount) END *dt.rate AS total
		FROM ".TB_PREF."debtor_trans dt
			LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no
		WHERE (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";
	if ($tax_id)
		$sql .= "AND tax_id<>'' ";
	$sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
		ORDER BY d.debtor_no"; 
    return db_query($sql,"No transactions were returned");
}

function getTaxes($type, $trans_no)
{
	$sql = "SELECT included_in_price, SUM(CASE WHEN trans_type=".ST_CUSTCREDIT." THEN -amount ELSE amount END * ex_rate) AS tax
		FROM ".TB_PREF."trans_tax_details WHERE trans_type=$type AND trans_no=$trans_no GROUP BY included_in_price";

    $result = db_query($sql,"No transactions were returned");
    if ($result !== false)
    	return db_fetch($result);
    else
    	return null;
}    	

   	
function getAmountWithoutTax($debtor_no,$from, $to)
{
	$fromdate 	= date2sql($from);
	$todate 	= date2sql($to);

	$sql = "SELECT sum(ov_amount+ov_freight+ov_discount) as Amount
		FROM ".TB_PREF."debtor_trans dt			
		WHERE dt.ov_gst = 0 AND (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";
	
	$sql .= " AND dt.debtor_no=".db_escape($debtor_no)." AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
		GROUP BY dt.debtor_no"; 
    return db_query($sql,"No transactions were returned");
}


function get_nontaxable_amount($from, $to,$customer_id){

		$fromdate = date2sql($from);
		$todate = date2sql($to);


	     $sql = "SELECT SUM((trans.unit_price * trans.quantity) * d.rate) AS NonTaxableTotalAmount FROM
            ".TB_PREF."debtor_trans_details AS trans,  ".TB_PREF."debtor_trans AS d 
            WHERE trans.debtor_trans_type=d.type AND trans.debtor_trans_no=d.trans_no 
            AND (d.type=".ST_SALESINVOICE." OR d.type=".ST_CUSTCREDIT.") and d.debtor_no=".db_escape($customer_id)." AND trans.unit_tax = 0 ";

        $sql .= "AND d.tran_date >=".db_escape($fromdate)." AND d.tran_date<=".db_escape($todate)."
		ORDER BY d.debtor_no"; 
		
		$query =  db_query($sql,"No transactions were returned");
		$fetch =  db_fetch($query);

		return $fetch['NonTaxableTotalAmount'];
}





function get_taxable_transactions($debtorno, $from, $to)
{
	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		 JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type = trans.type AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE tx.tran_date >= '$from'
 			AND tx.tran_date <= '$to'
 			AND trans.debtor_no = ".db_escape($debtorno)." 			
 			AND tx.trans_type = ".ST_SALESINVOICE." 			
 			AND tx.amount > '0'
 			AND trans.ov_amount > '0'
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	

function get_nontaxable_transactions($debtorno, $from, $to)
{

	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SALESINVOICE." AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to'
 			AND trans.debtor_no = ".db_escape($debtorno)."
 			AND trans.type = ".ST_SALESINVOICE."
 			AND tx.amount = '0'
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	

function get_withouttax_transactions($debtorno, $from, $to)
{

	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SALESINVOICE." AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to'
 			AND trans.debtor_no = ".db_escape($debtorno)."
 			AND trans.type = ".ST_SALESINVOICE." 			
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	



function get_customer_sumamount($debtorno, $from, $to)
{

	$from 	= date2sql($from);
	$to 	= date2sql($to);

	$sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_SALESINVOICE." , ".ST_SALESINVOICE.") AND t.ov_amount>0),
             -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,";

    $sql .= "SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_SALESINVOICE." , ".ST_SALESINVOICE.") AND t.ov_amount>0),
             -abs(t.ov_gst + t.ov_freight_tax), 0)) AS TotalTax,";


	$sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
             abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,";		

    $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")), t.alloc * -1, t.alloc)) AS Allocated,";

 	$sql .=	"SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0), 1, -1) *
 			(abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
		FROM ".TB_PREF."debtor_trans t
    	WHERE
    		t.tran_date >= '$from'
 			AND t.tran_date <= '$to'
 			AND  	 t.debtor_no = ".db_escape($debtorno)
		." AND t.type = ".ST_SALESINVOICE;
    		

	$sql .= " GROUP BY debtor_no";

    $result = db_query($sql,"No transactions were returned");
    return db_fetch($result);
}

function get_taxable_summarytransactions($from, $to)
{
	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SALESINVOICE." AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to' 			
 			AND trans.type = ".ST_SALESINVOICE."
 			AND tx.amount > '0'
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	

function get_notaxamount_summarytransactions($from, $to)
{
	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SALESINVOICE." AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to' 			
 			AND trans.type = ".ST_SALESINVOICE." 			
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	

function get_nontaxable_summarytransactions($from, $to)
{

	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_CUSTCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."debtor_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SALESINVOICE." AND tx.trans_no=trans.trans_no
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to' 			
 			AND trans.type = ".ST_SALESINVOICE."
 			AND tx.amount = '0'
 			AND ISNULL(voided.id)
     	ORDER BY trans.tran_date";

    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}    	

function get_customer_summarytotalamount($from, $to)
{	

	$from = date2sql($from);
	$to = date2sql($to);


	$sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_SALESINVOICE." , ".ST_SALESINVOICE.") AND t.ov_amount>0),
             -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,";

    $sql .= "SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_SALESINVOICE." , ".ST_SALESINVOICE.") AND t.ov_amount>0),
             -abs(t.ov_gst), 0)) AS TotalTax,";


	$sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
             abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,";		

    $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")), t.alloc * -1, t.alloc)) AS Allocated,";

 	$sql .=	"SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0), 1, -1) *
 			(abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
		FROM ".TB_PREF."debtor_trans t
    	WHERE
    		t.tran_date >= '$from'
 			AND t.tran_date <= '$to'
 			AND t.type <> ".ST_CUSTDELIVERY;		   		

//	$sql .= " GROUP BY debtor_no";

    $result = db_query($sql,"No transactions were returned");
    return db_fetch($result);
}

function get_taxable_invoicetransactions($debtorno, $from, $to){

		$from 	= date2sql($from);
		$to 	= date2sql($to);

	     $sql = "SELECT SUM((trans.unit_price * trans.quantity) * d.rate) AS Total,SUM((trans.unit_tax) * d.rate) As tax,SUM((d.ov_freight_tax) * d.rate) AS freight_tax FROM
            ".TB_PREF."debtor_trans_details AS trans,  ".TB_PREF."debtor_trans AS d 
            WHERE trans.debtor_trans_type=d.type AND trans.debtor_trans_no=d.trans_no
            AND d.tran_date >= '$from'
 			AND d.tran_date <= '$to'
 			AND d.debtor_no = ".db_escape($debtorno)."
			AND d.type = ".ST_SALESINVOICE."
			AND trans.unit_tax != 0	"; 
            
		$result =  db_query($sql,"No transactions were returned".$sql);
		if ($result !== false)
	    	return db_fetch($result);
	    else
	    	return null;
}

//----------------------------------------------------------------------------------------------------
function get_freighttax_transactions($debtorno, $from, $to,$tax)
{
	$from = date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(trans.ov_freight) AS freight,SUM(trans.ov_freight_tax) AS freight_tax
     	FROM ".TB_PREF."debtor_trans trans     	     		 
 			LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
     	WHERE trans.tran_date >= '$from'
 			AND trans.tran_date <= '$to'
 			AND trans.debtor_no = ".db_escape($debtorno)." 			
 			AND trans.type = ".ST_SALESINVOICE." 			 			
 			AND trans.ov_amount > '0' 			
 			AND ISNULL(voided.id)";

    if ($tax)
    	$sql .= " AND trans.ov_freight_tax > '0'";
    else
    	$sql .= " AND trans.ov_freight_tax = '0'";
    	
	$sql .= " ORDER BY trans.tran_date";


    $result = db_query($sql,"No transactions were returned".$sql);
       if ($result !== false)
    	return db_fetch($result);
    else
    	return null;

}
//----------------------------------------------------------------------------------------------------
function print_sales_summary_report()
{
	global $path_to_root;
	
	$from 	= $_POST['PARAM_0'];
	$to 	= $_POST['PARAM_1'];
	$tax_id = $_POST['PARAM_2'];
	$more 	= $_POST['PARAM_3'];
    $less 	= $_POST['PARAM_4'];
    $amount_filter = $_POST['PARAM_5'];

	$comments = $_POST['PARAM_6'];
	$orientation = $_POST['PARAM_7'];
	$destination = $_POST['PARAM_8'];

	if ($tax_id == 0)
		$tid = _('No');
	else
		$tid = _('Yes');


	$more = (double)$more;
	$less = (double)$less;

	if($more > 0  && $less > 0)
		$amt_dis = "(From ".$more ." To ".$less.")";
	if($more > 0 && $less == '')
		$amt_dis = "(Above ".$more.")";
	if($more == '' && $less > 0)
		$amt_dis = "(Below ".$less.")";
	if($more == '' && $less == '')
			$amt_dis = '';



	if ($destination)
		include_once($path_to_root . "/reporting/includes/excel_report.inc");
	else
		include_once($path_to_root . "/reporting/includes/pdf_report.inc");
	$orientation = ($orientation ? 'L' : 'P');

	$dec = user_price_dec();

	$rep = new FrontReport(_('Sales Summary Report'), "SalesSummaryReport", user_pagesize(), 9, $orientation);

	$sel = array(_("All"), _("Taxable"), _("Grand Total"), 	_("Non Taxable"), _("Amount without Tax"));

	$filtertype = $sel[$amount_filter];


	$params =   array(  0 => $comments,
						1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
						2 => array(  'text' => _('Tax Id Only'),'from' => $tid,'to' => ''),
						3 => array('text' => _('Filter By Amount'), 'from' => $filtertype.' '.$amt_dis,   	'to' => '')
					);

	//$cols = array(0, 130, 180, 270, 350, 500);
	$cols = array(0, 130, 200, 275,355,435, 515, 515);

	$headers = array(_('Customer'), _('Tax Id'), _('Taxable'), _('Tax'), _('Non Taxable'), _('Total'));
	$aligns = array('left', 'left', 'right', 'right', 'right', 'right');
    if ($orientation == 'L')
    	recalculate_cols($cols);

	$rep->Font();
	$rep->Info($params, $cols, $headers, $aligns);
	$rep->NewPage();
	
	$totalnet 			= 0.0;
	$totaltax 			= 0.0;
	$total_taxable 		= 0.0;
	$total_non_taxable 	= 0.0;
	$transactions = getTaxTransactions($from, $to, $tax_id);

	$rep->TextCol(0, 4, _('Balances in Home Currency'));
	$rep->NewLine(2);

	$printcustomer = true;
	/*if($amount_filter	== 1){
			$get_taxable = get_taxable_summarytransactions($from, $to);
			$sumamount 	= round2(abs($get_taxable['Total']*1), $dec);
			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;			
		
	}

	if($amount_filter	== 3){
			$get_taxable = get_nontaxable_summarytransactions($from, $to);	
			$sumamount 	= round2(abs($get_taxable['Total']*1), $dec);

			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;			
	}	
	
	if($amount_filter	== 2){
		$result_sum = get_customer_summarytotalamount($from, $to);
		$sumamount 	= round2(abs($result_sum['charges']*1), $dec);
			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;
	}

	
	if($amount_filter	== 4){
			$get_taxable = get_notaxamount_summarytransactions($from, $to);
			$sumamount 	= round2(abs($get_taxable['Total']*1), $dec);
			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;			
		
	} */

if($amount_filter	>= 5){
		$printcustomer = false;						
	}

	if($printcustomer){

	$custno = 0;
	$tax = $total = 0;
	$custname = $tax_id = "";
	while ($trans=db_fetch($transactions))
	{
		if ($custno != $trans['debtor_no'])
		{
			if ($custno != 0)
			{
				$rate = get_exchange_rate_from_home_currency($trans['curr_code'], Today());

				//Check grand total
				$sumamount = 0;
				$result_sum 		= get_customer_sumamount($custno, $from, $to);
				$sumamount 			= round2(abs($result_sum['charges']*$rate), $dec);
				$sumTax 				= round2(abs($result_sum['TotalTax']*$rate), $dec);
				$TaxWithOutAmount = $sumamount - $sumTax;
				$TaxWithOutAmount = round2(abs($TaxWithOutAmount*$rate), $dec);
				$printcustomer = true;
				if($amount_filter	== 2){
					if ($more != 0.0 && $sumamount < (double)$more)
								$printcustomer = false;
					if ($less != 0.0 && $sumamount > (double)$less)
								$printcustomer = false;
				}
				
			$TaxableAmount = 0;		
			$get_invoicetax = get_taxable_invoicetransactions($custno, $from, $to);
			if($get_invoicetax != null)
			{
				$TotalTaxableInvoiceAmount	= round2(abs($get_invoicetax['Total']*$rate), $dec);
				$TaxableAmount = $TotalTaxableInvoiceAmount + $TaxableAmount; 
				$TaxableAmount	= round2(abs($TaxableAmount*$rate), $dec);
			}

			//Freight
			$sumTaxFreight = 0;
			$sumTaxFreightValue = 0;
			$get_freighttax = get_freighttax_transactions($custno, $from, $to,true);
			if($get_freighttax != null)
			{
				$sumTaxFreight 		= round2(abs($get_freighttax['freight']*$rate), $dec);			
				$sumTaxFreightValue	= round2(abs($get_freighttax['freight_tax']*$rate), $dec);			

				$TaxableAmount = $TaxableAmount + $sumTaxFreight; 
				$TaxableAmount	= round2(abs($TaxableAmount*$rate), $dec);
			}

				if($amount_filter	== 1){
						if ($more != 0.0 && $TaxableAmount < (double)$more)
									$printcustomer = false;
						if ($less != 0.0 && $TaxableAmount > (double)$less)
									$printcustomer = false;					
				}

				$NonTaxableAmount = 0;
				$get_taxable = get_nontaxable_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
						$NonTaxableAmount 	= round2(abs($get_taxable['Total']*$rate), $dec);
				}

				$sumNoTaxFreight = 0;
				$get_freighttax = get_freighttax_transactions($custno, $from, $to,false);
				if($get_freighttax != null)
				{
					$sumNOTaxFreight 			= round2(abs($get_freighttax['freight']*$rate), $dec);			
					$sumTaxFreightValue	 += round2(abs($get_freighttax['freight_tax']*$rate), $dec);			

					$NonTaxableAmount = $NonTaxableAmount + $sumNOTaxFreight; 
					$NonTaxableAmount	= round2(abs($NonTaxableAmount*$rate), $dec);
				} 


				if($amount_filter	== 3){
					if ($more != 0.0 && $NonTaxableAmount < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $NonTaxableAmount > (double)$less)
						$printcustomer = false;			
				}	

				$NoTaxAmount = 0;
				$NoTaxAmount = $TaxableAmount + $NonTaxableAmount;
				$NoTaxAmount	= round2(abs($NoTaxAmount*$rate), $dec);


				//$rep->AmountCol(5, 6, $NoTaxAmount, $dec);
				//$rep->NewLine();
				
				/*$get_taxable = get_withouttax_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
					$NoTaxAmount	= round2(abs($get_taxable['Total']*$rate), $dec);
				} */

				if($amount_filter	== 4){
					if ($more != 0.0 && $NoTaxAmount < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $NoTaxAmount > (double)$less)
						$printcustomer = false;			
				}

				
				if($printcustomer)	{
					$rep->TextCol(0, 1, $custname);
					$rep->TextCol(1, 2,	$tax_id);
					$rep->AmountCol(2, 3, $TaxableAmount, $dec);
					$rep->AmountCol(3, 4, $sumTax, $dec);
					$rep->AmountCol(4, 5, $NonTaxableAmount, $dec);
					$rep->AmountCol(5, 6, $sumamount, $dec);
					$totalnet += $sumamount;
					$totaltax += $sumTax;
					$total_taxable += $TaxableAmount;
					$total_non_taxable += $NonTaxableAmount;
					$total = $tax = $NonTaxableAmount = 0;

					$rep->NewLine();
				}

				$total = $tax = $non_taxable = 0;

				if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
				{
					$rep->Line($rep->row - 2);
					$rep->NewPage();
				}
			}
			$custno 		= $trans['debtor_no'];
			$curr_code = $trans['curr_code'];

			$custname = $trans['cust_name'];
			$tax_id = $trans['tax_id'];
		}	
		$taxes = getTaxes($trans['type'], $trans['trans_no']);
		if ($taxes != null)
		{
			if ($taxes['included_in_price'])
				$trans['total'] -= $taxes['tax'];
			$tax += $taxes['tax'];
		}	
		$total += $trans['total']; 
	}

	if ($custno != 0)
	{

				$rate = get_exchange_rate_from_home_currency($curr_code, Today());
				$printcustomer = true;
				//Check grand total
				$result_sum 		= get_customer_sumamount($custno, $from, $to);
				$sumamount 			= round2(abs($result_sum['charges']*$rate), $dec);
				$sumTax 			= round2(abs($result_sum['TotalTax']*$rate), $dec);
				$TaxWithOutAmount 	= $sumamount - $sumTax;
				$TaxWithOutAmount 	= round2(abs($TaxWithOutAmount*$rate), $dec);

				if($amount_filter	== 2){
					if ($more != 0.0 && $sumamount < (double)$more)
								$printcustomer = false;
					if ($less != 0.0 && $sumamount > (double)$less)
								$printcustomer = false;
				}


		$TaxableAmount = 0;
		$get_invoicetax = get_taxable_invoicetransactions($custno, $from, $to);
		if($get_invoicetax != null)
		{
			$TotalTaxableInvoiceAmount	= round2(abs($get_invoicetax['Total']*$rate), $dec);
			$TaxableAmount = $TotalTaxableInvoiceAmount + $TaxableAmount; 
			$TaxableAmount	= round2(abs($TaxableAmount*$rate), $dec);
		}

		//Freight
		$sumTaxFreight = 0;
		$sumTaxFreightValue = 0;
		$get_freighttax = get_freighttax_transactions($custno, $from, $to,true);
		if($get_freighttax != null)
		{
			$sumTaxFreight 		= round2(abs($get_freighttax['freight']*$rate), $dec);			
			$sumTaxFreightValue	= round2(abs($get_freighttax['freight_tax']*$rate), $dec);			

			$TaxableAmount = $TaxableAmount + $sumTaxFreight; 
			$TaxableAmount	= round2(abs($TaxableAmount*$rate), $dec);
		}

				if($amount_filter	== 1){
						if ($more != 0.0 && $TaxableAmount < (double)$more)
									$printcustomer = false;
						if ($less != 0.0 && $TaxableAmount > (double)$less)
									$printcustomer = false;					
				}

				$NonTaxableAmount = 0;
				$get_taxable = get_nontaxable_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
						$NonTaxableAmount 	= round2(abs($get_taxable['Total']*$rate), $dec);
				}

				$sumNoTaxFreight = 0;
				$custTotalNoFreight = 0;
				$TotalTaxWithAmount = 0;
				$get_freighttax = get_freighttax_transactions($custno, $from, $to,false);
				if($get_freighttax != null)
				{
					$sumNOTaxFreight 	= round2(abs($get_freighttax['freight']*$rate), $dec);
					$sumTaxFreightValue	+= round2(abs($get_freighttax['freight_tax']*$rate), $dec);			
					$NonTaxableAmount = ($NonTaxableAmount + $custTotalNoFreight + $sumNOTaxFreight) - $TotalTaxWithAmount;
				} 
						
				
				$NonTaxableAmount = round2(abs($NonTaxableAmount*$rate), $dec);
				

				if($amount_filter	== 3){
					if ($more != 0.0 && $NonTaxableAmount < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $NonTaxableAmount > (double)$less)
						$printcustomer = false;			
				}	

				/*$NoTaxAmount = 0;
				$get_taxable = get_withouttax_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
					$NoTaxAmount	= round2(abs($get_taxable['Total']*$rate), $dec);
				}*/

				
				$NoTaxAmount = 0;
				$NoTaxAmount = $TaxableAmount + $NonTaxableAmount;
				$NoTaxAmount	= round2(abs($NoTaxAmount*$rate), $dec);
				
				//$rep->AmountCol(5, 6, $NoTaxAmount, $dec);
				//$rep->NewLine();

				if($amount_filter	== 4){
					if ($more != 0.0 && $NoTaxAmount < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $NoTaxAmount > (double)$less)
						$printcustomer = false;			
				}
				
				


				if($printcustomer)	{

					$rep->TextCol(0, 1, $custname);
					$rep->TextCol(1, 2,	$tax_id);
					$rep->AmountCol(2, 3, $TaxableAmount, $dec);
					$rep->AmountCol(3, 4, $sumTax, $dec);
					$rep->AmountCol(4, 5, $NonTaxableAmount, $dec);
					$rep->AmountCol(5, 6, $sumamount, $dec);
					$totalnet += $sumamount;
					$totaltax += $sumTax;
					$total_taxable += $TaxableAmount;
					$total_non_taxable += $NonTaxableAmount;
					$total = $tax = $NonTaxableAmount = 0;
					$rep->NewLine();
					
				}
		}
	}
	$rep->Font('bold');
	$rep->NewLine();
	$rep->Line($rep->row + $rep->lineHeight);
	$rep->TextCol(0, 2,	_("Total"));
	$rep->AmountCol(2, 3, $total_taxable, $dec);
	$rep->AmountCol(3, 4, $totaltax, $dec);
	$rep->AmountCol(4, 5, $total_non_taxable, $dec);
	$rep->AmountCol(5, 6, $totalnet, $dec);
	$rep->Line($rep->row - 5);
	$rep->Font();

	$rep->End();
 
}

