New model for deals total due amount
This commit is contained in:
parent
2746977671
commit
d50442e887
6 changed files with 129 additions and 82 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
{% set pending_transactions = "('AUTHORISED')" %}
|
||||||
|
|
||||||
with
|
with
|
||||||
int_xero__invoices as (select * from {{ ref("int_xero__invoices") }}),
|
int_xero__invoices as (select * from {{ ref("int_xero__invoices") }}),
|
||||||
int_xero__credit_notes as (select * from {{ ref("int_xero__credit_notes") }}),
|
int_xero__credit_notes as (select * from {{ ref("int_xero__credit_notes") }}),
|
||||||
|
|
@ -6,33 +8,32 @@ with
|
||||||
select * from {{ ref("stg_seed__accounting_aggregations") }}
|
select * from {{ ref("stg_seed__accounting_aggregations") }}
|
||||||
),
|
),
|
||||||
amount_due_deals as (
|
amount_due_deals as (
|
||||||
select c.id_contact as id_contact,
|
select
|
||||||
|
c.id_contact as id_contact,
|
||||||
c.id_deal as id_deal,
|
c.id_deal as id_deal,
|
||||||
c.contact_name as contact_name,
|
c.contact_name as contact_name,
|
||||||
i.total_due_in_gbp
|
i.total_due_in_gbp
|
||||||
from int_xero__invoices i
|
from int_xero__invoices i
|
||||||
left join int_xero__contacts c on c.id_contact = i.id_contact
|
left join int_xero__contacts c on c.id_contact = i.id_contact
|
||||||
where i.invoice_status = 'AUTHORISED' and c.id_deal is not null
|
where i.invoice_status = {{ pending_transactions }} and c.id_deal is not null
|
||||||
union all
|
union all
|
||||||
select c.id_contact,
|
select c.id_contact, c.id_deal, c.contact_name, cn.remaining_credit_in_gbp * -1
|
||||||
c.id_deal,
|
|
||||||
c.contact_name, cn.remaining_credit_in_gbp * -1
|
|
||||||
from int_xero__credit_notes cn
|
from int_xero__credit_notes cn
|
||||||
left join int_xero__contacts c on c.id_contact = cn.id_contact
|
left join int_xero__contacts c on c.id_contact = cn.id_contact
|
||||||
where cn.credit_note_status = 'AUTHORISED' and c.id_deal is not null
|
where
|
||||||
|
cn.credit_note_status = {{ pending_transactions }} and c.id_deal is not null
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
id_contact,
|
id_contact,
|
||||||
id_deal,
|
id_deal,
|
||||||
contact_name,
|
contact_name,
|
||||||
sum(total_due_in_gbp) as total_outstanding_in_gbp,
|
sum(coalesce(total_due_in_gbp, 0)) as total_due_amount_in_gbp,
|
||||||
case
|
case
|
||||||
when sum(total_due_in_gbp) < 0
|
when sum(coalesce(total_due_in_gbp, 0)) < 0
|
||||||
then 'Outstanding Resolutions'
|
then 'Outstanding Resolutions'
|
||||||
when sum(total_due_in_gbp) >= 0
|
when sum(coalesce(total_due_in_gbp, 0)) > 0
|
||||||
then 'Outstanding Invoices'
|
then 'Outstanding Invoices'
|
||||||
|
else null
|
||||||
end as outstanding_type
|
end as outstanding_type
|
||||||
from amount_due_deals
|
from amount_due_deals
|
||||||
group by id_contact,
|
group by id_contact, id_deal, contact_name
|
||||||
id_deal,
|
|
||||||
contact_name
|
|
||||||
|
|
|
||||||
|
|
@ -10,30 +10,6 @@ with
|
||||||
int_xero__contacts as (select * from {{ ref("int_xero__contacts") }}),
|
int_xero__contacts as (select * from {{ ref("int_xero__contacts") }}),
|
||||||
stg_seed__accounting_aggregations as (
|
stg_seed__accounting_aggregations as (
|
||||||
select * from {{ ref("stg_seed__accounting_aggregations") }}
|
select * from {{ ref("stg_seed__accounting_aggregations") }}
|
||||||
),
|
|
||||||
amount_due_deals as (
|
|
||||||
select c.id_deal, i.total_due_in_gbp
|
|
||||||
from int_xero__invoices i
|
|
||||||
left join int_xero__contacts c on c.id_contact = i.id_contact
|
|
||||||
where i.invoice_status = 'AUTHORISED' and c.id_deal is not null
|
|
||||||
union all
|
|
||||||
select c.id_deal, cn.remaining_credit_in_gbp * -1
|
|
||||||
from int_xero__credit_notes cn
|
|
||||||
left join int_xero__contacts c on c.id_contact = cn.id_contact
|
|
||||||
where cn.credit_note_status = 'AUTHORISED' and c.id_deal is not null
|
|
||||||
),
|
|
||||||
outstanding_deals as (
|
|
||||||
select
|
|
||||||
id_deal,
|
|
||||||
sum(total_due_in_gbp) as total_outstanding_in_gbp,
|
|
||||||
case
|
|
||||||
when sum(total_due_in_gbp) < 0
|
|
||||||
then 'Outstanding Resolutions'
|
|
||||||
when sum(total_due_in_gbp) >= 0
|
|
||||||
then 'Outstanding Invoices'
|
|
||||||
end as outstanding_type
|
|
||||||
from amount_due_deals
|
|
||||||
group by id_deal
|
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
ili.id_line_item as id_line_item,
|
ili.id_line_item as id_line_item,
|
||||||
|
|
@ -87,7 +63,6 @@ select
|
||||||
i.total_tax_in_gbp as header_total_tax_in_gbp,
|
i.total_tax_in_gbp as header_total_tax_in_gbp,
|
||||||
i.total_due_local_curr as header_total_due_local_curr,
|
i.total_due_local_curr as header_total_due_local_curr,
|
||||||
i.total_due_in_gbp as header_total_due_in_gbp,
|
i.total_due_in_gbp as header_total_due_in_gbp,
|
||||||
od.outstanding_type as outstanding_type,
|
|
||||||
|
|
||||||
c.id_contact as id_contact,
|
c.id_contact as id_contact,
|
||||||
c.id_deal as id_deal,
|
c.id_deal as id_deal,
|
||||||
|
|
@ -103,8 +78,6 @@ from int_xero__invoice_line_items ili
|
||||||
left join int_xero__invoices i on i.id_invoice = ili.id_invoice
|
left join int_xero__invoices i on i.id_invoice = ili.id_invoice
|
||||||
left join int_xero__contacts c on c.id_contact = i.id_contact
|
left join int_xero__contacts c on c.id_contact = i.id_contact
|
||||||
left join stg_seed__accounting_aggregations aa on aa.account_code = ili.account_code
|
left join stg_seed__accounting_aggregations aa on aa.account_code = ili.account_code
|
||||||
left join
|
|
||||||
outstanding_deals od on od.id_deal = c.id_deal and i.invoice_status = 'AUTHORISED'
|
|
||||||
|
|
||||||
union all
|
union all
|
||||||
|
|
||||||
|
|
@ -163,7 +136,6 @@ select
|
||||||
cn.total_tax_in_gbp * -1,
|
cn.total_tax_in_gbp * -1,
|
||||||
cn.remaining_credit_local_curr * -1,
|
cn.remaining_credit_local_curr * -1,
|
||||||
cn.remaining_credit_in_gbp * -1,
|
cn.remaining_credit_in_gbp * -1,
|
||||||
od.outstanding_type as outstanding_type,
|
|
||||||
|
|
||||||
c.id_contact,
|
c.id_contact,
|
||||||
c.id_deal,
|
c.id_deal,
|
||||||
|
|
@ -179,7 +151,3 @@ from int_xero__credit_note_line_items cnli
|
||||||
left join int_xero__credit_notes cn on cn.id_credit_note = cnli.id_credit_note
|
left join int_xero__credit_notes cn on cn.id_credit_note = cnli.id_credit_note
|
||||||
left join int_xero__contacts c on c.id_contact = cn.id_contact
|
left join int_xero__contacts c on c.id_contact = cn.id_contact
|
||||||
left join stg_seed__accounting_aggregations aa on aa.account_code = cnli.account_code
|
left join stg_seed__accounting_aggregations aa on aa.account_code = cnli.account_code
|
||||||
left join
|
|
||||||
outstanding_deals od
|
|
||||||
on od.id_deal = c.id_deal
|
|
||||||
and cn.credit_note_status = 'AUTHORISED'
|
|
||||||
|
|
|
||||||
|
|
@ -322,17 +322,6 @@ models:
|
||||||
data_type: numeric
|
data_type: numeric
|
||||||
description: ""
|
description: ""
|
||||||
|
|
||||||
- name: outstanding_type
|
|
||||||
data_type: character varying
|
|
||||||
description: |
|
|
||||||
Field indicating whether Truvi ows more money to the deal account ('Outstanding Resolutions')
|
|
||||||
or the deal account owes more money to Truvi ('Outstanding Invoices').
|
|
||||||
data_tests:
|
|
||||||
- accepted_values:
|
|
||||||
values:
|
|
||||||
- "Outstanding Resolutions"
|
|
||||||
- "Outstanding Invoices"
|
|
||||||
|
|
||||||
- name: id_contact
|
- name: id_contact
|
||||||
data_type: character varying
|
data_type: character varying
|
||||||
description: ""
|
description: ""
|
||||||
|
|
@ -567,4 +556,48 @@ models:
|
||||||
data_type: character varying
|
data_type: character varying
|
||||||
description: "The currency of the transaction, represented in ISO 4217 format."
|
description: "The currency of the transaction, represented in ISO 4217 format."
|
||||||
|
|
||||||
|
- name: int_xero__deals_due_amounts
|
||||||
|
description: |
|
||||||
|
This model provides a view on outstanding amounts related to deals,
|
||||||
|
specifically focusing on the amounts owed by Truvi or to Truvi.
|
||||||
|
This includes all AUTHORISED invoices and credit notes.
|
||||||
|
|
||||||
|
columns:
|
||||||
|
|
||||||
|
- name: id_contact
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Unique identifier of a contact.
|
||||||
|
data_tests:
|
||||||
|
- not_null
|
||||||
|
- unique
|
||||||
|
|
||||||
|
- name: id_deal
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Identifier of the account.
|
||||||
|
data_tests:
|
||||||
|
- not_null
|
||||||
|
- unique
|
||||||
|
|
||||||
|
- name: contact_name
|
||||||
|
data_type: character varying
|
||||||
|
description: "Name of the contact associated with the deal."
|
||||||
|
|
||||||
|
- name: total_due_amount_in_gbp
|
||||||
|
data_type: numeric
|
||||||
|
description: |
|
||||||
|
Total outstanding amount in GBP, which is the sum of all AUTHORISED invoices and credit notes
|
||||||
|
for the deal, converted to GBP.
|
||||||
|
|
||||||
|
- name: outstanding_type
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Field indicating whether Truvi ows more money to the deal account ('Outstanding Resolutions')
|
||||||
|
or the deal account owes more money to Truvi ('Outstanding Invoices').
|
||||||
|
data_tests:
|
||||||
|
- accepted_values:
|
||||||
|
values:
|
||||||
|
- "Outstanding Resolutions"
|
||||||
|
- "Outstanding Invoices"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1426,17 +1426,6 @@ models:
|
||||||
data_type: numeric
|
data_type: numeric
|
||||||
description: ""
|
description: ""
|
||||||
|
|
||||||
- name: outstanding_type
|
|
||||||
data_type: character varying
|
|
||||||
description: |
|
|
||||||
Field indicating whether Truvi ows more money to the deal account ('Outstanding Resolutions')
|
|
||||||
or the deal account owes more money to Truvi ('Outstanding Invoices').
|
|
||||||
data_tests:
|
|
||||||
- accepted_values:
|
|
||||||
values:
|
|
||||||
- "Outstanding Resolutions"
|
|
||||||
- "Outstanding Invoices"
|
|
||||||
|
|
||||||
- name: id_contact
|
- name: id_contact
|
||||||
data_type: character varying
|
data_type: character varying
|
||||||
description: ""
|
description: ""
|
||||||
|
|
@ -1734,3 +1723,49 @@ models:
|
||||||
- name: contact_status
|
- name: contact_status
|
||||||
data_type: character varying
|
data_type: character varying
|
||||||
description: ""
|
description: ""
|
||||||
|
|
||||||
|
- name: xero__deals_due_amounts
|
||||||
|
description: |
|
||||||
|
This model provides a view on outstanding amounts related to deals,
|
||||||
|
specifically focusing on the amounts owed by Truvi or to Truvi.
|
||||||
|
This includes all AUTHORISED invoices and credit notes.
|
||||||
|
|
||||||
|
columns:
|
||||||
|
|
||||||
|
- name: id_contact
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Unique identifier of a contact.
|
||||||
|
data_tests:
|
||||||
|
- not_null
|
||||||
|
- unique
|
||||||
|
|
||||||
|
- name: id_deal
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Identifier of the account.
|
||||||
|
data_tests:
|
||||||
|
- not_null
|
||||||
|
- unique
|
||||||
|
|
||||||
|
- name: contact_name
|
||||||
|
data_type: character varying
|
||||||
|
description: "Name of the contact associated with the deal."
|
||||||
|
|
||||||
|
- name: total_due_amount_in_gbp
|
||||||
|
data_type: numeric
|
||||||
|
description: |
|
||||||
|
Total outstanding amount in GBP, which is the sum of all AUTHORISED invoices and credit notes
|
||||||
|
for the deal, converted to GBP.
|
||||||
|
|
||||||
|
- name: outstanding_type
|
||||||
|
data_type: character varying
|
||||||
|
description: |
|
||||||
|
Field indicating whether Truvi ows more money to the deal account ('Outstanding Resolutions')
|
||||||
|
or the deal account owes more money to Truvi ('Outstanding Invoices').
|
||||||
|
data_tests:
|
||||||
|
- accepted_values:
|
||||||
|
values:
|
||||||
|
- "Outstanding Resolutions"
|
||||||
|
- "Outstanding Invoices"
|
||||||
|
|
||||||
11
models/reporting/xero/xero__deals_due_amounts.sql
Normal file
11
models/reporting/xero/xero__deals_due_amounts.sql
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
with
|
||||||
|
int_xero__deals_due_amounts as (
|
||||||
|
select * from {{ ref("int_xero__deals_due_amounts") }}
|
||||||
|
)
|
||||||
|
select
|
||||||
|
dda.id_contact as id_contact,
|
||||||
|
dda.id_deal as id_deal,
|
||||||
|
dda.contact_name as contact_name,
|
||||||
|
dda.total_due_amount_in_gbp as total_due_amount_in_gbp,
|
||||||
|
dda.outstanding_type as outstanding_type
|
||||||
|
from int_xero__deals_due_amounts dda
|
||||||
|
|
@ -40,7 +40,6 @@ select
|
||||||
sdm.header_total_tax_in_gbp as header_total_tax_in_gbp,
|
sdm.header_total_tax_in_gbp as header_total_tax_in_gbp,
|
||||||
sdm.header_total_due_local_curr as header_total_due_local_curr,
|
sdm.header_total_due_local_curr as header_total_due_local_curr,
|
||||||
sdm.header_total_due_in_gbp as header_total_due_in_gbp,
|
sdm.header_total_due_in_gbp as header_total_due_in_gbp,
|
||||||
sdm.outstanding_type as outstanding_type,
|
|
||||||
sdm.id_contact as id_contact,
|
sdm.id_contact as id_contact,
|
||||||
sdm.id_deal as id_deal,
|
sdm.id_deal as id_deal,
|
||||||
sdm.contact_name as contact_name,
|
sdm.contact_name as contact_name,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue