diff --git a/models/intermediate/core/int_core__guest_product_payments_v2.sql b/models/intermediate/core/int_core__guest_product_payments_v2.sql new file mode 100644 index 0000000..9fd6cae --- /dev/null +++ b/models/intermediate/core/int_core__guest_product_payments_v2.sql @@ -0,0 +1,143 @@ +{% set check_in_cover_as_id_verification_payment = "5" %} +{% set cut_off_date_check_in_cover_as_id_verification_payment = "'2025-06-08'" %} + +{{ config(materialized="table") }} +with + stg_core__verification_to_payment as ( + select * + from {{ ref("stg_core__verification_to_payment") }} + where + created_date_utc + <= date({{ cut_off_date_check_in_cover_as_id_verification_payment }}) + and id_verification_payment_type + = {{ check_in_cover_as_id_verification_payment }} + ), + stg_core__verification_payment_type as ( + select * from {{ ref("stg_core__verification_payment_type") }} + ), + stg_core__verification as ( + select * + from {{ ref("stg_core__verification") }} + where + created_date_utc + <= date({{ cut_off_date_check_in_cover_as_id_verification_payment }}) + ), + stg_core__verification_request as ( + select * from {{ ref("stg_core__verification_request") }} + ), + stg_core__payment as (select * from {{ ref("stg_core__payment") }}), + stg_core__payment_status as (select * from {{ ref("stg_core__payment_status") }}), + int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}), + stg_core__verification_request_guest_product_to_payment as ( + select * + from {{ ref("stg_core__verification_request_guest_product_to_payment") }} + ), + stg_core__verification_request_to_guest_product as ( + select * from {{ ref("stg_core__verification_request_to_guest_product") }} + ), + int_core__guest_products as (select * from {{ ref("int_core__guest_products") }}), + guest_product_payments_from_verification_flow as ( + select + -- Tags Guest Product Payments that come from the legacy flow, namely + -- CheckInCover from Verification To Payment. + 'leg_' + || cast(vtp.id_verification_to_payment as text) as id_guest_product_payment, + vtp.id_payment, + vtp.created_at_utc, + vtp.updated_at_utc, + vtp.payment_due_at_utc, + vtp.payment_due_date_utc, + p.paid_at_utc as payment_paid_at_utc, + p.paid_date_utc as payment_paid_date_utc, + p.payment_reference, + vtp.refund_due_at_utc, + vtp.refund_due_date_utc, + p.refunded_at_utc as payment_refunded_at_utc, + p.refunded_date_utc as payment_refunded_date_utc, + p.refund_payment_reference, + vr.id_user_host, + vtp.id_guest_user as id_user_guest, + v.id_verification_request, + upper(vpt.verification_payment_type) as guest_product_name, + 'VERIFICATION_PAYMENT' as guest_product_payment_source, + p.currency, + p.amount as total_amount_in_txn_currency, + p.amount * r.rate as total_amount_in_gbp, + upper(coalesce(ps.payment_status, 'UNKNOWN')) as payment_status, + p.notes + from stg_core__verification_to_payment vtp + left join stg_core__verification v on vtp.id_verification = v.id_verification + inner join + stg_core__verification_payment_type vpt + on vtp.id_verification_payment_type = vpt.id_verification_payment_type + left join stg_core__payment p on vtp.id_payment = p.id_payment + inner join + stg_core__payment_status ps on p.id_payment_status = ps.id_payment_status + inner join + int_simple_exchange_rates r + on vtp.payment_due_date_utc = r.rate_date_utc + and p.currency = r.from_currency + and r.to_currency = 'GBP' + left join + stg_core__verification_request vr + on v.id_verification_request = vr.id_verification_request + ), + guest_product_payments_from_guest_product_flow as ( + select + cast( + vrgptp.id_verification_request_guest_product_to_payment as text + ) as id_guest_product_payment, + vrgptp.id_payment, + vrgptp.created_at_utc, + vrgptp.updated_at_utc, + -- For the new Guest Product flow, there's no concept of Due Date, as we + -- take the payment up front and if the payment fails, then they aren't + -- able to complete the Journey. + p.paid_at_utc as payment_due_at_utc, + p.paid_date_utc as payment_due_date_utc, + p.paid_at_utc as payment_paid_at_utc, + p.paid_date_utc as payment_paid_date_utc, + p.payment_reference, + -- Same comment regarding Due Date + At the moment, Guest Products are not + -- refundable, but these could be in the future + p.refunded_at_utc as refund_due_at_utc, + p.refunded_date_utc as refund_due_date_utc, + p.refunded_at_utc as payment_refunded_at_utc, + p.refunded_date_utc as payment_refunded_date_utc, + p.refund_payment_reference, + vr.id_user_host as id_user_host, + vr.id_user_guest as id_user_guest, + vrtgp.id_verification_request, + gp.guest_product_name, + 'GUEST_PRODUCT_PAYMENT' as guest_product_payment_source, + p.currency, + p.amount as total_amount_in_txn_currency, + p.amount * r.rate as total_amount_in_gbp, + upper(coalesce(ps.payment_status, 'UNKNOWN')) as payment_status, + p.notes + from stg_core__verification_request_guest_product_to_payment vrgptp + left join stg_core__payment p on vrgptp.id_payment = p.id_payment + inner join + stg_core__payment_status ps on p.id_payment_status = ps.id_payment_status + inner join + int_simple_exchange_rates r + -- Note that the following conversion is made on Paid date, not Due Date, + -- as Due Date does not make sense for Guest Products + on p.paid_date_utc = r.rate_date_utc + and p.currency = r.from_currency + and r.to_currency = 'GBP' + left join + stg_core__verification_request_to_guest_product vrtgp + on vrgptp.id_verification_request_to_guest_product + = vrtgp.id_verification_request_to_guest_product + left join + int_core__guest_products gp on vrtgp.id_guest_product = gp.id_guest_product + left join + stg_core__verification_request vr + on vrtgp.id_verification_request = vr.id_verification_request + ) +select * +from guest_product_payments_from_verification_flow +union all +select * +from guest_product_payments_from_guest_product_flow diff --git a/models/intermediate/core/schema.yml b/models/intermediate/core/schema.yml index 4beaba5..ce0b1db 100644 --- a/models/intermediate/core/schema.yml +++ b/models/intermediate/core/schema.yml @@ -5798,6 +5798,7 @@ models: - not_null - name: int_core__guest_product_payments + latest_version: 1 description: | A model that holds guest products payments with details around when they happen, what service was being paid, what was the related @@ -5809,6 +5810,12 @@ models: (!) At this moment, this model only includes Check In Cover payments made as Verification Payments. + versions: + - v: 1 + - v: 2 + columns: + - include: all + columns: - name: id_guest_product_payment data_type: text