data-dwh-dbt-project/models/intermediate/xero/int_xero__bank_transactions.sql

50 lines
1.7 KiB
MySQL
Raw Normal View History

2024-06-25 11:38:37 +02:00
with
stg_xero__bank_transactions as (
select * from {{ ref("stg_xero__bank_transactions") }}
2024-06-25 11:56:51 +02:00
),
int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }})
2024-06-25 11:38:37 +02:00
select
2024-06-25 11:44:00 +02:00
bt.id_bank_transaction,
bt.id_prepayment,
bt.id_overpayment,
2024-06-25 12:00:50 +02:00
bt.full_contact_details ->> 'ContactID' as id_contact,
2024-06-25 15:06:43 +02:00
bt.bank_account ->> 'BankAccountID' as id_bank_account,
2024-06-25 11:44:00 +02:00
bt.reference,
bt.transaction_type,
2024-07-01 11:44:55 +02:00
bt.transaction_sign,
2024-06-25 11:44:00 +02:00
bt.transaction_status,
bt.transaction_at_utc,
2024-06-25 11:56:51 +02:00
bt.transaction_date_utc,
2024-06-25 11:44:00 +02:00
bt.date_string,
bt.total_amount_local_curr,
2024-06-25 11:56:51 +02:00
(bt.total_amount_local_curr * coalesce(bt.exchange_rate_to_gbp, ser.rate))::numeric(
2024-06-25 11:41:03 +02:00
18, 4
) as total_amount_in_gbp,
2024-06-25 11:44:00 +02:00
bt.total_amount_wo_tax_local_curr,
2024-06-25 11:56:51 +02:00
(
bt.total_amount_wo_tax_local_curr * coalesce(bt.exchange_rate_to_gbp, ser.rate)
)::numeric(18, 4) as total_amount_wo_tax_in_gbp,
2024-06-25 11:44:00 +02:00
bt.total_tax_local_curr,
2024-06-25 11:56:51 +02:00
(bt.total_tax_local_curr * coalesce(bt.exchange_rate_to_gbp, ser.rate))::numeric(
2024-06-25 11:44:00 +02:00
18, 4
) as total_tax_in_gbp,
bt.transaction_currency_iso_4217,
2024-06-25 11:56:51 +02:00
-- We try to use Xero's rate. If no rate is available, we fall back to the
-- oficial dwh rate.
coalesce(bt.exchange_rate_to_gbp, ser.rate) as exchange_rate_to_gbp,
2024-06-25 11:44:00 +02:00
bt.line_amount_tax_inclusiveness,
bt.line_items,
2024-06-25 11:44:00 +02:00
bt.is_reconciled,
bt.has_attachments,
bt.url,
bt.external_link_provider_name,
bt.updated_at_utc,
bt.updated_date_utc,
bt.dwh_extracted_at_utc
from stg_xero__bank_transactions bt
2024-06-25 11:56:51 +02:00
left join
int_simple_exchange_rates ser
on bt.transaction_currency_iso_4217 = ser.from_currency
and ser.to_currency = 'GBP'
and bt.transaction_date_utc = ser.rate_date_utc