﻿-- 87496
SET @NOWADAY = '20260112';
SET @FIRSTDATE = DATE(@NOWADAY)
;
SET @F_R1 = CAST(DATE_FORMAT(@FIRSTDATE , '%Y%m%d') AS CHAR);
SET @T_R1 = CAST(DATE_FORMAT(DATE_ADD(@FIRSTDATE, INTERVAL 30 DAY) , '%Y%m%d') AS CHAR);
SET @F_R3 = CAST(DATE_FORMAT(DATE_ADD(@FIRSTDATE, INTERVAL 31 DAY) , '%Y%m%d') AS CHAR);
SET @T_R3 = CAST(DATE_FORMAT(DATE_ADD(@FIRSTDATE, INTERVAL 60 DAY) , '%Y%m%d') AS CHAR);
SET @F_R4 = CAST(DATE_FORMAT(DATE_ADD(@FIRSTDATE, INTERVAL 61 DAY) , '%Y%m%d') AS CHAR);
SET @T_R4 = CAST(DATE_FORMAT(DATE_ADD(@FIRSTDATE, INTERVAL 90 DAY) , '%Y%m%d') AS CHAR);

WITH 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.HOTEL_CONTRACT_SEQ,
      VHC.CONTRACT_STATUS_CODE,
      VRT.VENDOR_ROOM_TYPE_SEQ,
      VVRP.ROOM_PLAN_SEQ,
      VRT.ROOM_TYPE_NAME_EN
  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'
  INNER JOIN VD_VENDOR_ROOM_PLAN VVRP ON VVRP.VENDOR_ROOM_TYPE_SEQ = VRT.VENDOR_ROOM_TYPE_SEQ AND VVRP.SALES_STOP_YN = 'N'
    AND VVRP.HOTEL_CONTRACT_SEQ = VHC.HOTEL_CONTRACT_SEQ
  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
  LIMIT 10000 OFFSET 90000
)
SELECT DISTINCT
    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 
              VRDP.VENDOR_NET_AMOUNT > 0 AND VRDP.SALES_STOP_YN = 'N'
              AND VRA.LEFT_COUNT_SUM > 0 AND VRA.SALES_STOP_YN = 'N'
            THEN 1 ELSE 0 END
          ))
        ) > 10
        THEN 'Bookable'
        ELSE 'No bookable'
    END AS Bookable,
    HI.HOTEL_CONTRACT_SEQ AS HotelContractSeq,
    VRDP.VENDOR_ROOM_TYPE_SEQ AS RoomCode,
    HI.ROOM_TYPE_NAME_EN AS RoomName
FROM HOTEL_INFO HI
LEFT JOIN VD_VENDOR_ROOM_DAILY_PRICE VRDP ON VRDP.ROOM_PLAN_SEQ = HI.ROOM_PLAN_SEQ -- 831925
  AND VRDP.HOTEL_CODE = HI.HOTEL_CODE -- 229424
LEFT JOIN VD_VENDOR_ROOM_ALLOTMENT VRA ON VRA.VENDOR_ROOM_TYPE_SEQ = VRDP.VENDOR_ROOM_TYPE_SEQ
  AND VRA.HOTEL_CONTRACT_SEQ = HI.HOTEL_CONTRACT_SEQ
  AND VRA.APPLIED_DATE = VRDP.APPLIED_DATE
WHERE 1=1
--   AND VRDP.APPLIED_DATE BETWEEN @F_R1 AND @T_R1 -- 0-30
--       AND VRDP.APPLIED_DATE BETWEEN @F_R3 AND @T_R3 -- 31-60
      AND VRDP.APPLIED_DATE BETWEEN @F_R4 AND @T_R4    -- 61-90 

GROUP BY HI.HOTEL_CODE, HI.ROOM_PLAN_SEQ, VRDP.ADULT_COUNT