// Global Var for last POS_SALE_CODE
var LAST_POS_CODE = '';

/** 
 * =================================================================================
 * Load current POS device data
 * =================================================================================
 */
/*function PRELOAD_POSDEVICE(CALLBACK_METHOD) 
{
	JSCUS_SQL._RECORD._POSDEVICE.CUS_ME
	(
		function(IS_SUCCESS, RESPONSE_STRING) 
		{
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				PRELOAD_LOCK_POS_SCREEN();
				return;
			}

			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) 
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				PRELOAD_LOCK_POS_SCREEN();
				return;
			}

			// No result [Invalid device]
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				window.location = JSCUS_CACHE.SERVER_URL + '/p/login_device.html';	
				return;
			}

			// Open shift exists
			// ...........................................				
			else 
			{
				PRELOAD_POS_SHIFT();

				if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER"))
				{
					PRELOAD_POS_CODE();
					PRELOAD_DISCOUNT();		
					AUTO_UPDATE_PRODUCT_DATA();
				}

				if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("DEVICE_ID_LBL")) {
					JSOC_ELEMENT.VAR_VALUE('DEVICE_ID_LBL', JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID);
				}
				
				if(JSVC_VAR.VAR_ISSET(CALLBACK_METHOD))
				{
					CALLBACK_METHOD();
				}
			}
		}
	);
}*/


/** 
 * =================================================================================
 * Load current POS shift data
 * =================================================================================
 */
var SHIFT_ID = -1;
var SHIFT_TIME_START = null;
var SHIFT_TIME_START_AMOUNT = -1;
var SHIFT_TIME_END_AMOUNT = -1;

function PRELOAD_POS_SHIFT(CALLBACK_METHOD) 
{
	// Load current shift if exists
	// _______________________________________________________________________
	var GET_MAP_ARRAY = JSCUS_SQL._RECORD._POS_SHIFT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_SHIFT_ACC_DEVICE_ID', JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID); 
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_SHIFT_ACC_ACCOUNT_ID', JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ID);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_SHIFT_STATUS', 2);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_LIMIT', 1);

	JSCUS_SQL._RECORD._POS_SHIFT.CUS_GET
	(
		GET_MAP_ARRAY, 
		function(IS_SUCCESS, RESPONSE_STRING) 
		{
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				PRELOAD_LOCK_POS_SCREEN();
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) 
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				PRELOAD_LOCK_POS_SCREEN();
				return;
			}
	
			// No result [Create new shift]
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) 
			{
				if(JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_LEVEL_ID) == 4) //cashiers only
				{
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER"))
					{
						if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SHIFT_START_MODAL") == true)
						{
							// Clear Modal
							if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
							{
								JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
							}
											
							// Display Modal
							JSOC_DIALOG.VAR_MODAL("SHIFT_START_MODAL", true);
							return;
						}
					}
				}

				if(JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_LEVEL_ID) == 5) //waiter only
				{
					HTTP_START_SHIFT
    				(
    					function(IS_SUCCESS) 
    					{
    						if(IS_SUCCESS == false)
    						{	
    							return;
    						}

    						alertify.success('Shift started.');
    					}
    				);
				}
				
				return;
			}
	
			// Open shift exists [Cache data]
			// ...........................................		
			else 
			{
				SHIFT_ID = JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_SHIFT_ID", 1));
				SHIFT_TIME_START = JSRC_JSON.REF_READ(JSON_OBJECT, "REC_SHIFT_TIME_BEGIN", 1);
				SHIFT_TIME_START_AMOUNT = JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_SHIFT_CASH_BEGIN", 1));

				if(JSVC_VAR.VAR_ISSET(CALLBACK_METHOD))
				{
					CALLBACK_METHOD(true);
				}
			}
		}
	);
};


/**
 * =================================================================================
 * discounts  
 * =================================================================================
 */

var CACHE_DISCOUNT_JSON = [];

