<?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_PRICEREP';

include_once($path_to_root . '/modules/pos/includes/pos_db.inc');

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

function get_pos_invoice_range($from, $to) {
	global $SysPrefs;

	$ref = ($SysPrefs->print_invoice_no() == 1 ? 'trans_no' : 'reference');

	$sql = "SELECT trans.trans_no, trans.reference 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.type=".ST_SALESINVOICE." AND ISNULL(voided.id) AND trans.trans_no IN (SELECT trans_no FROM ".TB_PREF."pos_trans) AND trans.trans_no BETWEEN ".db_escape($from)." AND ".db_escape($to)." ORDER BY trans.tran_date, trans.$ref";

	return db_query($sql, 'Cant retrieve invoice range');
}

print_pos_receipt();

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

function print_pos_receipt() {
	global $path_to_root, $SysPrefs;

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

	$from = $_POST['PARAM_0'];
	$to = $_POST['PARAM_1'];
	$customer = $_POST['PARAM_2'];
	$comments = $_POST['PARAM_3'];
	$orientation = $_POST['PARAM_4'];

	if (!$from || !$to) return;

	$orientation = ($orientation ? 'L' : 'P');
	$fno = explode("-", $from);
	$tno = explode("-", $to);
	$from = min($fno[0], $tno[0]);
	$to = max($fno[0], $tno[0]);

	$rep = new FrontReport(_('RECEIPT'), 'PosReceipt', 'A6', 10, $orientation);

	if ($orientation == 'P') {
		$rep->pageWidth=298;
		$rep->pageHeight=420;
		if (!isset($margins)) {
			$rep->topMargin=5;
			$rep->bottomMargin=4;
			$rep->leftMargin=5;
			$rep->rightMargin=4;
		}
	}
	else { // Landscape
		$rep->pageWidth=420;
		$rep->pageHeight=298;
		if (!isset($margins)) {
			$rep->topMargin=4;
			$rep->bottomMargin=4;
			$rep->leftMargin=5;
			$rep->rightMargin=4;
		}
	}

	$cols = array(0, 60, 80, 110, 150, 200, 240, 280);
	$headers = array(_('Quantity'), '', '', _('Price'), _('Disc'), '', _('Total'));
	$aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right');
	$params = array('comments' => '');

	$comp_details = get_company_prefs();
	$comp_name = $comp_details['coy_name'];
	$comp_adrs = $comp_details['postal_address'];
	$comp_phone = $comp_details['phone'];

	$range = get_pos_invoice_range($from, $to);
	$count = 0;
	$start_time = microtime(true);

	while($row = db_fetch($range)) {

		$myrow = get_customer_trans($row['trans_no'], ST_SALESINVOICE);

		if($customer && $myrow['debtor_no'] != $customer)
			continue;

		$sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
		$result = get_customer_trans_details(ST_SALESINVOICE, $row['trans_no']);
		$trans = get_pos_trans($row['trans_no']);
		$cash = $trans['cash_amount'];
		$bill_discount = $trans['discount'];
		
		if($trans['order_type'] == 1)
			$order_type = _('Dine-In');
		elseif($trans['order_type'] == 2)
			$order_type = _('Take Away');
		elseif($trans['order_type'] == 3)
			$order_type = _('Delivery');
		else
			$order_type = '';

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

		$rep->row = $rep->row - $rep->topMargin - (2*$rep->lineHeight);

		$rep->Font('bold');
		$rep->NewLine();
		$rep->TextCol(0, 1, _('Address:'));
		$rep->Font();
		$rep->TextCol(1, 7, $comp_adrs);
		$rep->NewLine();
		$rep->Font('bold');
		$rep->TextCol(0, 1, _('Phone:'));
		$rep->Font();
		$rep->TextCol(1, 4, $comp_phone);
		$rep->NewLine();
		$rep->Font('bold');
		$rep->TextCol(0, 1, _('Date:'));
		$rep->Font();
		$rep->TextCol(1, 4, sql2date($myrow['tran_date']));
		$rep->Font('bold');
		$rep->TextCol(4, 5, _('Order Type:'));
		$rep->Font();
		$rep->TextCol(5, 6, $order_type);
		$rep->NewLine();
		$rep->Font('bold');
		$rep->TextCol(0, 1, _('Reference:'));
		$rep->Font();
		$rep->TextCol(1, 4, $myrow['reference']);
		$rep->Font('bold');
		$rep->TextCol(4, 5, _('Table:'));
		$rep->Font();
		$rep->TextCol(5, 6, $trans['table_id']);

		$rep->NewLine(5);

		$rep->TextCol(0, 1, _('Customer:'));
		$rep->TextCol(1, 6, $myrow['DebtorName']);
		$rep->NewLine();
		$rep->TextCol(0, 1, _('Sales Person:'));
		$rep->TextCol(1, 6, $_SESSION['wa_current_user']->name);
		$rep->NewLine(3.5);

		$sub_total = 0;
		$row_count = 0;

		while ($item_row = db_fetch($result)) {
			if($item_row['quantity']==0 || $item_row['unit_price'] == 0) continue;

			$value = round2(((1 - $item_row['discount_percent']) * $item_row['unit_price'] * $item_row['quantity']), user_price_dec());
			$sub_total += $value;

			if ($item_row['discount_percent'] == 0)
				$display_discount = "";
			else
				$display_discount = percent_format($item_row['discount_percent']*100) . "%";

			$rep->Font('bold');
			$rep->TextCol(0, 7, $item_row['StockDescription']);
			$rep->Font();
			if($rep->row <= 2*$rep->bottomMargin) {
				$rep->SetHeaderType('');
				$rep->NewPage();
				$rep->row = $rep->pageHeight;
			}
			$rep->NewLine();
			$rep->TextCol(0, 2, $item_row['quantity'].'&nbsp;'.$item_row['units']);
			$rep->TextCol(3, 4, price_format($item_row['unit_price']));
			$rep->TextCol(4, 5, $display_discount);
			$rep->TextCol(6, 7, price_format($value));
			if($rep->row <= 2*$rep->bottomMargin) {
				$rep->SetHeaderType('');
				$rep->NewPage();
				$rep->row = $rep->pageHeight;
			}
			$rep->NewLine();

			$display_total = $myrow['ov_freight']+$myrow['ov_gst']+$myrow['ov_amount']+$myrow['ov_freight_tax'];
			$refund = $cash - ($display_total - $bill_discount);
			
			$row_count++;
		}
		$rep->NewLine();
		$rep->TextCol(4, 6, _('Subtotal'));
		$rep->TextCol(6, 7, price_format($sub_total));
		$rep->NewLine();
		$rep->TextCol(4, 6, _('After Taxes'));
		$rep->TextCol(6, 7, price_format($display_total));
		$rep->NewLine();
		$rep->TextCol(4, 6, _('Bill Discount'));
		$rep->TextCol(6, 7, price_format($bill_discount));
		$rep->NewLine();
		$rep->Font('bold');
		$rep->TextCol(4, 6, _('TOTAL RECEIPT'));
		$rep->TextCol(6, 7, price_format($display_total-$bill_discount));
		$rep->NewLine();
		$rep->Font();
		$rep->TextCol(4, 6, _('Cash Tendered'));
		$rep->TextCol(6, 7, price_format($cash));
		$rep->NewLine();
		$rep->TextCol(4, 6, _('Refund'));
		$rep->TextCol(6, 7, price_format($refund));

		$count++;
	}
	
	$rep->End();
}