<?php
/*-------------------------------------------------------+
| Kvcodes ERP
| http://www.kvcodes.com/
+--------------------------------------------------------+
| Author: Kvvaradha  
| Email: admin@kvcodes.com
| Title:	Tax Report
| ---------------------------------------------------------*/
$page_security = 'SA_TAXREP';
$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_tax_report();

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

	$sql = "SELECT tt.name as taxname, taxrec.*, taxrec.amount*ex_rate AS amount,
	            taxrec.net_amount*ex_rate AS net_amount,
				IF(taxrec.trans_type=".ST_BANKPAYMENT." OR taxrec.trans_type=".ST_BANKDEPOSIT.", 
					IF(gl.person_type_id<>".PT_MISC.", gl.memo_, gl.person_id), 
					IF(ISNULL(supp.supp_name), debt.name, supp.supp_name)) as name,
				branch.br_name
		FROM ".TB_PREF."trans_tax_details taxrec
		LEFT JOIN ".TB_PREF."tax_types tt
			ON taxrec.tax_type_id=tt.id
		LEFT JOIN ".TB_PREF."gl_trans gl 
			ON taxrec.trans_type=gl.type AND taxrec.trans_no=gl.type_no AND gl.amount<>0 AND
			(tt.purchasing_gl_code=gl.account OR tt.sales_gl_code=gl.account)
		LEFT JOIN ".TB_PREF."supp_trans strans
			ON taxrec.trans_no=strans.trans_no AND taxrec.trans_type=strans.type
		LEFT JOIN ".TB_PREF."suppliers as supp ON strans.supplier_id=supp.supplier_id
		LEFT JOIN ".TB_PREF."debtor_trans dtrans
			ON taxrec.trans_no=dtrans.trans_no AND taxrec.trans_type=dtrans.type
		LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
		LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code
		WHERE (taxrec.amount <> 0 OR taxrec.net_amount <> 0)
			AND !ISNULL(taxrec.reg_type)
			AND taxrec.tran_date >= '$fromdate'
			AND taxrec.tran_date <= '$todate'
			AND taxrec.trans_type !=".ST_CUSTDELIVERY." AND taxrec.trans_type !=".ST_SUPPRECEIVE." 
		ORDER BY taxrec.trans_type, taxrec.tran_date, taxrec.trans_no, taxrec.ex_rate";

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

function getTaxTypes()
{
	$sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
    return db_query($sql,"No transactions were returned");
}

function getTaxInfo($id)
{
	$sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
    $result = db_query($sql,"No transactions were returned");
    return db_fetch($result);
}

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

function print_tax_report()
{
	global $path_to_root, $systypes_array;
	
    $from = $_POST['PARAM_0'];
    $to = $_POST['PARAM_1'];
	$comments = $_POST['PARAM_2'];
	$orientation = $_POST['PARAM_3'];
	$destination = $_POST['PARAM_4'];
	$summaryOnly =1;

	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();
	$summary = _('VAT Summary');
	$rep = new FrontReport($summary, "VATReport", user_pagesize(), 9, $orientation);	
	$res = getTaxTypes();

	$taxes[0] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
	while ($tax=db_fetch($res))
		$taxes[$tax['id']] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);

	$cols = array(0,400, 520);

	$headers = array(_("Name"), _('Total'));
	$aligns = array('left', 'right');
    if ($orientation == 'L')
    	recalculate_cols($cols);

    $params =   array( 	0 => $comments,
						1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
						2 => array('text' => _('Type'), 'from' => $summary, 'to' => ''));

	$rep->Font();
	$rep->Info($params, $cols, $headers, $aligns);
	$rep->NewPage();

	$dec = user_price_dec();

	$totalnet = $totalsalesnet =	$totaltax = $totaloutput = 	$totalinput = 0.0;
	$transactions = getTaxTransactions($from, $to);

	while ($trans=db_fetch($transactions))	{
		if (in_array($trans['trans_type'], array(ST_CUSTCREDIT,ST_SUPPINVOICE,ST_JOURNAL))) {
			$trans['net_amount'] *= -1;
			$trans['amount'] *= -1;
		}

		$tax_type = $trans['tax_type_id'];
		if ($trans['reg_type']==TR_INPUT) {
			$totalinput +=$trans['amount'];
		}
		elseif ($trans['reg_type']==TR_OUTPUT) {
			$totaloutput +=$trans['amount'];
			$totalsalesnet +=$trans['net_amount'];
		}
		// elseif (in_array($trans['trans_type'], array(ST_BANKDEPOSIT,ST_SALESINVOICE,ST_CUSTCREDIT))) {
		// 	$totaloutput +=$trans['amount'];
		// 	$totalsalesnet +=$trans['net_amount'];
		// } elseif ($trans['reg_type'] !== NULL) {
		// 	$totalinput +=$trans['amount'];
		// }
		$totalnet += $trans['net_amount'];
		$totaltax += $trans['amount'];
	}

	$rep->Textcol(0,1,'S Standard Sales (Excluding VAT)');
	
	$rep->AmountCol(1,2,$totalsalesnet,$dec);
	$rep->NewLine();
	$rep->Textcol(0,1,'C Capital Sales (Excluding VAT)');
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'BLOCK 1 STANDARD RATE (EXCLUDING CAPITAL GOODS AND/OR SERVICES AND ACCOMMODATION)');
	$rep->AmountCol(1,2,$totalsalesnet+$totaloutput,$dec);
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'BLOCK 1A STANDARD RATE (ONLY CAPITAL GOODS AND/OR SERVICES)');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 2 Zero Rated (Excluding goods exported)');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 2A Zero Rate (Only exported goods)');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 3 Exempt and non supplies');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 4 VAT on Standard Rate Goods and Services ');
	$rep->AmountCol(1,2,$totaloutput,$dec);
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 4A VAT on Capital Goods and Services at Standard Rate');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 10 Change in use and Export of second-hand goods');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 11 VAT on Change in use and Export of second-hand goods');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 12 Other Adjustments');
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'BLOCK 13 TOTAL A: TOTAL OUTPUT (4 + 4A + 11 + 12)');
	$rep->AmountCol(1,2,$totaloutput,$dec);
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'Block 14 VAT on Capital Goods and/or services supplied to you');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 14A VAT on Capital Goods Imported by you');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 15 VAT on Other goods and services supplied to you (not capital goods)');
	$rep->AmountCol(1,2,abs($totalinput),$dec);
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 15A VAT on Other Goods Imported by you');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 16 Change In Use (Input VAT Adjustments)');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 17 Bad Debts (Input VAT Adjustments)');
	$rep->NewLine();
	$rep->Textcol(0,1,'Block 18 Other Input VAT Adjustments');
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'BLOCK 19 TOTAL B: TOTAL INPUT (14 + 14A + 15 + 15A + 16 + 17 + 18)');
	$rep->AmountCol(1,2,abs($totalinput),$dec);
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->Textcol(0,1,'BLOCK 20 VAT PAYABLE');
	$rep->AmountCol(1,2,$totaloutput-abs($totalinput),$dec);
	$rep->Line($rep->row-5);
	$rep->NewLine(1.5);
	$rep->End();
}