<?php
/*=======================================================\
|                 Point of Sales Module                  |
|--------------------------------------------------------|
|   Creator: Phương                                      |
|   Date :   18-Dec-2017                                 |
|   Description: ERP Point of Sales Extension            |
|   Website: http://notrinos.com                         |
|                                                        |
\=======================================================*/

$page_security = 'SA_SALESTRANSVIEW';

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 . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");

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

print_sales_method_list();

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

function GetPosBankTrans($from, $to, $bank_id, $user=false, $point=false, $suppress_return=false) {

	$sql = "SELECT trans.trans_no, 
				trans.type, 
				trans.tran_date, 
				trans.rate, 
				IF(trans.type = ".ST_SALESINVOICE.", ov_amount+ov_discount+ov_gst-pt.discount, bt.amount) AS InvoiceTotal, 
				cust.name AS DebtorName, 
				u.user_id, 
				pos.pos_name 
		FROM 
			".TB_PREF."pos_bank_trans pb, 
			".TB_PREF."debtors_master cust, 
			".TB_PREF."users u, 
			".TB_PREF."audit_trail at, 
			".TB_PREF."bank_trans bt, 
			".TB_PREF."debtor_trans trans 
		LEFT JOIN ".TB_PREF."pos_trans pt ON trans.trans_no = pt.trans_no AND trans.type = ".ST_SALESINVOICE." 
		LEFT JOIN ".TB_PREF."sales_pos pos ON pt.pos_id = pos.id 
		WHERE trans.trans_no = pb.pos_trans_no 
			AND pb.trans_type = trans.type 
			AND bt.id = pb.bank_trans_no 
		    AND trans.debtor_no = cust.debtor_no 
		    AND trans.trans_no = at.trans_no 
		    AND trans.type = at.type 
		    AND at.user = u.id 
		    AND bt.bank_act = ".db_escape($bank_id)."

		    AND trans.tran_date >= '".date2sql($from)."'
		    AND trans.tran_date <= '".date2sql($to)."'";

		if($user != -1)
			$sql .= " AND u.id = ".db_escape($user);
		if(!empty($point))
			$sql .= " AND pos.id = ".db_escape($point);

		$sql .= " GROUP BY trans.trans_no, trans.type ORDER BY trans.trans_no, trans.tran_date";

	return db_query($sql, 'Error getting order details');
}

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

function print_sales_method_list() {
	global $path_to_root;

	$point = $_POST['PARAM_0'];
	$user = $_POST['PARAM_1'];
	$from = $_POST['PARAM_2'];
	$to = $_POST['PARAM_3'];
	$zeros = $_POST['PARAM_4'];
	$suppress_return = $_POST['PARAM_5'];
	$summaryOnly = $_POST['PARAM_6'];
	$comments = $_POST['PARAM_7'];
	$orientation = $_POST['PARAM_8'];

	$bank_acts = get_bank_accounts();
	$user_id = $user <= 0 ? _('All Users') : get_user($user)['user_id'];
	$pos_row = get_sales_point($point);

	include_once($path_to_root.'/reporting/includes/pdf_report.inc');

	$orientation = ($orientation ? 'L' : 'P');

	$dec = user_price_dec();

	$cols = array(0, 40, 100, 250, 310, 400, 460, 525);

	$headers = array(_('Trans#'), _('Trans Type'), _('Customer'), _('User'), _('POS'),
		_('Inv Date'),	_('Total'));

	$aligns = array('left',	'left', 'left', 'left', 'left', 'right', 'right');

    $params = array(0 => $comments,
	    			1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
	    			2 => array('text' => _('User Filter'), 'from' => $user_id, 'to' => ''),
	    			3 => array('text' => _('Sales Point'), 'from' => $point == 0 ? _('All sales points') : $pos_row['pos_name'], 'to' => ''),
	    			4 => array('text' => _('Suppress Zeros'), 'from' => $zeros ? _('No') : _('Yes'), 'to' => ''),
	    			5 => array('text' => _('Type'),		'from' => $summaryOnly ? _('Summary Only') : _('Detailed Report'), 'to' => '')
	    		);

	$rep = new FrontReport(_('Payment Wise Listing'), "PaymentWiseListing", user_pagesize(), 9, $orientation);

    if ($orientation == 'L')
    	recalculate_cols($cols);

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

	$rep->NewPage();

	$subtotal = 0;
	$total = 0;
	$temp = 0;

	foreach($bank_acts as $row) {

		$result = GetPosBankTrans($from, $to, $row['id'], $user, $point, $suppress_return);

		foreach ($result as $row2) {
			$temp += $row2['InvoiceTotal'] * $row2['rate'];
		}

		if($temp == 0 && $zeros == 0)
			continue;

		$rep->NewLine(0, 2, false);
		$rep->Font('bold');
		$rep->TextCol(0, 2,	$row['bank_account_name']);
		$rep->AmountCol(6, 7, $temp, $dec);
		$rep->Font();
		$rep->NewLine();

		foreach($result as $myrow) {

			$amt = $myrow['InvoiceTotal'] * $myrow['rate'];

			if(!$summaryOnly) {
				$rep->TextCol(0, 1,	$myrow['trans_no']);
				$rep->TextCol(1, 2,	$myrow['type'] == ST_SALESINVOICE ? _('Invoice') : _('Return'));
				$rep->TextCol(2, 3,	$myrow['DebtorName']);
				$rep->TextCol(3, 4,	$myrow['user_id']);
				$rep->TextCol(4, 5,	$myrow['pos_name']);
				$rep->DateCol(5, 6,	$myrow['tran_date'], true);
				$rep->AmountCol(6, 7, $amt, $dec);
				
				$rep->NewLine();
			}

			$subtotal += $amt;
		}

		$rep->Line($rep->row+5);
		$rep->NewLine();
		$total += $subtotal;
		$subtotal = 0;
		$temp = 0;
	}

	$rep->fontSize += 2;
	$rep->Font('bold');
	$rep->TextCol(0, 3, _('Grand Total'));
	$rep->fontSize -= 2;
	$rep->AmountCol(6, 7, $total, $dec);
	$rep->Line($rep->row  - 4);
	$rep->NewLine();
	$rep->Font();
	$rep->End();
}