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

Refactor to use new view based RETURNS models #568

Merged
merged 6 commits into from
Dec 6, 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
1 change: 0 additions & 1 deletion app/models/legacy-base.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class LegacyBaseModel extends BaseModel {
static get modelPaths () {
const currentPath = __dirname
return [
path.join(currentPath, 'returns'),
path.join(currentPath, 'water'),
path.join(currentPath, 'crm-v2'),
path.join(currentPath, 'idm')
Expand Down
46 changes: 0 additions & 46 deletions app/models/returns/line.model.js

This file was deleted.

46 changes: 0 additions & 46 deletions app/models/returns/return.model.js

This file was deleted.

16 changes: 0 additions & 16 deletions app/models/returns/returns-base.model.js

This file was deleted.

54 changes: 0 additions & 54 deletions app/models/returns/version.model.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
'use strict'

/**
* Fetches SROC Returns linked to licences flagged for inclusion in next SROC 2PT billing
* @module FetchReturnsForLicenceService
* Fetches SROC return logs linked to licences flagged for inclusion in next SROC 2PT billing
* @module FetchReturnLogsForLicenceService
*/

const { ref } = require('objection')

const ReturnModel = require('../../../models/returns/return.model')
const ReturnLogModel = require('../../../models/return-log.model.js')

/**
* Fetch all SROC returns to be processed as part of two-part-tariff billing
* Fetch all SROC return logs to be processed as part of two-part-tariff billing
* *
* @param {String} licenceRef The reference of the licence that the return relates to
* @param {String} licenceRef The reference of the licence that the return log relates to
* @param {Object} billingPeriod Object with a `startDate` and `endDate` property representing the period being billed
*
* @returns {Object} Contains an array of Returns and the associated current Version, and Lines if they exist
* @returns {Object} Contains an array of `returnLogs` and the associated current `returnSubmissions`, and
* `returnSubmissionLines` if they exist
*/
async function go (licenceRef, billingPeriod) {
return _fetch(licenceRef, billingPeriod)
}

async function _fetch (licenceRef, billingPeriod) {
const returns = await ReturnModel.query()
const returnLogs = await ReturnLogModel.query()
.select([
'returnId',
'id',
'returnRequirement',
ref('metadata:description').castText().as('description'),
'startDate',
Expand All @@ -40,37 +41,37 @@ async function _fetch (licenceRef, billingPeriod) {
ref('metadata:purposes').as('purposes')
])
.where('licenceRef', licenceRef)
// water-abstraction-service filters out old returns in this way: see `src/lib/services/returns/api-connector.js`
// water-abstraction-service filters out old return logs in this way: see `src/lib/services/returns/api-connector.js`
.where('startDate', '>=', '2008-04-01')
.where('startDate', '<=', billingPeriod.endDate)
.where('endDate', '>=', billingPeriod.startDate)
.whereJsonPath('metadata', '$.isTwoPartTariff', '=', true)
.orderBy('startDate', 'ASC')
.orderBy('returnRequirement', 'ASC')
.withGraphFetched('versions')
.modifyGraph('versions', builder => {
.withGraphFetched('returnSubmissions')
.modifyGraph('returnSubmissions', builder => {
builder
.select([
'versionId',
'id',
'nilReturn'
])
.where('versions.current', true)
.where('returnSubmissions.current', true)
})
.withGraphFetched('versions.lines')
.modifyGraph('versions.lines', builder => {
.withGraphFetched('returnSubmissions.returnSubmissionLines')
.modifyGraph('returnSubmissions.returnSubmissionLines', builder => {
builder
.select([
'lineId',
'id',
'startDate',
'endDate',
'quantity'
])
.where('lines.quantity', '>', 0)
.where('lines.startDate', '<=', billingPeriod.endDate)
.where('lines.endDate', '>=', billingPeriod.startDate)
.where('returnSubmissionLines.quantity', '>', 0)
.where('returnSubmissionLines.startDate', '<=', billingPeriod.endDate)
.where('returnSubmissionLines.endDate', '>=', billingPeriod.startDate)
})

return returns
return returnLogs
}

module.exports = {
Expand Down
18 changes: 9 additions & 9 deletions app/services/check/allocate-returns-volumes.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

function go (chargeReference) {
let returnVolumeInMegalitres
// Loop through each return
chargeReference.returns.forEach((returnData) => {
// The volumes on the return are in Cubic Metres so we convert to Megalitres to match the charge version data
returnVolumeInMegalitres = returnData.volumes.total / 1000
// Loop through each return log
chargeReference.returnLogs.forEach((returnLogData) => {
// The volumes on the return log are in Cubic Metres so we convert to Megalitres to match the charge version data
returnVolumeInMegalitres = returnLogData.volumes.total / 1000
// Loop through each charge element
chargeReference.chargeElements.forEach((chargeElement) => {
if (!chargeElement.allocatedReturnVolume) {
chargeElement.allocatedReturnVolume = 0
}
// Check the chargeElement is not already fully allocated
if (chargeElement.allocatedReturnVolume < chargeElement.authorisedAnnualQuantity) {
// Check if the return's purpose and abstraction period match the charge element
if (_matchReturnToElement(returnData.metadata, chargeElement)) {
// Check if the return log's purpose and abstraction period match the charge element
if (_matchReturnToElement(returnLogData.metadata, chargeElement)) {
// Calculate how much is left to allocated to the ChargeElement from the return
let volumeLeftToAllocate = chargeElement.authorisedAnnualQuantity - chargeElement.allocatedReturnVolume
// Check for the case that the return does not cover the full allocation
// Check for the case that the return log does not cover the full allocation
if (returnVolumeInMegalitres < volumeLeftToAllocate) {
volumeLeftToAllocate = returnVolumeInMegalitres
}
Expand All @@ -35,9 +35,9 @@ function go (chargeReference) {

if (returnVolumeInMegalitres > 0) {
// Convert any remaining volume back to Cubic Metres and add it to the volumes object
returnData.volumes.unallocated = returnVolumeInMegalitres * 1000
returnLogData.volumes.unallocated = returnVolumeInMegalitres * 1000
} else {
returnData.volumes.unallocated = 0
returnLogData.volumes.unallocated = 0
}
})
}
Expand Down
Loading
Loading