-
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.
Add transaction service to water-abstraction-system (#109)
https://eaflood.atlassian.net/browse/WATER-3894 Thanks to #97 we now know what the billable days are for each abstraction period (`charge_purpose`) linked to a charge reference (`charge_element` && `billing_charge_categories`) within a charge version. The final step is to take that information and start generating the transaction line information. For each charge reference in a charge version, there should be one transaction line. So, if a charge reference is linked to multiple abstraction periods, we need the sum of their billable days.
- Loading branch information
Showing
3 changed files
with
190 additions
and
0 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
49 changes: 49 additions & 0 deletions
49
app/services/supplementary-billing/create-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,49 @@ | ||
'use strict' | ||
|
||
/** | ||
* Generates the transaction lines for a supplementary bill run | ||
* @module CreateTransactionsService | ||
*/ | ||
|
||
const AbstractionBillingPeriodService = require('./abstraction-billing-period.service.js') | ||
|
||
/** | ||
* Creates a transaction line and calculates the billable days for each charge element | ||
* | ||
* Each SROC charge version linked to a licence will have one or more charge elements that are linked to a charge reference. | ||
* | ||
* A transaction line is needed for each of these charge references. Linked to the charge element are one or more abstraction periods. | ||
* | ||
* We need to calculate the sum of the billable days for these periods and apply it to the transaction line. | ||
* | ||
* @param {Object} billingPeriod Object that has a `startDate` and `endDate` that defines the billing period | ||
* @param {Object[]} chargeElements An array of `chargeElements` containing an array of `chargePurposes` which define | ||
* the abstraction periods | ||
* | ||
* @returns {Object[]} An array that has the `reference` and `billableDays` for each charge element on a charge version | ||
*/ | ||
function go (billingPeriod, chargeElements) { | ||
const transactionLines = chargeElements.map((chargeElement) => { | ||
return { | ||
reference: chargeElement.billingChargeCategory.reference, | ||
billableDays: _calculateBillableDays(billingPeriod, chargeElement.chargePurposes) | ||
} | ||
}) | ||
|
||
return transactionLines | ||
} | ||
|
||
function _calculateBillableDays (billingPeriod, chargePurposes) { | ||
let billableDays = 0 | ||
for (const chargePurpose of chargePurposes) { | ||
const abstractionPeriods = AbstractionBillingPeriodService.go(billingPeriod, chargePurpose) | ||
for (const abstractionPeriod of abstractionPeriods) { | ||
billableDays = billableDays + abstractionPeriod.billableDays | ||
} | ||
} | ||
return billableDays | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
128 changes: 128 additions & 0 deletions
128
test/services/supplementary-billing/create-transactions.service.test.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' | ||
|
||
// Test framework dependencies | ||
const Lab = require('@hapi/lab') | ||
const Code = require('@hapi/code') | ||
|
||
const { describe, it, beforeEach } = exports.lab = Lab.script() | ||
const { expect } = Code | ||
|
||
// Thing under test | ||
const CreateTransactionsService = require('../../../app/services/supplementary-billing/create-transactions.service.js') | ||
|
||
describe('Create Transactions service', () => { | ||
const billingPeriod = { | ||
startDate: new Date('2022-04-01'), | ||
endDate: new Date('2023-03-31') | ||
} | ||
let chargeElements | ||
|
||
describe('when there is one chargeElement', () => { | ||
describe('containing one chargePurpose', () => { | ||
beforeEach(() => { | ||
chargeElements = [ | ||
{ | ||
billingChargeCategory: { reference: '1.2.3' }, | ||
chargePurposes: [ | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 4, | ||
abstractionPeriodEndDay: 30, | ||
abstractionPeriodEndMonth: 9 | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
|
||
it('returns a correctly calculated array of transaction lines', () => { | ||
const result = CreateTransactionsService.go(billingPeriod, chargeElements) | ||
|
||
expect(result).to.be.an.array() | ||
|
||
expect(result[0].reference).to.equal(chargeElements[0].billingChargeCategory.reference) | ||
expect(result[0].billableDays).to.equal(183) | ||
}) | ||
}) | ||
|
||
describe('containing two chargePurposes', () => { | ||
beforeEach(() => { | ||
chargeElements = [ | ||
{ | ||
billingChargeCategory: { reference: '1.2.3' }, | ||
chargePurposes: [ | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 4, | ||
abstractionPeriodEndDay: 30, | ||
abstractionPeriodEndMonth: 9 | ||
}, | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 10, | ||
abstractionPeriodEndDay: 31, | ||
abstractionPeriodEndMonth: 3 | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
|
||
it('returns a correctly calculated array of transaction lines', () => { | ||
const result = CreateTransactionsService.go(billingPeriod, chargeElements) | ||
|
||
expect(result).to.be.an.array() | ||
|
||
expect(result[0].reference).to.equal(chargeElements[0].billingChargeCategory.reference) | ||
expect(result[0].billableDays).to.equal(365) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when there are multiple chargeElements', () => { | ||
beforeEach(() => { | ||
chargeElements = [ | ||
{ | ||
billingChargeCategory: { reference: '1.2.3' }, | ||
chargePurposes: [ | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 4, | ||
abstractionPeriodEndDay: 30, | ||
abstractionPeriodEndMonth: 9 | ||
}, | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 10, | ||
abstractionPeriodEndDay: 31, | ||
abstractionPeriodEndMonth: 3 | ||
} | ||
] | ||
}, | ||
{ | ||
billingChargeCategory: { reference: '4.5.6' }, | ||
chargePurposes: [ | ||
{ | ||
abstractionPeriodStartDay: 1, | ||
abstractionPeriodStartMonth: 4, | ||
abstractionPeriodEndDay: 28, | ||
abstractionPeriodEndMonth: 2 | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
|
||
it('returns a correctly calculated array of transaction lines', () => { | ||
const result = CreateTransactionsService.go(billingPeriod, chargeElements) | ||
|
||
expect(result).to.be.an.array() | ||
|
||
expect(result[0].reference).to.equal(chargeElements[0].billingChargeCategory.reference) | ||
expect(result[0].billableDays).to.equal(365) | ||
|
||
expect(result[1].reference).to.equal(chargeElements[1].billingChargeCategory.reference) | ||
expect(result[1].billableDays).to.equal(334) | ||
}) | ||
}) | ||
}) |