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

define('ST_POSRECEIPT', '_receipt');
define('ST_KITCHENORDER', 'kitchen');
define('ST_POSBILL', '_bill');
define('OT_DINEIN', '1');
define('OT_TAKEAWAY', '2');
define('OT_DELIVERY', '3');

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

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

function items_exclude_list($name, $selected_id=null, $all_option=false, $submit_on_change=false, $opts=array(), $editkey=false, $type='stock') {

	$sql = "SELECT i.item_code, i.description, c.description, s.inactive, s.editable FROM ".TB_PREF."item_codes i,".TB_PREF."pos_stock_exclude e,".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE i.stock_id = s.stock_id AND i.category_id = c.category_id AND i.item_code = e.stock_id AND s.mb_flag != 'F'";

	$ret = combo_input($name, $selected_id, $sql, 'i.item_code', 's.description',
		array_merge(
		  array(
			'format' => '_format_stock_items',
			'spec_option' => $all_option===true ?  _('All Items') : $all_option,
			'spec_id' => ALL_TEXT,
			'search_box' => true,
			'search' => array('i.item_code','c.description','i.description'),
			'search_submit' => get_company_pref('no_item_list')!=0 && (!isset($opts['fixed_asset']) || !$opts['fixed_asset']),
			'size'=>10,
			'select_submit'=> $submit_on_change,
			'category' => 2,
			'order' => array('s.description','i.item_code'),
			'editlink' => $editkey ? add_edit_combo('item') : false,
			'editable' => false,
			'max' => 255
		  ), $opts), $type );
	return $ret;
}

function returnable_items_exclude_list($name, $selected_id=null, $all_option=false, $submit_on_change=false, $opts=array(), $editkey=false, $type='stock') {

	$sql = "SELECT i.item_code, i.description, c.description, s.inactive, s.editable FROM ".TB_PREF."item_codes i,".TB_PREF."pos_returnable_exclude e,".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE i.stock_id = s.stock_id AND i.category_id = c.category_id AND i.item_code = e.stock_id AND s.mb_flag != 'F'";

	$ret = combo_input($name, $selected_id, $sql, 'i.item_code', 's.description',
		array_merge(
		  array(
			'format' => '_format_stock_items',
			'spec_option' => $all_option===true ?  _('All Items') : $all_option,
			'spec_id' => ALL_TEXT,
			'search_box' => true,
			'search' => array('i.item_code','c.description','i.description'),
			'search_submit' => get_company_pref('no_item_list')!=0 && (!isset($opts['fixed_asset']) || !$opts['fixed_asset']),
			'size'=>10,
			'select_submit'=> $submit_on_change,
			'category' => 2,
			'order' => array('s.description','i.item_code'),
			'editlink' => $editkey ? add_edit_combo('item') : false,
			'editable' => false,
			'max' => 255
		  ), $opts), $type );
	return $ret;
}

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

function pos_print_link($doc_no, $link_text, $link=true, $type_no, $icon=false, $class='printlink', $id='', $email=0, $extra=0) {
	global $path_to_root;

	$def_orientation = (user_def_print_orientation() == 1 ? 1 : 0);
	$dir = $path_to_root.'/modules/pos/reporting/redirect.php?';

	switch ($type_no) {

		case ST_POSRECEIPT :
			$rep = ST_POSRECEIPT;
			$ar = array(
				'PARAM_0' => $doc_no,
				'PARAM_1' => $doc_no,
				'PARAM_2' => 0,
				'PARAM_3' => '',
				'PARAM_4' => $def_orientation);
			break;
		case ST_KITCHENORDER :
			$rep = ST_KITCHENORDER;
			$ar = array(
				'PARAM_0' => $doc_no,  
				'PARAM_1' => $def_orientation,
				'PARAM_2' => $extra);
			break;
		case ST_POSBILL :
			$rep = ST_POSBILL;
			$ar = array(
				'PARAM_0' => $doc_no,  
				'PARAM_1' => $def_orientation,
				'PARAM_2' => $extra);
			break;
		default:
			return null;
	}

	return print_link($link_text, $rep, $ar, $dir, $icon, $class, $id);
}

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

function pos_salesman_list($name, $id=null, $spec_option=false, $submit_on_change=false, $show_inactive=false) {
	$sql = "SELECT id, name, inactive FROM ".TB_PREF."pos_salesman ";

	return combo_input($name, $id, $sql, 'id', 'name',
	array(       
		'spec_option' => $spec_option,
		'select_submit' => $submit_on_change,
		'async' => false,
		'show_inactive' => $show_inactive
	));
}

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

