<?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 GetSalesTypeTrans($from, $to, $method=false, $user=false, $point=false) {

	$sql = "SELECT DISTINCT trans.*,
			ov_amount+ov_discount+ov_gst AS InvoiceTotal,
			cust.name AS DebtorName,
			cust.curr_code,
			u.user_id,
			u.real_name,
			pt.*,
			pos.pos_name 
		FROM ".TB_PREF."debtor_trans trans,
			 ".TB_PREF."debtors_master cust,
			 ".TB_PREF."sales_orders sorder,
			 ".TB_PREF."pos_trans pt,
			 ".TB_PREF."users u,
			 ".TB_PREF."audit_trail at, 
			 ".TB_PREF."sales_pos pos 
		WHERE sorder.order_no=trans.order_ 
		    AND sorder.trans_type = ".ST_SALESORDER."
		    AND trans.trans_no = pt.trans_no
		    AND trans.debtor_no = cust.debtor_no
		    AND trans.trans_no = at.trans_no
		    AND (trans.type=".ST_SALESINVOICE." OR trans.type=".ST_CUSTCREDIT.")
		    AND (at.type=".ST_SALESINVOICE.")
		    AND at.user = u.id 
		    AND pt.pos_id = pos.id 
		    AND trans.tran_date >= '".date2sql($from)."'
		    AND trans.tran_date <= '".date2sql($to)."'";
		if($method)
			$sql .= " AND pt.order_type = ".db_escape($method);
		if($user > 0)
			$sql .= " AND u.id = ".db_escape($user);
		if($point > 0)
			$sql .= " AND pos.id = ".db_escape($point);

		$sql .= " 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;

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

	$methods_array = array(_('Other'), _('Dine-In'), _('Take Away'), _('Delivery'));
	$user_id = $user <= 0 ? _('All Users') : get_user($user)['user_id'];

	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#'), _('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 Method'),'from' => $method == 0 ? _('All Methods') : $methods_array[$method], 'to' => '')
	    		);

	$rep = new FrontReport(_('Sales Method Listing'), "SalesTypeListing", 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;

	$result = GetSalesTypeTrans($from, $to, $method, $user, $point);

	foreach($methods_array as $k=>$v) {

		foreach($result as $row) {

			if(empty($row['order_type']))
				$row['order_type'] = 0;

			if($row['order_type'] == $k) {
				if($row['type'] == ST_SALESINVOICE)
					$temp += $row['InvoiceTotal'] * $row['rate'];
				else
					$temp -= $row['InvoiceTotal'] * $row['rate'];
			}
		}

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

		$rep->NewLine(0, 2, false, $k);
		$rep->Font('bold');
		$rep->TextCol(0, 2,	$v);
		$rep->AmountCol(6, 7, $temp, $dec);
		$rep->Font();
		$rep->NewLine();

		foreach($result as $myrow) {

			if(empty($myrow['order_type']))
				$myrow['order_type'] = 0;

			if($myrow['order_type'] == $k) {

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

				if($myrow['type'] == ST_CUSTCREDIT)
				$amt = -$amt;

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

