JSCUS_CALC = function(){};
JSCUS_CALC._RECORD = function(){};
JSCUS_CALC._RECORD._SALES = function(){};
	
	
	JSCUS_CALC._RECORD._SALES.CUS_PREPARE_MAPPED_ARRAY_FOR_COMPUTE = function()
	{
		// Initialize
		// _______________________________________________________________________
		var COMPUTE_MAP_ARRAY = [];
		
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_ID'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_PRICE'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_VAT'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_POINT'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_ALLOW_DISCOUNT'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_ALLOW_SENIOR'] = null;
		COMPUTE_MAP_ARRAY['CAT_PRODUCT_PER_PIECE_QTY'] = null;	
			
		COMPUTE_MAP_ARRAY['CAT_TAXMODEL_ID'] = null;	
		COMPUTE_MAP_ARRAY['CAT_TAXMODEL_PERCENTAGE'] = null;
		COMPUTE_MAP_ARRAY['CAT_TAXMODEL_AMOUNT'] = null;
			
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_ID'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_PERCENTAGE'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_AMOUNT'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_VAT'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_HAS_POINT'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_CHANGE_TAX_TYPE'] = null;
		COMPUTE_MAP_ARRAY['CAT_DISCOUNT_CAT_TAXMODEL_ID'] = null;
			
		COMPUTE_MAP_ARRAY['ITEM_QUANTITY'] = null;
		
		
		// Return Result
		// _______________________________________________________________________
		return COMPUTE_MAP_ARRAY;
	}
	
	
	
	/** 
	 * =======================================================================================================
	 * Compute purchase line item
	 * =======================================================================================================
	 */
	JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_MA = function
	(
		COMPUTE_MAP_ARRAY
	)
	{
		return JSCUS_CALC._RECORD._SALES.CUS_COMPUTE
		(
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_ID']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_PRICE']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_VAT']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_AMOUNT_POINT']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_ALLOW_DISCOUNT']),
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_ALLOW_SENIOR']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_PRODUCT_PER_PIECE_QTY']),	
			
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_TAXMODEL_ID']),	
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_TAXMODEL_PERCENTAGE']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_TAXMODEL_AMOUNT']),
			
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_ID']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_PERCENTAGE']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_AMOUNT']),
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_VAT']),
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_HAS_POINT']),
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_CHANGE_TAX_TYPE']),
			JSVC_NUM.VAR_NVALL(COMPUTE_MAP_ARRAY['CAT_DISCOUNT_CAT_TAXMODEL_ID']),
			
			JSVC_NUM.VAR_NVAL(COMPUTE_MAP_ARRAY['ITEM_QUANTITY'])
		);
	}
	
	
	/** 
	 * =======================================================================================================
	 * Compute purchase line item
	 * =======================================================================================================
	 */
	JSCUS_CALC._RECORD._SALES.CUS_COMPUTE = function
	(
		CAT_PRODUCT_ID,
		CAT_PRODUCT_AMOUNT_PRICE,
		CAT_PRODUCT_AMOUNT_VAT,
		CAT_PRODUCT_AMOUNT_POINT,
		CAT_PRODUCT_ALLOW_DISCOUNT,
		CAT_PRODUCT_ALLOW_SENIOR,
		CAT_PRODUCT_PER_PIECE_QTY,	
		
		CAT_TAXMODEL_ID,	
		CAT_TAXMODEL_PERCENTAGE,
		CAT_TAXMODEL_AMOUNT,
		
		CAT_DISCOUNT_ID,
		CAT_DISCOUNT_PERCENTAGE,
		CAT_DISCOUNT_AMOUNT,
		CAT_DISCOUNT_VAT,
		CAT_DISCOUNT_HAS_POINT,
		CAT_DISCOUNT_CHANGE_TAX_TYPE,
		CAT_DISCOUNT_CAT_TAXMODEL_ID,
		
		ITEM_QUANTITY
	)
	{
		// Initialize
		// _______________________________________________________________________
		var RESULT_AMOUNT_PRICE = 0;
		var RESULT_AMOUNT_VAT = 0;
		var RESULT_QUANTITY = 0;
		var RESULT_QUANTITY_PCS = 0;
		var RESULT_CAT_DISCOUNT_ID = CAT_DISCOUNT_ID;
		
		var RESULT_TOPAY_SUBTOTAL = 0;
		var RESULT_TOPAY_DISCOUNT = 0;
		var RESULT_TOPAY_VATABLE = 0;
		var RESULT_TOPAY_VATEXEMPT = 0;
		var RESULT_TOPAY_NONVAT = 0;
		var RESULT_TOPAY_ZERORATED = 0;
		var RESULT_TOPAY_VATOTHER = 0;
		var RESULT_TOPAY_VAT = 0;
		var RESULT_TOPAY_VAT_LESS = 0;
		var RESULT_POINTS = 0;
		
		var TEMP_SUBTOTAL = 0;
		

		// Compute Quantity Adjustment
		// _______________________________________________________________________
		RESULT_AMOUNT_PRICE = CAT_PRODUCT_AMOUNT_PRICE;
		RESULT_AMOUNT_VAT = CAT_PRODUCT_AMOUNT_VAT;
		RESULT_QUANTITY = ITEM_QUANTITY;
		RESULT_QUANTITY_PCS = CAT_PRODUCT_PER_PIECE_QTY * ITEM_QUANTITY;
		RESULT_TOPAY_SUBTOTAL = CAT_PRODUCT_AMOUNT_PRICE * ITEM_QUANTITY;
		
		RESULT_TOPAY_VAT = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(RESULT_TOPAY_SUBTOTAL, CAT_TAXMODEL_PERCENTAGE);
		RESULT_TOPAY_VAT_LESS = 0;
		RESULT_POINTS = CAT_PRODUCT_AMOUNT_POINT * ITEM_QUANTITY;


		// Discounts
		// _______________________________________________________________________

		// Check if discountable
		// ...........................................	
		if(CAT_PRODUCT_ALLOW_DISCOUNT == 1 && CAT_DISCOUNT_ID > 1)
		{
			RESULT_CAT_DISCOUNT_ID = 1;
		}	
		
		// Check if senior / pwd discountable
		// ...........................................	
		else if
		(
			CAT_PRODUCT_ALLOW_SENIOR == 1 && 
			(
				CAT_DISCOUNT_ID == 2 ||
				CAT_DISCOUNT_ID == 3 ||
				CAT_DISCOUNT_ID == 4 ||
				CAT_DISCOUNT_ID == 5
			)
		)
		{
			RESULT_CAT_DISCOUNT_ID = 1;
		}	

		// Check if senior discountable
		// ...........................................	
		else if
		(
			CAT_PRODUCT_ALLOW_SENIOR == 2 && 
			(
				CAT_DISCOUNT_ID == 2 ||
				CAT_DISCOUNT_ID == 4
			)
		)
		{		
			// New VAT
			if(RESULT_AMOUNT_VAT > 0 && CAT_DISCOUNT_VAT !== CAT_TAXMODEL_PERCENTAGE)
			{
				RESULT_TOPAY_VAT = 0;
				RESULT_TOPAY_VAT_LESS = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(RESULT_TOPAY_SUBTOTAL, CAT_TAXMODEL_PERCENTAGE);
			}
			TEMP_SUBTOTAL = RESULT_TOPAY_SUBTOTAL - RESULT_TOPAY_VAT_LESS;		
			
			// Adjust Base Amount
			if(CAT_DISCOUNT_PERCENTAGE > 0)
			{
				RESULT_TOPAY_DISCOUNT = (TEMP_SUBTOTAL * CAT_DISCOUNT_PERCENTAGE * 0.01);
			}
			else
			{
				RESULT_TOPAY_DISCOUNT = CAT_DISCOUNT_AMOUNT;		
			}
			
			// Modify Tax Type
			if(CAT_DISCOUNT_CHANGE_TAX_TYPE == 2)
			{
				CAT_TAXMODEL_ID = CAT_DISCOUNT_CAT_TAXMODEL_ID;
			}
			
			// New Point
			if(CAT_DISCOUNT_HAS_POINT == 1)
			{
				RESULT_POINTS = 0;
			}
		}	
				
		// Apply discount
		// ...........................................	
		else if(CAT_DISCOUNT_ID > 1)
		{
			// Adjust Base Amount
			if(CAT_DISCOUNT_PERCENTAGE > 0)
			{
				RESULT_TOPAY_DISCOUNT = (RESULT_TOPAY_SUBTOTAL * CAT_DISCOUNT_PERCENTAGE * 0.01);
			}
			else
			{
				RESULT_TOPAY_DISCOUNT = CAT_DISCOUNT_AMOUNT;		
			}
			TEMP_SUBTOTAL = RESULT_TOPAY_SUBTOTAL - RESULT_TOPAY_DISCOUNT ;	
			
			// New VAT
			if(RESULT_AMOUNT_VAT > 0 && CAT_DISCOUNT_VAT !== CAT_TAXMODEL_PERCENTAGE)
			{
				RESULT_TOPAY_VAT = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(TEMP_SUBTOTAL, CAT_DISCOUNT_VAT);
				RESULT_TOPAY_VAT_LESS = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(TEMP_SUBTOTAL, CAT_TAXMODEL_PERCENTAGE) - RESULT_TOPAY_VAT;
			}
			else
			{
				// RESULT_AMOUNT_VAT = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(TEMP_SUBTOTAL, CAT_TAXMODEL_PERCENTAGE);
				RESULT_TOPAY_VAT = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT(TEMP_SUBTOTAL, CAT_TAXMODEL_PERCENTAGE);
			}
			
			// Catch negative VAT_LESS
			if(RESULT_TOPAY_VAT_LESS < 0)
			{
				RESULT_TOPAY_VAT_LESS = 0;
			}
			
			// Modify Tax Type
			if(CAT_DISCOUNT_CHANGE_TAX_TYPE == 2)
			{
				CAT_TAXMODEL_ID = CAT_DISCOUNT_CAT_TAXMODEL_ID;
			}
			
			// New Point
			if(CAT_DISCOUNT_HAS_POINT == 1)
			{
				RESULT_POINTS = 0;
			}
		}


		// VAT Sales Control
		// _______________________________________________________________________
		
		// Compute VAT Sales [VATable]
		// ...........................................	
		if(CAT_TAXMODEL_ID == 1)
		{
			RESULT_TOPAY_VATABLE = RESULT_TOPAY_SUBTOTAL - 
			(
				RESULT_TOPAY_DISCOUNT + RESULT_TOPAY_VAT + RESULT_TOPAY_VAT_LESS
			);
		}
		
		// Compute VAT Sales [VAT-Exempted]
		// ...........................................
		else if(CAT_TAXMODEL_ID == 2)
		{
			RESULT_TOPAY_VATEXEMPT = RESULT_TOPAY_SUBTOTAL - 
			(
				RESULT_TOPAY_DISCOUNT + RESULT_TOPAY_VAT + RESULT_TOPAY_VAT_LESS
			);
		}
		
		// Compute VAT Sales [Non-VATable]
		// ...........................................
		else if(CAT_TAXMODEL_ID == 3)
		{
			RESULT_TOPAY_NONVAT = RESULT_TOPAY_SUBTOTAL - 
			(
				RESULT_TOPAY_DISCOUNT + RESULT_TOPAY_VAT + RESULT_TOPAY_VAT_LESS
			);
		}				

		// Compute VAT Sales [Zero-Rated]
		// ...........................................
		else if(CAT_TAXMODEL_ID == 4)
		{
			RESULT_TOPAY_ZERORATED = RESULT_TOPAY_SUBTOTAL - 
			(
				RESULT_TOPAY_DISCOUNT + RESULT_TOPAY_VAT + RESULT_TOPAY_VAT_LESS
			);
		}	
	
		// Compute VAT Sales [Other VAT]
		// ...........................................
		else if(CAT_TAXMODEL_ID == 5)
		{
			RESULT_TOPAY_VATOTHER = RESULT_TOPAY_SUBTOTAL - 
			(
				RESULT_TOPAY_DISCOUNT + RESULT_TOPAY_VAT + RESULT_TOPAY_VAT_LESS
			);	
		}	
		
		
		// Store to Result Map Array
		// _______________________________________________________________________
		var RETURN_MAP_ARRAY = [];
		RETURN_MAP_ARRAY['AMOUNT_PRICE'] = RESULT_AMOUNT_PRICE;
		RETURN_MAP_ARRAY['AMOUNT_VAT'] = RESULT_AMOUNT_VAT;
		RETURN_MAP_ARRAY['QUANTITY'] = RESULT_QUANTITY;
		RETURN_MAP_ARRAY['QUANTITY_PCS'] = RESULT_QUANTITY_PCS;
		RETURN_MAP_ARRAY['CAT_DISCOUNT_ID'] = RESULT_CAT_DISCOUNT_ID;
		
		RETURN_MAP_ARRAY['TOPAY_SUBTOTAL'] = RESULT_TOPAY_SUBTOTAL;
		RETURN_MAP_ARRAY['TOPAY_DISCOUNT'] = RESULT_TOPAY_DISCOUNT;
		RETURN_MAP_ARRAY['TOPAY_VATABLE'] = RESULT_TOPAY_VATABLE;
		RETURN_MAP_ARRAY['TOPAY_VATEXEMPT'] = RESULT_TOPAY_VATEXEMPT;
		RETURN_MAP_ARRAY['TOPAY_NONVAT'] = RESULT_TOPAY_NONVAT;
		RETURN_MAP_ARRAY['TOPAY_ZERORATED'] = RESULT_TOPAY_ZERORATED;
		RETURN_MAP_ARRAY['TOPAY_VATOTHER'] = RESULT_TOPAY_VATOTHER;
		RETURN_MAP_ARRAY['TOPAY_VAT'] = RESULT_TOPAY_VAT;
		RETURN_MAP_ARRAY['TOPAY_VAT_LESS'] = RESULT_TOPAY_VAT_LESS;
		RETURN_MAP_ARRAY['POINTS'] = RESULT_POINTS;
		
		
		// Return Result
		// _______________________________________________________________________
		return RETURN_MAP_ARRAY;
	};
	
	
	
	
	/** 
	 * =======================================================================================================
	 * Compute purchase line item
	 * =======================================================================================================
	 */
	JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_VAT = function
	(
		TOTAL_AMOUNT,
		VAT_PERCENTAGE
	)
	{
		return TOTAL_AMOUNT - 
		(
			TOTAL_AMOUNT / 
			(
				(VAT_PERCENTAGE + 100) * 0.01
			)
		);
	}
	