Invoice model completed

This commit is contained in:
Joaquin Ossa 2024-12-23 15:25:38 +01:00
parent 1fff7b91a2
commit 9a4c7a312d
5 changed files with 201 additions and 297 deletions

View file

@ -18,19 +18,20 @@ select
then true then true
else false else false
end as is_protected, end as is_protected,
(au.json_document_user_data ->> 'PriceIncrease')::decimal as price_increase, (au.json_document_user_data ->> 'PriceIncrease')::decimal
as price_increase_percentage,
(au.json_document_user_data ->> 'PriceIncreaseStartDate')::date (au.json_document_user_data ->> 'PriceIncreaseStartDate')::date
as price_increase_start_date_utc, as price_increase_start_date_utc,
(au.json_document_user_data ->> 'MonthlyVolumeDiscount')::decimal (au.json_document_user_data ->> 'MonthlyVolumeDiscount')::decimal
as monthly_volume_discount, as monthly_volume_discount_percentage,
(au.json_document_user_data ->> 'ThresholdApprovedBookingVolume')::integer (au.json_document_user_data ->> 'ThresholdApprovedBookingVolume')::integer
as threshold_approved_booking_volume, as threshold_approved_or_flagged_booking_volume,
(au.json_document_user_data ->> 'MonthlyVolumeDiscountStartDate')::date (au.json_document_user_data ->> 'MonthlyVolumeDiscountStartDate')::date
as monthly_volume_discount_start_date_utc, as monthly_volume_discount_start_date_utc,
(au.json_document_user_data ->> 'MonthlyVolumeDiscountEndDate')::date (au.json_document_user_data ->> 'MonthlyVolumeDiscountEndDate')::date
as monthly_volume_discount_end_date_utc, as monthly_volume_discount_end_date_utc,
(au.json_document_user_data ->> 'MonthlyGeneralDiscount')::decimal (au.json_document_user_data ->> 'MonthlyGeneralDiscount')::decimal
as monthly_general_discount, as monthly_general_discount_percentage,
(au.json_document_user_data ->> 'MonthlyGeneralDiscountStartDate')::date (au.json_document_user_data ->> 'MonthlyGeneralDiscountStartDate')::date
as monthly_general_discount_start_date_utc, as monthly_general_discount_start_date_utc,
(au.json_document_user_data ->> 'MonthlyGeneralDiscountEndDate')::date (au.json_document_user_data ->> 'MonthlyGeneralDiscountEndDate')::date

View file

@ -4742,7 +4742,7 @@ models:
data_tests: data_tests:
- not_null - not_null
- name: price_increase - name: price_increase_percentage
data_type: numeric data_type: numeric
description: The percentage or value of the price increase description: The percentage or value of the price increase
applied to the user's account. applied to the user's account.
@ -4758,7 +4758,7 @@ models:
data_tests: data_tests:
- is_first_day_of_month - is_first_day_of_month
- name: monthly_volume_discount - name: monthly_volume_discount_percentage
data_type: numeric data_type: numeric
description: The discount percentage or value offered based on the description: The discount percentage or value offered based on the
volume of bookings achieved within a month. volume of bookings achieved within a month.
@ -4769,7 +4769,7 @@ models:
max_value: 100 max_value: 100
strictly: true strictly: true
- name: threshold_approved_booking_volume - name: threshold_approved_or_flagged_booking_volume
data_type: numeric data_type: numeric
description: The minimum number of bookings required to qualify for description: The minimum number of bookings required to qualify for
the monthly volume discount. the monthly volume discount.
@ -4792,7 +4792,7 @@ models:
data_tests: data_tests:
- is_last_day_of_month - is_last_day_of_month
- name: monthly_general_discount - name: monthly_general_discount_percentage
data_type: numeric data_type: numeric
description: The general discount percentage or value applied to all description: The general discount percentage or value applied to all
bookings within the applicable period. bookings within the applicable period.

View file

