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

Persist charge reference authorised volume for two-part tariff review #966

Merged
merged 11 commits into from
May 2, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function go (billRun, reviewChargeElement, licenceId) {
function _authorisedQuantity (reviewChargeElement) {
const { chargeElement, reviewChargeReference } = reviewChargeElement

return Math.min(chargeElement.authorisedAnnualQuantity, reviewChargeReference.chargeReference.volume)
return Math.min(chargeElement.authorisedAnnualQuantity, reviewChargeReference.amendedAuthorisedVolume)
}

function _financialYear (financialYearEnding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,12 @@ function _returnTotal (returnLog) {

function _totalBillableReturns (reviewChargeReference) {
let totalBillableReturns = 0
let totalQuantity = 0

reviewChargeReference.reviewChargeElements.forEach((reviewChargeElement) => {
totalBillableReturns += reviewChargeElement.amendedAllocated
totalQuantity += reviewChargeElement.chargeElement.authorisedAnnualQuantity
})

return `${totalBillableReturns} ML / ${totalQuantity} ML`
return `${totalBillableReturns} ML / ${reviewChargeReference.amendedAuthorisedVolume} ML`
}

function _unmatchedReturns (returnLogs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ async function _fetchReviewChargeElement (reviewChargeElementId) {
.withGraphFetched('reviewChargeReference')
.modifyGraph('reviewChargeReference', (builder) => {
builder.select([
'id'
])
})
.withGraphFetched('reviewChargeReference.chargeReference')
.modifyGraph('reviewChargeReference.chargeReference', (builder) => {
builder.select([
'volume'
'id',
'amendedAuthorisedVolume'
])
})
.withGraphFetched('reviewChargeReference.reviewChargeVersion')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ async function _persistChargeReference (chargeReference, reviewChargeVersionId)
winterDiscount: chargeReference.winter,
abatementAgreement: chargeReference.s126 ?? 1,
twoPartTariffAgreement: chargeReference.s127,
canalAndRiverTrustAgreement: chargeReference.s130
canalAndRiverTrustAgreement: chargeReference.s130,
authorisedVolume: chargeReference.volume,
amendedAuthorisedVolume: chargeReference.volume
}

const { id: reviewChargeReferenceId } = await ReviewChargeReferenceModel.query().insert(data).returning('id')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const tableName = 'review_charge_references'

exports.up = function (knex) {
return knex
.schema
.alterTable(tableName, (table) => {
table.decimal('authorised_volume', null, null).defaultTo(0)
table.decimal('amended_authorised_volume', null, null).defaultTo(0)
})
}

exports.down = function (knex) {
return knex
.schema
.alterTable(tableName, (table) => {
table.dropColumn('authorised_volume')
table.dropColumn('amended_authorised_volume')
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Amend Billable Returns presenter', () => {

describe('when the charge reference has a lower authorised volume than the element', () => {
beforeEach(() => {
reviewChargeElement.reviewChargeReference.chargeReference.volume = 150
reviewChargeElement.reviewChargeReference.amendedAuthorisedVolume = 150
})

it('displays the lower volume from the two', () => {
Expand Down Expand Up @@ -83,12 +83,10 @@ function _reviewChargeElementData () {
},
reviewChargeReference: {
id: '9e5d87d7-073e-420e-b12d-73ca220dd8ef',
amendedAuthorisedVolume: 250,
reviewChargeVersion: {
chargePeriodStartDate: new Date('2022-04-01'),
chargePeriodEndDate: new Date('2022-06-05')
},
chargeReference: {
volume: 250
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ function _licenceData () {
reviewChargeVersionId: 'bd16e7b0-c2a3-4258-b873-b965fd74cdf5',
chargeReferenceId: '82ce8695-5841-41b0-a1e7-d016407adad4',
aggregate: 1,
authorisedVolume: 200,
amendedAuthorisedVolume: 200,
chargeAdjustment: 1,
createdAt: new Date('2024-03-18'),
updatedAt: new Date('2024-03-18'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,10 @@ describe('Fetch Match Details service', () => {
},
reviewChargeReference: {
id: reviewChargeReference.id,
amendedAuthorisedVolume: reviewChargeReference.amendedAuthorisedVolume,
reviewChargeVersion: {
chargePeriodStartDate: reviewChargeVersion.chargePeriodStartDate,
chargePeriodEndDate: reviewChargeVersion.chargePeriodEndDate
},
chargeReference: {
volume: chargeReference.volume
}
}
})
Expand Down Expand Up @@ -161,12 +159,10 @@ describe('Fetch Match Details service', () => {
},
reviewChargeReference: {
id: reviewChargeReference.id,
amendedAuthorisedVolume: reviewChargeReference.amendedAuthorisedVolume,
reviewChargeVersion: {
chargePeriodStartDate: reviewChargeVersion.chargePeriodStartDate,
chargePeriodEndDate: reviewChargeVersion.chargePeriodEndDate
},
chargeReference: {
volume: chargeReference.volume
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ describe('Fetch Review Licence Results Service', () => {
abatementAgreement: reviewChargeReference.abatementAgreement,
winterDiscount: reviewChargeReference.winterDiscount,
twoPartTariffAgreement: reviewChargeReference.twoPartTariffAgreement,
authorisedVolume: reviewChargeReference.authorisedVolume,
amendedAuthorisedVolume: reviewChargeReference.amendedAuthorisedVolume,
createdAt: reviewChargeReference.createdAt,
updatedAt: reviewChargeReference.updatedAt,
chargeReference: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ describe('Persist Allocated Licence to Results service', () => {
// Check the charge reference persisted correctly
// NOTE: As the aggregate is null on the charge reference the service returns 1
const { reviewChargeReferences } = reviewChargeVersions[0]

expect(reviewChargeReferences).to.have.length(1)
expect(reviewChargeReferences[0].reviewChargeVersionId).to.equal(result[0].reviewChargeVersions[0].id)
expect(reviewChargeReferences[0].chargeReferenceId).to.equal(testChargeVersions[0].chargeReferences[0].id)
expect(reviewChargeReferences[0].aggregate).to.equal(1)
expect(reviewChargeReferences[0].authorisedVolume).to.equal(testChargeVersions[0].chargeReferences[0].volume)
expect(reviewChargeReferences[0].amendedAuthorisedVolume).to.equal(
testChargeVersions[0].chargeReferences[0].volume
)

// Check the charge element persisted correctly
const { reviewChargeElements } = reviewChargeReferences[0]
Expand Down Expand Up @@ -158,10 +161,16 @@ describe('Persist Allocated Licence to Results service', () => {
.withGraphFetched('reviewChargeVersions.reviewChargeReferences.reviewChargeElements.reviewReturns')

expect(result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements).to.have.length(1)
expect(result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].chargeElementId).to.equal(testLicence.chargeVersions[0].chargeReferences[0].chargeElements[0].id)

expect(result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].reviewReturns).to.have.length(0)
expect(result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].reviewReturns).to.equal([])
expect(
result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].chargeElementId
).to.equal(testLicence.chargeVersions[0].chargeReferences[0].chargeElements[0].id)

expect(
result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].reviewReturns
).to.have.length(0)
expect(
result[0].reviewChargeVersions[0].reviewChargeReferences[0].reviewChargeElements[0].reviewReturns
).to.equal([])
})
})

Expand Down Expand Up @@ -254,6 +263,7 @@ function _generateData (returnMatched = true) {
s126: null,
s127: true,
s130: false,
volume: 200,
chargeElements: [
{
id: generateUUID(),
Expand Down
6 changes: 5 additions & 1 deletion test/support/helpers/review-charge-reference.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const ReviewChargeReferenceModel = require('../../../app/models/review-charge-re
* - `abatementAgreement` - 0
* - `chargeAdjustment - 1
* - `amendedChargeAdjustment` - 1
* - `authorisedVolume` - 50
* - `amendedAuthorisedVolume` - 50
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
Expand Down Expand Up @@ -54,7 +56,9 @@ function defaults (data = {}) {
winterDiscount: false,
abatementAgreement: 0,
chargeAdjustment: 0,
amendedChargeAdjustment: 0
amendedChargeAdjustment: 0,
authorisedVolume: 50,
amendedAuthorisedVolume: 50
}

return {
Expand Down
Loading