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

Consider Revoked, Expired, Lapsed licences dates when calculating charges #204

Merged
merged 7 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@ function go (chargeVersion, financialYearEnding) {
throw new Error(`Charge version is outside billing period ${financialYearEnding}`)
}

const chargeVersionStartDate = chargeVersion.startDate
const latestStartDateTimestamp = Math.max(financialYearStartDate, chargeVersionStartDate)
const latestStartDateTimestamp = Math.max(
financialYearStartDate,
chargeVersion.startDate,
chargeVersion.licence.startDate
)

// If the charge version has no end date then use the financial year end date instead
// If the end date is null then use the financial year end date instead as Math.min() will count nulls as 0
const chargeVersionEndDate = chargeVersion.endDate || financialYearEndDate
const earliestEndDateTimestamp = Math.min(financialYearEndDate, chargeVersionEndDate)
const licenceExpiredDate = chargeVersion.licence.expiredDate || financialYearEndDate
const licencelapsedDate = chargeVersion.licence.lapsedDate || financialYearEndDate
const licencerevokedDate = chargeVersion.licence.revokedDate || financialYearEndDate

const earliestEndDateTimestamp = Math.min(
financialYearEndDate,
chargeVersionEndDate,
licenceExpiredDate,
licencelapsedDate,
licencerevokedDate
)

return {
startDate: new Date(latestStartDateTimestamp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ async function _fetch (regionId, billingPeriod) {
'licenceRef',
'isWaterUndertaker',
ref('licences.regions:historicalAreaCode').castText().as('historicalAreaCode'),
ref('licences.regions:regionalChargeArea').castText().as('regionalChargeArea')
ref('licences.regions:regionalChargeArea').castText().as('regionalChargeArea'),
'startDate',
'expiredDate',
'lapsedDate',
'revokedDate'
])
})
.withGraphFetched('licence.region')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('Determine charge period service', () => {
beforeEach(() => {
chargeVersion = {
startDate: new Date('2022-05-01'),
endDate: null
endDate: null,
licence: { startDate: new Date('2022-01-01') }
}
})

Expand Down Expand Up @@ -53,7 +54,8 @@ describe('Determine charge period service', () => {
beforeEach(() => {
chargeVersion = {
startDate: new Date('2022-02-01'),
endDate: null
endDate: null,
licence: { startDate: new Date('2022-01-01') }
}
})

Expand Down Expand Up @@ -90,6 +92,7 @@ describe('Determine charge period service', () => {

describe('and the charge version has an end date that is inside the financial period', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-01-01') }
chargeVersion.endDate = new Date('2022-05-31')
})

Expand All @@ -102,20 +105,77 @@ describe('Determine charge period service', () => {
})

describe('and the charge version does not have an end date', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-01-01') }
})

it('returns the financial start and end dates', () => {
const result = DetermineChargePeriodService.go(chargeVersion, financialYear.yearEnding)

expect(result.startDate).to.equal(financialYear.startDate)
expect(result.endDate).to.equal(financialYear.endDate)
})
})

describe('and the licence start date is after the charge versions and inside the financial period', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-08-31') }
})

it('returns the licence start and financial end date', () => {
const result = DetermineChargePeriodService.go(chargeVersion, financialYear.yearEnding)

expect(result.startDate).to.equal(chargeVersion.licence.startDate)
expect(result.endDate).to.equal(financialYear.endDate)
})
})

describe('and the licence revoked date is inside the financial period', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-01-01'), revokedDate: new Date('2022-08-01') }
})

it('returns the financial start and licence revoked end date', () => {
const result = DetermineChargePeriodService.go(chargeVersion, financialYear.yearEnding)

expect(result.startDate).to.equal(financialYear.startDate)
expect(result.endDate).to.equal(chargeVersion.licence.revokedDate)
})
})

describe('and the licence lapsed date is inside the financial period', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-01-01'), lapsedDate: new Date('2022-08-31') }
})

it('returns the financial start and licence lapsed end date', () => {
const result = DetermineChargePeriodService.go(chargeVersion, financialYear.yearEnding)

expect(result.startDate).to.equal(financialYear.startDate)
expect(result.endDate).to.equal(chargeVersion.licence.lapsedDate)
})
})

describe('and the licence expired date is inside the financial period', () => {
beforeEach(() => {
chargeVersion.licence = { startDate: new Date('2022-01-01'), expiredDate: new Date('2022-07-01') }
})

it('returns the financial start and licence expired end date', () => {
const result = DetermineChargePeriodService.go(chargeVersion, financialYear.yearEnding)

expect(result.startDate).to.equal(financialYear.startDate)
expect(result.endDate).to.equal(chargeVersion.licence.expiredDate)
})
})
})

describe('financial period starts before the charge version', () => {
beforeEach(() => {
chargeVersion = {
startDate: new Date('2022-05-01'),
endDate: null
endDate: null,
licence: { startDate: new Date('2022-01-01') }
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Determine minimum charge service', () => {
changeReasonId: changeReason.changeReasonId
})
chargeVersion.changeReason = changeReason
chargeVersion.licence = { startDate: new Date('2022-01-01') }
})

it('returns true', async () => {
Expand All @@ -49,6 +50,7 @@ describe('Determine minimum charge service', () => {
changeReasonId: changeReason.changeReasonId
})
chargeVersion.changeReason = changeReason
chargeVersion.licence = { startDate: new Date('2022-01-01') }
})

it('returns false', async () => {
Expand All @@ -67,6 +69,7 @@ describe('Determine minimum charge service', () => {
changeReasonId: changeReason.changeReasonId
})
chargeVersion.changeReason = changeReason
chargeVersion.licence = { startDate: new Date('2022-01-01') }
})

it('returns false', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('Fetch Charge Versions service', () => {
// This creates an SROC charge version with a start date before the billing period. This would have been
// picked up by a previous bill run
const alcsChargeVersion = await ChargeVersionHelper.add(
{ startDate: new Date(2022, 2, 31), licenceId } // 2022-03-01 - Months are zero indexed :-)
{ startDate: new Date('2022-03-01'), licenceId }
)
testRecords = [alcsChargeVersion]
})
Expand All @@ -276,7 +276,7 @@ describe('Fetch Charge Versions service', () => {
// This creates an SROC charge version with a start date after the billing period. This will be picked in
// next years bill runs
const alcsChargeVersion = await ChargeVersionHelper.add(
{ startDate: new Date(2023, 3, 1), licenceId } // 2023-04-01 - Months are zero indexed :-)
{ startDate: new Date('2023-04-01'), licenceId }
)
testRecords = [alcsChargeVersion]
})
Expand Down
5 changes: 4 additions & 1 deletion test/support/helpers/water/licence.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const LicenceModel = require('../../../../app/models/water/licence.model.js')
*
* - `licenceRef` - 01/123
* - `regionId` - bd114474-790f-4470-8ba4-7b0cc9c225d7
* - `regions` - { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' }
* - `startDate` - new Date('2022-01-01')
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
Expand All @@ -38,7 +40,8 @@ function defaults (data = {}) {
const defaults = {
licenceRef: '01/123',
regionId: 'bd114474-790f-4470-8ba4-7b0cc9c225d7',
regions: { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' }
regions: { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' },
startDate: new Date('2022-01-01')
}

return {
Expand Down