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 determine bill licence flags service #1507

Merged
merged 15 commits into from
Nov 27, 2024
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 @@ -7,10 +7,14 @@

const BillLicenceModel = require('../../models/bill-licence.model.js')
const LegacyDeleteBillLicenceRequest = require('../../requests/legacy/delete-bill-licence.request.js')
const ProcessBillingFlagService = require('../licences/supplementary/process-billing-flag.service.js')

/**
* Orchestrates the removing of a bill licence from a bill run
*
* This involves checking if the licence needs to be flagged for supplementary billing by calling the
* `ProcessBillingFlagService` and then to handle deleting the bill licence itself calling the legacy service
*
* @param {string} billLicenceId - UUID of the bill licence to be removed
* @param {object} user - Instance of `UserModel` that represents the user making the request
*
Expand All @@ -19,6 +23,11 @@ const LegacyDeleteBillLicenceRequest = require('../../requests/legacy/delete-bil
async function go(billLicenceId, user) {
const { bill } = await _fetchBillLicence(billLicenceId)

const payload = {
billLicenceId
}

await ProcessBillingFlagService.go(payload)
await LegacyDeleteBillLicenceRequest.send(billLicenceId, user)

return `/billing/batch/${bill.billRunId}/processing?invoiceId=${bill.id}`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict'

/**
* Determines which supplementary billing flag should be added to a licence removed from a bill run
* @module DetermineBillLicenceFlagsService
*/

const BillLicenceModel = require('../../../models/bill-licence.model.js')

/**
* Determines which supplementary billing flag should be added to a licence that is removed from a bill run.
*
* When a user removes a licence from a bill run, a flag needs to be added to the licence so it can be picked up in the
* next supplementary bill run.
* This service handles licences being removed from:
* - An annual sroc bill run (pre-sroc annual bill runs can no longer be run)
* - Sroc supplementary bill runs
* - Pre sroc supplementary bill runs
* - Two-part tariff annual bill runs
* - Two-part tariff supplementary bill runs
*
* Removing a licence from the review screen of a pre-sroc two-part tariff supplementary bill run is handled in a
* separate service (DetermineLicenceFlagsService).
*
* To determine the flags, the service first fetches the licence information using the billLicenceId passed to it. This
* includes details of which bill run it is being removed from.
*
* The service then checks which flags are already held on the licence so we can maintain these. Then depending on which
* bill run batch type and or scheme the billLicenceId has been removed from we set the corresponding flag to true.
*
* The flags are then passed back to the service, where it will work out which years the two-part tariff flags should be
* applied to if necessary.
*
* NOTE: Unlike pre-sroc and sroc flags (which apply at the licence level), two-part tariff flags are year specific.
* They are stored in the `LicenceSupplementaryYears` table for each affected year.
*
* @param {string} billLicenceId - The UUID of the bill licence being removed from a bill run
*
* @returns {object} - An object containing the related licenceId, regionId, start and end date and
* licence supplementary billing flags
*/
async function go(billLicenceId) {
const { licence, bill, licenceId } = await _fetchBillLicence(billLicenceId)

const { flagForPreSrocSupplementary, flagForSrocSupplementary, flagForTwoPartTariffSupplementary } = _updateFlags(
bill.billRun,
licence
)

const result = {
licenceId,
regionId: licence.regionId,
startDate: new Date(`${bill.billRun.toFinancialYearEnding - 1}-04-01`),
endDate: new Date(`${bill.billRun.toFinancialYearEnding}-03-31`),
flagForPreSrocSupplementary,
flagForSrocSupplementary,
flagForTwoPartTariffSupplementary
}

return result
}

async function _fetchBillLicence(billLicenceId) {
return BillLicenceModel.query()
.findById(billLicenceId)
.select('licenceId')
.withGraphFetched('licence')
.modifyGraph('licence', (builder) => {
builder.select(['regionId', 'includeInSrocBilling', 'includeInPresrocBilling'])
})
.withGraphFetched('bill')
.modifyGraph('bill', (builder) => {
builder.select(['id'])
})
.withGraphFetched('bill.billRun')
.modifyGraph('bill.billRun', (builder) => {
builder.select(['id', 'batchType', 'scheme', 'toFinancialYearEnding'])
})
}

function _updateFlags(billRun, licence) {
// Set the flags to what they currently are on the licence
let flagForSrocSupplementary = licence.includeInSrocBilling
let flagForPreSrocSupplementary = licence.includeInPresrocBilling === 'yes'
let flagForTwoPartTariffSupplementary = false

if (billRun.batchType === 'two_part_tariff' && billRun.scheme === 'sroc') {
flagForTwoPartTariffSupplementary = true
} else if (billRun.scheme === 'alcs') {
flagForPreSrocSupplementary = true
} else {
flagForSrocSupplementary = true
}

return { flagForPreSrocSupplementary, flagForSrocSupplementary, flagForTwoPartTariffSupplementary }
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

const DetermineBillingYearsService = require('./determine-billing-years.service.js')
const DetermineExistingBillRunYearsService = require('./determine-existing-bill-run-years.service.js')
const DetermineBillLicenceFlagsService = require('./determine-bill-licence-flags.service.js')
const DetermineChargeVersionFlagsService = require('./determine-charge-version-flags.service.js')
const DetermineExistingBillRunYearsService = require('./determine-existing-bill-run-years.service.js')
const DetermineLicenceFlagsService = require('./determine-licence-flags.service.js')
const DetermineReturnLogFlagsService = require('./determine-return-log-flags.service.js')
const DetermineWorkflowFlagsService = require('./determine-workflow-flags.service.js')
Expand Down Expand Up @@ -54,6 +55,8 @@ async function _determineFlags(payload) {
return DetermineReturnLogFlagsService.go(payload.returnId)
} else if (payload.workflowId) {
return DetermineWorkflowFlagsService.go(payload.workflowId)
} else if (payload.billLicenceId) {
return DetermineBillLicenceFlagsService.go(payload.billLicenceId)
} else if (payload.licenceId) {
return DetermineLicenceFlagsService.go(payload.licenceId, payload.scheme)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const BillLicenceHelper = require('../../support/helpers/bill-licence.helper.js'

// Things we need to stub
const LegacyDeleteBillLicenceRequest = require('../../../app/requests/legacy/delete-bill-licence.request.js')
const ProcessBillingFlagService = require('../../../app/services/licences/supplementary/process-billing-flag.service.js')

// Thing under test
const SubmitRemoveBillLicenceService = require('../../../app/services/bill-licences/submit-remove-bill-licence.service.js')
Expand All @@ -24,19 +25,27 @@ describe('Submit Remove Bill Licence service', () => {
let bill
let billLicence
let legacyDeleteBillLicenceRequestStub
let ProcessBillingFlagServiceStub

beforeEach(async () => {
bill = await BillHelper.add()
billLicence = await BillLicenceHelper.add({ billId: bill.id })

legacyDeleteBillLicenceRequestStub = Sinon.stub(LegacyDeleteBillLicenceRequest, 'send').resolves()
ProcessBillingFlagServiceStub = Sinon.stub(ProcessBillingFlagService, 'go').resolves()
})

afterEach(() => {
Sinon.restore()
})

describe('when called', () => {
it('calls the "ProcessBillingFlagService" to check if the licence needs a supplementary billing flag', async () => {
await SubmitRemoveBillLicenceService.go(billLicence.id, user)

expect(ProcessBillingFlagServiceStub.called).to.be.true()
})

it('sends a request to the legacy service to delete the bill licence', async () => {
await SubmitRemoveBillLicenceService.go(billLicence.id, user)

Expand Down
Loading
Loading