USE [LegitimateKZN_QA_SO]
GO

/****** Object:  StoredProcedure [App].[Pr_LicenceIssueDetaisReport]    Script Date: 2025/10/27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/****** 
 * Modified to include ShowEHailingAnnexure flag for E-Hailing mode (ModeID = 6)
 * This flag will be used by the LicenceIssueRequestController to include
 * the E-Hailing licence conditions annexure when printing
 ******/

ALTER PROCEDURE [App].[Pr_LicenceIssueDetaisReport]
(
    @LicenceIssueDetailID BIGINT,
    @UserID BIGINT
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP 1
        AMD.ModeID,
        LID.LicenceIssueDetailID,
        UPPER(L.OperatingLicenceNumber) AS OperatingLicenceNumber,
        AMD.ApplicationRequestDetailID,
        UPPER(L.CertificateNumber) AS CertificateNumber,
		(CASE WHEN AMD.ModeID = 4 /*Metered Taxi*/ THEN 1 ELSE 0 END) AS ShowMeteredTaxiAnnexure,
		(CASE WHEN AMD.ModeID = 6 /*E-Hailing*/ THEN 1 ELSE 0 END) AS ShowEHailingAnnexure,
		(CASE WHEN AMD.ModeID = 2 /*Midibus*/ THEN 1 ELSE 0 END) AS ShowMidiBusAnnexure,
		(CASE WHEN EXISTS(SELECT * FROM Verified.LicenceServiceDetails Lsd WHERE Lsd.LicenceID = Lid.VerifiedLicenceID AND Lsd.ServiceTypeID = 5 /*Staff*/) THEN 1 ELSE 0 END) AS ShowStaffServiceAnnexure,
		(CASE WHEN EXISTS(SELECT * FROM Verified.LicenceServiceDetails Lsd WHERE Lsd.LicenceID = Lid.VerifiedLicenceID AND Lsd.ServiceTypeID = 6 /*Scholar*/) THEN 1 ELSE 0 END) AS ShowScholarAnnexure,
		(CASE WHEN AMD.ModeID = 1 /*Bus*/ THEN 1 ELSE 0 END) AS ShowBusAnnexure,
		(CASE WHEN AMD.ModeID = 3 /*Minibus Taxi*/ THEN 1 ELSE 0 END) AS ShowMinibusAnnexure
    FROM App.LicenceIssueDetails AS LID
        INNER JOIN App.ApplicationModeDetails AS AMD
            ON LID.ApplicationRequestDetailID = AMD.ApplicationRequestDetailID
        INNER JOIN Verified.Licences AS L
            ON AMD.ApplicationRequestDetailID = L.ApplicationRequestDetailID
    WHERE LID.LicenceIssueDetailID = @LicenceIssueDetailID;

END;
GO