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

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

print_pos_receipt();

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

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

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

	$table = $_POST['PARAM_0'] ? $_POST['PARAM_0'] : _('Delivery/Take Away');
	$orientation = $_POST['PARAM_1'];
	$orientation = ($orientation ? 'L' : 'P');

	$rep = new FrontReport(_('Kitchen Order'), 'KitchenOrder', '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, 50, 70, 90, 140, 170, 220, 289);
	$headers = array(_('Item'), '', '', _('Quantity'), _('Unit'), _('Ordered'), _('This Order'));
	$aligns = array('left', 'left', 'left', 'right', 'center', 'right', 'right');
	$params = array('comments' => '');

	$result = get_table_order_details($_SESSION['wa_current_user']->pos, $table);

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

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

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

	$rep->NewLine(2);
	$rep->Font('bold');
	$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, Today().'&nbsp;'.date('H:i:s', time()));
	$rep->Font('bold');
	$rep->TextCol(5, 6, _('Table:'));
	$rep->Font();
	$rep->TextCol(6, 7, $table);

	$rep->NewLine(6);

	$rep->TextCol(0, 1, _('Waiter:'));
	$rep->TextCol(1, 6, $_SESSION['wa_current_user']->name);
	$rep->NewLine(4.5);

	$item_total = 0;
	$row_count = 0;
	while ($item_row = db_fetch($result)) {
		if($item_row['quantity']==0 || $item_row['price'] == 0 || $item_row['quantity'] - $item_row['ordered'] == 0)
			continue;
		
		$rep->Font('bold');
		$rep->TextCol(0, 7, $item_row['item_description']);
		$rep->Font();
		if($rep->row <= 2*$rep->bottomMargin) {
			$rep->SetHeaderType('');
			$rep->NewPage();
			$rep->row = $rep->pageHeight;
		}
		$rep->NewLine();

		$rep->TextCol(3, 4, $item_row['quantity']);
		$rep->TextCol(4, 5, $item_row['units']);
		$rep->TextCol(5, 6, $item_row['ordered']);
		$rep->TextCol(6, 7, $item_row['quantity'] - $item_row['ordered']);
		if($rep->row <= 2*$rep->bottomMargin) {
			$rep->SetHeaderType('');
			$rep->NewPage();
			$rep->row = $rep->pageHeight;
		}
		$rep->NewLine();
		$item_total += ($item_row['quantity'] - $item_row['ordered']);
	}
	$rep->NewLine();
	$rep->TextCol(4, 6, _('Total Items:'));
	$rep->TextCol(6, 7, $item_total);

	$dir =  company_path(). '/pdf_files';
	if (!file_exists($dir))
		mkdir ($dir, 0777);
	$pdf_name = random_id().'.pdf';
	$fname = $dir.'/'.$pdf_name;

	$rep->Output($fname, 'F');

	if (in_ajax()) {
		global $Ajax;

		if(!empty($comp_details['pos_kitchen_print_silent'])) {
			$file = root_folder_name().'/company/'.user_company().'/pdf_files/'.$pdf_name;
			$js = "var elm = window.parent.document.createElement('a');
			elm.setAttribute('href', '#');
			elm.setAttribute('hidden', true);
			elm.onclick = function(){printJS({printable:'$file', type:'pdf', showModal:false, onLoadingStart:()=>document.getElementById('pos_loading_spinner').style.display = 'block', onPrintDialogClose:()=>document.getElementById('pos_loading_spinner').style.display = 'none'});};
			var prnt = parent.document.getElementById('_page_body');
			prnt.appendChild(elm);
			elm.click();";
			$Ajax->addScript(true, $js);
		}
		else
			$rep->End();
	}
	else{
		header('Content-type: application/pdf');
		header('Content-Disposition: inline; filename='.$rep->filename);
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		$rep->Stream($rep->filename);
	}
	// $rep->End();
}