// NOTE: [This JS must loaded as last item]


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function NEW_SCHEDULE_VALIDATE(SCHEDULE_GROUP_ID, SCHEDULE_TIME)
	{
		// Check if pre-orderts in this month is already exists
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._PSCHEDULE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PSCHEDULE_ACC_GROUP_ID', SCHEDULE_GROUP_ID);	
		JSVC_MAP_ARRAY.VAR_SET
		(
			GET_MAP_ARRAY, 'PARAM_REC_PSCHEDULE_DATE_FROM', 
			JSVC_DATE.VAR_BEGIN_OF_MONTH(SCHEDULE_TIME)
		);
		JSVC_MAP_ARRAY.VAR_SET
		(
			GET_MAP_ARRAY, 'PARAM_REC_PSCHEDULE_DATE_TO', 
			JSVC_DATE.VAR_END_OF_MONTH(SCHEDULE_TIME)
		);
		
		// Create New Pre-order
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._PSCHEDULE.CUS_GET
		(
			GET_MAP_ARRAY,
			function(IS_SUCCESS, RESPONSE_STRING)
			{
				// Validate
				// ...........................................
				if(IS_SUCCESS === false)
				{
					CUS_SEND_DIALOG_TRY_DISMISS();
					console.error("CONNECTION ERROR: \n" + RESPONSE_STRING);
					return;
				}
				
				// Validate JSON Response
				// ...........................................
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
				if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
				{
					CUS_SEND_DIALOG_TRY_DISMISS();
					console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
					return;
				}
				
				// If Already exists
				// ...........................................
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") > 0)
				{
					CUS_SEND_DIALOG_TRY_DISMISS();
					JSVC_ERROR.VAR_ARGUMENT_EXCEPTION_ALREADY_EXISTS
					(
						"PRE-ORDER for month " + 
						JSVC_DATE.VAR_FORMAT(SCHEDULE_TIME, "MMMM YYYY"), 
						"", true
					);
					return;								
				}
				
				// Create Blank Pre-Order
				// ...........................................
				NEW_SCHEDULE_PREPARE_DGV_COLS(SCHEDULE_GROUP_ID, SCHEDULE_TIME);
				NEW_SCHEDULE_PREPARE_DGV_ROWS(SCHEDULE_GROUP_ID, SCHEDULE_TIME);
			},
			true
		); 		
	};
	
	
	// ===============================================================
	// Initialize Form
	// ===============================================================
	function NEW_SCHEDULE_PREPARE_DGV_COLS_CLEAR()
	{
		// Initialize
		// _______________________________________________________________________
		var DATE_INDEX = 1;
		
		// Prepare Columns
		// _______________________________________________________________________
		while(31 >= DATE_INDEX)
		{
			// Update WeeK
			// ...........................................
			JSOC_ELEMENT.VAR_INNER_HTML("PSCHEDULE_DGV_HEADER_D" + DATE_INDEX, '-');
			
			// Update Date
			// ...........................................
			JSOC_ELEMENT.VAR_INNER_HTML
			(
				PSCHEDULE_DGV.COLUMN.HEADER_ELEMENT
				("REC_PSCHEDULE_QTY_D" + JSVC_STR.VAR_FIX(DATE_INDEX.toString(), null, 2, 2, "0")), 
				'-'
			);
						
			// To Next Loop
			// ...........................................
			DATE_INDEX ++;
		}
	};
	
	
	// ===============================================================
	// Initialize Form
	// ===============================================================
	function NEW_SCHEDULE_PREPARE_DGV_COLS(SCHEDULE_GROUP_ID, SCHEDULE_TIME)
	{
		// Initialize
		// _______________________________________________________________________
		var CURRENT_MONTH = JSVC_DATE.VAR_FORMAT(SCHEDULE_TIME, "MM");
		var CURRENT_DATE = JSVC_DATE.VAR_BEGIN_OF_MONTH(SCHEDULE_TIME);
		
		// Prepare Columns
		// _______________________________________________________________________
		while(CURRENT_MONTH == JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MM"))
		{
			// Update WeeK
			// ...........................................
			JSOC_ELEMENT.VAR_INNER_HTML("PSCHEDULE_DGV_HEADER_D" + JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "D"), JSVC_STR.VAR_UCASE(JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "WWW")));
			
			// Update Date
			// ...........................................
			JSOC_ELEMENT.VAR_INNER_HTML(PSCHEDULE_DGV.COLUMN.HEADER_ELEMENT("REC_PSCHEDULE_QTY_D" + JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "DD")), JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "DD-MMM"));
						
			// To Next Loop
			// ...........................................
			 CURRENT_DATE = JSVC_DATE.VAR_ADD('DD', 1,  CURRENT_DATE);
		}
	};
	
	
	// ===============================================================
	// Initialize Form
	// ===============================================================
	function NEW_SCHEDULE_PREPARE_DGV_ROWS(SCHEDULE_GROUP_ID, SCHEDULE_TIME, CALLBACK_METHOD = null)
	{
		// Clear DGV
		// _______________________________________________________________________
		PSCHEDULE_DGV.ROW.CLEAR();
		
		// Populate Products (Mix) Callback
		// _______________________________________________________________________
		var MIXTURE_CALLBACK = function(IS_SUCCESS, RESPONSE_STRING)
		{
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			if(IS_SUCCESS === false)
			{
				console.error("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
			
			// Validate JSON Response
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
				return;
			}
			
			// Nothing to populate
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				JSOC_DIALOG.VAR_MESSAGE_BOX
				(
					'There is no products in the system.', 
					'Information',
					'OK', 'INFO',
					function(MESSAGE_RESPONSE)
					{
						return;
					}
				);	
				return;								
			}
							
			// Populate Rows
			// ...........................................
			var ROW_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
			var ROW_INDEX = 0;
			var JSON_INDEX = 1;
			
			while(ROW_COUNT >= JSON_INDEX)
			{
				PSCHEDULE_DGV.ROW.ADD();
				ROW_INDEX = PSCHEDULE_DGV.ROW.COUNT() - 1;

				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_ID", ROW_INDEX, "-1");
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_ACC_GROUP_ID", ROW_INDEX, SCHEDULE_GROUP_ID);
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_CAT_PRODUCT_ID", ROW_INDEX, JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "CAT_PRODUCT_ID", JSON_INDEX));
				PSCHEDULE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX));
				if(JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_QTY", JSON_INDEX) != 0)
				{
					PSCHEDULE_DGV.CELL.VALUE
					(
						"REC_PSCHEDULE_AMOUNT_COST", ROW_INDEX, 
						JSVC_NUM.VAR_ROUND
						(
							JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_COST", JSON_INDEX) / 
							JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_QTY", JSON_INDEX),
							2
						)
					);
				}
				else
				{
					PSCHEDULE_DGV.CELL.VALUE
					(
						"REC_PSCHEDULE_AMOUNT_COST", ROW_INDEX, 
						JSVC_NUM.VAR_ROUND(JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_COST", JSON_INDEX), 2)
					);					
				}
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_AMOUNT_PRICE", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_ING_PRICE", JSON_INDEX));
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_DATE", ROW_INDEX, JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_BEGIN_OF_MONTH(SCHEDULE_TIME), "YYYY-MM-DD HH:mm:ss") + ".000000");
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_STATUS", ROW_INDEX, 2);
				
				JSON_INDEX ++;
			}
			PSCHEDULE_DGV.ROW.REFRESH_ROW_COUNT();		
			
			// Reset Modified Status
			// ...........................................
			EDIT_HANDLER.REF_IS_MODIFIED = false;
			
			// Execute Callback
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(CALLBACK_METHOD))
			{
				CALLBACK_METHOD();
			}
			return;
		};
				
		// Populate Products (Mix)
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_HAS_RECIPE', 24);		
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_STATUS', 2);			
		JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET
		(
			GET_MAP_ARRAY,
			MIXTURE_CALLBACK,
			true
		); 		
	};	
	
	
	// ===============================================================
	// Initialize Form
	// ===============================================================
	function REFRESH_SCHEDULE_PREPARE_DGV_ROWS(SCHEDULE_GROUP_ID, SCHEDULE_TIME, CALLBACK_METHOD = null)
	{		
		// Abort if DGV is not ready
		// _______________________________________________________________________
		if(PSCHEDULE_DGV.ROW.COUNT() <= 0)
		{
			alert('No source data found. You can only run this function to existing data.');
			return;
		}
			
		
		// Populate Products (Mix)
		// _______________________________________________________________________
		var MIXTURE_CALLBACK = function(IS_SUCCESS, RESPONSE_STRING)
		{
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			if(IS_SUCCESS === false)
			{
				console.error("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}
			
			// Validate JSON Response
			// ...........................................
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
				return;
			}
			
			// Nothing to populate
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				JSOC_DIALOG.VAR_MESSAGE_BOX
				(
					'There is no products in the system.', 
					'Information',
					'OK', 'INFO',
					function(MESSAGE_RESPONSE)
					{
						return;
					}
				);	
				return;								
			}
							
			// Populate Rows
			// ...........................................
			var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
			var JSON_INDEX = 0;
			var NEW_INDEX = 0;
			
			while(JSON_COUNT > JSON_INDEX)
			{
				// Skip if product is already exists in the DGV
				if(PSCHEDULE_DGV.COLUMN.FIND("REC_PSCHEDULE_CAT_PRODUCT_ID", JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "CAT_PRODUCT_ID", JSON_INDEX + 1)) >= 0)
				{
					JSON_INDEX ++;
					continue;
				}
				
				// Add new product(s)
				PSCHEDULE_DGV.ROW.ADD();
				NEW_INDEX = PSCHEDULE_DGV.ROW.COUNT() - 1;
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_ID", NEW_INDEX, "-1");
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_ACC_GROUP_ID", NEW_INDEX, SCHEDULE_GROUP_ID);
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_CAT_PRODUCT_ID", NEW_INDEX, JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "CAT_PRODUCT_ID", JSON_INDEX + 1));
				PSCHEDULE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", NEW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX + 1));
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_AMOUNT_COST", NEW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_COST", JSON_INDEX + 1));
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_AMOUNT_PRICE", NEW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_ING_PRICE", JSON_INDEX + 1));
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_DATE", NEW_INDEX, JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_BEGIN_OF_MONTH(SCHEDULE_TIME), "YYYY-MM-DD HH:mm:ss") + ".000000");
				PSCHEDULE_DGV.CELL.VALUE("REC_PSCHEDULE_STATUS", NEW_INDEX, 2);
				
				// TO next loop
				JSON_INDEX ++;
			}
			
			// Execute Callback
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(CALLBACK_METHOD))
			{
				CALLBACK_METHOD();
			}
		};
			
		// Send Request	
		GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_HAS_RECIPE', 24);		
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_STATUS', 2);	
		JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET
		(
			GET_MAP_ARRAY,
			MIXTURE_CALLBACK,
			true
		); 		
	};	
	