// 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(IS_SUCCESS, RESPONSE_STRING)
	{
		// Validate result
		// _______________________________________________________________________
		var JSON_RES = JSCUS_PG._TOOL.VERIFY_GET.VERIFY(IS_SUCCESS, RESPONSE_STRING);
		
		// Nothing to process
		// _______________________________________________________________________
		if(JSON_RES[1] == false || JSON_RES[2] == true)
		{
			return;
		}
		
		// Populate Items
		// _______________________________________________________________________
		var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_RES[0], "JSON_COUNT");
		var JSON_INDEX = 1;
		var ROW_COUNT = EOD_PRODUCT_DGV.ROW.COUNT();
		var ROW_INDEX = 0;
		var DD_POSTFIX = '';
		
		while(JSON_COUNT >= JSON_INDEX)
		{
			ROW_INDEX = EOD_PRODUCT_DGV.COLUMN.FIND
			(
				'REC_EODPROD_ID', 
				JSRC_JSON.REF_READ(JSON_RES[0], "REC_EODPROD_ID", JSON_INDEX),
				false
			);
			
			if(ROW_INDEX > 0)
			{
				// Fill monitored stockflow
				// ...........................................
				var MONITORED_ID = JSRC_JSON.REF_READ(JSON_RES[0], "CAT_STOCKFLOW_IS_MONITORED", JSON_INDEX);
				EOD_PRODUCT_DGV.CELL.VALUE
				(
					'REC_EODPROD_MOVE_QTY_0' + MONITORED_ID, ROW_INDEX,
					JSRC_JSON.REF_READ(JSON_RES[0], "REC_STOCKCARD_QUANTITY", JSON_INDEX)
				);
				
				// Fill planned quantity
				// ...........................................
				DD_POSTFIX = JSRC_JSON.REF_READ_TO_DATE(JSON_RES[0], "REC_EODPROD_DATE_FROM", JSON_INDEX);
				DD_POSTFIX = JSVC_DATE.VAR_FORMAT(DD_POSTFIX, 'DD');
				if(JSVC_NUM.VAR_NVALL(DD_POSTFIX) >= 1)
				{
					EOD_PRODUCT_DGV.CELL.VALUE
					(
						'REC_EODPROD_MOVE_QTY_PLN', ROW_INDEX,
						JSRC_JSON.REF_READ(JSON_RES[0], "REC_PSACTUAL_QTY_D" + DD_POSTFIX, JSON_INDEX)
					);
				}			
			}
		
			JSON_INDEX ++;
		}
		
		return;
	};