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

Allocate returns volumes to charge elements #436

Merged
merged 13 commits into from
Oct 2, 2023
54 changes: 54 additions & 0 deletions app/services/check/allocate-returns-volumes.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

/**
* Used to allocate the abstraction volumes to the charge elements
* @module AllocateReturnsVolumes
*/

function go (chargeReference) {
let returnVolumeInMegalitres
// Loop through each return
chargeReference.returns.forEach((returnData) => {
// The volumes on the return are in Cubic Metres so we convert to Megalitres to match the charge version data
returnVolumeInMegalitres = returnData.volumes.total / 1000
// Loop through each charge element
chargeReference.chargeElements.forEach((chargeElement) => {
// Check the chargeElement is not already fully allocated
if (chargeElement.billableAnnualQuantity < chargeElement.authorisedAnnualQuantity) {
// Check if the return's purpose and abstraction period match the charge element
if (_matchReturnToElement(returnData.metadata, chargeElement)) {
// Calculate how much is left to allocated to the ChargeElement from the return
let volumeLeftToAllocate = chargeElement.authorisedAnnualQuantity - chargeElement.billableAnnualQuantity
// Check for the case that the return does not cover the full allocation
if (returnVolumeInMegalitres < volumeLeftToAllocate) {
volumeLeftToAllocate = returnVolumeInMegalitres
}

chargeElement.billableAnnualQuantity += volumeLeftToAllocate
returnVolumeInMegalitres -= volumeLeftToAllocate
}
}
})

if (returnVolumeInMegalitres > 0) {
// Convert any remaining volume back to Cubic Metres and add it to the volumes object
returnData.volumes.unallocated = returnVolumeInMegalitres * 1000
} else {
returnData.volumes.unallocated = 0
}
})
}

function _matchReturnToElement (returnMetadata, chargeElement) {
return (
returnMetadata.purposes[0].tertiary.code === chargeElement.purpose.legacyId &&
returnMetadata.nald.periodStartDay === chargeElement.abstractionPeriodStartDay.toString() &&
returnMetadata.nald.periodStartMonth === chargeElement.abstractionPeriodStartMonth.toString() &&
returnMetadata.nald.periodEndDay === chargeElement.abstractionPeriodEndDay.toString() &&
returnMetadata.nald.periodEndMonth === chargeElement.abstractionPeriodEndMonth.toString()
)
}

module.exports = {
go
}
9 changes: 7 additions & 2 deletions app/services/check/calculate-returns-volumes.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ function _calculateReturnLinesVolumes (lines, billableAbstractionPeriods) {
}
})

const inPeriod = _totalLineQuantities(inPeriodLines)
const outsidePeriod = _totalLineQuantities(outsidePeriodLines)
const total = inPeriod + outsidePeriod

return {
inPeriod: _totalLineQuantities(inPeriodLines),
outsidePeriod: _totalLineQuantities(outsidePeriodLines)
inPeriod,
outsidePeriod,
total
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/services/check/friendly-response.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function _formatFriendlyChargeElements (friendlyChargeElements, chargeElements)
chargeElements.forEach((chargeElement) => {
const {
authorisedAnnualQuantity,
billableAnnualQuantity,
chargePurposeId,
description,
isSection127AgreementEnabled,
Expand All @@ -124,7 +125,8 @@ function _formatFriendlyChargeElements (friendlyChargeElements, chargeElements)
id: chargePurposeId,
description,
abstractionPeriod: formatAbstractionPeriod(startDay, startMonth, endDay, endMonth),
annualQuantities: authorisedAnnualQuantity,
authorisedAnnualQuantity,
billableAnnualQuantity,
timeLimit: _formatTimeLimit(timeLimitedStartDate, timeLimitedEndDate),
loss
}
Expand Down
2 changes: 2 additions & 0 deletions app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const { ref } = require('objection')

const AllocateReturnsVolumes = require('./allocate-returns-volumes.service.js')
const CalculateReturnsVolumes = require('./calculate-returns-volumes.service.js')
const ChargeReferenceModel = require('../../models/water/charge-reference.model.js')
const ChargeVersionModel = require('../../models/water/charge-version.model.js')
Expand Down Expand Up @@ -126,6 +127,7 @@ async function _fetchAndApplyReturns (billingPeriod, chargeVersion) {
})

CalculateReturnsVolumes.go(billingPeriod, chargeReference.returns)
AllocateReturnsVolumes.go(chargeReference)

const chargeReferenceReturnsStatuses = chargeReference.returns.map((matchedReturn) => {
if (matchedReturn.underQuery) {
Expand Down
Loading