Merged PR 3901: Booking Service Detail improvements

# Description

Modifies Booking Service Detail table (int_core__booking_service_detail) with the following improvements:
- Considers waiver or deposit service offering as Deposit Management service business type instead of Unknown
- Includes Guest Payments (Waivers outside of Waiver PRO, Deposits) as chargeable services
- Computes total price of Waiver PRO multiplying base unit price per number of nights

# Checklist

- [X] The edited models and dependants run properly with production data.
- [X] The edited models are sufficiently documented.
- [X] The edited models contain PK tests, and I've ran and passed them.
- [X] I have checked for DRY opportunities with other models and docs.
- [X] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

Related work items: #25228
This commit is contained in:
Oriol Roqué Paniagua 2024-12-27 10:42:47 +00:00
parent 5fd69b7101
commit a51ad3dd69

View file

@ -1,4 +1,9 @@
{{ config(materialized="table", unique_key=["id_booking_service_detail"]) }}
{% set waiver_or_deposit_offered_but_not_chosen = (
"('BASIC DAMAGE DEPOSIT OR WAIVER PLUS')"
) %}
with
int_core__booking_to_service as (
select * from {{ ref("int_core__booking_to_service") }}
@ -18,14 +23,30 @@ with
select * from {{ ref("int_core__protection_plan_billing_item") }}
),
int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}),
int_core__verification_payments as (
select * from {{ ref("int_core__verification_payments", version = 2) }}
),
product_service_billing_aggregation as (
select
id_booking,
id_product_service,
sum(amount_in_gbp) as service_total_price_in_gbp,
min(chargeable_date_utc) as first_chargeable_date_utc,
max(chargeable_date_utc) as last_chargeable_date_utc
from int_core__product_service_billing_item
psbi.id_booking,
psbi.id_product_service,
-- The following logic is to fix Waiver Pro being charged per night and not being stored correctly in the database.
-- To keep in mind, in the product service billing item it currently contains the unit price of the service, while it
-- should already include the total price of the service.
sum(case
when pstp.price_base_unit = 'PER NIGHT'
then bts.booking_number_of_nights
else 1
end * psbi.amount_in_gbp) as service_total_price_in_gbp,
min(psbi.chargeable_date_utc) as first_chargeable_date_utc,
max(psbi.chargeable_date_utc) as last_chargeable_date_utc
from int_core__product_service_billing_item psbi
left join
int_core__product_service_to_price pstp
on psbi.id_product_service_to_price = pstp.id_product_service_to_price
left join
int_core__booking_to_service bts
on psbi.id_product_service_to_price = bts.id_booking_service_detail
group by 1, 2
),
protection_plan_billing_aggregation as (
@ -38,6 +59,25 @@ with
from int_core__protection_plan_billing_item
group by 1, 2
),
product_service_guest_payment_aggregation as (
select
bts.id_booking,
bts.id_product_service,
vp.currency as currency_code,
sum(vp.amount_without_taxes_in_txn_currency) as service_total_price_in_local_currency,
sum(vp.amount_without_taxes_in_gbp) as service_total_price_in_gbp,
min(vp.payment_paid_date_utc) as first_chargeable_date_utc,
max(vp.payment_paid_date_utc) as last_chargeable_date_utc
from int_core__booking_to_service bts
inner join
int_core__verification_payments vp
on bts.id_verification = vp.id_verification
-- We only consider 1) cases in which verification is informed and 2) payments that are paid
where bts.id_verification is not null
and upper(vp.payment_status) = {{ var("paid_payment_state") }}
and bts.id_product_service is not null
group by 1, 2, 3
),
applied_product_services as (
select
bts.id_booking,
@ -58,11 +98,17 @@ with
pstp.payment_type,
pstp.price_base_unit,
pstp.invoicing_trigger,
pstp.currency_code,
pstp.amount_local_curr as service_unit_price_local_curr,
psba.service_total_price_in_gbp,
psba.first_chargeable_date_utc,
psba.last_chargeable_date_utc,
-- If the service has a guest payment, we use the currency of the payment.
coalesce(psgpa.currency_code, pstp.currency_code) as currency_code,
coalesce(psgpa.service_total_price_in_local_currency,
pstp.amount_local_curr) as service_unit_price_local_curr,
-- If the service has a guest payment, we use the total price and dates of the payment.
coalesce(psgpa.service_total_price_in_gbp,
psba.service_total_price_in_gbp) as service_total_price_in_gbp,
coalesce(psgpa.first_chargeable_date_utc,
psba.first_chargeable_date_utc) as first_chargeable_date_utc,
coalesce(psgpa.last_chargeable_date_utc,
psba.last_chargeable_date_utc) as last_chargeable_date_utc,
bts.is_missing_host_currency_code as is_missing_currency_code,
bts.is_booking_cancelled,
(not pstp.is_default_service) as is_upgraded_service
@ -78,6 +124,10 @@ with
product_service_billing_aggregation psba
on bts.id_booking = psba.id_booking
and bts.id_product_service = psba.id_product_service
left join
product_service_guest_payment_aggregation psgpa
on bts.id_booking = psgpa.id_booking
and bts.id_product_service = psgpa.id_product_service
where bts.id_product_service is not null
),
applied_protection_services as (
@ -133,8 +183,16 @@ with
bts.booking_check_in_at_utc,
bts.booking_check_out_at_utc,
'PLATFORM' as service_business_scope,
'UNKNOWN' as service_business_type,
'UNKNOWN' as service_source,
case
when bts.service_name in {{ waiver_or_deposit_offered_but_not_chosen }}
then 'DEPOSIT_MANAGEMENT'
else 'UNKNOWN'
end as service_business_type,
case
when bts.service_name in {{ waiver_or_deposit_offered_but_not_chosen }}
then 'PRODUCT'
else 'UNKNOWN'
end as service_source,
bts.service_status,
bts.booking_status,
bts.program_name,
@ -149,10 +207,14 @@ with
cast(null as date) as last_chargeable_date_utc,
bts.is_missing_host_currency_code as is_missing_currency_code,
bts.is_booking_cancelled,
-- This case shouldn't occur, but the current missing
-- cases are all for Basic Screening which is not an
-- upgraded service.
false as is_upgraded_service
-- This case shouldn't occur, but the current missing cases aside of
-- Waiver or Deposits being offered but not yet chosen are all for Basic
-- Screening which is not an upgraded service.
case
when bts.service_name in {{ waiver_or_deposit_offered_but_not_chosen }}
then true
else false
end as is_upgraded_service
from int_core__booking_to_service bts
where bts.id_protection_plan is null and bts.id_product_service is null
),