function PRELOAD_DISCOUNTS()
{
	// Request active discount data
	// _______________________________________________________________________
	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._DISCOUNT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_DISCOUNT_STATUS', 2);

	var RESPONSE_HANDLER =
		function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Schedule for next update
			// ...........................................
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					PRELOAD_DISCOUNTS();
				},
				1000 * 60 * 5
			);	
			
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				DISCOUNT_DGV.ROW.CLEAR();
				alertify.error('No discounts found.');
				return; 
			}
	
			// Populate discounts
			// ...........................................

			var JSON_COUNT = JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT");
			var COUNTER = 1;

			while(COUNTER <= JSON_COUNT)
			{
				var R_INDEX = 1; 
				var IS_INSERTED = 0;

				while(DISCOUNT_DGV.ROW.COUNT() >= R_INDEX)
				{
					if
					(
						(JSVC_NUM.VAR_NVALL(DISCOUNT_DGV.CELL.VALUE("CAT_DISCOUNT_ID", R_INDEX-1)) ==
						JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER))) &&
						(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER)) !== 1)
					)
					{


						IS_INSERTED = 1;
					}

					R_INDEX++;
				}


				if(IS_INSERTED == 0)
				{
					//if(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER)) !== 1)
					//{
						var RINDEX = DISCOUNT_DGV.ROW.COUNT();

						DISCOUNT_DGV.ROW.INSERT_AT(RINDEX);

						if(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER)) == 3)
							SENIOR_5P = COUNTER;

						if
						(
							JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_PERCENTAGE", COUNTER)) >= 0 &&
							JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_AMOUNT", COUNTER)) == 0
						)
						{
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER));
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_DESCRIPTION', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_DESCRIPTION", COUNTER));
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_VALUE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_PERCENTAGE", COUNTER) + ' &#37;');
							DISCOUNT_DGV.CELL.VALUE('TYPE', RINDEX, 1);
						}
						
						if
						(
							JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_PERCENTAGE", COUNTER)) == 0 &&
							JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_AMOUNT", COUNTER)) >= 0
						)
						{
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER));
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_DESCRIPTION', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_DESCRIPTION", COUNTER));
							DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_VALUE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_AMOUNT", COUNTER));
							DISCOUNT_DGV.CELL.VALUE('TYPE', RINDEX, 2);
						}
					//}
				}

				COUNTER++;
			}

			CACHE_DISCOUNT_JSON = JSON_OBJECT;
		};
	
	JSCUS_SQL._CATEGORY._DISCOUNT.CUS_GET
	(
		GET_MAP_ARRAY, 
		RESPONSE_HANDLER, 
		true
	); 
}



/**
 * =================================================================================
 * banks  
 * =================================================================================
 */
function PRELOAD_BANKS()
{
 	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._BANK.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_BANK_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_BANK_ID');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	var RESPONSE_HANDLER =
		function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				alertify.error('No banks found.');
				return; 
			}
	
			// Populate banks
			// ...........................................

			JSOC_CMB.VAR_ADD_FROM_JSON
			(
				'CARD_BANK',
				JSON_OBJECT,
				'CAT_BANK_ID',
				'CAT_BANK_DESCRIPTION', 
				true,
				false
			)

			JSOC_CMB.VAR_ADD_FROM_JSON
			(
				'CHECK_BANK',
				JSON_OBJECT,
				'CAT_BANK_ID',
				'CAT_BANK_DESCRIPTION', 
				true,
				false
			)

			JSOC_CMB.VAR_ADD_FROM_JSON
			(
				'ACCTS_CHECK_BANK',
				JSON_OBJECT,
				'CAT_BANK_ID',
				'CAT_BANK_DESCRIPTION', 
				true,
				false
			)
		};
	
	JSCUS_SQL._CATEGORY._BANK.CUS_GET
	(
		GET_MAP_ARRAY, 
		RESPONSE_HANDLER, 
		true
	);
}



/**
 * =================================================================================
 * card types  
 * =================================================================================
 */
function PRELOAD_CARD_TYPES()
{
	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._CARD_TYPE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_CARDTYPE_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_CARDTYPE_ID');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	var RESPONSE_HANDLER =
		function(IS_SUCCESS, RESPONSE_STRING)
		{			
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				alertify.error('No card types found.');
				return; 
			}
	
			// Populate card types
			// ...........................................

			JSOC_CMB.VAR_ADD("CARD_TYPE", FLAG_BIGINT_NULL.toString(), "No Card Selected");

			JSOC_CMB.VAR_ADD_FROM_JSON
			(
				'CARD_TYPE',
				JSON_OBJECT,
				'CAT_CARDTYPE_ID',
				'CAT_CARDTYPE_DESCRIPTION', 
				true,
				false
			)
		};
	
	JSCUS_SQL._CATEGORY._CARD_TYPE.CUS_GET
	(
		GET_MAP_ARRAY, 
		RESPONSE_HANDLER, 
		true
	);
}



