USE [LegitimateKZN_QA_SO]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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;

    -- NOTE: Previous SUP0007250 logic that subtracted the COV cost for
    -- ZN number changes has been removed so those applications now pay
    -- the normal COV fee.

    RETURN @Cost;
END;
GO