@ -4,7 +4,7 @@
{% set damage_waiver = "DAMAGE WAIVER" %} {% set damage_waiver = "DAMAGE WAIVER" %}
{% set screen_and_protect = "SCREEN & PROTECT" %} {% set screen_and_protect = "SCREEN & PROTECT" %}
{% set standalone_protection = "STANDALONE PROTECTION" %} {% set standalone_protection = "STANDALONE PROTECTION" %}
{% set cancel_rejected_fee = 0.25 %} {% set cancelled_rejected_fee = 0.25 %}
{% set long_stay_booking_days_threshold = 30 %} {% set long_stay_booking_days_threshold = 30 %}
with with
int_screen_and_protect__verification_requests as ( int_screen_and_protect__verification_requests as (
@ -39,17 +39,20 @@ with
and vr.monthly_volume_discount_end_date_utc and vr.monthly_volume_discount_end_date_utc
then 1 then 1
else 0 else 0
end as is_monthly_volume_discount_on end as is_monthly_volume_discount_on,
vr.threshold_approved_or_flagged_booking_volume
from int_screen_and_protect__verification_requests vr from int_screen_and_protect__verification_requests vr
where upper(vr.verification_status) in {{ approved_flagged_status }} where upper(vr.verification_status) in {{ approved_flagged_status }}
), ),
approved_verifications as ( approved_or_flagged_verifications as (
select select
vr.id_user_partner, vr.id_user_partner,
date_trunc('month', vr.checkout_date_utc) as verification_month, date_trunc('month', vr.checkout_date_utc) as verification_month,
count(*) as monthly_verifications count(*) as monthly_verifications
from int_screen_and_protect__verification_requests vr from int_screen_and_protect__verification_requests vr
where upper(vr.verification_status) in {{ approved_flagged_status }} where
upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
group by vr.id_user_partner, date_trunc('month', vr.checkout_date_utc) group by vr.id_user_partner, date_trunc('month', vr.checkout_date_utc)
), ),
active_discounts as ( active_discounts as (
@ -60,17 +63,174 @@ with
adt.is_monthly_general_discount_active, adt.is_monthly_general_discount_active,
case case
when when
av.monthly_verifications >= adt.threshold_approved_booking_volume av.monthly_verifications
>= adt.threshold_approved_or_flagged_booking_volume
and adt.is_monthly_volume_discount_on = 1 and adt.is_monthly_volume_discount_on = 1
then 1 then 1
else 0 else 0
end as is_monthly_volume_discount_active, end as is_monthly_volume_discount_active
adt.is_price_increase_active from approved_or_flagged_verifications av
from approved_verifications av
left join left join
active_discount_timeframe adt active_discount_timeframe adt
on av.id_user_partner = adt.id_user_partner on av.id_user_partner = adt.id_user_partner
and av.verification_month = adt.verification_month and av.verification_month = adt.verification_month
),
-- All rejected bookings, including those with flagged status but the user's
-- protection is not active
rejected_bookings as (
select
vr.id_verification,
{{ cancelled_rejected_fee }} as booking_fee_in_local_currency,
vr.creation_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
where
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_protected is false
),
-- All cancelled bookings, excluding those with damage waiver protection or that
-- have been charged the rejected fee
cancelled_bookings as (
select
vr.id_verification,
{{ cancelled_rejected_fee }} as booking_fee_in_local_currency,
vr.cancelled_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
where
upper(vr.protection_type) <> '{{ damage_waiver }}'
and vr.is_cancelled = true
and upper(vr.verification_status) <> '{{ rejected_status }}'
and vr.is_protected is true
),
-- Damage Waiver bookings that have been approved or flagged, including cancelled
-- ones.
damage_waiver_bookings as (
select
vr.id_verification,
dw.booking_fee_in_local_currency,
vr.creation_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__damage_waiver dw
on vr.id_currency = dw.id_currency
and vr.protection_basic_amount_in_local_currency
= dw.protection_basic_amount_in_local_currency
where
upper(vr.protection_type) = '{{ damage_waiver }}'
and upper(vr.verification_status) <> '{{ rejected_status }}'
),
bookings_with_booking_fee as (
select *
from rejected_bookings
union all
select *
from cancelled_bookings
union all
select *
from damage_waiver_bookings
),
-- Basic Protection bookings that have been approved or flagged and are not
-- cancelled.
approved_or_flagged_basic_protection_bookings as (
select
vr.id_verification,
case
when vr.number_of_nights > {{ long_stay_booking_days_threshold }}
then bp.long_stay_fee_in_local_currency
else bp.short_stay_fee_in_local_currency
end as nightly_fee_in_local_currency,
vr.monthly_volume_discount_percentage * ad.is_monthly_volume_discount_active
+ vr.monthly_general_discount_percentage
* ad.is_monthly_general_discount_active as discount_percentage,
vr.checkout_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__basic_protection bp
on vr.id_currency = bp.id_currency
and vr.protection_basic_amount_in_local_currency
= bp.protection_basic_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where
upper(vr.protection_type) = '{{ basic_protection }}'
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_cancelled is false
and vr.is_protected is true
),
-- Screen & Protect bookings that have been approved or flagged and are not
-- cancelled.
approved_or_flagged_screen_and_protect_bookings as (
select
vr.id_verification,
case
when vr.number_of_nights > {{ long_stay_booking_days_threshold }}
then sp.long_stay_fee_in_local_currency
else sp.short_stay_fee_in_local_currency
end as nightly_fee_in_local_currency,
vr.monthly_volume_discount_percentage * ad.is_monthly_volume_discount_active
+ vr.monthly_general_discount_percentage
* ad.is_monthly_general_discount_active as discount_percentage,
vr.checkout_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__screen_and_protect sp
on vr.id_currency = sp.id_currency
and vr.protection_basic_amount_in_local_currency
= sp.protection_basic_amount_in_local_currency
and vr.protection_extended_amount_in_local_currency
= sp.protection_extended_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where
upper(vr.protection_type) = '{{ screen_and_protect }}'
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_cancelled is false
and vr.is_protected is true
),
-- Standalone Protection bookings that have been approved or flagged and are not
-- cancelled.
approved_or_flagged_standalone_protection_bookings as (
select
vr.id_verification,
case
when vr.number_of_nights > {{ long_stay_booking_days_threshold }}
then sp.long_stay_fee_in_local_currency
else sp.short_stay_fee_in_local_currency
end as nightly_fee_in_local_currency,
vr.monthly_volume_discount_percentage * ad.is_monthly_volume_discount_active
+ vr.monthly_general_discount_percentage
* ad.is_monthly_general_discount_active as discount_percentage,
vr.checkout_date_utc as invoice_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__standalone_protection sp
on vr.id_currency = sp.id_currency
and vr.protection_starting_amount_in_local_currency
= sp.protection_starting_amount_in_local_currency
and vr.protection_extended_amount_in_local_currency
= sp.protection_extended_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where
upper(vr.protection_type) = '{{ standalone_protection }}'
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_cancelled is false
and vr.is_protected is true
),
bookings_with_nightly_fee as (
select *
from approved_or_flagged_basic_protection_bookings
union all
select *
from approved_or_flagged_screen_and_protect_bookings
union all
select *
from approved_or_flagged_standalone_protection_bookings
) )
select select
vr.id_verification, vr.id_verification,
@ -80,63 +240,6 @@ select
vr.is_protected, vr.is_protected,
vr.protection_type, vr.protection_type,
vr.verification_status, vr.verification_status,
case
when
vr.number_of_nights > {{ long_stay_booking }}
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
bp.long_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
when
upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
bp.short_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
else 0
end as booking_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
or vr.is_protected is false
then {{ cancel_rejected_fee }}
else 0
end as cancel_or_rejected_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
then creation_date_utc
else checkout_date_utc
end as invoice_date_utc,
vr.id_currency, vr.id_currency,
vr.checkin_date_utc, vr.checkin_date_utc,
vr.checkout_date_utc, vr.checkout_date_utc,
@ -144,6 +247,20 @@ select
vr.is_cancelled, vr.is_cancelled,
vr.cancelled_at_utc, vr.cancelled_at_utc,
vr.cancelled_date_utc, vr.cancelled_date_utc,
bbf.booking_fee_in_local_currency,
bnf.nightly_fee_in_local_currency,
bnf.nightly_fee_in_local_currency
* vr.number_of_nights as total_fee_in_local_currency,
bnf.discount_percentage,
bnf.nightly_fee_in_local_currency
* vr.number_of_nights
* bnf.discount_percentage
/ 100 as discount_amount_in_local_currency,
bnf.nightly_fee_in_local_currency
* vr.number_of_nights
* (1 - bnf.discount_percentage)
/ 100 as fee_after_discount_in_local_currency,
coalesce(bbf.invoice_date_utc, bnf.invoice_date_utc) as invoice_date_utc,
vr.user_email, vr.user_email,
vr.company_name, vr.company_name,
vr.property_manager_name, vr.property_manager_name,
@ -162,219 +279,5 @@ select
vr.creation_date_utc, vr.creation_date_utc,
vr.cosmos_created_date_utc vr.cosmos_created_date_utc
from int_screen_and_protect__verification_requests vr from int_screen_and_protect__verification_requests vr
inner join left join bookings_with_booking_fee bbf on vr.id_verification = bbf.id_verification
stg_core__basic_protection bp left join bookings_with_nightly_fee bnf on vr.id_verification = bnf.id_verification
on vr.id_currency = bp.id_currency
and vr.protection_basic_amount_in_local_currency
= bp.protection_basic_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where upper(vr.protection_type) = '{{ basic_protection }}'
union all
select
vr.id_verification,
vr.id_booking,
vr.id_user_partner,
vr.id_accommodation,
vr.is_protected,
vr.protection_type,
vr.verification_status,
case
when
vr.number_of_nights > {{ long_stay_booking }}
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
sp.long_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
when
upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
sp.short_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
else 0
end as booking_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
or vr.is_protected is false
then {{ cancel_rejected_fee }}
else 0
end as cancel_or_rejected_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
then creation_date_utc
else checkout_date_utc
end as invoice_date_utc,
vr.id_currency,
vr.checkin_date_utc,
vr.checkout_date_utc,
vr.number_of_nights,
vr.is_cancelled,
vr.cancelled_at_utc,
vr.cancelled_date_utc,
vr.user_email,
vr.company_name,
vr.property_manager_name,
vr.property_manager_email,
vr.listing_name,
vr.listing_address,
vr.listing_town,
vr.listing_country,
vr.listing_postcode,
vr.pets_allowed,
vr.status_updated_at_utc,
vr.status_updated_date_utc,
vr.updated_at_utc,
vr.updated_date_utc,
vr.creation_at_utc,
vr.creation_date_utc,
vr.cosmos_created_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__screen_and_protect sp
on vr.id_currency = sp.id_currency
and vr.protection_basic_amount_in_local_currency
= sp.protection_basic_amount_in_local_currency
and vr.protection_extended_amount_in_local_currency
= sp.protection_extended_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where upper(vr.protection_type) = '{{ screen_and_protect }}'
union all
select
vr.id_verification,
vr.id_booking,
vr.id_user_partner,
vr.id_accommodation,
vr.is_protected,
vr.protection_type,
vr.verification_status,
case
when
vr.number_of_nights > {{ long_stay_booking }}
and upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
sp.long_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
when
upper(vr.verification_status) in {{ approved_flagged_status }}
and vr.is_protected is true
then
sp.short_stay_fee_in_local_currency
* vr.number_of_nights
* (
1
- vr.monthly_volume_discount
/ 100
* ad.is_monthly_volume_discount_active
)
* (
1
- vr.monthly_general_discount
/ 100
* ad.is_monthly_general_discount_active
+ vr.price_increase / 100 * ad.is_price_increase_active
)
else 0
end as booking_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
or vr.is_protected is false
then {{ cancel_rejected_fee }}
else 0
end as cancel_or_rejected_fee_in_local_currency,
case
when
upper(vr.verification_status) = '{{ rejected_status }}'
or vr.is_cancelled = true
then creation_date_utc
else checkout_date_utc
end as invoice_date_utc,
vr.id_currency,
vr.checkin_date_utc,
vr.checkout_date_utc,
vr.number_of_nights,
vr.is_cancelled,
vr.cancelled_at_utc,
vr.cancelled_date_utc,
vr.user_email,
vr.company_name,
vr.property_manager_name,
vr.property_manager_email,
vr.listing_name,
vr.listing_address,
vr.listing_town,
vr.listing_country,
vr.listing_postcode,
vr.pets_allowed,
vr.status_updated_at_utc,
vr.status_updated_date_utc,
vr.updated_at_utc,
vr.updated_date_utc,
vr.creation_at_utc,
vr.creation_date_utc,
vr.cosmos_created_date_utc
from int_screen_and_protect__verification_requests vr
inner join
stg_core__standalone_protection sp
on vr.id_currency = sp.id_currency
and vr.protection_starting_amount_in_local_currency
= sp.protection_starting_amount_in_local_currency
and vr.protection_extended_amount_in_local_currency
= sp.protection_extended_amount_in_local_currency
inner join
active_discounts ad
on vr.id_user_partner = ad.id_user_partner
and date_trunc('month', vr.checkout_date_utc) = ad.verification_month
where upper(vr.protection_type) = '{{ standalone_protection }}'

