cte for pre-defining if vat is applicable or not

This commit is contained in:
Pablo Martin 2024-09-10 16:23:02 +02:00
parent f58e97a3f2
commit 6136db8403

View file

@ -16,6 +16,35 @@ with
int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}),
stg_seed__guest_services_vat_rates_by_country as (
select * from {{ ref("stg_seed__guest_services_vat_rates_by_country") }}
),
vat_details as (
select
vtp.id_verification_to_payment,
vat.vat_rate,
case
when vpt.verification_payment_type in ('Waiver', 'Fee', 'CheckInCover')
then true
else false
end as is_service_subject_to_vat,
case
when
vpt.verification_payment_type
not in ('Waiver', 'Fee', 'CheckInCover')
then false
when vat.vat_rate = 0
then false
when vat.vat_rate < 1 and vat.vat_rate > 0
then true
end as is_vat_taxed
from stg_core__verification_to_payment vtp
left join
stg_core__verification_payment_type vpt
on vtp.id_verification_payment_type = vpt.id_verification_payment_type
left join int_core__unified_user uu on vtp.id_guest_user = uu.id_user
left join
stg_seed__guest_services_vat_rates_by_country vat
on uu.billing_country_iso_3 = vat.alpha_3
)
select
vtp.id_verification_to_payment,
@ -46,7 +75,7 @@ select
vpt.verification_payment_type,
p.currency,
p.amount as total_amount_in_txn_currency,
(p.amount * r.rate) as total_amount_in_in_gbp,
(p.amount * r.rate)::decimal(19, 4) as total_amount_in_in_gbp,
/*
Helping comment for logic below.
Given that guest payments are tax inclusive, the tax (column
@ -61,17 +90,24 @@ select
amount without tax = ( ------------- )
1 + VAT Rate
*/
(p.amount - (p.amount / (1 + vat.vat_rate))) as tax_amount_in_txn_currency,
(p.amount - (p.amount / (1 + vat.vat_rate))) * r.rate as tax_amount_in_gbp,
(p.amount / (1 + vat.vat_rate)) as amount_without_taxes_in_txn_currency,
(p.amount / (1 + vat.vat_rate)) * r.rate as amount_without_taxes_in_gbp,
vat.vat_rate as applicable_vat_tax_rate,
case
when vat.vat_rate = 0
then false
when vat.vat_rate < 1 and vat.vat_rate > 0
then true
end as is_vat_taxed,
(
(p.amount - (p.amount / (1 + vat.vat_rate)))
* vat.is_service_subject_to_vat::int
)::decimal(19, 4) as tax_amount_in_txn_currency,
(
(p.amount - (p.amount / (1 + vat.vat_rate)))
* vat.is_service_subject_to_vat::int
* r.rate
)::decimal(19, 4) as tax_amount_in_gbp,
(p.amount / (1 + vat.vat_rate))::decimal(
19, 4
) as amount_without_taxes_in_txn_currency,
((p.amount / (1 + vat.vat_rate)) * r.rate)::decimal(
19, 4
) as amount_without_taxes_in_gbp,
vat.vat_rate,
vat.is_service_subject_to_vat,
vat.is_vat_taxed,
ps.payment_status,
p.notes
from stg_core__verification_to_payment vtp
@ -89,7 +125,5 @@ left join
left join
stg_core__verification_request vr
on v.id_verification_request = vr.id_verification_request
left join int_core__unified_user uu on vtp.id_guest_user = uu.id_user
left join
stg_seed__guest_services_vat_rates_by_country vat
on uu.billing_country_iso_3 = vat.alpha_3
vat_details vat on vat.id_verification_to_payment = vtp.id_verification_to_payment