	var IS_IDEAL_ON_HAND = false;


	/**
	 * =================================================================================
	 *
	 * Updates the captions on DGV header
	 *
	 * =================================================================================
	 */
	function PREPARE_MONTH()
	{
		// Initialize
		// _______________________________________________________________________
		var COLUMN_INDEX = 1;
		var COLUMN_MAX = 31;
		var CURRENT_POSTFIX = "";
		var CURRENT_DATE = null;
		
		var QUERY_TYPE = JSOC_CMB.VAR_SELECTED_VALUE_NVAL(SEARCH_FORECAST_MODE_CMB);
		var SELECTED_MONTH = null
		var CURRENT_MONTH = 0;
		
		// Get Date
		// _______________________________________________________________________
		if(QUERY_TYPE == 1)
		{
			SELECTED_MONTH = JSVC_DATE.VAR_NOW();	
			SELECTED_MONTH = JSOC_ELEMENT.VAR_VALUE_TO_DATE("SEARCH_INVANTORY_DATE_DTP");
		}
		else
		{
			SELECTED_MONTH = JSVC_DATE.VAR_BEGIN_OF_MONTH(JSOC_ELEMENT.VAR_VALUE("SEARCH_INVANTORY_DATE_DTP"));
			CURRENT_MONTH = JSVC_NUM.VAR_NVAL(JSVC_DATE.VAR_FORMAT(SELECTED_MONTH, "MM"));
		}
		
		// Populate Calendar
		// _______________________________________________________________________
		while(COLUMN_MAX >= COLUMN_INDEX)
		{
			CURRENT_POSTFIX = "D" + JSVC_STR.VAR_FIX(COLUMN_INDEX.toString(), null, 2, 2, "0");
			
			if(COLUMN_INDEX <= 1)
			{
				CURRENT_DATE = SELECTED_MONTH;
			}
			else
			{
				CURRENT_DATE = JSVC_DATE.VAR_ADD('DAY', COLUMN_INDEX - 1, SELECTED_MONTH);
			}
			
			if(QUERY_TYPE == 2 && CURRENT_MONTH != JSVC_NUM.VAR_NVAL(JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MM")))
			{
				INVENTORY_DGV.COLUMN.HEADER_CAPTION(CURRENT_POSTFIX, "-");
				INVENTORY_PPACK_DGV.COLUMN.HEADER_CAPTION(CURRENT_POSTFIX, "-");
			}
			else
			{
				INVENTORY_DGV.COLUMN.HEADER_CAPTION(CURRENT_POSTFIX, JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MMM DD"));
				INVENTORY_PPACK_DGV.COLUMN.HEADER_CAPTION(CURRENT_POSTFIX, JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MMM DD"));
			}
			COLUMN_INDEX ++;
		}	
	};
	


	/**
	 * =================================================================================
	 *
	 * Manages the Collapse / Retract events of the Item
	 *
	 * =================================================================================
	 */	
	var LAST_COLLAPSE_ROW_INDEX = -1;
	function COLLAPSE_ITEMS(REQUEST_COLLAPSE_ROW_INDEX)
	{
		// Initialize
		// _______________________________________________________________________
		var CURRENT_PRODUCT_ID = -1;
		var ROW_INDEX = 1;	
		var TO_COLLAPSE = false;
		
		
		// Abort if last item
		// _______________________________________________________________________
		if(INVENTORY_DGV.ROW.COUNT() <= REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX)
		{
			return;
		}

		// Abort if No Child
		// _______________________________________________________________________		
		CURRENT_PRODUCT_ID = INVENTORY_DGV.CELL.VALUE_NVALL("REC_INVENTORY_CAT_PRODUCT_ID", REQUEST_COLLAPSE_ROW_INDEX);
		if(INVENTORY_DGV.CELL.VALUE_NVALL("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX) != CURRENT_PRODUCT_ID)
		{
			return;
		}
	
	
		// Identify To Collapse or Retract
		// _______________________________________________________________________
		if(INVENTORY_DGV.ROW.DISPLAY(REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX) == 'none')
		{
			TO_COLLAPSE = true;
			LAST_COLLAPSE_ROW_INDEX = REQUEST_COLLAPSE_ROW_INDEX;
		}
		
		
		// Collapse / Retract all the children
		// _______________________________________________________________________
		while
		(
			INVENTORY_DGV.ROW.COUNT() > REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX &&
			INVENTORY_DGV.CELL.VALUE_NVALL("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX) == CURRENT_PRODUCT_ID
		)
		{
			if(TO_COLLAPSE == true)
			{
				INVENTORY_DGV.ROW.DISPLAY(REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX, "DEFAULT");
				INVENTORY_PPACK_DGV.ROW.DISPLAY(REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX, "DEFAULT");
			}
			else
			{
				INVENTORY_DGV.ROW.DISPLAY(REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX, "NONE");
				INVENTORY_PPACK_DGV.ROW.DISPLAY(REQUEST_COLLAPSE_ROW_INDEX + ROW_INDEX, "NONE");
			}
			ROW_INDEX ++;
		}

		
	};
	
	
	
	/**
	 * =================================================================================
	 *
	 * Removes the Finished Product / Premix with All-Zero columns
	 *
	 * =================================================================================
	 */
	function REMOVE_BLANK_LINES(REQUEST_ROW_INDEX)
	{
		// Initialize
		// _______________________________________________________________________
		var CURRENT_PRODUCT_ID = -1;
		var ROW_INDEX = 1;	
		var ROWS_TO_REMOVE = [];
		
		
		// Abort if last item
		// _______________________________________________________________________
		if(INVENTORY_DGV.ROW.COUNT() <= REQUEST_ROW_INDEX + ROW_INDEX)
		{
			return;
		}

		// Abort if No Child
		// _______________________________________________________________________		
		CURRENT_PRODUCT_ID = INVENTORY_DGV.CELL.VALUE_NVALL("REC_INVENTORY_CAT_PRODUCT_ID", REQUEST_ROW_INDEX);
		if(INVENTORY_DGV.CELL.VALUE_NVALL("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", REQUEST_ROW_INDEX + ROW_INDEX) != CURRENT_PRODUCT_ID)
		{
			return;
		}
		
		
		// Check blanks all the children
		// _______________________________________________________________________
		while
		(
			INVENTORY_DGV.ROW.COUNT() > REQUEST_ROW_INDEX + ROW_INDEX &&
			INVENTORY_DGV.CELL.VALUE_NVALL("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", REQUEST_ROW_INDEX + ROW_INDEX) == CURRENT_PRODUCT_ID
		)
		{
			if
			(
				INVENTORY_DGV.CELL.VALUE_NVALL("D01", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D02", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D03", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D04", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D05", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D06", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D07", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D08", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D09", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D10", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D11", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D12", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D13", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D14", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D15", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D16", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D17", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D18", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D19", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D20", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D21", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D22", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D23", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D24", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D25", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D26", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D27", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D28", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D29", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				INVENTORY_DGV.CELL.VALUE_NVALL("D30", REQUEST_ROW_INDEX + ROW_INDEX) == 0 &&
				
				INVENTORY_DGV.CELL.VALUE_NVALL("D31", REQUEST_ROW_INDEX + ROW_INDEX) == 0
			)
			{
				INVENTORY_DGV.ROW.REMOVE_AT(REQUEST_ROW_INDEX + ROW_INDEX, true);
			}
			else
			{
				ROW_INDEX ++;
			}
		}
		
	};
	
	
	
	/**
	 * =================================================================================
	 *
	 * Copies parent DGV data to secondary DGV with converted UoM (Per Pack)
	 *
	 * =================================================================================
	 */
	function COPY_TO_PACKAGE_DGV()
	{
		// Initialize
		// _______________________________________________________________________	
		var INV_PARENT_ROW_INDEX = 0;
		var INV_ROW_INDEX = 0;
		var INV_ROW_COUNT = INVENTORY_DGV.ROW.COUNT();
		var PPACK_ROW_INDEX = 0;
		
		
		// Populate Data
		// _______________________________________________________________________	
		while(INV_ROW_COUNT > INV_ROW_INDEX)
		{
			if(INVENTORY_DGV.CELL.VALUE_NVAL("REC_INVENTORY_CAT_PRODUCT_ID", INV_ROW_INDEX) > 0)
			{
				REMOVE_BLANK_LINES(INV_PARENT_ROW_INDEX);
				INV_ROW_COUNT = INVENTORY_DGV.ROW.COUNT();
			}
			INV_ROW_INDEX ++;
		}
		
		
		// Clone Data
		// _______________________________________________________________________	
		var CLONE_DATA_TO_PPACK = function(COLUMN_NAME, APPLY_CONVERTION = false)
		{
			if(APPLY_CONVERTION == false)
			{
				INVENTORY_PPACK_DGV.CELL.VALUE
				(
					COLUMN_NAME, PPACK_ROW_INDEX,
					INVENTORY_DGV.CELL.VALUE(COLUMN_NAME, INV_ROW_INDEX)
				);
			}
			else
			{
				INVENTORY_PPACK_DGV.CELL.VALUE
				(
					COLUMN_NAME, PPACK_ROW_INDEX,
					JSVC_NUM.VAR_CFORMAT
					(
						INVENTORY_DGV.CELL.VALUE_NVAL(COLUMN_NAME, INV_ROW_INDEX) / 
						INVENTORY_DGV.CELL.VALUE_NVAL("CAT_PRODUCT_ING_NET_CONTENT", INV_PARENT_ROW_INDEX)
					)
				);			
			}
		};
		
		
		// Add line
		// _______________________________________________________________________	
		var CLONE_LINE = function()
		{
			INVENTORY_PPACK_DGV.ROW.ADD();
			PPACK_ROW_INDEX = INVENTORY_PPACK_DGV.ROW.COUNT() - 1;
			
			// ~~~~~
			
			CLONE_DATA_TO_PPACK("ROW_COUNT");
			CLONE_DATA_TO_PPACK("REC_INVENTORY_CAT_PRODUCT_ID");
			CLONE_DATA_TO_PPACK("CAT_PRODUCT_DESCRIPTION");
			CLONE_DATA_TO_PPACK("CAT_PRODUCT_ING_PRICE");
			CLONE_DATA_TO_PPACK("CAT_PRODUCT_ING_NET_CONTENT");
			CLONE_DATA_TO_PPACK("CAT_UOM_DESCRIPTION");
			
			CLONE_DATA_TO_PPACK("REC_INVENTORY_QUANTITY", true);
			CLONE_DATA_TO_PPACK("ECD");
			CLONE_DATA_TO_PPACK("ECDDIFF");
			CLONE_DATA_TO_PPACK("CAT_MIXTURE_BASE_CAT_PRODUCT_ID");	
			
			// ~~~~~
			
			CLONE_DATA_TO_PPACK("D01", true);
			CLONE_DATA_TO_PPACK("D02", true);
			CLONE_DATA_TO_PPACK("D03", true);
			CLONE_DATA_TO_PPACK("D04", true);
			CLONE_DATA_TO_PPACK("D05", true);
			
			CLONE_DATA_TO_PPACK("D06", true);
			CLONE_DATA_TO_PPACK("D07", true);
			CLONE_DATA_TO_PPACK("D08", true);
			CLONE_DATA_TO_PPACK("D09", true);
			CLONE_DATA_TO_PPACK("D10", true);
			
			CLONE_DATA_TO_PPACK("D11", true);
			CLONE_DATA_TO_PPACK("D12", true);
			CLONE_DATA_TO_PPACK("D13", true);
			CLONE_DATA_TO_PPACK("D14", true);
			CLONE_DATA_TO_PPACK("D15", true);
			
			CLONE_DATA_TO_PPACK("D16", true);
			CLONE_DATA_TO_PPACK("D17", true);
			CLONE_DATA_TO_PPACK("D18", true);
			CLONE_DATA_TO_PPACK("D19", true);
			CLONE_DATA_TO_PPACK("D20", true);
			
			CLONE_DATA_TO_PPACK("D21", true);
			CLONE_DATA_TO_PPACK("D22", true);
			CLONE_DATA_TO_PPACK("D23", true);
			CLONE_DATA_TO_PPACK("D24", true);
			CLONE_DATA_TO_PPACK("D25", true);
			
			CLONE_DATA_TO_PPACK("D26", true);
			CLONE_DATA_TO_PPACK("D27", true);
			CLONE_DATA_TO_PPACK("D28", true);
			CLONE_DATA_TO_PPACK("D29", true);
			CLONE_DATA_TO_PPACK("D30", true);
			
			CLONE_DATA_TO_PPACK("D31", true);
		};
		
		
		// Populate Data
		// _______________________________________________________________________	
		INVENTORY_PPACK_DGV.ROW.CLEAR();
		INV_ROW_INDEX = 0;
		while(INV_ROW_COUNT > INV_ROW_INDEX)
		{
			if(INVENTORY_DGV.CELL.VALUE_NVAL("REC_INVENTORY_CAT_PRODUCT_ID", INV_ROW_INDEX) > 0)
			{
				if(INV_PARENT_ROW_INDEX > 0)
				{
					COLLAPSE_ITEMS(INV_PARENT_ROW_INDEX);
				}
				INV_PARENT_ROW_INDEX = INV_ROW_INDEX;
			}
			CLONE_LINE();
			INV_ROW_INDEX ++;
		}
		COLLAPSE_ITEMS(INV_PARENT_ROW_INDEX);
	};
		
	
	// ===============================================================
	// Request Search - PART.B
	// ===============================================================
	function HTTP_SEARCH_CALLBACK_POPULATE_NEXT(PARENT_JSON_OBJECT, PARENT_JSON_INDEX)
	{
		// Initialize
		// _______________________________________________________________________
		var PARENT_ROW_INDEX = 0;
		var CHILD_ROW_INDEX = 0;
		var QUERY_TYPE = JSOC_CMB.VAR_SELECTED_VALUE_NVAL(SEARCH_FORECAST_MODE_CMB);
		var SELECTED_MONTH = null
		var REQUESTED_MONTH = null;
		
		
		// Get Date
		// _______________________________________________________________________
		if(QUERY_TYPE == 1)
		{
			SELECTED_MONTH = JSVC_DATE.VAR_NOW();	
			SELECTED_MONTH = JSOC_ELEMENT.VAR_VALUE_TO_DATE("SEARCH_INVANTORY_DATE_DTP");
		}
		else
		{
			SELECTED_MONTH = JSVC_DATE.VAR_BEGIN_OF_MONTH(JSOC_ELEMENT.VAR_VALUE("SEARCH_INVANTORY_DATE_DTP"));
			REQUESTED_MONTH = SELECTED_MONTH;
		}
		
				
		// Insert Current Product
		// _______________________________________________________________________
		INVENTORY_DGV.ROW.ADD();
		PARENT_ROW_INDEX = INVENTORY_DGV.ROW.COUNT() - 1; 
		
		INVENTORY_DGV.CELL.VALUE("ROW_COUNT", PARENT_ROW_INDEX, PARENT_JSON_INDEX);
		INVENTORY_DGV.CELL.VALUE("REC_INVENTORY_CAT_PRODUCT_ID", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_PRODUCT_ID", PARENT_JSON_INDEX));
		INVENTORY_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", PARENT_JSON_INDEX));
		INVENTORY_DGV.CELL.VALUE("CAT_PRODUCT_ING_PRICE", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_PRODUCT_ING_PRICE", PARENT_JSON_INDEX));
		INVENTORY_DGV.CELL.VALUE("CAT_PRODUCT_ING_NET_CONTENT", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_PRODUCT_ING_NET_CONTENT", PARENT_JSON_INDEX));
		INVENTORY_DGV.CELL.VALUE("CAT_UOM_DESCRIPTION", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_UOM_DESCRIPTION", PARENT_JSON_INDEX));
		
		INVENTORY_DGV.CELL.VALUE("REC_INVENTORY_QUANTITY", PARENT_ROW_INDEX, JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "REC_INVENTORY_QUANTITY", PARENT_JSON_INDEX));						
		INVENTORY_DGV.CELL.VALUE("ECD", PARENT_ROW_INDEX, 0);			
		INVENTORY_DGV.CELL.VALUE("ECDDIFF", PARENT_ROW_INDEX, 0);			
		INVENTORY_DGV.CELL.VALUE("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", PARENT_ROW_INDEX, 0);			
	

		// Get consumption of the month
		// _______________________________________________________________________
		var REQUEST_CONSUMPTION_FORECAST = function(CALLBACK_METHOD, NEXT_MONTH = false)
		{
			// Prepare Get Parameter
			// ...........................................
			GET_MAP_ARRAY = JSCUS_SQL._RECORD._INVENTORY_CUSTOM.CUS_PREPARE_MAPPED_ARRAY_FOR_CONSUMPTION();	
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ING_CAT_PRODUCT_ID', JSRC_JSON.REF_READ(PARENT_JSON_OBJECT, "CAT_PRODUCT_ID", PARENT_JSON_INDEX));	
			
			
			// Load Covered Month
			// ...........................................
			if(NEXT_MONTH == true)
			{
				SELECTED_MONTH = JSVC_DATE.VAR_ADD
				(
					'MONTH', 1, SELECTED_MONTH
				);
			}	
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_TARGET_MONTH', SELECTED_MONTH);	
			
			
			// Send Request
			// ...........................................
			JSOC_DIALOG.VAR_LOADING_SPINNER(true);
			JSCUS_SQL._RECORD._INVENTORY_CUSTOM.CUS_CONSUMPTION_FORECAST
			(
				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 CHILD_JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
					if(JSRC_JSON.REF_IS_SUCCESS(CHILD_JSON_OBJECT) === false)
					{
						CUS_SEND_DIALOG_TRY_DISMISS();
						console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
						return;
					}
			
					// Proceed
					CALLBACK_METHOD(CHILD_JSON_OBJECT, NEXT_MONTH);
				},
				true
			);
		};

		
		// REGULAR Search Mode
		// _______________________________________________________________________		
		var CALLBACK_METHOD_MAIN = function(CHILD_JSON_OBJECT, IF_NEXT_MONTH)
		{
			// JSON Empty (Clears the DataGrodView)
			// ...........................................
			if(JSRC_JSON.REF_READ(CHILD_JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				//INVENTORY_DGV.CELL.VALUE("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", 0);
				if(QUERY_TYPE == 2 || IF_NEXT_MONTH == true)
				{
					POPULATE_COMPLETION();
				}
				else
				{
					REQUEST_CONSUMPTION_FORECAST(CALLBACK_METHOD_MAIN, true);
				}
				return;
			}
			
			
			// Initialize
			// ...........................................
			var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(CHILD_JSON_OBJECT, "JSON_COUNT");
			var JSON_INDEX = 1;
			
			var CURRENT_COLUMN = 1;
			var COLUMN_BEGIN = 1;
			var COLUMN_END = 1;
			
			var CURRENT_DATE = SELECTED_MONTH;
			var DATE_BEGIN = JSVC_NUM.VAR_NVALL(JSVC_DATE.VAR_FORMAT(SELECTED_MONTH, "DD"));
			var DATE_END = DATE_BEGIN;
			var DATE_MONTH = JSVC_NUM.VAR_NVALL(JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MM"));
			
			
			// Compute Start-date
			// ...........................................
			if(IF_NEXT_MONTH == true)
			{
				COLUMN_BEGIN = 32 - JSVC_NUM.VAR_NVALL(JSVC_DATE.VAR_FORMAT(REQUESTED_MONTH, "DD"));
			}
			else
			{
				COLUMN_BEGIN = 1;
			}
			
			// Compute Columns and Date
			// ...........................................
			while(31 >= COLUMN_END)
			{
				// Assign Value
				CURRENT_DATE = JSVC_DATE.VAR_ADD('DAY', 1, CURRENT_DATE);
				if(DATE_MONTH != JSVC_NUM.VAR_NVALL(JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "MM")))
				{
					break;
				}
				DATE_END = JSVC_NUM.VAR_NVALL(JSVC_DATE.VAR_FORMAT(CURRENT_DATE, "DD"));
				
				// To next loop
				COLUMN_END ++;
			}
			if(COLUMN_END > 31)
			{
				COLUMN_END --;
			}
			
			
			// Load Data 
			// ...........................................
			while(JSON_COUNT >= JSON_INDEX)
			{
				// Populate Data
				INVENTORY_DGV.ROW.ADD();
				CHILD_ROW_INDEX = INVENTORY_DGV.ROW.COUNT() - 1; 		
				INVENTORY_DGV.CELL.VALUE
				(
					"CAT_MIXTURE_BASE_CAT_PRODUCT_ID", CHILD_ROW_INDEX, 
					JSRC_JSON.REF_READ_NVALL(CHILD_JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID", JSON_INDEX)
				);
				INVENTORY_DGV.CELL.VALUE
				(
					"ROW_COUNT", CHILD_ROW_INDEX, "-"
				);
				INVENTORY_DGV.CELL.VALUE
				(
					"CAT_PRODUCT_DESCRIPTION", CHILD_ROW_INDEX, 
					JSRC_JSON.REF_READ(CHILD_JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX)
				);

				// Populate Per day consumption
				CURRENT_COLUMN = COLUMN_BEGIN;
				CURRENT_DATE = DATE_BEGIN;
				while(COLUMN_END >= CURRENT_COLUMN && DATE_END >= CURRENT_DATE)
				{
					INVENTORY_DGV.CELL.VALUE
					(
						"D" + JSVC_STR.VAR_FIX(CURRENT_COLUMN.toString(), null, 2, 2, "0"), 
						CHILD_ROW_INDEX, 
						JSVC_NUM.VAR_ROUND
						(
							JSRC_JSON.REF_READ_NVAL
							(
								CHILD_JSON_OBJECT, "D" + JSVC_STR.VAR_FIX(CURRENT_DATE.toString(), null, 2, 2, "0"), 
								JSON_INDEX
							), 4
						)
					);				
					
					CURRENT_COLUMN ++;
					CURRENT_DATE ++;
				}
								
				JSON_INDEX ++;
			}
			
			
			// This month only
			// ...........................................
			if(QUERY_TYPE == 2)
			{
				POPULATE_COMPLETION();
			}
			
			// Recent Days
			// ...........................................
			else
			{
				REQUEST_CONSUMPTION_FORECAST(CALLBACK_METHOD_MAIN, true);
			}
		};
		
		
		var POPULATE_COMPLETION = function()
		{
			// Initialize
			// ...........................................
			var CURRENT_DATE_PREFIX = '';
			var CURRENT_DATE_INDEX = 1;
			var CURRENT_PRODUCT_ID = INVENTORY_DGV.CELL.VALUE_NVAL("REC_INVENTORY_CAT_PRODUCT_ID", PARENT_ROW_INDEX);
			var ROW_INDEX = PARENT_ROW_INDEX + 1;
			var ROW_COUNT = INVENTORY_DGV.ROW.COUNT();
			var TOTAL_QTY = 0;
			
			
			// Populate Data
			// ...........................................
			while(31 >= CURRENT_DATE_INDEX)
			{
				// Initialize Loop
				CURRENT_DATE_PREFIX = 'D' + JSVC_STR.VAR_FIX(CURRENT_DATE_INDEX.toString(), null, 2, 2, "0");
				TOTAL_QTY = 0;
				ROW_INDEX = PARENT_ROW_INDEX + 1;
				
				// Sum Value of childs
				while
				(
					ROW_COUNT > ROW_INDEX && 
					CURRENT_PRODUCT_ID == INVENTORY_DGV.CELL.VALUE_NVAL("CAT_MIXTURE_BASE_CAT_PRODUCT_ID", ROW_INDEX)
				)
				{
					//console.log('Line : ' + INVENTORY_DGV.CELL.VALUE_NVAL(CURRENT_DATE_PREFIX, ROW_INDEX));
					INVENTORY_DGV.CELL.VALUE
					(
						CURRENT_DATE_PREFIX, ROW_INDEX,
						INVENTORY_DGV.CELL.VALUE_NVAL(CURRENT_DATE_PREFIX, ROW_INDEX)
					);
					TOTAL_QTY += INVENTORY_DGV.CELL.VALUE_NVAL(CURRENT_DATE_PREFIX, ROW_INDEX);
					ROW_INDEX ++;
				}
				//console.log('Result : ' + TOTAL_QTY);
				
				// Apply Value
				if(INVENTORY_DGV.COLUMN.HEADER_CAPTION(CURRENT_DATE_PREFIX) != "-")
				{
					INVENTORY_DGV.CELL.VALUE
					(
						CURRENT_DATE_PREFIX, PARENT_ROW_INDEX, 
						JSVC_NUM.VAR_ROUND(TOTAL_QTY, 4)
					);
				}

				// Next loop				
				CURRENT_DATE_INDEX ++;
			}
			
			
			// Call next loop if required
			// ...........................................
			if(JSRC_JSON.REF_READ_NVALL(PARENT_JSON_OBJECT, "JSON_COUNT") >= PARENT_JSON_INDEX + 1)
			{
				HTTP_SEARCH_CALLBACK_POPULATE_NEXT(PARENT_JSON_OBJECT, PARENT_JSON_INDEX + 1);	
			}
			else
			{
				if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(SEARCH_UOM_CMB) == 2)
				{
					COMPUTE_RM_IDEAL_FORECAST();
				}
				else if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(SEARCH_UOM_CMB) == 3)
				{
					COMPUTE_RM_RESTOCK_FORECAST();
				}
				
				// ~~~~~
				
				INVENTORY_DGV.COLUMN.CFORMAT("REC_INVENTORY_QUANTITY");
				INVENTORY_DGV.COLUMN.CFORMAT("D01");
				INVENTORY_DGV.COLUMN.CFORMAT("D02");
				INVENTORY_DGV.COLUMN.CFORMAT("D03");
				INVENTORY_DGV.COLUMN.CFORMAT("D04");
				INVENTORY_DGV.COLUMN.CFORMAT("D05");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D06");
				INVENTORY_DGV.COLUMN.CFORMAT("D07");
				INVENTORY_DGV.COLUMN.CFORMAT("D08");
				INVENTORY_DGV.COLUMN.CFORMAT("D09");
				INVENTORY_DGV.COLUMN.CFORMAT("D10");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D11");
				INVENTORY_DGV.COLUMN.CFORMAT("D12");
				INVENTORY_DGV.COLUMN.CFORMAT("D13");
				INVENTORY_DGV.COLUMN.CFORMAT("D14");
				INVENTORY_DGV.COLUMN.CFORMAT("D15");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D16");
				INVENTORY_DGV.COLUMN.CFORMAT("D17");
				INVENTORY_DGV.COLUMN.CFORMAT("D18");
				INVENTORY_DGV.COLUMN.CFORMAT("D19");
				INVENTORY_DGV.COLUMN.CFORMAT("D20");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D21");
				INVENTORY_DGV.COLUMN.CFORMAT("D22");
				INVENTORY_DGV.COLUMN.CFORMAT("D23");
				INVENTORY_DGV.COLUMN.CFORMAT("D24");
				INVENTORY_DGV.COLUMN.CFORMAT("D25");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D26");
				INVENTORY_DGV.COLUMN.CFORMAT("D27");
				INVENTORY_DGV.COLUMN.CFORMAT("D28");
				INVENTORY_DGV.COLUMN.CFORMAT("D29");
				INVENTORY_DGV.COLUMN.CFORMAT("D30");
				
				INVENTORY_DGV.COLUMN.CFORMAT("D31");
				
				// ~~~~~
				
				COPY_TO_PACKAGE_DGV();
				CUS_SEND_DIALOG_TRY_DISMISS();

				if(JSVC_VAR.VAR_ISSET(COL_FIXED_ACTIVATE))
				{
					COL_FIXED_ACTIVATE();
				}
			}
		};
		
		
		// Auto-total tool
		// _______________________________________________________________________	
		if(INVENTORY_DGV.COLUMN.TOTAL_IS_READY())
		{
			INVENTORY_DGV.COLUMN.TOTAL_REFRESH();
		}
		REQUEST_CONSUMPTION_FORECAST
		(
			CALLBACK_METHOD_MAIN, false
		);
	};
	

	/**
	 * =================================================================================
	 *
	 * Compute for differences in stock on hand vs expected consumption
	 *
	 * =================================================================================
	 */
	function COMPUTE_RM_RESTOCK_FORECAST()
	{
		// Initialize
		// _______________________________________________________________________
		var DD_PREFIX = "";
		var DD_PREFIX_YDAY = "";
		var DD_INDEX = 1;
		var ROW_COUNT = INVENTORY_DGV.ROW.COUNT();
		var ROW_INDEX = 0;
		var ON_HAND_QTY = 0;
		var CURRENT_TOTAL_QTY = 0;
		
		
		// Remove all the children
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX)
		{
			if(INVENTORY_DGV.CELL.VALUE_NVALL("REC_INVENTORY_CAT_PRODUCT_ID", ROW_INDEX) <= 0)
			{
				INVENTORY_DGV.ROW.REMOVE_AT(ROW_INDEX, true);
				ROW_COUNT --;
			}
			else
			{
				ROW_INDEX ++;	
			}
		}
		
		
		// Replace value with RM differences
		// _______________________________________________________________________
		ROW_INDEX = 0;
		while(ROW_COUNT > ROW_INDEX)
		{
			// Convert Consumptions to RM differences
			// ...........................................
			ON_HAND_QTY = INVENTORY_DGV.CELL.VALUE_NVAL("REC_INVENTORY_QUANTITY", ROW_INDEX);
			CURRENT_TOTAL_QTY = 0;
			DD_INDEX = 1;
			while(31 >= DD_INDEX)
			{	
				// Initialize Loop
				DD_PREFIX_YDAY = DD_PREFIX;
				DD_PREFIX = "D" + JSVC_STR.VAR_FIX(DD_INDEX.toString(), null, 2, 2, "0");
				CURRENT_TOTAL_QTY += INVENTORY_DGV.CELL.VALUE_NVAL(DD_PREFIX, ROW_INDEX);
				
				// First Day
				if(ON_HAND_QTY >= CURRENT_TOTAL_QTY)
				{
					INVENTORY_DGV.CELL.VALUE(DD_PREFIX, ROW_INDEX, "0.00");
				}
				else
				{
					INVENTORY_DGV.CELL.VALUE
					(
						DD_PREFIX, ROW_INDEX,
						CURRENT_TOTAL_QTY - ON_HAND_QTY
					);	
				}
			
				// To next day
				DD_INDEX ++;
			}
			
			// To next ingredient
			// ...........................................
			ROW_INDEX ++;	
		}
	};
	
				
	/**
	 * =================================================================================
	 *
	 * Compute for ideal stock on hand
	 *
	 * =================================================================================
	 */
	function COMPUTE_RM_IDEAL_FORECAST()
	{
		// Initialize
		// _______________________________________________________________________
		var DD_PREFIX = "";
		var DD_PREFIX_YDAY = "";
		var DD_INDEX = 1;
		var ROW_COUNT = INVENTORY_DGV.ROW.COUNT();
		var ROW_INDEX = 0;
		
		
		// Remove all the children
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX)
		{
			if(INVENTORY_DGV.CELL.VALUE_NVALL("REC_INVENTORY_CAT_PRODUCT_ID", ROW_INDEX) <= 0)
			{
				INVENTORY_DGV.ROW.REMOVE_AT(ROW_INDEX, true);
				ROW_COUNT --;
			}
			else
			{
				ROW_INDEX ++;	
			}
		}
		
		
		// Replace value with RM differences
		// _______________________________________________________________________
		ROW_INDEX = 0;
		while(ROW_COUNT > ROW_INDEX)
		{
			// Convert Consumptions to RM differences
			// ...........................................
			DD_INDEX = 1;
			while(31 >= DD_INDEX)
			{	
				// Initialize Loop
				DD_PREFIX_YDAY = DD_PREFIX;
				DD_PREFIX = "D" + JSVC_STR.VAR_FIX(DD_INDEX.toString(), null, 2, 2, "0");
				
				// First Day
				if(DD_INDEX == 1)
				{
					
					INVENTORY_DGV.CELL.VALUE
					(
						DD_PREFIX, ROW_INDEX,
						INVENTORY_DGV.CELL.VALUE_NVAL(DD_PREFIX, ROW_INDEX)
					);
				}
				else
				{
					INVENTORY_DGV.CELL.VALUE
					(
						DD_PREFIX, ROW_INDEX,
						INVENTORY_DGV.CELL.VALUE_NVAL(DD_PREFIX_YDAY, ROW_INDEX) +
						INVENTORY_DGV.CELL.VALUE_NVAL(DD_PREFIX, ROW_INDEX)
					);	
				}
			
				// To next day
				DD_INDEX ++;
			}
			
			// To next ingredient
			// ...........................................
			ROW_INDEX ++;	
		}
	};
	