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

page(_($help_context = 'POS settings'));

include_once($path_to_root.'/includes/ui.inc');
include_once($path_to_root.'/sales/includes/db/sales_points_db.inc');

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

simple_page_mode();

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

if ($Mode=='UPDATE_ITEM') {
	write_pos_credit_account($selected_id, $_POST['account']);
	display_notification(_('Selected point of sale has been updated'));
	$Mode = 'RESET';
}

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

if ($Mode == 'RESET') {
	$selected_id = -1;
	unset($_POST);
}

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

$result = get_all_sales_points();

start_form();
start_table(TABLESTYLE);

$th = array (_('POS ID'), _('POS Name'), _('Credit sale'), _('Cash sale'), _('Location'), _('Default bank account'), '');
table_header($th);
$k = 0;

while ($myrow = db_fetch($result)) {
    alt_table_row_color($k);
    $pos = get_pos_credit_account($myrow['id']);
    $credit_acc = empty($pos['account']) ? _('Not set') : $pos['bank_account_name'];

    label_cell($myrow['id'], 'nowrap');
	label_cell($myrow['pos_name'], 'nowrap');
	label_cell($myrow['credit_sale'] ? _('Yes') : _('No'));
	label_cell($myrow['cash_sale'] ? _('Yes') : _('No'));
	label_cell($myrow['location_name'], '');
	label_cell($credit_acc, "");
 	edit_button_cell('Edit'.$myrow['id'], _('Edit'));
	end_row();
}

end_table(1);

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

$crd = db_has_credit_accounts();

if(!$crd)
	display_note(_('To have credit POS first define at least one credit bank account.'));

start_table(TABLESTYLE2);

if($selected_id != -1) {

 	if ($Mode == 'Edit') {
		$myrow = get_sales_point($selected_id);
		$pos = get_pos_credit_account($selected_id);
		$_POST['account']  = $pos['account'];
		label_row(_('POS Name:'), $myrow['pos_name']);
		credit_accounts_list_row(_('Default bank payment account:'), 'account', null, false, _('Not set'));
	}
	hidden('selected_id', $selected_id);
}
else
	hidden('account');

end_table(1);

if($selected_id != -1) {
	echo "<center>";
	echo submit('UPDATE_ITEM', _("Update"), true, _('Submit changes'), 'default');
	echo "</center>";
}

end_form();
end_page();