View file

@ -19,13 +19,13 @@ select
vr.pet_protection, vr.pet_protection,
vr.verification_status, vr.verification_status,
vr.verification_status_reason, vr.verification_status_reason,
spu.price_increase, spu.price_increase_percentage,
spu.price_increase_start_date_utc, spu.price_increase_start_date_utc,
spu.monthly_volume_discount, spu.monthly_volume_discount_percentage,
spu.threshold_approved_booking_volume, spu.threshold_approved_or_flagged_booking_volume,
spu.monthly_volume_discount_start_date_utc, spu.monthly_volume_discount_start_date_utc,
spu.monthly_volume_discount_end_date_utc, spu.monthly_volume_discount_end_date_utc,
spu.monthly_general_discount, spu.monthly_general_discount_percentage,
spu.monthly_general_discount_start_date_utc, spu.monthly_general_discount_start_date_utc,
spu.monthly_general_discount_end_date_utc, spu.monthly_general_discount_end_date_utc,
vr.email_flag, vr.email_flag,

View file

@ -113,7 +113,7 @@ models:
- "FLAGGED" - "FLAGGED"
- "REJECTED" - "REJECTED"
- name: price_increase - name: price_increase_percentage
data_type: numeric data_type: numeric
description: The percentage or value of the price increase description: The percentage or value of the price increase
applied to the user's account. applied to the user's account.
@ -129,7 +129,7 @@ models:
data_tests: data_tests:
- is_first_day_of_month - is_first_day_of_month
- name: monthly_volume_discount - name: monthly_volume_discount_percentage
data_type: numeric data_type: numeric
description: The discount percentage or value offered based on the description: The discount percentage or value offered based on the
volume of bookings achieved within a month. volume of bookings achieved within a month.
@ -140,7 +140,7 @@ models:
max_value: 100 max_value: 100
strictly: true strictly: true
- name: threshold_approved_booking_volume - name: threshold_approved_or_flagged_booking_volume
data_type: numeric data_type: numeric
description: The minimum number of bookings required to qualify for description: The minimum number of bookings required to qualify for
the monthly volume discount. the monthly volume discount.
@ -163,7 +163,7 @@ models:
data_tests: data_tests:
- is_last_day_of_month - is_last_day_of_month
- name: monthly_general_discount - name: monthly_general_discount_percentage
data_type: numeric data_type: numeric
description: The general discount percentage or value applied to all description: The general discount percentage or value applied to all
bookings within the applicable period. bookings within the applicable period.