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

	// ===============================================================
	// Initialize Form [DGV]
	// ===============================================================
	function FORM_LOAD_STOCK_MOVEMENT()
	{
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._STOCKFLOW.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STOCKFLOW_IS_MONITORED_FROM', 1);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STOCKFLOW_IS_MONITORED_TO', 5);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STOCKFLOW_STATUS', 2);		
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_STOCKFLOW_IS_MONITORED');	
		
		
		// Callback Method
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Disable Dialogs
			// ...........................................
			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;
			}
			
			// JSON Empty (Clears the DataGrodView)
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				return;
			}

			// Apply result to DGV
			// ...........................................
			var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
			var JSON_INDEX = 1;
			var CURRENT_POSTFIX = '';
			
			// Update Header
			// ...........................................
			while(JSON_COUNT >= JSON_INDEX && JSON_INDEX <= 5)
			{
				CURRENT_POSTFIX = JSVC_STR.VAR_FIX(JSON_INDEX.toString(), null, 2, 2, "0");
				EOD_PRODUCT_DGV.COLUMN.HEADER_CAPTION
				(
					"REC_EODPROD_MOVE_QTY_" + CURRENT_POSTFIX, 
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_STOCKFLOW_DESCRIPTION", JSON_INDEX)
				);
				JSON_INDEX ++;
			}
			
			// Remove Unassigned columns
			// ...........................................
			while(JSON_INDEX <= 5)
			{
				CURRENT_POSTFIX = JSVC_STR.VAR_FIX(JSON_INDEX.toString(), null, 2, 2, "0");
				EOD_PRODUCT_DGV.COLUMN.DISPLAY("REC_EODPROD_MOVE_QTY_" + CURRENT_POSTFIX, "NONE");
				JSON_INDEX ++;
			}
		};
			
		// REGULAR Search Mode
		// _______________________________________________________________________
		JSCUS_SQL._CATEGORY._STOCKFLOW.CUS_GET
		(
			GET_MAP_ARRAY,
			CALLBACK_METHOD,
			true
		); 		
		
	};


	// ===============================================================
	// Controls show / hide of item children
	// ===============================================================
	function FORM_ITEM_SHOWHIDE(REQ_ROW_INDEX)
	{
		// Initialize
		// _______________________________________________________________________
		var CURRENT_PRODUCT_ID = EOD_PRODUCT_DGV.CELL.VALUE_NVALL("REC_EODPROD_CAT_PRODUCT_ID", REQ_ROW_INDEX);
		var IS_SHOW = false;
		var ROW_COUNT = EOD_PRODUCT_DGV.ROW.COUNT();
		var ROW_INDEX = REQ_ROW_INDEX + 1;
		
		
		// Abort EoF, not a parent row, or no child
		// _______________________________________________________________________
		if
		(
			ROW_COUNT <= REQ_ROW_INDEX ||
			JSVC_STR.VAR_IS_BLANK(EOD_PRODUCT_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", REQ_ROW_INDEX)) ||
			CURRENT_PRODUCT_ID != EOD_PRODUCT_DGV.CELL.VALUE_NVALL("REC_EODPROD_CAT_PRODUCT_ID", REQ_ROW_INDEX + 1)
		)
		{
			return;
		}
		
		
		// Get Show / Hide
		// _______________________________________________________________________
		if(EOD_PRODUCT_DGV.ROW.DISPLAY(ROW_INDEX + 1) == 'none')
		{
			IS_SHOW = true;
		}
		
		
		// Apply Show / Hide to Children
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX)
		{
			// Complete Applying Show / Hide
			// ...........................................
			if(CURRENT_PRODUCT_ID != EOD_PRODUCT_DGV.CELL.VALUE_NVALL("REC_EODPROD_CAT_PRODUCT_ID", ROW_INDEX))
			{
				return;
			}
			
			// Apply Show / Hide to Row
			// ...........................................
			if(IS_SHOW == true)
			{
				EOD_PRODUCT_DGV.ROW.DISPLAY(ROW_INDEX, 'DEFAULT');
			}
			else
			{
				EOD_PRODUCT_DGV.ROW.DISPLAY(ROW_INDEX, 'NONE');
			}
			
			// To next loop
			// ...........................................
			ROW_INDEX ++;
		}
	};
	


	function FORM_GROUP_RESULT()
	{
		// Initialize
		// _______________________________________________________________________
		var ROW_COUNT = EOD_PRODUCT_DGV.ROW.COUNT();
		var ROW_INDEX = 0;
		var PRODUCT_COUNT = 1;
		
		var CURRENT_PRODUCT_ID = 0;
		var CURRENT_PRODUCT_NAME = "";
		var CURRENT_DATE = "";
		var CURRENT_BAL_BEGIN = 0;
		var CURRENT_BAL_END = 0;
		var CURRENT_USAGE = [];
		
		var MONITORED_INDEX = -1;
		var MONITORED_PRODUCT_ID = "";
		var MONITORED_PRODUCT_NAME = "";
		var MONITORED_DATE_MIN = null;
		var MONITORED_DATE_MAX = null;
		var MONITORED_BAL_BEGIN = 0;
		var MONITORED_BAL_END = 0;
		var MONITORED_BAL_VARIANCE = 0;
		var MONITORED_STOCK_IN = 0;
		var MONITORED_STOCK_OUT = 0;
		var MONITORED_USAGE = [];
		
		
		// Collect and merge usages from the text
		// _______________________________________________________________________
		var MERGE_USAGE = function(OLD_USAGE, NEW_USAGE)
		{
			if(JSVC_STR.VAR_IS_BLANK(NEW_USAGE))
			{
				return OLD_USAGE;
			}
			
			
			var USAGE_LIST = NEW_USAGE.split(",");
			var CURRENT_ITEM = "";
			var USAGE_INDEX = 0;
			

			while(USAGE_LIST.length > USAGE_INDEX)
			{
				CURRENT_ITEM = JSVC_STR.VAR_FIX(USAGE_LIST[USAGE_INDEX]);
				
				if
				(
					JSVC_STR.VAR_IS_BLANK(CURRENT_ITEM) == false &&
					JSVC_ARR.VAR_CONTAINS(OLD_USAGE, CURRENT_ITEM) == false
				)
				{
					OLD_USAGE.push(CURRENT_ITEM);	
				}
				USAGE_INDEX ++;
			}
			
			return OLD_USAGE;
		};
		
		
		var COMMIT_PARENT_LINE = function()
		{
				var USAGE_TO_TEXT = "";
				var USAGE_INDEX = 0;
			
				while(MONITORED_USAGE.length > USAGE_INDEX)
				{
					if(USAGE_INDEX == 0)
					{
						USAGE_TO_TEXT = MONITORED_USAGE[USAGE_INDEX];
					}
					else
					{
						USAGE_TO_TEXT = USAGE_TO_TEXT + ', ' + MONITORED_USAGE[USAGE_INDEX];
					}
					USAGE_INDEX ++;
				}
			
				EOD_PRODUCT_DGV.ROW.ADD(MONITORED_INDEX);
				EOD_PRODUCT_DGV.CELL.VALUE("ROW_COUNT", MONITORED_INDEX, PRODUCT_COUNT);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_CAT_PRODUCT_ID", MONITORED_INDEX, MONITORED_PRODUCT_ID);
				EOD_PRODUCT_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", MONITORED_INDEX, MONITORED_PRODUCT_NAME);
				EOD_PRODUCT_DGV.CELL.VALUE
				(
					"REC_EODPROD_DATE", MONITORED_INDEX, 
					JSVC_DATE.VAR_FORMAT(MONITORED_DATE_MIN, 'MMM DD, YYYY') + ' ~ ' +
					JSVC_DATE.VAR_FORMAT(MONITORED_DATE_MAX, 'MMM DD, YYYY')
				);
				
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_STOCK_IN", MONITORED_INDEX, MONITORED_STOCK_IN);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_STOCK_OUT", MONITORED_INDEX, MONITORED_STOCK_OUT);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_QTY_BEGIN", MONITORED_INDEX, MONITORED_BAL_BEGIN);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_QTY_END", MONITORED_INDEX, MONITORED_BAL_END);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_STOCK_ADJUSTMENT", MONITORED_INDEX, MONITORED_BAL_VARIANCE);
				EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_USAGE", MONITORED_INDEX, USAGE_TO_TEXT);
				
				FORM_ITEM_SHOWHIDE(MONITORED_INDEX);
				EOD_PRODUCT_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(MONITORED_INDEX);
				MONITORED_INDEX -1;
				
				PRODUCT_COUNT ++;
				ROW_COUNT ++;
				ROW_INDEX ++;			
		};
		
				
		// Loop Results
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX)
		{
			// Initialize Loop
			// ...........................................
			CURRENT_PRODUCT_ID = EOD_PRODUCT_DGV.CELL.VALUE_NVALL("REC_EODPROD_CAT_PRODUCT_ID", ROW_INDEX);
			CURRENT_PRODUCT_NAME = EOD_PRODUCT_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX);
			CURRENT_DATE = JSVC_DATE.VAR_TO_DATE(EOD_PRODUCT_DGV.CELL.ELEMENT("REC_EODPROD_DATE", ROW_INDEX).dataset.rawvalue);
			CURRENT_BAL_BEGIN = EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_QTY_BEGIN", ROW_INDEX);
			CURRENT_BAL_END = EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_QTY_END", ROW_INDEX);
			
			
			// Group Item data
			// ...........................................
			if(MONITORED_PRODUCT_ID == CURRENT_PRODUCT_ID)
			{
				EOD_PRODUCT_DGV.CELL.VALUE("ROW_COUNT", ROW_INDEX - 1, "");
				EOD_PRODUCT_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX - 1, "");
				EOD_PRODUCT_DGV.CELL.VALUE("ROW_COUNT", ROW_INDEX, "");
				EOD_PRODUCT_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, "");
								
				if(MONITORED_DATE_MAX < CURRENT_DATE)
				{
					MONITORED_DATE_MAX = CURRENT_DATE;
					MONITORED_BAL_END = CURRENT_BAL_END;
				}
				else if(MONITORED_DATE_MIN > CURRENT_DATE)
				{
					MONITORED_DATE_MIN = CURRENT_DATE;
					MONITORED_BAL_BEGIN = CURRENT_BAL_BEGIN;
				}
				
				MONITORED_BAL_VARIANCE += EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_ADJUSTMENT", ROW_INDEX);
				MONITORED_STOCK_IN += EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_IN", ROW_INDEX);
				MONITORED_STOCK_OUT += EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_OUT", ROW_INDEX);
			}
			
			
			// Write Group header into the DGV
			// ...........................................
			else if(ROW_INDEX - 1 > MONITORED_INDEX && MONITORED_PRODUCT_ID != CURRENT_PRODUCT_ID)
			{
				COMMIT_PARENT_LINE();
			}

			
			// New Line
			// ...........................................
			if(MONITORED_PRODUCT_ID != CURRENT_PRODUCT_ID)
			{
				MONITORED_PRODUCT_ID = CURRENT_PRODUCT_ID;
				MONITORED_PRODUCT_NAME = CURRENT_PRODUCT_NAME;
				MONITORED_DATE_MIN = CURRENT_DATE;
				MONITORED_DATE_MAX = CURRENT_DATE;
				MONITORED_USAGE = MERGE_USAGE([], EOD_PRODUCT_DGV.CELL.VALUE("REC_EODPROD_USAGE", ROW_INDEX));
				
				MONITORED_BAL_VARIANCE = EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_ADJUSTMENT", ROW_INDEX);
				MONITORED_STOCK_IN = EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_IN", ROW_INDEX);
				MONITORED_STOCK_OUT = EOD_PRODUCT_DGV.CELL.VALUE_NVAL("REC_EODPROD_STOCK_OUT", ROW_INDEX);
				
				MONITORED_INDEX = ROW_INDEX;				
			}
			
			
			// ...........................................
			ROW_INDEX ++;
		}
		
		
		
		if(ROW_INDEX - 1 > MONITORED_INDEX)
		{
			COMMIT_PARENT_LINE();
		}
		
		
		
		// For Applying CFormat
		// _______________________________________________________________________	
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_QTY_BEGIN");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_QTY_END");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_IN");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_OUT");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_ADJUSTMENT");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_01");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_02");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_03");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_04");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_05");
	};