Changed logic to verification_payments
This commit is contained in:
parent
369608af05
commit
2f1bf584c4
1 changed files with 7 additions and 113 deletions
|
|
@ -4,123 +4,18 @@ with
|
|||
),
|
||||
int_core__user_host as (select * from {{ ref("int_core__user_host") }}),
|
||||
int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}),
|
||||
int_core__payaway as (select * from {{ ref("int_core__payaway") }}),
|
||||
int_core__bookings as (select * from {{ ref("int_core__bookings") }}),
|
||||
int_core__accommodation as (select * from {{ ref("int_core__accommodation") }}),
|
||||
int_daily_currency_exchange_rates as (
|
||||
select * from {{ ref("int_daily_currency_exchange_rates") }}
|
||||
),
|
||||
stg_seed__guest_services_vat_rates_by_country as (
|
||||
select * from {{ ref("stg_seed__guest_services_vat_rates_by_country") }}
|
||||
),
|
||||
-- CTE to obtain amount due to host and superhog fee.
|
||||
-- This depends on their payaway plan at the moment of the payment.
|
||||
-- If they don't have a payaway plan, then superhog is taking the risk.
|
||||
-- For more details on the calculation, please refer to the documentation.
|
||||
-- https://www.notion.so/truvi/Guest-Services-Taxes-How-to-calculate-a5ab4c049d61427fafab669dbbffb3a2?pvs=4
|
||||
payaways as (
|
||||
select
|
||||
vp.id_payment,
|
||||
pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr
|
||||
* cer.rate as payaway_minimum_commission_local_curr,
|
||||
case
|
||||
when pa.id_payaway_plan is not null then true else false
|
||||
end as is_host_taking_waiver_risk,
|
||||
case
|
||||
when pa.id_payaway_plan is not null
|
||||
then
|
||||
vp.total_amount_in_txn_currency - greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
else 0
|
||||
end as amount_due_to_host_in_txn_currency,
|
||||
case
|
||||
when pa.id_payaway_plan is not null
|
||||
then
|
||||
(
|
||||
vp.total_amount_in_txn_currency - greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
)
|
||||
* der.rate
|
||||
else 0
|
||||
end as amount_due_to_host_in_gbp,
|
||||
case
|
||||
when pa.id_payaway_plan is not null and uh.billing_country_iso_3 = 'GBR'
|
||||
then
|
||||
(
|
||||
greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
)
|
||||
/ (vat.vat_rate + 1)
|
||||
when
|
||||
pa.id_payaway_plan is not null and uh.billing_country_iso_3 <> 'GBR'
|
||||
then
|
||||
greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
else vp.amount_without_taxes_in_txn_currency
|
||||
end as superhog_fee_in_txn_currency,
|
||||
case
|
||||
when pa.id_payaway_plan is not null and uh.billing_country_iso_3 = 'GBR'
|
||||
then
|
||||
(
|
||||
greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
)
|
||||
/ (vat.vat_rate + 1)
|
||||
* der.rate
|
||||
when
|
||||
pa.id_payaway_plan is not null and uh.billing_country_iso_3 <> 'GBR'
|
||||
then
|
||||
(
|
||||
greatest(
|
||||
vp.total_amount_in_txn_currency * pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr * cer.rate
|
||||
)
|
||||
)
|
||||
* der.rate
|
||||
else vp.amount_without_taxes_in_txn_currency
|
||||
end as superhog_fee_in_gbp
|
||||
from int_core__verification_payments vp
|
||||
left join
|
||||
int_core__payaway pa
|
||||
on vp.id_user_host = pa.id_user_host
|
||||
and vp.payment_paid_at_utc
|
||||
between pa.start_at_utc and coalesce(pa.end_at_utc, '2050-12-31')
|
||||
left join
|
||||
int_daily_currency_exchange_rates der
|
||||
on vp.payment_paid_date_utc = der.rate_date_utc
|
||||
and der.from_currency = vp.currency
|
||||
and der.to_currency = 'GBP'
|
||||
left join int_core__user_host uh on vp.id_user_host = uh.id_user_host
|
||||
-- We need to exchange the minimum payaway commissions from host currency
|
||||
-- to currency used by guest in the payment.
|
||||
left join
|
||||
int_daily_currency_exchange_rates cer
|
||||
on vp.payment_paid_date_utc = cer.rate_date_utc
|
||||
and cer.from_currency = uh.account_currency_iso4217
|
||||
and cer.to_currency = vp.currency
|
||||
left join
|
||||
stg_seed__guest_services_vat_rates_by_country vat
|
||||
on uh.billing_country_iso_3 = vat.alpha_3
|
||||
where upper(vp.verification_payment_type) = 'WAIVER'
|
||||
)
|
||||
select
|
||||
vp.id_payment,
|
||||
vp.payment_reference,
|
||||
vp.verification_payment_type,
|
||||
pa.is_host_taking_waiver_risk,
|
||||
pa.payaway_percentage,
|
||||
pa.payaway_minimum_commission_local_curr,
|
||||
vp.is_host_taking_waiver_risk,
|
||||
vp.payaway_percentage,
|
||||
vp.payaway_minimum_commission_local_curr,
|
||||
vp.payment_status,
|
||||
vp.payment_due_date_utc,
|
||||
vp.payment_paid_date_utc,
|
||||
|
|
@ -133,10 +28,10 @@ select
|
|||
vp.amount_without_taxes_in_gbp,
|
||||
vp.tax_amount_in_txn_currency,
|
||||
vp.tax_amount_in_gbp,
|
||||
pa.amount_due_to_host_in_txn_currency,
|
||||
pa.amount_due_to_host_in_gbp,
|
||||
pa.superhog_fee_in_txn_currency,
|
||||
pa.superhog_fee_in_gbp,
|
||||
vp.amount_due_to_host_in_txn_currency,
|
||||
vp.amount_due_to_host_in_gbp,
|
||||
vp.superhog_fee_in_txn_currency,
|
||||
vp.superhog_fee_in_gbp,
|
||||
vp.currency,
|
||||
der.rate as exchange_rate_to_gbp,
|
||||
uh.id_user_host,
|
||||
|
|
@ -158,7 +53,6 @@ select
|
|||
uh.account_currency_iso4217 as host_currency
|
||||
from int_core__verification_payments vp
|
||||
left join int_core__user_host uh on vp.id_user_host = uh.id_user_host
|
||||
left join payaways pa on vp.id_payment = pa.id_payment
|
||||
left join int_core__bookings b on vp.id_verification_request = b.id_verification_request
|
||||
left join int_core__unified_user uu on uu.id_user = b.id_user_guest
|
||||
left join int_core__accommodation a on b.id_accommodation = a.id_accommodation
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue