{% set ok_status = ("Approved", "Flagged") %} {% set rejected_status = "Rejected" %} {% set rejected_fee = 0.25 %} {% set cancellation_fee = 0.25 %} {% set cancellation_threshold = 0.05 %} with int_edeposit__verifications as ( select * from {{ ref("int_edeposit__verifications") }} ), stg_core__edeposit_users as (select * from {{ ref("stg_core__edeposit_users") }}), stg_xedotcom__exchange_rates as ( select * from {{ ref("stg_xedotcom__exchange_rates") }} ), edeposit_records as ( select v.id_verification, v.id_user_partner, v.id_booking, v.currency, er.rate, v.is_cancelled, v.creation_at_utc, case when v.verification_status in {{ ok_status }} then v.nightly_fee_local * v.number_nights else 0 end as ok_status_fee_in_txn_currency, case when v.verification_status in {{ ok_status }} then er.rate * v.nightly_fee_local * v.number_nights else 0 end as ok_status_fee_in_gbp, case when v.verification_status = '{{ rejected_status }}' then {{ rejected_fee }} else 0 end as rejected_fee_in_txn_currency, case when v.verification_status = '{{ rejected_status }}' then er.rate * {{ rejected_fee }} else 0 end as rejected_fee_in_gbp, to_char(v.checkout_at_utc, 'YYYY-MM') as year_month_checkout from int_edeposit__verifications v inner join stg_core__edeposit_users eu on v.id_user_partner = eu.id_user_partner inner join stg_xedotcom__exchange_rates er on er.from_currency = eu.currency and er.rate_date_utc = v.checkout_date_utc and er.to_currency = 'GBP' where version = 'V2' ), monthly_cancellation_threshold as ( select id_user_partner, year_month_checkout, case when sum(cast(is_cancelled as integer))::decimal / count(id_booking) >= {{ cancellation_threshold }} then true else false end as is_cancellation_threshold_surpassed from edeposit_records group by id_user_partner, year_month_checkout ) select id_verification, currency, ok_status_fee_in_txn_currency, ok_status_fee_in_gbp, rejected_fee_in_txn_currency, rejected_fee_in_gbp, case when ct.is_cancellation_threshold_surpassed is true then {{ cancellation_fee }} else 0 end as cancelled_fee_in_txn_currency, case when ct.is_cancellation_threshold_surpassed is true then rate * {{ cancellation_fee }} else 0 end as cancelled_fee_in_gbp from edeposit_records er left join monthly_cancellation_threshold ct on ( er.id_user_partner = ct.id_user_partner and er.year_month_checkout = ct.year_month_checkout )