function pos_salesman_list_cells($label=null, $name, $id=null, $all_option=false, $submit_on_change=false, $show_inactive=false) {
	if ($label != null)
		echo "<td class='label'>".$label."</td>\n";
	echo "<td>";
	echo pos_salesman_list($name, $id, $all_option, $submit_on_change, $show_inactive);
	echo "</td>\n";
}

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

function pos_salesman_list_row($label, $name, $id = null, $all_option=false, $submit_on_change=false, $show_inactive=false) {
	echo "<tr><td class='label'>".$label."</td>";
	pos_salesman_list_cells(null, $name, $id, $all_option, $submit_on_change, $show_inactive);
	echo "</tr>";
}

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

function pos_number_list($name, $selected, $from, $to, $no_option=false, $submit_on_change=false) {
	$items = array();
	for ($i = $from; $i <= $to; $i++)
		$items[$i] = $i;

	return array_selector($name, $selected, $items,
		array('select_submit'=> $submit_on_change,
				'spec_option' => $no_option,
				'spec_id' => 0));
}

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

function pos_order_type_list($name, $selected, $submit_on_change=false, $all_option=false) {

	$types = array('1'=>_('Dine-in'), '2'=>_('Take Away'), '3'=>_('Delivery'));

	return array_selector($name, $selected, $types, array('select_submit'=> $submit_on_change, 'spec_option' => $all_option));
}

function pos_order_type_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false) {

	if ($label != null)
		echo "<td class='label'>".$label."</td>\n";
	echo "<td>";
	echo pos_order_type_list($name, $selected_id, $submit_on_change, $all_option);
	echo "</td>\n";
}

function pos_order_type_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false) {

	echo "<tr><td class='label'>".$label."</td>\n";
	echo "<td>";
	echo pos_order_type_list($name, $selected_id, $submit_on_change, $all_option);
	echo "</td></tr>\n";
}

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

function credit_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false) {

	$sql = "SELECT id, bank_account_name, bank_curr_code, inactive FROM ".TB_PREF."bank_accounts WHERE account_type=".BT_CREDIT;

	echo "<tr>";
	if ($label != null)
		echo "<td class='label'>".$label."</td>\n";
	echo "<td>";
	echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
		array(
			'spec_option' => $all_option,
			'spec_id' => ALL_TEXT,
			'format' => '_format_add_curr',
			'select_submit'=> $submit_on_change,
			'async' => true
		));
	echo "</td></tr>\n";
}

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

function pos_sales_point_list($name, $selected_id, $submit_on_change=false, $all_option=false) {
	global $security_areas;

	$sql = "SELECT id, pos_name FROM ".TB_PREF."sales_pos WHERE !inactive";

	$options = array(
			'spec_option'=> empty($all_option) ? _('All Sales Points') : $all_option,
			'select_submit'=> $submit_on_change,
			'async' => true
		);

	if(!array_search($security_areas['SA_POSSUPERADMIN'][0], $_SESSION['wa_current_user']->role_set)) {
		$sql .= " AND id = ".db_escape($_SESSION['wa_current_user']->pos);
		unset($options['spec_option']);
	}

	return combo_input($name, '', $sql, 'id', 'pos_name', $options);
}

function pos_sales_point_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false) {

	if ($label != null)
		echo "<td class='label'>".$label."</td>\n";
	echo "<td>";
	echo pos_sales_point_list($name, $selected_id, $submit_on_change, $all_option);
	echo "</td>\n";
}

function pos_sales_point_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false) {

	echo "<tr><td class='label'>".$label."</td>\n";
	echo "<td>";
	echo pos_sales_point_list($name, $selected_id, $submit_on_change, $all_option);
	echo "</td></tr>\n";
}

function pos_meta_forward($forward_to, $params, $timeout=0, $return=false) {
	global $Ajax;

	echo "<meta http-equiv='Refresh' content='".$timeout."; url=$forward_to?$params'>\n";
	echo "<center><br>" . _("Please wait. You should automatically be forwarded.")."<br><br></center>\n";;
	if ($params !='') $params = '?'.$params;
	$Ajax->redirect($forward_to.$params);
	if (!$return) exit;
}