From de4ab5b6e5f14ce8375de42f50939dd10d7673b0 Mon Sep 17 00:00:00 2001 From: uri Date: Wed, 29 May 2024 18:03:58 +0200 Subject: [PATCH 1/4] Created xero__net_fees_by_deal with guesty --- .../reporting/xero/xero__net_fees_by_deal.sql | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 models/reporting/xero/xero__net_fees_by_deal.sql diff --git a/models/reporting/xero/xero__net_fees_by_deal.sql b/models/reporting/xero/xero__net_fees_by_deal.sql new file mode 100644 index 0000000..33b56ef --- /dev/null +++ b/models/reporting/xero/xero__net_fees_by_deal.sql @@ -0,0 +1,56 @@ +{% set relevant_document_statuses = "('PAID', 'AUTHORISED')" %} +{% set guesty_id_deal = "('17814677813')" %} + +with + xero__invoices as (select * from {{ ref("xero__invoices") }}), + xero__credit_notes as (select * from {{ ref("xero__credit_notes") }}), + xero__contacts as (select * from {{ ref("xero__contacts") }}), + fees_invoiced as ( + select + cast( + date_trunc('month', i.invoice_issued_date_utc) as date + ) as invoice_issued_year_month, + case + when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null + end as fee_deal, + sum(i.total_amount_wo_tax_in_gbp) as fees_invoiced + from xero__invoices i + left join xero__contacts c on i.id_contact = c.id_contact + where + i.invoice_status in {{ relevant_document_statuses }} + and (c.id_deal in {{ guesty_id_deal }}) + group by + date_trunc('month', i.invoice_issued_date_utc), + case when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null end + ), + fees_credited as ( + select + cast( + date_trunc('month', cn.credit_note_issued_at_utc) as date + ) as credit_note_issued_year_month, + case + when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null + end as fee_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 }} + and (c.id_deal in {{ guesty_id_deal }}) + group by + date_trunc('month', cn.credit_note_issued_at_utc), + case when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null end + ) +select + coalesce( + i.invoice_issued_year_month, c.credit_note_issued_year_month + ) as issued_year_month, + coalesce(i.fee_deal, c.fee_deal) as fee_deal, + coalesce(i.fees_invoiced, 0) as fees_invoiced_in_gbp, + coalesce(c.fees_credited, 0) as fees_credited_in_gbp, + (coalesce(i.fees_invoiced, 0) - coalesce(c.fees_credited, 0)) as net_fees_in_gbp +from fees_invoiced i +full outer join + fees_credited c + on i.invoice_issued_year_month = c.credit_note_issued_year_month + and i.fee_deal = c.fee_deal From bf0010ceebb1cc0adae308ded0d9add88ec0f930 Mon Sep 17 00:00:00 2001 From: uri Date: Thu, 30 May 2024 09:15:53 +0200 Subject: [PATCH 2/4] Adding model xero__net_fees_by_deal documentation in schema.yaml. Added Waiver Fees in xero__net_fees documentation. --- models/reporting/xero/schema.yaml | 44 ++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/models/reporting/xero/schema.yaml b/models/reporting/xero/schema.yaml index a0d92c0..36fb9e0 100644 --- a/models/reporting/xero/schema.yaml +++ b/models/reporting/xero/schema.yaml @@ -1139,7 +1139,49 @@ models: data_type: text description: | A fee category, one of out of booking_fees, listing_fees, - verification_fees. + verification_fees, waiver fees. + + - name: fees_invoiced_in_gbp + data_type: numeric + description: The total sum of invoiced fees. + + - name: fees_credited_in_gbp + data_type: numeric + description: The total sum of credited fees. + + - name: net_fees_in_gbp + data_type: numeric + description: | + Net fees are calculated by subtracting credited fees from invoiced + fees. It might be the case that net fees are negative in some month, + if it happens to be that we have given back to customers more than we + have invoiced them. + + - name: xero__net_fees_by_deal + description: | + A summary aggregation table showing the total raw and net fees by month + and year, dedicated for deal attribution. + + Information comes from both Invoices and Credit notes. + + Only documents in statuses ('PAID', 'AUTHORISED') are kept. + + Net fees get computed by subtracting credited amounts from invoiced + amounts. + + columns: + - name: issued_year + data_type: numeric + description: The issuing year of the aggregated documents. + + - name: issued_month + data_type: numeric + description: The issuing month of the aggregated documents. + + - name: fee_deal + data_type: text + description: | + A fee category per deal, at the moment only considering guesty fees. - name: fees_invoiced_in_gbp data_type: numeric From e360c1c785c722b06afe9d977951079026914c8d Mon Sep 17 00:00:00 2001 From: uri Date: Thu, 30 May 2024 10:00:39 +0200 Subject: [PATCH 3/4] Setting xero__net_fees_by_deal as per deal id, not dedicated to guesty --- models/reporting/xero/schema.yaml | 2 +- .../reporting/xero/xero__net_fees_by_deal.sql | 29 +++++-------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/models/reporting/xero/schema.yaml b/models/reporting/xero/schema.yaml index 36fb9e0..c20cf36 100644 --- a/models/reporting/xero/schema.yaml +++ b/models/reporting/xero/schema.yaml @@ -1181,7 +1181,7 @@ models: - name: fee_deal data_type: text description: | - A fee category per deal, at the moment only considering guesty fees. + A fee category per deal. - name: fees_invoiced_in_gbp data_type: numeric diff --git a/models/reporting/xero/xero__net_fees_by_deal.sql b/models/reporting/xero/xero__net_fees_by_deal.sql index 33b56ef..19e5698 100644 --- a/models/reporting/xero/xero__net_fees_by_deal.sql +++ b/models/reporting/xero/xero__net_fees_by_deal.sql @@ -1,5 +1,4 @@ {% set relevant_document_statuses = "('PAID', 'AUTHORISED')" %} -{% set guesty_id_deal = "('17814677813')" %} with xero__invoices as (select * from {{ ref("xero__invoices") }}), @@ -10,42 +9,30 @@ with cast( date_trunc('month', i.invoice_issued_date_utc) as date ) as invoice_issued_year_month, - case - when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null - end as fee_deal, + c.id_deal, sum(i.total_amount_wo_tax_in_gbp) as fees_invoiced from xero__invoices i left join xero__contacts c on i.id_contact = c.id_contact - where - i.invoice_status in {{ relevant_document_statuses }} - and (c.id_deal in {{ guesty_id_deal }}) - group by - date_trunc('month', i.invoice_issued_date_utc), - case when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null end + where i.invoice_status in {{ relevant_document_statuses }} + group by date_trunc('month', i.invoice_issued_date_utc), c.id_deal ), fees_credited as ( select cast( date_trunc('month', cn.credit_note_issued_at_utc) as date ) as credit_note_issued_year_month, - case - when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null - end as fee_deal, + 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 }} - and (c.id_deal in {{ guesty_id_deal }}) - group by - date_trunc('month', cn.credit_note_issued_at_utc), - case when c.id_deal in {{ guesty_id_deal }} then 'guesty_fees' else null end + where cn.credit_note_status in {{ relevant_document_statuses }} + group by date_trunc('month', cn.credit_note_issued_at_utc), c.id_deal ) select coalesce( i.invoice_issued_year_month, c.credit_note_issued_year_month ) as issued_year_month, - coalesce(i.fee_deal, c.fee_deal) as fee_deal, + coalesce(i.id_deal, c.id_deal) as id_deal, coalesce(i.fees_invoiced, 0) as fees_invoiced_in_gbp, coalesce(c.fees_credited, 0) as fees_credited_in_gbp, (coalesce(i.fees_invoiced, 0) - coalesce(c.fees_credited, 0)) as net_fees_in_gbp @@ -53,4 +40,4 @@ from fees_invoiced i full outer join fees_credited c on i.invoice_issued_year_month = c.credit_note_issued_year_month - and i.fee_deal = c.fee_deal + and i.id_deal = c.id_deal From f021f3cb2995db332227eb47a6d06180305cfa0f Mon Sep 17 00:00:00 2001 From: uri Date: Thu, 30 May 2024 10:03:05 +0200 Subject: [PATCH 4/4] Changed schema.yaml with the correct field --- models/reporting/xero/schema.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/reporting/xero/schema.yaml b/models/reporting/xero/schema.yaml index c20cf36..0067437 100644 --- a/models/reporting/xero/schema.yaml +++ b/models/reporting/xero/schema.yaml @@ -1178,7 +1178,7 @@ models: data_type: numeric description: The issuing month of the aggregated documents. - - name: fee_deal + - name: id_deal data_type: text description: | A fee category per deal.