From 3f1e2ea1d95f53ea299c753982627b05e07d0642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oriol=20Roqu=C3=A9=20Paniagua?= Date: Tue, 4 Mar 2025 11:32:52 +0000 Subject: [PATCH] Merged PR 4572: Applies logic to modify the invoicing cycle date depending on the issuing source # Description Hyperline invoices/credit notes have an issuing date that corresponds to the moment in which these are actually created. This is an issue since it means that an invoice or credit note posted today, 4th March 2025, that is supposed to be related to February 2025 invoicing cycle, will have: * If the document is posted in Hyperline, an issuing date on the March 2025 * If the document is NOT posted in Hyperline, an issuing date on February 2025 This PR just creates a field that handles the logic effectively, to be used further in other reports/kpis purposes. # 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. - [X] I have checked for DRY opportunities with other models and docs. - [X] I've picked the right materialization for the affected models. # Other - [ ] Check if a full-refresh is required after this PR is merged. Related work items: #28158 --- .../xero/int_xero__sales_denom_mart.sql | 28 +++++++++++++++++++ models/intermediate/xero/schema.yml | 8 ++++++ models/reporting/xero/schema.yml | 9 ++++++ .../reporting/xero/xero__sales_denom_mart.sql | 2 ++ 4 files changed, 47 insertions(+) diff --git a/models/intermediate/xero/int_xero__sales_denom_mart.sql b/models/intermediate/xero/int_xero__sales_denom_mart.sql index 27fab53..e7521cd 100644 --- a/models/intermediate/xero/int_xero__sales_denom_mart.sql +++ b/models/intermediate/xero/int_xero__sales_denom_mart.sql @@ -36,6 +36,20 @@ select i.invoice_number as document_number, i.invoice_issued_at_utc as document_issued_at_utc, i.invoice_issued_date_utc as document_issued_date_utc, + case + when i.is_invoice_posted_in_hyperline = true + -- If the invoice is posted in Hyperline, we want to use the last day of the + -- previous month + then (date_trunc('month', invoice_issued_date_utc) - interval '1 day')::date + -- If the invoice is not posted in Hyperline, we want to use the last day of + -- the current month + else + ( + date_trunc('month', invoice_issued_date_utc) + + interval '1 month' + - interval '1 day' + )::date + end as document_is_effective_at_end_of_month_utc, i.invoice_type as document_type, i.invoice_currency_iso_4217 as document_currency_iso_4217, i.exchange_rate_to_gbp as exchange_rate_to_gbp, @@ -95,6 +109,20 @@ select cn.credit_note_number, cn.credit_note_issued_at_utc, cn.credit_note_issued_date_utc, + case + when cn.is_credit_note_posted_in_hyperline = true + -- If the credit note is posted in Hyperline, we want to use the last day of the + -- previous month + then (date_trunc('month', credit_note_issued_date_utc) - interval '1 day')::date + -- If the credit note is not posted in Hyperline, we want to use the last day of + -- the current month + else + ( + date_trunc('month', credit_note_issued_date_utc) + + interval '1 month' + - interval '1 day' + )::date + end, cn.credit_note_type, cn.credit_note_currency_iso_4217, cn.exchange_rate_to_gbp, diff --git a/models/intermediate/xero/schema.yml b/models/intermediate/xero/schema.yml index 268e7c7..59e822b 100644 --- a/models/intermediate/xero/schema.yml +++ b/models/intermediate/xero/schema.yml @@ -204,6 +204,14 @@ models: data_type: date description: "" + - name: document_is_effective_at_end_of_month_utc + data_type: date + description: | + The last day of the month when the document is effective. + This is used to align the document with the correct invoicing cycle period. + For documents posted in Hyperline, this represents the last day of the previous + month to the issuing date. For documents not posted in Hyperline, this is the + last day of the month when the document was issued. - name: document_type data_type: character varying description: "" diff --git a/models/reporting/xero/schema.yml b/models/reporting/xero/schema.yml index 4c153e4..b5f58da 100644 --- a/models/reporting/xero/schema.yml +++ b/models/reporting/xero/schema.yml @@ -1310,6 +1310,15 @@ models: data_type: date description: "" + - name: document_is_effective_at_end_of_month_utc + data_type: date + description: | + The last day of the month when the document is effective. + This is used to align the document with the correct invoicing cycle period. + For documents posted in Hyperline, this represents the last day of the previous + month to the issuing date. For documents not posted in Hyperline, this is the + last day of the month when the document was issued. + - name: document_type data_type: character varying description: "" diff --git a/models/reporting/xero/xero__sales_denom_mart.sql b/models/reporting/xero/xero__sales_denom_mart.sql index 2351e02..db18fa9 100644 --- a/models/reporting/xero/xero__sales_denom_mart.sql +++ b/models/reporting/xero/xero__sales_denom_mart.sql @@ -25,6 +25,8 @@ select sdm.document_number as document_number, sdm.document_issued_at_utc as document_issued_at_utc, sdm.document_issued_date_utc as document_issued_date_utc, + sdm.document_is_effective_at_end_of_month_utc + as document_is_effective_at_end_of_month_utc, sdm.document_type as document_type, sdm.document_currency_iso_4217 as document_currency_iso_4217, sdm.exchange_rate_to_gbp as exchange_rate_to_gbp,