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

Add validation for amend billable returns page #947

Merged
merged 8 commits into from
Apr 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function go (billRun, reviewChargeElement, licenceId) {
reviewChargeElement.chargeElement,
reviewChargeElement.reviewChargeReference.reviewChargeVersion
),
authorisedQuantity: reviewChargeElement.chargeElement.authorisedAnnualQuantity,
reviewChargeElementId: reviewChargeElement.id
},
billRun: {
Expand All @@ -38,10 +37,24 @@ function go (billRun, reviewChargeElement, licenceId) {
reviewChargeElement.reviewChargeReference.reviewChargeVersion.chargePeriodEndDate
)
},
licenceId
licenceId,
authorisedQuantity: _authorisedQuantity(reviewChargeElement)
}
}

/**
* The user can only enter a volume on the billable returns that is less than the authorised volume. The authorised
* volume is either the authorised volume on the charge element or the authorised volume on the charge reference.
* Whichever is lower.
*/
function _authorisedQuantity (reviewChargeElement) {
const { chargeElement, reviewChargeReference } = reviewChargeElement
const chargeElementQuantity = chargeElement.authorisedAnnualQuantity
const chargeReferenceQuantity = reviewChargeReference.chargeReference.volume

return chargeElementQuantity > chargeReferenceQuantity ? chargeReferenceQuantity : chargeElementQuantity
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
}

function _financialYear (financialYearEnding) {
const startYear = financialYearEnding - 1
const endYear = financialYearEnding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ async function _fetchReviewChargeElement (reviewChargeElementId) {
'id'
])
})
.withGraphFetched('reviewChargeReference.chargeReference')
.modifyGraph('reviewChargeReference.chargeReference', (builder) => {
builder.select([
'volume'
])
})
.withGraphFetched('reviewChargeReference.reviewChargeVersion')
.modifyGraph('reviewChargeReference.reviewChargeVersion', (builder) => {
builder.select([
Expand Down
6 changes: 3 additions & 3 deletions app/views/bill-runs/amend-billable-returns.njk
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
items: [
{
id: 'authorised-quantity',
value: chargeElement.authorisedQuantity,
html: 'Authorised ' + chargeElement.authorisedQuantity + 'ML'
value: authorisedQuantity,
html: 'Authorised ' + authorisedQuantity + 'ML'
},
{
id: 'custom-quantity',
Expand All @@ -96,7 +96,7 @@
}) }}

{# Hidden input for authorised volume #}
<input type="hidden" name="authorisedVolume" value="{{ chargeElement.authorisedQuantity }}">
<input type="hidden" name="authorisedVolume" value="{{ authorisedQuantity }}">

{{ govukButton({ text: 'Confirm' }) }}
</form>
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/bill-runs.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ describe('Bill Runs controller', () => {
Sinon.stub(AmendBillableReturnsService, 'go').resolves({
chargeElement: {
description: 'Spray irrigation - storage, Abstraction from borehole at Chipping Norton',
dates: '25 July 2022 to 29 December 2022',
authorisedQuantity: 40
dates: '25 July 2022 to 29 December 2022'
},
billRun: {
financialYear: '2022 to 2023'
},
chargeVersion: {
chargePeriod: '1 April 2022 to 31 March 2023'
}
},
authorisedQuantity: 40
})
})

Expand Down
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('Amend Billable Returns presenter', () => {
chargeElement: {
description: 'Trickle Irrigation - Direct',
dates: ['1 April 2022 to 5 June 2022'],
authorisedQuantity: 200,
reviewChargeElementId: 'b4d70c89-de1b-4f68-a47f-832b338ac044'
},
billRun: {
Expand All @@ -33,7 +32,8 @@ describe('Amend Billable Returns presenter', () => {
chargeVersion: {
chargePeriod: '1 April 2022 to 5 June 2022'
},
licenceId: '5aa8e752-1a5c-4b01-9112-d92a543b70d1'
licenceId: '5aa8e752-1a5c-4b01-9112-d92a543b70d1',
authorisedQuantity: 150
})
})
})
Expand Down Expand Up @@ -69,6 +69,9 @@ function _reviewChargeElementData () {
reviewChargeVersion: {
chargePeriodStartDate: new Date('2022-04-01'),
chargePeriodEndDate: new Date('2022-06-05')
},
chargeReference: {
volume: 150
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { expect } = Code
// Test helpers
const BillRunHelper = require('../../../support/helpers/bill-run.helper.js')
const ChargeElementHelper = require('../../../support/helpers/charge-element.helper.js')
const ChargeReferenceHelper = require('../../../support/helpers/charge-reference.helper.js')
const DatabaseSupport = require('../../../support/database.js')
const ReviewChargeElementHelper = require('../../../support/helpers/review-charge-element.helper.js')
const ReviewChargeElementReturnHelper = require('../../../support/helpers/review-charge-element-return.helper.js')
Expand Down Expand Up @@ -40,12 +41,14 @@ describe('Fetch Match Details service', () => {
describe('and a valid review charge element', () => {
let reviewChargeElement
let chargeElement
let chargeReference
let reviewChargeVersion
let reviewChargeReference

beforeEach(async () => {
reviewChargeVersion = await ReviewChargeVersionHelper.add()
reviewChargeReference = await ReviewChargeReferenceHelper.add({ reviewChargeVersionId: reviewChargeVersion.id })
chargeReference = await ChargeReferenceHelper.add()
reviewChargeReference = await ReviewChargeReferenceHelper.add({ reviewChargeVersionId: reviewChargeVersion.id, chargeReferenceId: chargeReference.id })

chargeElement = await ChargeElementHelper.add({ chargeReferenceId: reviewChargeReference.chargeReferenceId })
reviewChargeElement = await ReviewChargeElementHelper.add({ reviewChargeReferenceId: reviewChargeReference.id, chargeElementId: chargeElement.id })
Expand Down Expand Up @@ -123,6 +126,9 @@ describe('Fetch Match Details service', () => {
reviewChargeVersion: {
chargePeriodStartDate: reviewChargeVersion.chargePeriodStartDate,
chargePeriodEndDate: reviewChargeVersion.chargePeriodEndDate
},
chargeReference: {
volume: chargeReference.volume
}
}
})
Expand Down Expand Up @@ -158,6 +164,9 @@ describe('Fetch Match Details service', () => {
reviewChargeVersion: {
chargePeriodStartDate: reviewChargeVersion.chargePeriodStartDate,
chargePeriodEndDate: reviewChargeVersion.chargePeriodEndDate
},
chargeReference: {
volume: chargeReference.volume
}
}
})
Expand Down
Loading