-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Reverse Billing Batch Licences service (#167)
https://eaflood.atlassian.net/browse/WATER-3936 When we generate a supplementary bill run we are required to reverse the debits from the previous one. Essentially, when a licence is flagged for supplementary billing, for whatever reason, we reverse any previous bill runs and generate a new one. With the exception of Annual billing, because we know any previous supplementary bill run will have reversed the debits on the one that came before, any new runs also just need to focus on the ones previous to them. This is complicated, however, by the fact we then need to ignore transactions that cancel each other out. For example, Customer **ABC1** was billed 120 days for licence **LIC001** on the previous bill run. Our supplementary process calculates 120 days for the new charge version. These cancel each other out so should not be included. So, for each charge version `FetchChargeVersionsService` returns we'll need to identify if they are linked to a previous `billing_batch`. From there we'll need to locate the transactions, determine if they cancel out what we've calculated and if not include them as credits in the supplementary bill run we're creating. > Note - This PR originated to create a service to solve a part of this problem. It then became our 'SPIKE' branch and has culminated in all the changes needed. We _will_ be doing further cleanup and documentation after this change has been merged.
- Loading branch information
Showing
11 changed files
with
643 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/services/supplementary-billing/fetch-previous-billing-transactions.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches the previously billed transactions that match the invoice, licence and year provided | ||
* @module FetchPreviousBillingTransactionsService | ||
*/ | ||
|
||
const { db } = require('../../../db/db.js') | ||
|
||
async function go (billingInvoice, billingInvoiceLicence, financialYearEnding) { | ||
const billingTransactions = await _fetch( | ||
billingInvoiceLicence.licenceId, | ||
billingInvoice.invoiceAccountId, | ||
financialYearEnding | ||
) | ||
|
||
return billingTransactions | ||
} | ||
|
||
async function _fetch (licenceId, invoiceAccountId, financialYearEnding) { | ||
return db | ||
.select( | ||
'bt.authorisedDays', | ||
'bt.billableDays', | ||
'bt.isWaterUndertaker', | ||
'bt.chargeElementId', | ||
'bt.startDate', | ||
'bt.endDate', | ||
'bt.source', | ||
'bt.season', | ||
'bt.loss', | ||
'bt.isCredit', | ||
'bt.chargeType', | ||
'bt.authorisedQuantity', | ||
'bt.billableQuantity', | ||
'bt.description', | ||
'bt.volume', | ||
'bt.section126Factor', | ||
'bt.section127Agreement', | ||
'bt.section130Agreement', | ||
'bt.isTwoPartSecondPartCharge', | ||
'bt.scheme', | ||
'bt.aggregateFactor', | ||
'bt.adjustmentFactor', | ||
'bt.chargeCategoryCode', | ||
'bt.chargeCategoryDescription', | ||
'bt.isSupportedSource', | ||
'bt.supportedSourceName', | ||
'bt.isWaterCompanyCharge', | ||
'bt.isWinterOnly', | ||
'bt.purposes', | ||
'validBillingInvoices.invoiceAccountId', | ||
'validBillingInvoices.invoiceAccountNumber' | ||
) | ||
.from('water.billingTransactions as bt') | ||
.innerJoin( | ||
db | ||
.select( | ||
'bil.billingInvoiceLicenceId', | ||
'bi.invoiceAccountId', | ||
'bi.invoiceAccountNumber' | ||
) | ||
.max('bil.date_created as latest_date_created') | ||
.from('water.billingInvoiceLicences as bil') | ||
.innerJoin('water.billingInvoices as bi', 'bil.billingInvoiceId', 'bi.billingInvoiceId') | ||
.innerJoin('water.billingBatches as bb', 'bi.billingBatchId', 'bb.billingBatchId') | ||
.where({ | ||
'bil.licenceId': licenceId, | ||
'bi.invoiceAccountId': invoiceAccountId, | ||
'bi.financialYearEnding': financialYearEnding, | ||
'bb.status': 'sent', | ||
'bb.scheme': 'sroc' | ||
}) | ||
.groupBy('bil.billingInvoiceLicenceId', 'bi.invoiceAccountId', 'bi.invoiceAccountNumber') | ||
.as('validBillingInvoices'), | ||
'bt.billingInvoiceLicenceId', 'validBillingInvoices.billingInvoiceLicenceId' | ||
) | ||
.where({ | ||
'bt.isCredit': false | ||
}) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.