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

$path_to_root = '../../..';

include_once($path_to_root . '/includes/db_pager.inc');
include_once($path_to_root . '/includes/session.inc');
include_once($path_to_root . '/sales/includes/sales_ui.inc');

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

$page_security = 'SA_SALESTRANSVIEW';


$js = '';
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 600);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page($_SESSION['page_title'] = _($help_context = 'Point of Sales Inquiry'), false, false, '', $js);

if(get_post('return'))
	$return = $_POST['return'];
elseif(isset($_GET['return']) && $_GET['return'] == 'Yes')
	$return = $_GET['return'];
else
	$return = '';

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

function trans_view($trans) {
	return get_trans_view_str($trans['type'], $trans['trans_no']);
}

function return_link($trans) {

	if ($trans['Outstanding'] > 0)
		return pager_link(_('Receive returns for this invoice'), '/modules/pos/manage/pos_credit_note.php?InvoiceNo='.$trans['trans_no'], ICON_CREDIT);
}

function sales_method($trans) {

	if($trans['order_type'] == 1)
		$type = _('Dine-In');
	elseif($trans['order_type'] == 2)
		$type = _('Take Away');
	elseif($trans['order_type'] == 3)
		$type = _('Delivery');
	else
		$type = '';

	return $type;
}

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

start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();

ref_cells(_('Enter search string:'), 'string', '',null, '', true);

customer_list_cells(_('Select a customer: '), 'customer_id', null, true, true);
pos_order_type_list_cells(null, 'order_type', null, true, _('All Sales Methods'));
pos_sales_point_list_cells(null, 'pos_id', null, true);

end_row();

start_row();
date_cells(_('From:'), 'from_date', '', null, -user_transaction_days(), 0, 0, null, true);
date_cells(_('To:'), 'to_date', '', null, 0, 0, 0, null, true);
echo "<td>".sales_items_list('stock_id', null, _('All Items'), true, '', array('search_box'=>false))."</td>";
submit_cells('SearchOrders', _("Search"), "align='right'", _('Select documents'), 'default');
end_row();
end_table(1);

hidden('return', $return);

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

$sql = get_pos_sales_sql(get_post('pos_id', $_SESSION['wa_current_user']->pos), get_post('order_type'), get_post('customer_id'), get_post('from_date'), get_post('to_date'), get_post('stock_id'), get_post('string'));

$cols = array(
	_('Invoice #') => array('fun'=>'trans_view'),
	_('Ref #'),
	_('Sales Person') => array('type' => 'u.user_id' , 'ord' => '') ,
	_('Sales Method') => array('fun'=>'sales_method'),
	_('Customer') => array('type' => 'debtor.name' , 'ord' => '') ,
	_('Invoice Date') => array('type' =>  'date', 'ord' => ''),
	_('Invoice Time') => array('align'=>'center'),
	_('Invoice Total') => array('type'=>'amount', 'ord'=>''),
);

if(!empty($return) && $return == 'Yes')
	$cols[] = array('fun'=>'return_link');

$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = '80%';

display_db_pager($table);
// var_dump($_SESSION['wa_current_user']);
// print_r($security_areas);
end_form();
end_page();