/**
 * =================================================================================
 * Apply discount to all items
 * =================================================================================
 */

var SENIOR_DISCOUNT = 0;

function COMPUTE_DISCOUNT_APPLY(DISCOUNT_INDEX)
{
	if(JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_ID', DISCOUNT_INDEX) == 2) //senior 20%
	{
		JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);
	}
	else
	{
		JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);

		alertify.confirm
		(
			// Dialog message
			// ...........................................	
			'PeakPOS', 'Are you sure you want to apply discount to all items?', 
			
			// Apply discount to all items
			// ...........................................	
			function() 
			{ 
				// Apply Discount
				if(CART_DGV.ROW.COUNT() > 0)
				{
					var ROW_INDEX = 0;

					while(CART_DGV.ROW.COUNT() > ROW_INDEX)
					{
						COMPUTE_CART_ITEMS(ROW_INDEX, DISCOUNT_INDEX);
						ROW_INDEX++;
					}
				}

				// Replace Remove Button onClick event
				$('#REMOVE_DISCOUNT').removeClass('d-none');

				// Close discount modal
				JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);
				
			},
			
			// Close discount modal
			// ...........................................	
			function()
			{
				JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);
			}
	    ).set('labels', {ok:'YES', cancel:'Cancel'}); 
	}
	
};

/*function COMPUTE_SENIORS_DISCOUNT()
{
	DISCOUNT_INDEX = $('#DISCOUNTS_SENIOR_MODAL #DISCOUNT_INDEX').val();
	QTY_CUSTOMERS = $('#DISCOUNTS_SENIOR_MODAL #DISCOUNTS_QTY_CUSTOMERS').val();
	QTY_SENIORS = $('#DISCOUNTS_SENIOR_MODAL #DISCOUNTS_QTY_SENIORS').val();

	SENIOR_DISCOUNT = QTY_SENIORS / QTY_CUSTOMERS;

	// Apply Discount
	$('#ORDER_TABLE > tbody > tr').each
	(
		function() 
		{
			if($(this).is(':hidden') === false) 
			{
				if
				(
					$('#PROD_DISCOUNT_ISBASICCOM_'+$(this).attr('id').replace('PROD_ORDER_', '')).val() == 2 &&
					JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_ID', DISCOUNT_INDEX) == 2 
				)
				{
					console.log(SENIOR_5P);
					DISCOUNT_INDEX = SENIOR_5P;
				}

				COMPUTE_CART_ITEM
				(
					$(this).attr('id').replace('PROD_ORDER_', ''), 
					DISCOUNT_INDEX
				);			
			}
		}
	);

	$('#DISCOUNTS_SENIOR_MODAL').modal('hide');
	$('#REMOVE_DISCOUNT').removeClass('d-none');
				
	// Replace Remove Button onClick event
	$('#REMOVE_DISCOUNT').attr("onclick", "COMPUTE_DISCOUNT_REMOVE_ALL()");
}*/

/**
 * =================================================================================
 * Apply discount for selected item only
 * =================================================================================
 */
function PREPARE_DISCOUNT_APPLY(DISCOUNT_INDEX)
{
	JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);

	alertify.confirm
	(
		// Dialog message
		// ...........................................	
		'PeakPOS', 'Are you sure you want to apply discount to current selected item?', 
		
		// Apply discount to all items
		// ...........................................	
		function() 
		{ 
			// Apply Discount
			COMPUTE_CART_ITEMS(PROPERTY_CART_ROW_INDEX, DISCOUNT_INDEX);
			$('#REMOVE_DISCOUNT').removeClass('d-none');
		},
		
		// Close discount modal
		// ...........................................	
		function()
		{
			JSOC_DIALOG.VAR_MODAL("POS_DISCOUNT_MODAL", false);
		}
    ).set('labels', {ok:'YES', cancel:'Cancel'}); 
}

/**
 * =================================================================================
 * Remove applied discounts
 * =================================================================================
 */
function COMPUTE_DISCOUNT_REMOVE_ALL()
{
	alertify.confirm
	(
		'PeakPOS', 'Are you sure you want to remove discounts applied?', 
		function() 
		{
			if(CART_DGV.ROW.COUNT() > 0)
			{
				var ROW_INDEX = 1;

				while(CART_DGV.ROW.COUNT() >= ROW_INDEX)
				{
					COMPUTE_CART_ITEMS(ROW_INDEX-1, -1);
					ROW_INDEX++;
				}
			}
			$('#REMOVE_DISCOUNT').addClass('d-none'); 
		},
		function()
		{}
	);
};
	
			
/**
 * =================================================================================
 * Compute cart item pricing + discount + VAT etc.
 * =================================================================================
 */		

