ALTER FUNCTION [App].[Fn_GetReceiptCost]
(
	@ApplicationRequestDetailID BIGINT,
	@ApplicationRequestTypeID BIGINT,
	@IsRenewal BIT,
	@IsConversion BIT,
	@VehicleCount SMALLINT
)
RETURNS MONEY
AS
BEGIN			
	DECLARE @Cost MONEY

	SELECT @Cost = (
				(
					Art.Cost +
					CASE 
						WHEN @ApplicationRequestTypeID = 27 /*temp replacement*/ THEN 0
						WHEN @IsRenewal = 0 OR App.Fn_IsRenewalType(@ApplicationRequestTypeID) = 1 OR @ApplicationRequestTypeID IN (11 /*Temporary (Special) licence*/) THEN 0
						WHEN @ApplicationRequestTypeID = 31 /*Renewal (Pre 02-08-2024 cost adjustment)*/ THEN 0
						ELSE (SELECT  Cost FROM    App.ApplicationRequestTypes WHERE   ApplicationRequestTypeID = 8)
					END + 
					CASE
						WHEN @ApplicationRequestTypeID = 27 /*temp replacement*/ THEN 0 
						WHEN @IsConversion = 0 OR @ApplicationRequestTypeID IN (5 /*Conversion (LTPS to legiti-mate)*/,11 /*Temporary (Special) licence*/) THEN 0
						WHEN @ApplicationRequestTypeID = 31 /*Renewal (Pre 02-08-2024 cost adjustment)*/ THEN 0
						ELSE (SELECT  Cost FROM    App.ApplicationRequestTypes WHERE   ApplicationRequestTypeID = 5)
					END 
				) * @VehicleCount
		)
	FROM app.ApplicationRequestTypes Art
	WHERE ApplicationRequestTypeID = @ApplicationRequestTypeID

	/*
	Change as per SUP0007250 START. 
	Not charging for new KZN number change to to ZN.
	*/
	IF [App].[Fn_IsCovType](@ApplicationRequestTypeID)  = 1
	BEGIN
		IF EXISTS(
			SELECT *
			FROM app.Vw_ApplicationRequestDetails
			WHERE ApplicationRequestDetailID = @ApplicationRequestDetailID
				AND VehicleRegNumber LIKE '%ZN'
				AND ExistingVehicleRegNumber NOT LIKE '%ZN'
				AND ChassisNumber = ExistingVehicleChassisNumber
		)
		BEGIN
			DECLARE @CovTransactionCost MONEY = (SELECT Cost FROM app.ApplicationRequestTypes WHERE ApplicationRequestTypeID = 3 /*Replacements (COV)*/)

			SET @Cost = @Cost - @CovTransactionCost		
		END		
	END
	/*
	Change as per SUP0007250 END. 
	*/

	RETURN @cost 
END
GO

