﻿-- Define date ranges
SET @NOWADAY = NOW();
SET @F_R1 = DATE(@NOWADAY);
SET @T_R1 = DATE_ADD(@F_R1, INTERVAL 30 DAY);
SET @F_R3 = DATE_ADD(@F_R1, INTERVAL 31 DAY);
SET @T_R3 = DATE_ADD(@F_R1, INTERVAL 60 DAY);
SET @F_R4 = DATE_ADD(@F_R1, INTERVAL 61 DAY);
SET @T_R4 = DATE_ADD(@F_R1, INTERVAL 90 DAY);
 
WITH
-- 1 Daily rate summary by hotel + room type
VRDP_SUM AS (  
    SELECT
        VVHC.HOTEL_CODE,
        VVRP.VENDOR_ROOM_TYPE_SEQ,
         SUM(CASE WHEN VRDP.VENDOR_NET_AMOUNT > 0 AND VRDP.SALES_STOP_YN = 'N' THEN 1 ELSE 0 END) AS CNT
    FROM VD_VENDOR_ROOM_DAILY_PRICE VRDP
    INNER JOIN VD_VENDOR_ROOM_PLAN VVRP ON VVRP.ROOM_PLAN_SEQ = VRDP.ROOM_PLAN_SEQ AND VVRP.SALES_STOP_YN = 'N'
    INNER JOIN VD_VENDOR_HOTEL_CONTRACT VVHC ON VVRP.HOTEL_CONTRACT_SEQ = VVHC.HOTEL_CONTRACT_SEQ AND VVHC.CONTRACT_STATUS_CODE = 'A'
    -- WHERE VRDP.APPLIED_DATE BETWEEN @F_R1 AND @T_R1 -- 0-30
    -- WHERE VRDP.APPLIED_DATE BETWEEN @F_R3 AND @T_R3 -- 31-60
    WHERE VRDP.APPLIED_DATE BETWEEN @F_R4 AND @T_R4    -- 61-90 
    GROUP BY VRDP.HOTEL_CODE, VRDP.VENDOR_ROOM_TYPE_SEQ
    
),

-- ⃣ Allotment summary by room type
VRA_SUM AS (
    SELECT
        VRA.VENDOR_ROOM_TYPE_SEQ,
        SUM(CASE WHEN VRA.LEFT_COUNT_SUM > 0 AND VRA.SALES_STOP_YN = 'N' THEN 1 ELSE 0 END) AS CNT
    FROM VD_VENDOR_ROOM_ALLOTMENT VRA
    INNER JOIN VD_VENDOR_ROOM_TYPE VRT ON VRA.VENDOR_ROOM_TYPE_SEQ = VRT.VENDOR_ROOM_TYPE_SEQ AND VRT.SALES_STOP_YN = 'N'
    INNER JOIN VD_VENDOR_HOTEL_CONTRACT VVHC ON VVHC.HOTEL_CODE = VRT.HOTEL_CODE AND VVHC.CONTRACT_STATUS_CODE = 'A'
    -- WHERE VRA.APPLIED_DATE BETWEEN @F_R1 AND @T_R1 -- 0-30
    -- WHERE VRA.APPLIED_DATE BETWEEN @F_R3 AND @T_R3 -- 31-60
    WHERE VRA.APPLIED_DATE BETWEEN @F_R4 AND @T_R4    -- 61-90
    GROUP BY VRA.VENDOR_ROOM_TYPE_SEQ
),

HOTEL_INFO AS (
    SELECT DISTINCT
        HM.COUNTRY_CODE,
        BCC.COUNTRY_NAME_EN AS COUNTRY_NAME,
        HM.HOTEL_CODE,
        IFNULL(HM.LEGACY_HOTEL_CODE, VHC.LEGACY_HOTEL_CODE) AS LEGACY_HOTEL_CODE,
        HM.HOTEL_NAME_EN AS HOTEL_NAME,
        HM.REGION_CODE,
        BRC.REGION_NAME_EN AS REGION_NAME,
        HM.REGISTER_STATUS_CODE,
        DC.DETAIL_CODE_NAME_EN AS HOTEL_STATUS,
        HM.ADDRESS_EN,
        HM.PHONE_NO,
        HM.STAR_RATING,
        VHC.CONTRACT_STATUS_CODE,
        VRT.VENDOR_ROOM_TYPE_SEQ
    FROM HO_HOTEL_MASTER HM
    INNER JOIN VD_VENDOR_HOTEL_CONTRACT VHC ON HM.HOTEL_CODE = VHC.HOTEL_CODE AND VHC.CONTRACT_STATUS_CODE = 'A'
    INNER JOIN VD_VENDOR_ROOM_TYPE VRT ON HM.HOTEL_CODE = VRT.HOTEL_CODE AND VRT.SALES_STOP_YN = 'N'
    LEFT JOIN BS_COUNTRY_CODE BCC ON HM.COUNTRY_CODE = BCC.COUNTRY_CODE
    LEFT JOIN BS_REGION_CODE BRC ON BRC.REGION_CODE = HM.REGION_CODE
    LEFT JOIN BS_DETAIL_CODE DC ON DC.MASTER_CODE = 'BS018' AND HM.REGISTER_STATUS_CODE = DC.DETAIL_CODE
)
 
SELECT 
    HI.COUNTRY_CODE,
    HI.COUNTRY_NAME,
    HI.REGION_NAME,
    HI.HOTEL_CODE,    
    HI.HOTEL_NAME,
    HI.LEGACY_HOTEL_CODE,
    HI.HOTEL_STATUS,
    CASE 
        WHEN SUM(CASE WHEN (IFNULL(RDP.CNT,0) > 10 OR IFNULL(RA.CNT,0) > 10) THEN 0 ELSE 1 END) > 0
        THEN 'No bookable'
        ELSE 'Bookable'
    END AS Bookable
FROM HOTEL_INFO HI
LEFT JOIN VRDP_SUM RDP ON RDP.HOTEL_CODE = HI.HOTEL_CODE AND RDP.VENDOR_ROOM_TYPE_SEQ = HI.VENDOR_ROOM_TYPE_SEQ
LEFT JOIN VRA_SUM RA ON RA.VENDOR_ROOM_TYPE_SEQ = HI.VENDOR_ROOM_TYPE_SEQ 
GROUP BY 
    HI.COUNTRY_CODE,
    HI.COUNTRY_NAME,
    HI.REGION_NAME,
    HI.HOTEL_CODE,    
    HI.HOTEL_NAME,
    HI.LEGACY_HOTEL_CODE,
    HI.HOTEL_STATUS
ORDER BY 
    HI.COUNTRY_CODE, 
    HI.HOTEL_CODE;
