diff --git a/models/intermediate/int_core__booking_charge_events.sql b/models/intermediate/int_core__booking_charge_events.sql index 99bd453..98a503a 100644 --- a/models/intermediate/int_core__booking_charge_events.sql +++ b/models/intermediate/int_core__booking_charge_events.sql @@ -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 diff --git a/models/intermediate/int_core__bookings.sql b/models/intermediate/int_core__bookings.sql index 88e3d05..6dda1f4 100644 --- a/models/intermediate/int_core__bookings.sql +++ b/models/intermediate/int_core__bookings.sql @@ -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, diff --git a/models/intermediate/int_core__price_plans.sql b/models/intermediate/int_core__price_plans.sql index 351965b..90ea8df 100644 --- a/models/intermediate/int_core__price_plans.sql +++ b/models/intermediate/int_core__price_plans.sql @@ -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, diff --git a/models/reporting/core/core__bookings.sql b/models/reporting/core/core__bookings.sql new file mode 100644 index 0000000..0612615 --- /dev/null +++ b/models/reporting/core/core__bookings.sql @@ -0,0 +1,12 @@ +with int_core__bookings as (select * from {{ ref("int_core__bookings") }}) + +select + b.id_booking, + b.id_user_guest, + b.id_user_host, + b.id_accommodation, + b.is_duplicate_booking, + b.booking_fee_local, + b.booking_fee_charge_at_utc, + b.booking_fee_charge_date_utc +from int_core__bookings b diff --git a/models/staging/core/stg_core__booking.sql b/models/staging/core/stg_core__booking.sql index 68e3c8e..3122101 100644 --- a/models/staging/core/stg_core__booking.sql +++ b/models/staging/core/stg_core__booking.sql @@ -1,3 +1,14 @@ +{{ + config( + indexes=[ + {"columns": ["id_booking"]}, + {"columns": ["id_user_host"]}, + {"columns": ["id_user_guest"]}, + {"columns": ["id_verification_request"]}, + ] + ) +}} + with raw_booking as (select * from {{ source("core", "Booking") }}), stg_core__booking as ( diff --git a/models/staging/core/stg_core__price_plan_to_user.sql b/models/staging/core/stg_core__price_plan_to_user.sql index 5ea2258..0e93f1a 100644 --- a/models/staging/core/stg_core__price_plan_to_user.sql +++ b/models/staging/core/stg_core__price_plan_to_user.sql @@ -1,3 +1,5 @@ +{{ config(indexes=[{"columns": ["id_price_plan"]}, {"columns": ["id_user_host"]}]) }} + with raw_price_plan_to_user as (select * from {{ source("core", "PricePlanToUser") }}), stg_core__price_plan_to_user as ( @@ -10,12 +12,13 @@ with cast({{ adapter.quote("StartDate") }} as date) as start_date_utc, {{ adapter.quote("EndDate") }} as end_at_utc, cast({{ adapter.quote("EndDate") }} as date) end_date_utc, - {{ adapter.quote("BookingFeeNet") }} as booking_fee, - {{ adapter.quote("ListingFeeNet") }} as listing_fee, - {{ adapter.quote("SupportFeeNet") }} as support_fee, + {{ adapter.quote("BookingFeeNet") }} as booking_fee_local, + {{ adapter.quote("ListingFeeNet") }} as listing_fee_local, + {{ adapter.quote("SupportFeeNet") }} as support_fee_local, {{ adapter.quote("TaxPercentage") }} as tax_percentage, {{ adapter.quote("MinBillableListings") }} as minimum_billable_listings, - {{ adapter.quote("MinMonthlyListingFee") }} as minimum_monthly_listing_fee, + {{ adapter.quote("MinMonthlyListingFee") }} + as minimum_monthly_listing_fee_local, {{ adapter.quote("CreatedDate") }} as created_at_utc, cast({{ adapter.quote("CreatedDate") }} as date) as created_date_utc, {{ adapter.quote("UpdatedDate") }} as updated_at_utc, diff --git a/models/staging/core/stg_core__superhog_user.sql b/models/staging/core/stg_core__superhog_user.sql index 07d962c..0acb83d 100644 --- a/models/staging/core/stg_core__superhog_user.sql +++ b/models/staging/core/stg_core__superhog_user.sql @@ -1,3 +1,5 @@ +{{ config(indexes=[{"columns": ["id_superhoguser"]}]) }} + with raw_superhog_user as (select * from {{ source("core", "superhog_user") }}), stg_core__superhog_user as ( diff --git a/models/staging/core/stg_core__user.sql b/models/staging/core/stg_core__user.sql index 8f390a2..c70a553 100644 --- a/models/staging/core/stg_core__user.sql +++ b/models/staging/core/stg_core__user.sql @@ -1,3 +1,5 @@ +{{ config(indexes=[{"columns": ["id_user"]}]) }} + with raw_user as (select * from {{ source("core", "User") }}), stg_core__user as ( diff --git a/models/staging/core/stg_core__verification_request.sql b/models/staging/core/stg_core__verification_request.sql index c1d2967..5caba63 100644 --- a/models/staging/core/stg_core__verification_request.sql +++ b/models/staging/core/stg_core__verification_request.sql @@ -1,3 +1,14 @@ +{{ + config( + indexes=[ + {"columns": ["id_verification_request"]}, + {"columns": ["id_user_guest"]}, + {"columns": ["id_user_host"]}, + ] + ) +}} + + with raw_verification_request as ( select * from {{ source("core", "VerificationRequest") }}