/**
 * =================================================================================
 * Request EoD. (Only once per day. Returns the result of the first request, when called more than once.)
 * =================================================================================
 */
function EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST = []) 
{
	// Initialize
	// _______________________________________________________________________	
	var GET_MAP_ARRAY = JSCUS_SQL._RECORD._EOD_CSREP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_REC_EODREP_REC_POSDEVICE_ID", JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_ORDER_FIELD", "REC_EODREP_TIME_CLOSED");
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_ORDER_TYPE", "DESC"); 
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_LIMIT", "1"); 
	
	// Check Result
	// _______________________________________________________________________	
	JSCUS_SQL._RECORD._EOD_CSREP.CUS_GET
	(
		GET_MAP_ARRAY,
		function(IS_SUCCESS, RESPONSE_STRING) 
		{
			// Connection issue
			// ...........................................
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				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); 			
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
					
				return;
			}

			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				CALLBACK_METHOD(Z_READING_LIST);
				return;
			}		
					
			// Check last date
			// ...........................................
			var LAST_PROCESSED_DATE = JSRC_JSON.REF_READ(JSON_OBJECT, "REC_EODREP_TIME_CLOSED", 1);
			LAST_PROCESSED_DATE = JSVC_DATE.VAR_ADD('DATE', 1, JSVC_DATE.VAR_TO_DATE(LAST_PROCESSED_DATE));
			LAST_PROCESSED_DATE = JSVC_DATE.VAR_TO_DATE(JSVC_DATE.VAR_FORMAT(LAST_PROCESSED_DATE, 'YYYY-MM-DD') + ' 23:59:59.999999');
					
			// Loop until meet today
			// ...........................................
			if(JSVC_DATE.VAR_DATE_DIFF('DATE', LAST_PROCESSED_DATE, JSVC_DATE.VAR_NOW()) > 0)
			{
				EOD_Z_CHECKER_CSEOD_GENERATE(LAST_PROCESSED_DATE, CALLBACK_METHOD, Z_READING_LIST);
			}
			else
			{
				CALLBACK_METHOD(Z_READING_LIST);
			}
		},
		false
	);
}


/**
 * =================================================================================
 * Generates Z-Reading from existing EoD. (Only once per day. Returns the result of the first request, when called more than once.)
 * =================================================================================
 */
function EOD_Z_CHECKER_CSEOD_GENERATE(TODAY, CALLBACK_METHOD, Z_READING_LIST) 
{
	// Request server to do Z-Reading
	// _______________________________________________________________________		
	var SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._EOD_CSREP.CUS_PREPARE_MAPPED_ARRAY_FOR_COMPUTE();
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_EODREP_REC_POSDEVICE_ID", JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID);
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_EODREP_TIME_CLOSED",  JSVC_DATE.VAR_FORMAT(TODAY, "YYYY-MM-DD HH:mm:ss") + ".999999");

	// Request Z-reading Generate
	// _______________________________________________________________________	
	JSCUS_SQL._RECORD._EOD_CSREP.CUS_COMPUTE
	(
		SAVE_MAP_ARRAY, 
		function(IS_SUCCESS, RESPONSE_STRING) 
		{
			// Dismiss Progress
			// ...........................................
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			
			// Connection issue
			// ...........................................		
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				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); 
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) 
			{
				alert("REQUEST ERROR: \n" + "Unable to process request.");
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				return;
			}
			
			// Actual Data
			// ...........................................		
			else 
			{
				EOD_Z_CHECKER_GENERATE(TODAY, CALLBACK_METHOD, Z_READING_LIST);
				return;
			}
		}
	);
}


/**
 * =================================================================================
 * Generates Z-Reading from existing EoD. (Only once per day. Returns the result of the first request, when called more than once.)
 * =================================================================================
 */
function EOD_Z_CHECKER_GENERATE(TODAY, CALLBACK_METHOD, Z_READING_LIST) 
{
	// Request server to do Z-Reading
	// _______________________________________________________________________		
	var SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._EOD_ZREADING.CUS_PREPARE_MAPPED_ARRAY_FOR_COMPUTE();
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_EODREP_REC_POSDEVICE_ID", JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ID);
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_EODREP_TIME_CLOSED",  JSVC_DATE.VAR_FORMAT(TODAY, "YYYY-MM-DD HH:mm:ss") + ".999999");

	// Request Z-reading Generate
	// _______________________________________________________________________	
	JSCUS_SQL._RECORD._EOD_ZREADING.CUS_COMPUTE
	(
		SAVE_MAP_ARRAY, 
		function(IS_SUCCESS, RESPONSE_STRING) 
		{
			// Dismiss Progress
			// ...........................................
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			
			// Connection issue
			// ...........................................		
			if(IS_SUCCESS === false) 
			{
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				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); 
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				return;
			}
	
			// No result
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) 
			{
				alert("REQUEST ERROR: \n" + "Unable to process request.");
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
					},
					1000 * 5
				);	
				
				return;
			}
			
			// Actual Data
			// ...........................................		
			else 
			{
				Z_READING_LIST.push(JSON_OBJECT);
				EOD_Z_CHECKER(CALLBACK_METHOD, Z_READING_LIST);
				return;
			}
		}
	);
}