function COMPUTE_CART_ITEMS(CART_INDEX, DISCOUNT_INDEX)
{
	// Initialize
	// _______________________________________________________________________
	var CAT_DISCOUNT_ID = 0;
	var CAT_DISCOUNT_AMOUNT = 0;
	var CAT_DISCOUNT_CAT_TAXMODEL_ID = 0;
	var CAT_DISCOUNT_CHANGE_TAX_TYPE = 0;
	var CAT_DISCOUNT_HAS_POINT = 0;
	var CAT_DISCOUNT_PERCENTAGE = 0;
	var CAT_DISCOUNT_VAT = 0;

	// Apply discount
	// _______________________________________________________________________
	if(DISCOUNT_INDEX >= 0)
	{
		CAT_DISCOUNT_ID = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_ID', DISCOUNT_INDEX);
		CAT_DISCOUNT_AMOUNT = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_AMOUNT', DISCOUNT_INDEX);
		CAT_DISCOUNT_CAT_TAXMODEL_ID = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_CAT_TAXMODEL_ID', DISCOUNT_INDEX);
		CAT_DISCOUNT_CHANGE_TAX_TYPE = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_CHANGE_TAX_TYPE', DISCOUNT_INDEX);
		CAT_DISCOUNT_HAS_POINT = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_HAS_POINT', DISCOUNT_INDEX);
		CAT_DISCOUNT_PERCENTAGE = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_PERCENTAGE', DISCOUNT_INDEX);
		CAT_DISCOUNT_VAT = JSRC_JSON.REF_READ(CACHE_DISCOUNT_JSON, 'CAT_DISCOUNT_VAT', DISCOUNT_INDEX);			
	}
	
	// Non-discounted computation
	// _______________________________________________________________________
	else
	{
		CAT_DISCOUNT_ID = 1;
		CAT_DISCOUNT_AMOUNT = 0;
		CAT_DISCOUNT_CAT_TAXMODEL_ID = 1;
		CAT_DISCOUNT_CHANGE_TAX_TYPE = 1;
		CAT_DISCOUNT_HAS_POINT = 2;
		CAT_DISCOUNT_PERCENTAGE = 0;
		CAT_DISCOUNT_VAT = 12;					
	}
	
	var COMPUTE_MAP_ARRAY = JSCUS_CALC._RECORD._SALES.CUS_PREPARE_MAPPED_ARRAY_FOR_COMPUTE();
	var PRODUCT_LV_INDEX =  JSVC_NUM.VAR_NVALL(CART_DGV.CELL.VALUE("PRODUCT_LV_INDEX", CART_INDEX));

	var ITEM_PRICE = JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODBRANCH_AMOUNT_PRICE', PRODUCT_LV_INDEX));

	//check price model used
	if(PRICE_MODEL_ID > 1)
	{
		var ITEM_INDEX = 0;

		while(JSRC_JSON.REF_READ(CACHE_PRODUCTS_JSON, "JSON_COUNT") > ITEM_INDEX)
		{
			if(JSVC_NUM.VAR_NVALL(CART_DGV.CELL.VALUE("PRODUCT_ID", CART_INDEX)) == JSRC_JSON.REF_READ(CACHE_PRODUCTS_JSON, "CAT_PRODUCT_ID", ITEM_INDEX))
			{
				ITEM_PRICE = JSVC_NUM.VAR_NVAL(JSRC_JSON.REF_READ(CACHE_PRODUCTS_JSON, "CAT_PRODBRANCH_AMOUNT_PRICE", ITEM_INDEX));
			}

			ITEM_INDEX++;
		}
	}

	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_ID', JSVC_NUM.VAR_NVALL(CART_DGV.CELL.VALUE("PRODUCT_ID", CART_INDEX)));

	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_AMOUNT_PRICE', ITEM_PRICE);
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_AMOUNT_VAT', JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_VAT', PRODUCT_LV_INDEX)));
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_AMOUNT_POINT', JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_POINT', PRODUCT_LV_INDEX)));

	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_ALLOW_DISCOUNT', JSVC_NUM.VAR_NVALL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_DISCOUNT', PRODUCT_LV_INDEX)));
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_ALLOW_SENIOR', JSVC_NUM.VAR_NVALL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_SENIOR', PRODUCT_LV_INDEX)));
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_PRODUCT_PER_PIECE_QTY', JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_PER_PIECE_QTY', PRODUCT_LV_INDEX)));

	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_TAXMODEL_ID', JSVC_NUM.VAR_NVALL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_TAXMODEL_ID', PRODUCT_LV_INDEX)));
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_TAXMODEL_PERCENTAGE', JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_AMOUNT', PRODUCT_LV_INDEX)));
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_TAXMODEL_AMOUNT', JSVC_NUM.VAR_NVAL(PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_PERCENTAGE', PRODUCT_LV_INDEX))); 

	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_ID', CAT_DISCOUNT_ID);
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_PERCENTAGE', CAT_DISCOUNT_PERCENTAGE);
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_AMOUNT', CAT_DISCOUNT_AMOUNT);
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_VAT', CAT_DISCOUNT_VAT);
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_HAS_POINT', CAT_DISCOUNT_HAS_POINT); 
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_CHANGE_TAX_TYPE', CAT_DISCOUNT_CHANGE_TAX_TYPE); 
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'CAT_DISCOUNT_CAT_TAXMODEL_ID', CAT_DISCOUNT_CAT_TAXMODEL_ID);
				
	JSVC_MAP_ARRAY.VAR_SET(COMPUTE_MAP_ARRAY, 'ITEM_QUANTITY', 	JSVC_NUM.VAR_NVAL(CART_DGV.CELL.VALUE("QTY", CART_INDEX)));

	console.log(COMPUTE_MAP_ARRAY);
	// Compute Adjustments
	// _______________________________________________________________________
	TOTALS = JSCUS_CALC._RECORD._SALES.CUS_COMPUTE_MA(COMPUTE_MAP_ARRAY);
	console.log(TOTALS);


	if(CAT_DISCOUNT_ID == 2)
	{
		TOTALS['TOPAY_DISCOUNT'] = TOTALS['TOPAY_DISCOUNT'] * SENIOR_DISCOUNT;
	}

	CART_DGV.CELL.VALUE("QTY", CART_INDEX, JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(TOTALS, "QUANTITY")));
	CART_DGV.CELL.VALUE("QTY_PCS", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "QUANTITY_PCS"));

	CART_DGV.CELL.VALUE("POINTS", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "POINTS"));

	CART_DGV.CELL.VALUE("DISCOUNT_ID", CART_INDEX, CAT_DISCOUNT_ID);
	CART_DGV.CELL.VALUE("DISCOUNT_VALUE", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_DISCOUNT"));

	CART_DGV.CELL.VALUE("DISCOUNT_PERCENTAGE", CART_INDEX, CAT_DISCOUNT_PERCENTAGE);
	CART_DGV.CELL.VALUE("DISCOUNT_AMOUNT", CART_INDEX, CAT_DISCOUNT_AMOUNT);
	CART_DGV.CELL.VALUE("DISCOUNT_VAT", CART_INDEX, CAT_DISCOUNT_VAT);
	CART_DGV.CELL.VALUE("DISCOUNT_HAS_POINT", CART_INDEX, CAT_DISCOUNT_HAS_POINT);
	CART_DGV.CELL.VALUE("DISCOUNT_CHANGE_TAX_TYPE", CART_INDEX, CAT_DISCOUNT_CHANGE_TAX_TYPE);
	CART_DGV.CELL.VALUE("DISCOUNT_CAT_TAXMODEL_ID", CART_INDEX, CAT_DISCOUNT_CAT_TAXMODEL_ID);

	CART_DGV.CELL.VALUE("UOM_ID", CART_INDEX, PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_UOM_ID', PRODUCT_LV_INDEX));
	CART_DGV.CELL.VALUE("UOM_CODE", CART_INDEX, PRODUCT_BRANCH_LV.CELL.VALUE('CAT_UOM_CODE', PRODUCT_LV_INDEX));

	CART_DGV.CELL.VALUE("PRICE", CART_INDEX, JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(TOTALS, "AMOUNT_PRICE")));
	CART_DGV.CELL.VALUE("AMOUNT", CART_INDEX, JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(TOTALS, "TOPAY_SUBTOTAL")));

	CART_DGV.CELL.VALUE("NONVAT", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_NONVAT"));
	CART_DGV.CELL.VALUE("VAT", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_VAT"));
	CART_DGV.CELL.VALUE("VATABLE", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_VATABLE"));
	CART_DGV.CELL.VALUE("VATEXEMPT", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_VATEXEMPT"));
	CART_DGV.CELL.VALUE("VATOTHER", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_VATOTHER"));
	CART_DGV.CELL.VALUE("VATLESS", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_VAT_LESS"));
	CART_DGV.CELL.VALUE("ZERORATED", CART_INDEX, JSRC_JSON.REF_READ(TOTALS, "TOPAY_ZERORATED"));

	HTTP_CALCULATE_CART();
};
