2024-06-25 14:07:45 +02:00
|
|
|
with
|
|
|
|
|
int_xero__bank_transactions as (
|
|
|
|
|
select * from {{ ref("int_xero__bank_transactions") }}
|
2024-06-25 14:47:11 +02:00
|
|
|
),
|
|
|
|
|
stg_xero__accounts as (select * from {{ ref("stg_xero__accounts") }})
|
2024-06-25 14:07:45 +02:00
|
|
|
|
|
|
|
|
select
|
|
|
|
|
bt.id_bank_transaction,
|
|
|
|
|
j.id_line_item,
|
2024-06-25 14:47:11 +02:00
|
|
|
j.line_description,
|
|
|
|
|
j.id_account,
|
|
|
|
|
j.account_code,
|
|
|
|
|
a.account_name,
|
|
|
|
|
j.item_code,
|
2024-06-25 14:07:45 +02:00
|
|
|
j.quantity::numeric,
|
|
|
|
|
j.unit_amount::numeric,
|
|
|
|
|
j.line_amount_local_curr::numeric,
|
|
|
|
|
(j.line_amount_local_curr::numeric * bt.exchange_rate_to_gbp)::numeric(
|
|
|
|
|
18, 4
|
|
|
|
|
) as line_amount_in_gbp,
|
|
|
|
|
case
|
|
|
|
|
when bt.line_amount_tax_inclusiveness = 'Inclusive'
|
|
|
|
|
then j.line_amount_local_curr::numeric - j.tax_amount_local_curr::numeric
|
|
|
|
|
when bt.line_amount_tax_inclusiveness = 'Exclusive'
|
|
|
|
|
then j.line_amount_local_curr::numeric
|
|
|
|
|
when bt.line_amount_tax_inclusiveness = 'NoTax'
|
|
|
|
|
then j.line_amount_local_curr::numeric
|
|
|
|
|
else null
|
|
|
|
|
end as line_amount_wo_taxes_local_curr,
|
|
|
|
|
case
|
|
|
|
|
when bt.line_amount_tax_inclusiveness = 'Inclusive'
|
|
|
|
|
then
|
2024-06-25 16:24:39 +02:00
|
|
|
(
|
|
|
|
|
(j.line_amount_local_curr::numeric - j.tax_amount_local_curr::numeric)
|
|
|
|
|
* bt.exchange_rate_to_gbp
|
|
|
|
|
)::numeric(18, 4)
|
2024-06-25 14:07:45 +02:00
|
|
|
when bt.line_amount_tax_inclusiveness = 'Exclusive'
|
|
|
|
|
then
|
|
|
|
|
(j.line_amount_local_curr::numeric * bt.exchange_rate_to_gbp)::numeric(
|
|
|
|
|
18, 4
|
|
|
|
|
)
|
|
|
|
|
when bt.line_amount_tax_inclusiveness = 'NoTax'
|
|
|
|
|
then
|
|
|
|
|
(j.line_amount_local_curr::numeric * bt.exchange_rate_to_gbp)::numeric(
|
|
|
|
|
18, 4
|
|
|
|
|
)
|
|
|
|
|
else null
|
|
|
|
|
end as line_amount_wo_taxes_in_gbp,
|
|
|
|
|
j.tax_amount_local_curr::numeric,
|
|
|
|
|
(j.tax_amount_local_curr::numeric * bt.exchange_rate_to_gbp)::numeric(
|
|
|
|
|
18, 4
|
|
|
|
|
) as tax_amount_in_gbp,
|
|
|
|
|
j.tax_type,
|
2024-06-25 14:47:11 +02:00
|
|
|
bt.transaction_currency_iso_4217
|
2024-06-25 14:07:45 +02:00
|
|
|
from int_xero__bank_transactions bt
|
|
|
|
|
cross join
|
|
|
|
|
lateral(
|
|
|
|
|
select
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'LineItemID') as id_line_item,
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'AccountID') as id_account,
|
|
|
|
|
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'Quantity') as quantity,
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'UnitAmount') as unit_amount,
|
|
|
|
|
(
|
|
|
|
|
jsonb_array_elements(bt.line_items) ->> 'LineAmount'
|
|
|
|
|
) as line_amount_local_curr,
|
|
|
|
|
(
|
|
|
|
|
jsonb_array_elements(bt.line_items) ->> 'TaxAmount'
|
|
|
|
|
) as tax_amount_local_curr,
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'TaxType') as tax_type,
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'Description') as line_description,
|
|
|
|
|
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'AccountCode') as account_code,
|
|
|
|
|
(jsonb_array_elements(bt.line_items) ->> 'ItemCode') as item_code
|
|
|
|
|
) j
|
2024-06-25 14:47:11 +02:00
|
|
|
left join stg_xero__accounts a on j.account_code = a.account_code
|