﻿SELECT 
  HHM.COUNTRY_CODE AS CountryCode
  , HHM.HOTEL_CODE AS HotelCode
  , IFNULL(HHM.LEGACY_HOTEL_CODE, (
    SELECT VHC.LEGACY_HOTEL_CODE
    FROM VD_VENDOR_HOTEL_CONTRACT VHC
    WHERE VHC.HOTEL_CODE = HHM.HOTEL_CODE
    LIMIT 1
  )) AS LegacyCode
  , HHM.HOTEL_NAME_EN AS HotelName
  , (
    SELECT DC.DETAIL_CODE_NAME_EN
    FROM BS_DETAIL_CODE DC
    WHERE DC.MASTER_CODE = 'BS018'
      AND DC.DETAIL_CODE = HHM.REGISTER_STATUS_CODE
    LIMIT 1
  ) AS HotelStatus
  , RT.VENDOR_ROOM_TYPE_SEQ AS RoomTypeSeq
  , RT.LEGACY_ROOM_TYPE_CODE AS EllisRoomTypeCode
  , RT.ROOM_TYPE_NAME_EN AS RoomTypeNameEn
  , CASE WHEN RT.SALES_STOP_YN = 'N' THEN 'Active' ELSE 'Pending' END AS RoomTypeStatus
  , RP.ROOM_PLAN_SEQ AS PlanSeq
  , RP.LEGACY_ROOM_PLAN_CODE AS EllisRoomPlanCode
  , RP.ROOM_PLAN_NAME_EN AS PlanNameEn
--   , CASE WHEN RP.SALES_STOP_YN = 'N' THEN 'Active' ELSE 'Pending' END AS PlanStatus
  , P.PLAN_PROMOTION_SEQ AS PromotionSeq
  , P.LEGACY_PROMOTION_CODE AS EllisPromotionCode
  , P.VENDOR_PROMOTION_NAME AS PromotionNameEn
  , CASE WHEN P.SALES_STOP_YN = 'N' THEN 'Active' ELSE 'Pending' END AS PromotionStatus
FROM HO_HOTEL_MASTER HHM
INNER JOIN VD_VENDOR_ROOM_TYPE RT ON HHM.HOTEL_CODE = RT.HOTEL_CODE
INNER JOIN VD_VENDOR_ROOM_PLAN RP ON RT.VENDOR_ROOM_TYPE_SEQ = RP.VENDOR_ROOM_TYPE_SEQ AND RP.ORIGIN_ROOM_PLAN_SEQ IS NOT NULL
INNER JOIN VD_VENDOR_ROOM_PLAN_PROMOTION RPP ON RP.ROOM_PLAN_SEQ = RPP.ROOM_PLAN_SEQ
INNER JOIN VD_VENDOR_PLAN_PROMOTION P ON RPP.PLAN_PROMOTION_SEQ = P.PLAN_PROMOTION_SEQ
WHERE HHM.REGISTER_STATUS_CODE IN (
  'RST01', -- Approval Pending
  'RST02' -- Approved
)
  AND HHM.COUNTRY_CODE IN (
--     'KR'
    -- ,
    'JP'
  )
ORDER BY HHM.COUNTRY_CODE, HHM.HOTEL_CODE, RT.VENDOR_ROOM_TYPE_SEQ, RP.ROOM_PLAN_SEQ, P.PLAN_PROMOTION_SEQ
;