Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix re-processing REPLACED charge versions #192

Merged
merged 10 commits into from
Apr 20, 2023
25 changes: 12 additions & 13 deletions app/services/supplementary-billing/fetch-charge-versions.service.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
'use strict'

const { ref } = require('objection')
/**
* Fetches SROC charge versions that might be included in a supplementary bill run
* Fetches SROC charge versions linked to licences flagged for inclusion in next SROC supplementary billing
* @module FetchChargeVersionsService
*/

const { ref } = require('objection')

const ChargeVersion = require('../../models/water/charge-version.model.js')
const ChargeVersionWorkflow = require('../../models/water/charge-version-workflow.model.js')

/**
* Fetch all 'current' SROC charge versions linked to licences flagged for supplementary billing that are in the period
* being billed
*
* > This is not the final form of the service. It is a 'work in progress' as we implement tickets that gradually
* > build up our understanding of SROC supplementary billing
* Fetch all SROC charge versions linked to licences flagged for supplementary billing that are in the period being
* billed
*
* @param {string} regionId GUID of the region which the licences will be linked to
* @param {string} regionId UUID of the region being billed that the licences must be linked to
* @param {Object} billingPeriod Object with a `startDate` and `endDate` property representing the period being billed
*
* @returns an array of Objects containing the relevant charge versions
* @returns {Object[]} an array of matching charge versions
*/
async function go (regionId, billingPeriod) {
const chargeVersions = await _fetch(regionId, billingPeriod)
Expand All @@ -34,16 +32,17 @@ async function _fetch (regionId, billingPeriod) {
'scheme',
'chargeVersions.startDate',
'chargeVersions.endDate',
'invoiceAccountId'
'invoiceAccountId',
'status'
])
.innerJoinRelated('licence')
.where('scheme', 'sroc')
.whereNotNull('invoiceAccountId')
.where('includeInSrocSupplementaryBilling', true)
.where('regionId', regionId)
.where('chargeVersions.status', 'current')
.where('chargeVersions.scheme', 'sroc')
.whereNotNull('chargeVersions.invoiceAccountId')
.where('chargeVersions.startDate', '>=', billingPeriod.startDate)
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.whereNot('chargeVersions.status', 'draft')
.whereNotExists(
ChargeVersionWorkflow.query()
.select(1)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const HandleErroredBillingBatchService = require('./handle-errored-billing-batch
const LegacyRequestLib = require('../../lib/legacy-request.lib.js')
const LicenceModel = require('../../models/water/licence.model.js')
const ProcessBillingTransactionsService = require('./process-billing-transactions.service.js')
const ProcessReplacedChargeVersionsService = require('./process-replaced-charge-versions.service.js')

/**
* Creates the invoices and transactions in both WRLS and the Charging Module API
Expand Down Expand Up @@ -73,13 +72,18 @@ async function go (billingBatch, billingPeriod) {
currentBillingData.billingInvoice = billingInvoice
currentBillingData.billingInvoiceLicence = billingInvoiceLicence

const calculatedTransactions = _generateCalculatedTransactions(billingPeriod, chargeVersion, billingBatchId, billingInvoiceLicence)
currentBillingData.calculatedTransactions.push(...calculatedTransactions)
// If the charge version has a status of 'current' (APPROVED) then it is likely to be something we have never
// billed previously so we need to calculate a debit line.
// Else the charge version has been 'superseded' (REPLACED). So, we won't be adding a new debit line to the bill
// for it. But we still need to process it to understand what, if anything, needs to be credited back or if our
// calculated debit line has already been billed.
if (chargeVersion.status === 'current') {
const calculatedTransactions = _generateCalculatedTransactions(billingPeriod, chargeVersion, billingBatchId, billingInvoiceLicence)
currentBillingData.calculatedTransactions.push(...calculatedTransactions)
}
}
await _finaliseCurrentInvoiceLicence(currentBillingData, billingPeriod, billingBatch)

await _processReplacedChargeVersions(currentBillingData, billingBatch, billingPeriod)

await _finaliseBillingBatch(billingBatch, chargeVersions, currentBillingData.isEmpty)

// Log how long the process took
Expand Down Expand Up @@ -285,19 +289,6 @@ function _logError (billingBatch, error) {
})
}

async function _processReplacedChargeVersions (currentBillingData, billingBatch, billingPeriod) {
try {
const anythingGenerated = await ProcessReplacedChargeVersionsService.go(billingBatch, billingPeriod)
if (anythingGenerated) {
currentBillingData.isEmpty = false
}
} catch (error) {
HandleErroredBillingBatchService.go(billingBatch.billingBatchId)

throw error
}
}

async function _updateStatus (billingBatchId, status) {
try {
await BillingBatchModel.query()
Expand Down

This file was deleted.

Loading