<?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:	Purchase 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.supplier_id, d.supp_name AS cust_name,d.supp_name AS supp_name, d.curr_code,d.gst_no AS tax_id, dt.type, dt.trans_no,  
			CASE WHEN dt.type=".ST_SUPPCREDIT." THEN (ov_amount+ov_gst+ov_discount)*-1 
			ELSE (ov_amount+ov_gst+ov_discount) END *dt.rate AS total
		FROM ".TB_PREF."supp_trans dt
			LEFT JOIN ".TB_PREF."suppliers d ON d.supplier_id=dt.supplier_id
		WHERE (dt.type=".ST_SUPPINVOICE." OR dt.type=".ST_SUPPCREDIT.") ";
	if ($tax_id)
		$sql .= "AND gst_no<>'' ";
	$sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
		ORDER BY d.supplier_id"; 
    return db_query($sql,"No transactions were returned");
}

function getTaxes($type, $trans_no)
{
	$sql = "SELECT included_in_price, SUM(CASE WHEN trans_type=".ST_SUPPCREDIT." 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($supplier_id,$from, $to)
{
	$fromdate 	= date2sql($from);
	$todate 	= date2sql($to);

	$sql = "SELECT sum(ov_amount+ov_freight+ov_discount) as Amount
		FROM ".TB_PREF."supp_trans dt			
		WHERE dt.ov_gst = 0 AND (dt.type=".ST_SUPPINVOICE." OR dt.type=".ST_SUPPCREDIT.") ";
	
	$sql .= " AND dt.supplier_id=".db_escape($supplier_id)." AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
		GROUP BY dt.supplier_id"; 
    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."supp_trans_details AS trans,  ".TB_PREF."supp_trans AS d 
            WHERE trans.supp_trans_type=d.type AND trans.supp_trans_no=d.trans_no 
            AND (d.type=".ST_SUPPINVOICE." OR d.type=".ST_SUPPCREDIT.") and d.supplier_id=".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.supplier_id"; 
		
		$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_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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.supplier_id = ".db_escape($debtorno)."
 			AND trans.type = ".ST_SUPPINVOICE ."
 			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_nontaxable_transactions($debtorno, $from, $to)
{
	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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.supplier_id = ".db_escape($debtorno)."
 			AND trans.type = ".ST_SUPPINVOICE."
 			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_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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.supplier_id = ".db_escape($debtorno)."
 			AND trans.type = ".ST_SUPPINVOICE." 			
 			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_supplier_sumamount($supplier_id, $from, $to)
{
    $from 	= date2sql($from);
	$to 	= date2sql($to);

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

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


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

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

    $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
        (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS outstanding
        FROM ".TB_PREF."supp_trans t
        WHERE

		t.tran_date >= '$from'
 			AND t.tran_date <= '$to'
 			AND  	 t.supplier_id = ".db_escape($supplier_id)
		." AND t.type = ".ST_SUPPINVOICE;

         
    
    $sql .= " GROUP BY supplier_id";

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

}

function get_supplier_credit($supplier_id, $from, $to)
{
    $from 	= date2sql($from);
	$to 	= date2sql($to);

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

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


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

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

    $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
        (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS outstanding
        FROM ".TB_PREF."supp_trans t
        WHERE

		t.tran_date >= '$from'
 			AND t.tran_date <= '$to'
 			AND  	 t.supplier_id = ".db_escape($supplier_id);
		//." AND t.type = ".ST_SUPPINVOICE;

         
    
    $sql .= " GROUP BY supplier_id";

    $result = db_query($sql,"No transactions were returned".$sql);
    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_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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_SUPPINVOICE ."
 			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_nontaxable_summarytransactions($from, $to)
{
	$from 	= date2sql($from);
	$to 	= date2sql($to);

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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_SUPPINVOICE."
 			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_supplier_summaryamount($from, $to)
{
    $from 	= date2sql($from);
	$to 	= date2sql($to);

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

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


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

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

    $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
        (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS OutStanding
        FROM ".TB_PREF."supp_trans t
        WHERE

		t.tran_date >= '$from'
 			AND t.tran_date <= '$to'
 			AND t.type = ".ST_SUPPINVOICE; 

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

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

    $sql = "SELECT SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.net_amount ELSE tx.net_amount END * ex_rate) AS Total,SUM(CASE WHEN tx.trans_type=".ST_SUPPCREDIT." THEN -tx.amount ELSE tx.amount END * ex_rate) AS tax
     	FROM ".TB_PREF."supp_trans trans     	
     		LEFT JOIN ".TB_PREF."trans_tax_details tx ON tx.trans_type=".ST_SUPPINVOICE." 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_SUPPINVOICE ." 			
 			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 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');

$dec = user_price_dec();

if ($more != ''){
		$has_more = 1;
		$morestr = _('Greater than ') . number_format2($more, $dec);
	}
	else{
		$has_more = '';
		$morestr = '';
	}

	if ($less != ''){
		$lessstr = _('Less than ') . number_format2($less, $dec);	
		$has_less = 1;
	}
	else{
		$lessstr = '';
		$has_less = '';
	}

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

		$amt_dis = '';
	if($has_more && $has_less)
	$amt_dis = "(From ".$more ." To ".$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 <= 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');

//	$orientation = 'L';

	$dec = user_price_dec();

	$rep = new FrontReport(_('Purchase 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, 100, 150, 220,285,355, 420,485, 550);
	$cols = array(0, 100, 150, 220,285,355, 420);
	$cols = array(0, 130, 200, 275,355,435, 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;
	$total_credits 		= 0.0;
	$total_outstanding 	= 0.0;
	$transactions = getTaxTransactions($from, $to, $tax_id);

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

	$printcustomer = true;
	$rate = 1;

	if($amount_filter	== 11){
			$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	== 33){
		$get_taxable = get_nontaxable_summarytransactions($from, $to);
		$sumamount 	= round2(abs($get_taxable['Total']*$rate), $dec);
	
			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;			
	}	
	
	if($amount_filter	== 22){
		$result_sum 		= get_supplier_summaryamount($from, $to);
		$sumamount 			= round2(abs($result_sum['charges']*$rate), $dec);
	
			if ($more != 0.0 && $sumamount < (double)$more)
						$printcustomer = false;
			if ($less != 0.0 && $sumamount > (double)$less)
						$printcustomer = false;
	}

	
	if($amount_filter	== 44){
			$get_taxable = get_notaxamount_transactions($from, $to);
			$sumamount 	= round2(abs($get_taxable['Total']*$rate), $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;						
	}
	
	$printcustomer = true;

	if($printcustomer)	{

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

				//Check grand total
				$sumamount = 0;
				$result_sum 		= get_supplier_sumamount($custno, $from, $to);
				$sumamount 			= round2(abs($result_sum['charges']*$rate), $dec);
				
				$result_credit 		= get_supplier_credit($custno, $from, $to);
				$Credits 			= round2(abs($result_credit['credits']*$rate), $dec);

				$Outstanding 		= round2(abs($result_credit['outstanding']*$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_taxable = get_taxable_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
					$TaxableAmount 	= round2(abs($get_taxable['Total']*$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);
				}

				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);
				}

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

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

				if($amount_filter	== 6){
					if ($more != 0.0 && $Outstanding < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $Outstanding > (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_credits += $Credits;
					$total_outstanding += $Outstanding;
					$total = $tax = $NonTaxableAmount = $Credits = $Outstanding = 0;
					$rep->NewLine();
				}

				$total = $tax = $non_taxable = 0;

				if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
				{
					$rep->Line($rep->row - 2);
					$rep->NewPage();
				}

			}
			$custno 		= $trans['supplier_id'];
			$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
				$sumamount = 0;
				$result_sum 		= get_supplier_sumamount($custno, $from, $to);
				$sumamount 			= round2(abs($result_sum['charges']*$rate), $dec);
				$sumTax 			= round2(abs($result_sum['TotalTax']*$rate), $dec);

				$Credits 			= round2(abs($result_sum['credits']*$rate), $dec);
				$Outstanding 		= round2(abs($result_sum['outstanding']*$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_taxable = get_taxable_transactions($custno, $from, $to);
				if($get_taxable != null)
				{
					$TaxableAmount 	= round2(abs($get_taxable['Total']*$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);
				}

				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);
				}

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

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

				if($amount_filter	== 6){
					if ($more != 0.0 && $Outstanding < (double)$more)
						$printcustomer = false;
					if ($less != 0.0 && $Outstanding > (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_credits += $Credits;
					$total_outstanding += $Outstanding;
					
					$total = $tax = $NonTaxableAmount = $Credits = $Outstanding = 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();
}

