a lot of changes

This commit is contained in:
Pablo Martin 2024-04-04 16:33:08 +02:00
parent 5101a2a45e
commit df61682d90
9 changed files with 88 additions and 29 deletions

View file

@ -4,19 +4,24 @@ with
int_core__verification_requests as (
select * from {{ ref("int_core__verification_requests") }}
),
booking_with_price_plans as (
booking_with_relevant_price_plans as (
select
b.id_booking,
pp.id_price_plan,
pp.booking_fee,
-- There is an obscure edge case where a single booking might be charged
-- twice according to our logic. We use this to untie by simply picking
-- the oldest charge event.
row_number() over (
partition by b.id_booking order by pp.id_price_plan
) as price_plan_priority_rank,
-- The below case will be true when a price plan is relevant for the
-- booking.
*,
case
when pp.price_plan_charged_by_type = 'CheckInDate'
then b.check_in_at_utc
when pp.price_plan_charged_by_type in ('VerificationStartDate', 'All')
then vr.verification_start_at_utc
end as booking_fee_charge_at_utc
from stg_core__booking b
left join
int_core__verification_requests vr
on b.id_verification_request = vr.id_verification_request
left join int_core__price_plans pp on b.id_user_host = pp.id_user_host
where
-- The dates that defines which price plan applies to the booking depends
-- on charged by type. With the below case, we evaluate if a certain price
-- plan relates to the booking
case
when pp.price_plan_charged_by_type = 'CheckInDate'
then b.check_in_at_utc between pp.start_at_utc and pp.end_at_utc
@ -30,14 +35,24 @@ with
false
)
else false
end as booking_belongs_to_price_plan
from stg_core__booking b
left join
int_core__verification_requests vr
on b.id_verification_request = vr.id_verification_request
left join int_core__price_plans pp on b.id_user_host = pp.id_user_host
end
= true
),
untied_bookings as (
-- If a booking has two valid price plans, take the earliest
select id_booking, min(id_price_plan) as id_price_plan
from booking_with_relevant_price_plans brpp
group by id_booking
)
select bpp.id_booking, bpp.id_price_plan, bpp.booking_fee
from booking_with_price_plans bpp
where bpp.booking_belongs_to_price_plan = true and price_plan_priority_rank = 1
select
ub.id_booking,
ub.id_price_plan,
brpp.booking_fee_local,
brpp.booking_fee_charge_at_utc,
cast(brpp.booking_fee_charge_at_utc as date) as booking_fee_charge_date_utc
from untied_bookings ub
left join
booking_with_relevant_price_plans brpp
on ub.id_booking = brpp.id_booking
and ub.id_price_plan = brpp.id_price_plan

View file

@ -28,6 +28,9 @@ select
b.check_in_date_utc,
b.check_out_at_utc,
b.check_out_date_utc,
bce.booking_fee_local,
booking_fee_charge_at_utc,
booking_fee_charge_date_utc,
b.summary,
b.guest_email,
b.guest_last_name,

View file

@ -25,12 +25,12 @@ select
case
when pp.id_price_plan = lpp.latest_id_price_plan then true else false
end as is_latest_price_plan_for_host,
pp.booking_fee,
pp.listing_fee,
pp.support_fee,
pp.booking_fee_local,
pp.listing_fee_local,
pp.support_fee_local,
pp.tax_percentage,
pp.minimum_billable_listings,
pp.minimum_monthly_listing_fee,
pp.minimum_monthly_listing_fee_local,
pp.created_at_utc,
pp.created_date_utc,
pp.updated_at_utc,