<?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_SALESMANAGER';
$path_to_root  = '../../..';
include_once($path_to_root.'/includes/session.inc');
add_access_extensions();

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

include_once($path_to_root.'/includes/ui.inc');
include_once($path_to_root.'/admin/db/company_db.inc');
include_once($path_to_root.'/modules/pos/includes/pos_ui.inc');

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

function can_process() {
	if(!check_num('pos_table_number', 1)) {
		display_error(_('Restaurant Tables Number must be a positive number'));
		set_focus('pos_table_number');
		return false;
	}
	return true;
}

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

if (isset($_POST['submit']) && can_process()) {
	update_company_prefs(get_post(array('pos_def_customer', 'pos_def_category', 'pos_def_sales_type', 'pos_commission', 'pos_amt_barcode', 'pos_prompt_receipt_print', 'pos_restaurant_mode', 'pos_kitchen_print_silent', 'pos_receipt_print_silent', 'pos_bill_print_silent', 'pos_def_restaurant_order_type', 'pos_table_number')));
	display_notification(_('POS setup has been updated.'));
}

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

page(_($help_context = 'POS Default Settings'), false, false, '', $js);

start_form();

start_outer_table(TABLESTYLE2);

table_section(1);

$myrow = get_company_prefs();

if(empty($myrow['pos_def_customer'])) {
	set_company_pref('pos_def_customer', NULL, 'int', NULL, 1);
	$myrow['pos_def_customer'] = 1;
}
if(empty($myrow['pos_def_category'])) {
	set_company_pref('pos_def_category', NULL, 'int', NULL, -1);
	$myrow['pos_def_category'] = -1;
}
if(empty($myrow['pos_def_sales_type'])) {
	set_company_pref('pos_def_sales_type', 'setup.company', 'int', '11', 1);
	$myrow['pos_def_sales_type'] = 1;
}
if(empty($myrow['pos_commission'])) {
	set_company_pref('pos_commission', NULL, 'int', '1', 0);
	$myrow['pos_commission'] = 0;
}
if(empty($myrow['pos_amt_barcode'])) {
	set_company_pref('pos_amt_barcode', NULL, 'int', '1', 0);
	$myrow['pos_amt_barcode'] = 0;
}
if(empty($myrow['pos_restaurant_mode'])) {
	set_company_pref('pos_restaurant_mode', NULL, 'int', '1', 0);
	$myrow['pos_restaurant_mode'] = 0;
}
if(empty($myrow['pos_kitchen_print_silent'])) {
	set_company_pref('pos_kitchen_print_silent', 'setup.company', 'int', '1', 0);
	$myrow['pos_kitchen_print_silent'] = 0;
}
if(empty($myrow['pos_receipt_print_silent'])) {
	set_company_pref('pos_receipt_print_silent', NULL, 'int', '1', 0);
	$myrow['pos_receipt_print_silent'] = 0;
}
if(empty($myrow['pos_bill_print_silent'])) {
	set_company_pref('pos_bill_print_silent', NULL, 'int', '1', 0);
	$myrow['pos_bill_print_silent'] = 0;
}
if(empty($myrow['pos_def_restaurant_order_type'])) {
	set_company_pref('pos_def_restaurant_order_type', NULL, 'tinyint', '1', 1);
	$myrow['pos_def_restaurant_order_type'] = 1;
}
if(empty($myrow['pos_table_number'])) {
	set_company_pref('pos_table_number',  NULL, 'int', '5', 10);
	$myrow['pos_table_number'] = 10;
}
if(empty($myrow['pos_prompt_receipt_print'])) {
	set_company_pref('pos_prompt_receipt_print',  NULL, 'tinyint', '1', 0);
	$myrow['pos_prompt_receipt_print'] = 0;
}

$_POST['pos_def_customer'] = $myrow['pos_def_customer'];
$_POST['pos_def_category'] = $myrow['pos_def_category'];
$_POST['pos_def_sales_type'] = $myrow['pos_def_sales_type'];
$_POST['pos_commission'] = $myrow['pos_commission'];
$_POST['pos_amt_barcode'] = $myrow['pos_amt_barcode'];
$_POST['pos_prompt_receipt_print'] = $myrow['pos_prompt_receipt_print'];
$_POST['pos_restaurant_mode'] = $myrow['pos_restaurant_mode'];
$_POST['pos_kitchen_print_silent'] = $myrow['pos_kitchen_print_silent'];
$_POST['pos_receipt_print_silent'] = $myrow['pos_receipt_print_silent'];
$_POST['pos_bill_print_silent'] = $myrow['pos_bill_print_silent'];
$_POST['pos_def_restaurant_order_type'] = $myrow['pos_def_restaurant_order_type'];
$_POST['pos_table_number'] = floor($myrow['pos_table_number']);

table_section_title(_('Default Settings'));

customer_list_row(_('Default Customer:'), 'pos_def_customer', $_POST['pos_def_customer']);
stock_categories_list_row(_('Default Category:'), 'pos_def_category', null, _('All categories'));
sales_types_list_row(_('Default Sales Type:'), 'pos_def_sales_type');
check_row(_('Salesman Commission:'), 'pos_commission');
check_row(_('Amount/Weight in Barcode:'), 'pos_amt_barcode');
check_row(_('Prompt Receipt Print:'), 'pos_prompt_receipt_print');

table_section_title(_('Restaurant Mode'));
check_row(_('Use Restaurant Mode:'), 'pos_restaurant_mode');
check_row(_('Kitchen Print Silently:'), 'pos_kitchen_print_silent');
check_row(_('Receipt Print Silently:'), 'pos_receipt_print_silent');
check_row(_('Bill Print Silently:'), 'pos_bill_print_silent');
small_amount_row('Restaurant Tables:', 'pos_table_number', null, null, null, null, true);
pos_order_type_list_row(_('Default Sales Method:'), 'pos_def_restaurant_order_type');

end_outer_table(1);

submit_center('submit', _('Update'), true, '', 'default');

end_form(1);
end_page();