refactor xero__net_fees_by_deal

This commit is contained in:
Joaquin 2025-04-25 11:55:14 +02:00
parent 0ffcfca6a8
commit 423457126f
4 changed files with 44 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") }}) stg_xero__accounts as (select * from {{ ref("stg_xero__accounts") }})
select select
cn.id_credit_note, cn.id_credit_note,
cn.credit_note_issued_at_utc,
cn.credit_note_status,
cn.id_contact,
j.id_line_item, j.id_line_item,
j.id_item, j.id_item,
j.item_code, 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) ->> 'AccountID') as id_account,
(jsonb_array_elements(cn.line_items) ->> 'AccountCode') as account_code (jsonb_array_elements(cn.line_items) ->> 'AccountCode') as account_code
) j ) 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 data_type: character varying
description: Xero's unique identifier for the credit note. 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 - name: id_line_item
data_type: text data_type: text
description: Xero's unique identifier for the line item. description: Xero's unique identifier for the line item.

View file

@ -5,6 +5,9 @@ with
select select
id_credit_note as id_credit_note, 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_line_item as id_line_item,
id_item as id_item, id_item as id_item,
item_code as item_code, item_code as item_code,

View file

@ -2,7 +2,9 @@
with with
xero__invoices as (select * from {{ ref("xero__invoices") }}), 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") }}), xero__contacts as (select * from {{ ref("xero__contacts") }}),
fees_invoiced as ( fees_invoiced as (
select select
@ -19,14 +21,14 @@ with
fees_credited as ( fees_credited as (
select select
cast( 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, ) as credit_note_issued_year_month,
c.id_deal, c.id_deal,
sum(cn.subtotal_in_gbp) as fees_credited sum(cnli.line_amount_wo_taxes_in_gbp) as fees_credited
from xero__credit_notes cn from xero__credit_note_line_items cnli
left join xero__contacts c on cn.id_contact = c.id_contact left join xero__contacts c on cnli.id_contact = c.id_contact
where cn.credit_note_status in {{ relevant_document_statuses }} where cnli.credit_note_status in {{ relevant_document_statuses }}
group by date_trunc('month', cn.credit_note_issued_at_utc), c.id_deal group by date_trunc('month', cnli.credit_note_issued_at_utc), c.id_deal
) )
select select
coalesce( coalesce(