/**
 * =================================================================================
 * payment terms
 * =================================================================================
 */

var CACHE_PAYMENT_TERMS_JSON = [];

function PRELOAD_PAYMENT_TERMS()
{
	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PAYMENT_TERM.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PAYTERM_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PAYTERM_ID');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	var RESPONSE_HANDLER =
		function(IS_SUCCESS, RESPONSE_STRING)
		{			
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				alertify.error('No payment terms found.');
				return; 
			}
	
			// Populate card types
			// ...........................................

			JSOC_CMB.VAR_ADD_FROM_JSON
			(
				'CREDIT_TERMS',
				JSON_OBJECT,
				'CAT_PAYTERM_ID',
				'CAT_PAYTERM_DESCRIPTION', 
				true,
				false
			)

			CACHE_PAYMENT_TERMS_JSON = JSON_OBJECT;
		};
	
	JSCUS_SQL._CATEGORY._PAYMENT_TERM.CUS_GET
	(
		GET_MAP_ARRAY, 
		RESPONSE_HANDLER, 
		true
	);
}


/**
 * =================================================================================
 * pricing schemes
 * =================================================================================
 */
var CACHE_PRICING_JSON = [];

function PRELOAD_PRICING_SCHEMES()
{
	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRICE_MODEL.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRICEMDL_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ACC_GROUP_ID));
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRICEMDL_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRICEMDL_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC'); 

	var RESPONSE_HANDLER =
		function(IS_SUCCESS, RESPONSE_STRING)
		{				
			// Schedule for next update
			// ...........................................
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					PRELOAD_PRICING_SCHEMES();
				},
				1000 * 60 * 5
			);

			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
	
			// Response is not valid JSON
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}
		
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				PRICE_MODEL_DGV.ROW.CLEAR();
				JSOC_ELEMENT.VAR_GET('ALERT_PRICING').style.display = '';
				return; 
			}
		
			// Populate discounts
			// ...........................................
			PRICE_MODEL_DGV.POPULATE_FROM_QUERY
			(
				JSON_OBJECT, 
				'',
				false,
				false,
				1,
				[2, 1]
			);

			JSOC_ELEMENT.VAR_GET('ALERT_PRICING').style.display = 'none';
				

			CACHE_PRICING_JSON = JSON_OBJECT;
		};

	JSCUS_SQL._CATEGORY._PRICE_MODEL.CUS_GET
	(
		GET_MAP_ARRAY,
		RESPONSE_HANDLER,
		true
	);
}


/**
 * =================================================================================
 * Locks POS activity when : device auth failure / there is no shift
 * =================================================================================
 */
function PRELOAD_LOCK_POS_SCREEN()
{
	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER"))
	{
		JSOC_STYLE.VAR_VISIBILITY("POS_MAIN_CONTAINER", "HIDDEN");
	}
}



/**
 * =================================================================================
 * Inital loading of POS_SALE_CODE
 * =================================================================================
 */
