Merged PR 1938: Add Damage Waiver to Net Fees models

Expands `xero__net_fees` with a new category dedicated to Damage Waivers.

Also fixes a small issue with the join. Should have been a full outer join from the start to properly catch months where we've only invoiced/credited some category.

Related work items: #16825
This commit is contained in:
Pablo Martín 2024-05-29 11:25:14 +00:00
commit 186d5804f8

View file

@ -1,6 +1,7 @@
{% set relevant_document_statuses = "('PAID', 'AUTHORISED')" %}
{% set booking_fee_items = "('EU Booking Fee','ZAR Bookings','Booking fee - non-UK','USD Bookings','CAD Bookings','Booking fee - UK','AUD Bookings')" %}
{% set listing_fee_items = "('USD Listings','Listing fee - non UK','ZAR Listings','CAD Listings','Listing fee - UK','AUD Listings','EU Listings')" %}
{% set waiver_items = "('Damage Waver', 'Damage Waiver')" %}
{% set verification_fee_items = "('Verification Fee')" %}
with
@ -22,6 +23,8 @@ with
then 'listing_fees'
when ili.item_code in {{ verification_fee_items }}
then 'verification_fees'
when ili.item_code in {{ waiver_items }}
then 'damager_waiver'
else null
end as fee_category,
sum(ili.line_amount_wo_taxes_in_gbp) as fees_invoiced
@ -33,6 +36,7 @@ with
ili.item_code in {{ booking_fee_items }}
or ili.item_code in {{ listing_fee_items }}
or ili.item_code in {{ verification_fee_items }}
or ili.item_code in {{ waiver_items }}
)
group by
date_trunc('month', i.invoice_issued_date_utc),
@ -43,6 +47,8 @@ with
then 'listing_fees'
when ili.item_code in {{ verification_fee_items }}
then 'verification_fees'
when ili.item_code in {{ waiver_items }}
then 'damager_waiver'
else null
end
),
@ -58,6 +64,8 @@ with
then 'listing_fees'
when cnli.item_code in {{ verification_fee_items }}
then 'verification_fees'
when cnli.item_code in {{ waiver_items }}
then 'damager_waiver'
else null
end as fee_category,
sum(cnli.line_amount_wo_taxes_in_gbp) as fees_credited
@ -70,6 +78,7 @@ with
cnli.item_code in {{ booking_fee_items }}
or cnli.item_code in {{ listing_fee_items }}
or cnli.item_code in {{ verification_fee_items }}
or cnli.item_code in {{ waiver_items }}
)
group by
date_trunc('month', cn.credit_note_issued_date_utc),
@ -80,17 +89,21 @@ with
then 'listing_fees'
when cnli.item_code in {{ verification_fee_items }}
then 'verification_fees'
when cnli.item_code in {{ waiver_items }}
then 'damager_waiver'
else null
end
)
select
i.invoice_issued_year_month as issued_year_month,
i.fee_category,
coalesce(
i.invoice_issued_year_month, c.credit_note_issued_year_month
) as issued_year_month,
coalesce(i.fee_category, c.fee_category) as fee_category,
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
left join
full outer join
fees_credited c
on i.invoice_issued_year_month = c.credit_note_issued_year_month
and i.fee_category = c.fee_category