-
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.
Implement annual two-part tariff billing engine (#1172)
https://eaflood.atlassian.net/browse/WATER-4196 > Part of the work for two-part tariff annual billing We're ready to generate a bill run from our two-part tariff review data and with [Add Continue bill run btn to 2PT review screen](#1122) and [Add new two-part tariff generate bill run endpoint](#1123) we have the means to trigger it. We can now also [Fetch all the billing accounts and related data](#1129) needed to generate the bills. This implements the remaining services needed to transform that data into actual bills using the same pattern we implemented for SROC annual billing. - a service to manage the process - `GenerateBillRunService` - a service to transform the data into bills `ProcessBillingPeriodService` - a mirror of `GenerateTransactionService` amended specifically for two-part tariff bill runs (because of the need to incorporate values from the review stage) With this in place, users will finally be able to generate an annual two-part tariff SROC bill run!
- Loading branch information
1 parent
0d2f958
commit 7162104
Showing
11 changed files
with
1,273 additions
and
14 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
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
128 changes: 128 additions & 0 deletions
128
app/services/bill-runs/two-part-tariff/generate-transaction.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,128 @@ | ||
'use strict' | ||
|
||
/** | ||
* Generate a two-part tariff transaction data from the the charge reference and other information passed in | ||
* @module GenerateTransactionService | ||
*/ | ||
|
||
const { generateUUID } = require('../../../lib/general.lib.js') | ||
|
||
/** | ||
* Generate a two-part tariff transaction data from the the charge reference and other information passed in | ||
* | ||
* Unlike a standard transaction, we don't have to calculate the billing days for the transaction. Instead, two-part | ||
* tariff transactions focus on volume. This information comes from the review data that results after the match & | ||
* allocate results have been checked and amended by users. | ||
* | ||
* As well as allocated volume, users can override in the review | ||
* | ||
* - the authorised volume | ||
* - the aggregate | ||
* - the charge adjustment | ||
* | ||
* So, we have to grab those values as well. Finally, because the standard annual bill run will have handled the | ||
* compensation charge we don't have to generate an additional transaction alongside our two-part tariff one. | ||
* | ||
* @param {String} billLicenceId - The UUID of the bill licence the transaction will be linked to | ||
* @param {module:ChargeReferenceModel} chargeReference - The charge reference the transaction generated will be | ||
* generated from | ||
* @param {Object} chargePeriod - A start and end date representing the charge period for the transaction | ||
* @param {Boolean} newLicence - Whether the charge reference is linked to a new licence | ||
* @param {Boolean} waterUndertaker - Whether the charge reference is linked to a water undertaker licence | ||
* | ||
* @returns {Object} the two-part tariff transaction | ||
*/ | ||
function go (billLicenceId, chargeReference, chargePeriod, newLicence, waterUndertaker) { | ||
const billableQuantity = _billableQuantity(chargeReference.chargeElements) | ||
|
||
return _standardTransaction( | ||
billLicenceId, | ||
billableQuantity, | ||
chargeReference, | ||
chargePeriod, | ||
newLicence, | ||
waterUndertaker | ||
) | ||
} | ||
|
||
function _billableQuantity (chargeElements) { | ||
return chargeElements.reduce((total, chargeElement) => { | ||
total += chargeElement.reviewChargeElements[0].amendedAllocated | ||
|
||
return total | ||
}, 0) | ||
} | ||
|
||
function _description (chargeReference) { | ||
// If the value is false, undefined, null or simply doesn't exist we return the standard description | ||
if (!chargeReference.adjustments.s127) { | ||
return `Water abstraction charge: ${chargeReference.description}` | ||
} | ||
|
||
return `Two-part tariff basic water abstraction charge: ${chargeReference.description}` | ||
} | ||
|
||
/** | ||
* Returns a json representation of all charge elements in a charge reference | ||
*/ | ||
function _generateElements (chargeReference) { | ||
const jsonChargeElements = chargeReference.chargeElements.map((chargeElement) => { | ||
delete chargeElement.reviewChargeElements | ||
|
||
return chargeElement.toJSON() | ||
}) | ||
|
||
return JSON.stringify(jsonChargeElements) | ||
} | ||
|
||
/** | ||
* Generates a standard transaction based on the supplied data, along with some default fields (eg. status) | ||
*/ | ||
function _standardTransaction ( | ||
billLicenceId, | ||
billableQuantity, | ||
chargeReference, | ||
chargePeriod, | ||
newLicence, | ||
waterUndertaker | ||
) { | ||
return { | ||
id: generateUUID(), | ||
billLicenceId, | ||
authorisedDays: 0, | ||
billableDays: 0, | ||
newLicence, | ||
waterUndertaker, | ||
chargeReferenceId: chargeReference.id, | ||
startDate: chargePeriod.startDate, | ||
endDate: chargePeriod.endDate, | ||
source: chargeReference.source, | ||
season: 'all year', | ||
loss: chargeReference.loss, | ||
credit: false, | ||
chargeType: 'standard', | ||
authorisedQuantity: chargeReference.reviewChargeReferences[0].amendedAuthorisedVolume, | ||
billableQuantity, | ||
status: 'candidate', | ||
description: _description(chargeReference), | ||
volume: chargeReference.volume, | ||
section126Factor: Number(chargeReference.adjustments.s126) || 1, | ||
section127Agreement: !!chargeReference.adjustments.s127, | ||
section130Agreement: !!chargeReference.adjustments.s130, | ||
secondPartCharge: true, | ||
scheme: 'sroc', | ||
aggregateFactor: chargeReference.reviewChargeReferences[0].amendedAggregate, | ||
adjustmentFactor: chargeReference.reviewChargeReferences[0].amendedChargeAdjustment, | ||
chargeCategoryCode: chargeReference.chargeCategory.reference, | ||
chargeCategoryDescription: chargeReference.chargeCategory.shortDescription, | ||
supportedSource: !!chargeReference.additionalCharges?.supportedSource?.name, | ||
supportedSourceName: chargeReference.additionalCharges?.supportedSource?.name || null, | ||
waterCompanyCharge: !!chargeReference.additionalCharges?.isSupplyPublicWater, | ||
winterOnly: !!chargeReference.adjustments.winter, | ||
purposes: _generateElements(chargeReference) | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.