function PRELOAD_POS_CODE() 
{
	
	// Show Queue Pending
	// _______________________________________________________________________
	JSOC_STYLE.VAR_VISIBILITY("SCHEDULER_PENDING_DIV", "VISIBLE");
			
			
	// Load from localstorage if exists
	// _______________________________________________________________________
	if(getValue('TOTAL') != null)
	{
		var LOCAL_STORAGE_LAST_INDEX = getValue('TOTAL');
		var LAST_MAPPED_ARRAY = getValue('FINAL_SAVE_MAP_ARRAY' + LOCAL_STORAGE_LAST_INDEX);
		LAST_MAPPED_ARRAY = JSVC_MAP_ARRAY.VAR_TO_MAP_ARRAY(LAST_MAPPED_ARRAY);
		
		LAST_POS_CODE = LAST_MAPPED_ARRAY[0]['PARAM_REC_POSSALE_CODE'];
		return;
	}
	

	// Load from Database
	// _______________________________________________________________________	
	var code = '';
	var GET_MAP_ARRAY = JSCUS_SQL._RECORD._POS_SALE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_POSSALE_REC_POSDEVICE_ID', JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'REC_POSSALE_ID');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'DESC');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_LIMIT', 1);

	JSCUS_SQL._RECORD._POS_SALE.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) 
	{
		// Connection issue
		// ...........................................
		if(IS_SUCCESS === false) 
		{
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		// Response is not valid JSON
		// ...........................................
		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) 
		{
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}
		
		// No result
		// ...........................................
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) 
		{
		    LAST_POS_CODE = 1;

		    if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_TRANSACTION_NO")) {
				JSOC_ELEMENT.VAR_VALUE('POS_TRANSACTION_NO', LAST_POS_CODE.toString().padStart(9, 0));
			}
		}
		
		// Read Last Item
		// ...........................................
		else 
		{
			//generate new code
			LAST_POS_CODE = parseInt(JSON_OBJECT.REC_POSSALE_CODE[1]) + 1;

			if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_TRANSACTION_NO")) {
				JSOC_ELEMENT.VAR_VALUE('POS_TRANSACTION_NO', LAST_POS_CODE.toString().padStart(9, 0));
			}
		}
	});
};


/**
 * =================================================================================
 * Auto updating of pricing
 * =================================================================================
 */
var PRICE_MODEL_ID = 1; //default SRP

