
	function GROUP_SHOW_HIDE(REQ_ROW_INDEX)
	{
		// Initialize
		// _______________________________________________________________________
		var ROW_COUNT = INVOICE_DGV.ROW.COUNT();
		var ROW_INDEX = REQ_ROW_INDEX + 1;
		var IS_HIDE_ROW = false;


		// Abort if no child
		// _______________________________________________________________________
		if
		(
			ROW_COUNT <= ROW_INDEX ||
			INVOICE_DGV.CELL.VALUE_NVAL("ROW_COUNT", REQ_ROW_INDEX) <= 0
		)
		{
			return;
		}
		
		
		// Identify to Show or Hide
		// _______________________________________________________________________
		if(INVOICE_DGV.ROW.DISPLAY(ROW_INDEX) != "none")
		{
			IS_HIDE_ROW = true;
		}
		

		// Group products in same branch
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX && INVOICE_DGV.CELL.VALUE_NVAL("ROW_COUNT", ROW_INDEX) <= 0)
		{
			// Hide Row
			// ...........................................
			if(IS_HIDE_ROW == true)
			{
				INVOICE_DGV.ROW.DISPLAY(ROW_INDEX, "NONE");
			}
			else
			{
				INVOICE_DGV.ROW.DISPLAY(ROW_INDEX, "DEFAULT");
			}
			
			// Next Loop
			// ...........................................
			ROW_INDEX ++;
		}		
	};



	function GROUP_SALES()
	{
		// Initialize
		// _______________________________________________________________________
		var CURRENT_BRANCH = '';
		var CURRENT_PARENT_INDEX = -1;
		var PARENT_COUNT = 1;
		var CURRENT_QTY = 0;
		var CURRENT_SALES = 0;
		var CURRENT_DISCOUNTS = 0;
		var CURRENT_RTN_QTY = 0;
		var CURRENT_RTN_SALES = 0;
				
		var ROW_COUNT = INVOICE_DGV.ROW.COUNT();
		var ROW_INDEX = 0;


		// Handle Updating the group header data
		// _______________________________________________________________________
		var UPDATE_PARENT_ROW = function()
		{
			INVOICE_DGV.ROW.ADD(CURRENT_PARENT_INDEX);
				
			INVOICE_DGV.CELL.VALUE("ROW_COUNT", CURRENT_PARENT_INDEX, PARENT_COUNT);
			INVOICE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", CURRENT_PARENT_INDEX, CURRENT_BRANCH);
			INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_QUANTITY", CURRENT_PARENT_INDEX, CURRENT_QTY);
			INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_SALES", CURRENT_PARENT_INDEX, CURRENT_SALES);
			INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_DISCOUNT", CURRENT_PARENT_INDEX, CURRENT_DISCOUNTS);				
			INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_QUANTITY", CURRENT_PARENT_INDEX, CURRENT_RTN_QTY);				
			INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_SALES", CURRENT_PARENT_INDEX, CURRENT_RTN_SALES);				

			if
			(
				JSOC_ELEMENT.VAR_ELEMENT_EXISTS("INVOICE_DGV_HEADER_REC_INVOICELIST_PRICE")
			)
			{
				INVOICE_DGV.CELL.VALUE
				(
					"REC_INVOICELIST_PRICE", CURRENT_PARENT_INDEX, 
					INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_SALES", CURRENT_PARENT_INDEX) /
					INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_QUANTITY", CURRENT_PARENT_INDEX) 
				);				
				INVOICE_DGV.CELL.VALUE_NVAL
				(
					"REC_INVOICE_NETSALES", CURRENT_PARENT_INDEX, 
					INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_SALES", CURRENT_PARENT_INDEX) -
					INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_DISCOUNT", CURRENT_PARENT_INDEX) -
					INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_SALES", CURRENT_PARENT_INDEX) 
				);				
			}
			
			GROUP_SHOW_HIDE(CURRENT_PARENT_INDEX);	
		};
		

		// Group products in same branch
		// _______________________________________________________________________
		while(ROW_COUNT > ROW_INDEX)
		{
			if(CURRENT_BRANCH != INVOICE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX))
			{
				if(CURRENT_PARENT_INDEX >= 0)
				{
					// console.log('Finalizing Parent ' + CURRENT_PARENT_INDEX + ' : ' + CURRENT_BRANCH);
					UPDATE_PARENT_ROW();
					PARENT_COUNT ++;
					ROW_COUNT ++;
					ROW_INDEX ++;
				}
				
				CURRENT_BRANCH = INVOICE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX);
				CURRENT_PARENT_INDEX = ROW_INDEX;
					
				CURRENT_QTY = 0;
				CURRENT_SALES = 0;
				CURRENT_DISCOUNTS = 0;
				CURRENT_RTN_QTY = 0;
				CURRENT_RTN_SALES = 0;
			}


			// Sum Childs
			INVOICE_DGV.CELL.VALUE("ROW_COUNT", ROW_INDEX, "");
			INVOICE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, "-");
			CURRENT_QTY += INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_QUANTITY", ROW_INDEX);
			CURRENT_SALES += INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_SALES", ROW_INDEX);
			CURRENT_DISCOUNTS += INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_DISCOUNT", ROW_INDEX);
			CURRENT_RTN_QTY += INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_QUANTITY", ROW_INDEX);
			CURRENT_RTN_SALES += INVOICE_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_SALES", ROW_INDEX);
			// console.log('Row : ' + ROW_INDEX + '/' + ROW_COUNT);
			
			// Next Loop
			ROW_INDEX ++;
		}
		UPDATE_PARENT_ROW();
		
		
		// Update CFormat
		// _______________________________________________________________________
		INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICELIST_QUANTITY");
		INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICELIST_SALES");
		INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICELIST_DISCOUNT");
		INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICERTN_QUANTITY");
		INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICERTN_SALES");
		
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("INVOICE_DGV_HEADER_REC_INVOICELIST_PRICE")
		)
		{
			INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICELIST_PRICE");
			INVOICE_DGV.COLUMN.CFORMAT("REC_INVOICE_NETSALES");
		}
		
	};