Merged PR 5063: refactor xero__net_fees_by_deal

# Description

Refactor `xero__net_fees_by_deal`
Changed it so the model reads directly from `xero__credit_note_line_items`, included some fields in this model and added test to schema.
Run the macro to verify both models matched.
There were some decimal approximation differences, verified this by casting to decimal with fewer decimal and they matched 100%.
Next step would be to exclude Resolution Payments from this model, which I will do in the next commit.

# Checklist

- [x] The edited models and dependants run properly with production data.
- [x] The edited models are sufficiently documented.
- [x] The edited models contain PK tests, and I've ran and passed them.
- [ ] I have checked for DRY opportunities with other models and docs.
- [ ] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

refactor xero__net_fees_by_deal

Related work items: #29373
This commit is contained in:
Joaquin Ossa 2025-04-25 12:03:14 +00:00
commit 2352716dae
5 changed files with 54 additions and 9 deletions

View file

@ -1,7 +1,11 @@
with int_xero__credit_notes as (select * from {{ ref("int_xero__credit_notes") }}),
with
int_xero__credit_notes as (select * from {{ ref("int_xero__credit_notes") }}),
stg_xero__accounts as (select * from {{ ref("stg_xero__accounts") }})
select
cn.id_credit_note,
cn.credit_note_issued_at_utc,
cn.credit_note_status,
cn.id_contact,
j.id_line_item,
j.id_item,
j.item_code,
@ -73,4 +77,4 @@ cross join
(jsonb_array_elements(cn.line_items) ->> 'AccountID') as id_account,
(jsonb_array_elements(cn.line_items) ->> 'AccountCode') as account_code
) j
left join stg_xero__accounts a on j.account_code = a.account_code
left join stg_xero__accounts a on j.account_code = a.account_code

View file

@ -661,6 +661,32 @@ models:
data_type: character varying
description: Xero's unique identifier for the credit note.
- name: credit_note_issued_at_utc
data_type: timestamp with time zone
description: Date on which the credit note was issued.
- name: credit_note_status
data_type: character varying
description: |
The status of the credit note.
Can be one of: PAID, VOIDED, DRAFT, DELETED, AUTHORISED, SUBMITTED.
data_tests:
- not_null
- accepted_values:
values:
- PAID
- VOIDED
- DRAFT
- DELETED
- AUTHORISED
- SUBMITTED
- name: id_contact
data_type: text
description: |
Xero's unique id for the contact related to this transaction.
- name: id_line_item
data_type: text
description: Xero's unique identifier for the line item.

View file

@ -5,6 +5,9 @@ with
select
id_credit_note as id_credit_note,
credit_note_issued_at_utc as credit_note_issued_at_utc,
credit_note_status as credit_note_status,
id_contact as id_contact,
id_line_item as id_line_item,
id_item as id_item,
item_code as item_code,

View file

@ -2,8 +2,13 @@
with
xero__invoices as (select * from {{ ref("xero__invoices") }}),
xero__credit_notes as (select * from {{ ref("xero__credit_notes") }}),
xero__credit_note_line_items as (
select * from {{ ref("xero__credit_note_line_items") }}
),
xero__contacts as (select * from {{ ref("xero__contacts") }}),
stg_seed__accounting_aggregations as (
select * from {{ ref("stg_seed__accounting_aggregations") }}
),
fees_invoiced as (
select
cast(
@ -19,14 +24,18 @@ with
fees_credited as (
select
cast(
date_trunc('month', cn.credit_note_issued_at_utc) as date
date_trunc('month', cnli.credit_note_issued_at_utc) as date
) as credit_note_issued_year_month,
c.id_deal,
sum(cn.subtotal_in_gbp) as fees_credited
from xero__credit_notes cn
left join xero__contacts c on cn.id_contact = c.id_contact
where cn.credit_note_status in {{ relevant_document_statuses }}
group by date_trunc('month', cn.credit_note_issued_at_utc), c.id_deal
sum(cnli.line_amount_wo_taxes_in_gbp) as fees_credited
from xero__credit_note_line_items cnli
left join xero__contacts c on cnli.id_contact = c.id_contact
left join
stg_seed__accounting_aggregations aa
on aa.account_code = cnli.account_code
and aa.root_aggregation <> 'Host Resolutions Payments'
where cnli.credit_note_status in {{ relevant_document_statuses }}
group by date_trunc('month', cnli.credit_note_issued_at_utc), c.id_deal
)
select
coalesce(

View file

@ -38,3 +38,6 @@ account_code,root_aggregation,kpis_aggregation,financial_l1_aggregation,financia
"239",Resolution Process for Deposit Management Services,Accounting Resolutions,2-Deposit Management,21-Deposit Management Services,215-Resolution Process for Deposit Management Services
"240",Screen and Protect API,Invoiced API Revenue,1-Guest Screening and Protection,18-Screen and Protect API,181-Screen and Protect API
"301",Damage Host-Waiver Payments,Damage Host-Waiver Payments,5-Damage Host-Waiver Payments,51-Damage Host-Waiver Payments,511-Damage Host-Waiver Payments
"316",Host Resolutions Payments,Host Resolutions Payments,4-Mediation and Resolution,42-Host Resolutions,421-Host Resolutions
"323",Host Resolutions Payments,Host Resolutions Payments,4-Mediation and Resolution,43-E-Deposit Resolutions,431-E-Deposit Resolutions
"324",Host Resolutions Payments,Host Resolutions Payments,4-Mediation and Resolution,44-Check In Hero Resolutions,441-Check In Hero Resolutions
1 account_code root_aggregation kpis_aggregation financial_l1_aggregation financial_l2_aggregation financial_l3_aggregation
38 239 Resolution Process for Deposit Management Services Accounting Resolutions 2-Deposit Management 21-Deposit Management Services 215-Resolution Process for Deposit Management Services
39 240 Screen and Protect API Invoiced API Revenue 1-Guest Screening and Protection 18-Screen and Protect API 181-Screen and Protect API
40 301 Damage Host-Waiver Payments Damage Host-Waiver Payments 5-Damage Host-Waiver Payments 51-Damage Host-Waiver Payments 511-Damage Host-Waiver Payments
41 316 Host Resolutions Payments Host Resolutions Payments 4-Mediation and Resolution 42-Host Resolutions 421-Host Resolutions
42 323 Host Resolutions Payments Host Resolutions Payments 4-Mediation and Resolution 43-E-Deposit Resolutions 431-E-Deposit Resolutions
43 324 Host Resolutions Payments Host Resolutions Payments 4-Mediation and Resolution 44-Check In Hero Resolutions 441-Check In Hero Resolutions