function AUTO_UPDATE_PRODUCT_DATA() 
{
	// Call every 5 mins
	JSRC_THREAD.REF_POST_DELAYED
	(
		function()
		{
			// Abort if current shift is not assigned
			// _______________________________________________________________________
			if
			(
				JSVC_VAR.VAR_ISSET(SHIFT_TIME_START) == false || 
				JSVC_VAR.VAR_IS_DATE(SHIFT_TIME_START) == false
			)
			{
				AUTO_UPDATE_PRODUCT_DATA();
				return;		
			}
	
	
			// Abort if not a POS page
			// _______________________________________________________________________
			if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER") == false)
			{
				AUTO_UPDATE_PRODUCT_DATA();
				return;						
			}	
				
			
			// Initialize
			// _______________________________________________________________________
			var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ACC_GROUP_ID));
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_CAT_PRICEMDL_ID', PRICE_MODEL_ID);
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_STATUS', 2);
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODUCT_DESCRIPTION');
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');
			
			
			// Request product data
			// _______________________________________________________________________
			JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) 
			{
				// Connection issue
				// ...........................................
				if(IS_SUCCESS === false) 
				{
					UPDATE_PRODUCT_DATA();
					return;
				}
		
				// Response is not valid JSON
				// ...........................................
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
				if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) 
				{
					UPDATE_PRODUCT_DATA();
					return;
				}
		
				// No result
				// ...........................................
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) 
				{
					UPDATE_PRODUCT_DATA();
					return; 
				}
					
				// Legit result
				// ...........................................
				else 
				{

					/*$.each(JSON_OBJECT.CAT_PRODUCT_ID, function(i, item) 
					{
						// Update Product List
						JSOC_ELEMENT.VAR_VALUE("CAT_NAME_" + item,  JSON_OBJECT.CAT_PRODUCT_CAT_PRODTYPE_ID_LV1[i]);
						JSOC_ELEMENT.VAR_VALUE("MENU_PROD_NAME_" + item, JSVC_STR.VAR_UCASE(JSON_OBJECT.CAT_PRODBRANCH_CAT_PRODUCT_ID_DESCRIPTION[i]));
						JSOC_ELEMENT.VAR_VALUE("MENU_PROD_PRICE_" + item, JSVC_NUM.VAR_CFORMAT(JSON_OBJECT.CAT_PRODBRANCH_AMOUNT_PRICE[i]));
	
						// Update Hidden fields in order list (Cart)
						JSOC_ELEMENT.VAR_VALUE("PROD_PRICE_" + item, JSON_OBJECT.CAT_PRODBRANCH_AMOUNT_PRICE[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_VAT_" + item, JSON_OBJECT.CAT_PRODUCT_AMOUNT_VAT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_POINT_" + item, JSON_OBJECT.CAT_PRODUCT_AMOUNT_POINT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_DISCOUNT_ALLOW_" + item, JSON_OBJECT.CAT_PRODUCT_ALLOW_DISCOUNT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_DISCOUNT_ALLOW_SENIOR_" + item, JSON_OBJECT.CAT_PRODUCT_ALLOW_SENIOR[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_PER_PIECE_QTY_" + item, JSON_OBJECT.CAT_PRODUCT_PER_PIECE_QTY[i]);
						
						JSOC_ELEMENT.VAR_VALUE("PROD_TAXMODEL_ID_" + item, JSON_OBJECT.CAT_PRODUCT_CAT_TAXMODEL_ID[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_TAXMODEL_PERCENTAGE_" + item, JSON_OBJECT.CAT_TAXMODEL_PERCENTAGE[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_TAXMODEL_AMOUNT_" + item, JSON_OBJECT.CAT_TAXMODEL_AMOUNT[i]);
						
						JSOC_ELEMENT.VAR_VALUE("PROD_UOM_ID_" + item, JSON_OBJECT.CAT_PRODUCT_CAT_UOM_ID[i]);
						
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_ID_" + item, JSON_OBJECT.CAT_DISCOUNT_ID[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_" + item + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_PERCENTAGE[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_AMOUNT_" + item  + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_AMOUNT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_VAT_" + item  + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_VAT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_POINT_" + item  + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_HAS_POINT[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_TAXTYPE_" + item  + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_CHANGE_TAX_TYPE[i]);
						JSOC_ELEMENT.VAR_VALUE("PROD_ORDER_DISCOUNT_TAXMODEL_" + item  + '_VALUE', JSON_OBJECT.CAT_DISCOUNT_CAT_TAXMODEL_ID[i]);
					});*/


					var JSON_COUNT = JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT");
					var COUNTER = 1;

					while(COUNTER <= JSON_COUNT)
					{
						var RINDEX = 1; 

						while(PRODUCT_BRANCH_LV.ROW.COUNT() >= RINDEX)
						{
							if
							(
								(JSVC_NUM.VAR_NVALL(PRODUCT_BRANCH_LV.CELL.VALUE("CAT_PRODUCT_ID", R_INDEX-1)) ==
								JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", COUNTER))) &&
								(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", COUNTER)) !== 1)
							)
							{
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_PRODTYPE_ID_LV1', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_PRODTYPE_ID_LV1", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_VAT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_AMOUNT_VAT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_POINT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_AMOUNT_POINT", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_DISCOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ALLOW_DISCOUNT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_SENIOR', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ALLOW_SENIOR", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_PER_PIECE_QTY', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_PER_PIECE_QTY", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_TAXMODEL_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_TAXMODEL_ID", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_AMOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_TAXMODEL_AMOUNT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_PERCENTAGE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_TAXMODEL_PERCENTAGE", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_PERCENTAGE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_PERCENTAGE", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_AMOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_AMOUNT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_VAT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_VAT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_HAS_POINT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_HAS_POINT", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_CHANGE_TAX_TYPE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_CHANGE_TAX_TYPE", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_CAT_TAXMODEL_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_CAT_TAXMODEL_ID", COUNTER));

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_UOM_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_UOM_ID", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_UOM_CODE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_UOM_CODE", COUNTER));
								
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_IMAGE', RINDEX, '<img src="' + JSRC_UPLOAD.REF_SUGGEST_PATH(JSCUS_CACHE.SERVER_URL + '/upload/pub/Category/product/', JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID", COUNTER)) + '.jpg" class="card-img-top">');

								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODBRANCH_CAT_PRODUCT_ID_DESCRIPTION', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID_DESCRIPTION", COUNTER));
								PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODBRANCH_AMOUNT_PRICE', RINDEX, JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_AMOUNT_PRICE", COUNTER)));
							}

							R_INDEX++;
						}
					}

					
					AUTO_UPDATE_PRODUCT_DATA();
					return;
				}
			});
		},
		1000 * 60 * 5
	);	
};
