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

Implement alternate version of 2PT matching #458

Closed
wants to merge 145 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
bfbc0ae
Implement alternate version of 2PT matching
Cruikshanks Oct 10, 2023
fa78590
Fix abstraction period matching
Cruikshanks Oct 12, 2023
03787fe
Debugging an issue in pre-prod
Cruikshanks Oct 12, 2023
470f20d
Fixed allocation issue
Cruikshanks Oct 12, 2023
e55f5a1
Record allocated lines against elements
Cruikshanks Oct 12, 2023
49a7702
Add 2pt check for single licence
Cruikshanks Oct 12, 2023
dd472dc
Tweak to allocation issue description
Cruikshanks Oct 12, 2023
5249ff1
Add user friendly IDs
Cruikshanks Oct 13, 2023
8bf1d91
Handle part allocating lines
Cruikshanks Oct 15, 2023
1796689
Oops! Fix line allocating
Cruikshanks Oct 15, 2023
0a55ded
Missed adding a test ID
Cruikshanks Oct 15, 2023
25a45d9
Ensure consistent results in returns
Cruikshanks Oct 15, 2023
5f18e19
Move test id and unallocated line assignment
Cruikshanks Oct 15, 2023
5e535f9
Handle returns with multiple `purposes`
Jozzey Oct 19, 2023
c5b069d
Tidy up
Jozzey Oct 19, 2023
57f8062
Refactor
Jozzey Oct 19, 2023
403c8b4
Tidy up comment
Jozzey Oct 19, 2023
0cd37ec
Refactor
Jozzey Oct 20, 2023
7103462
Update route to include `auth`
Jozzey Oct 23, 2023
cac1f2d
Add aggregate value to Charge Element
Jozzey Oct 25, 2023
dfebfd4
We blew up pre-prod
Cruikshanks Nov 9, 2023
9d695cd
Gotcha
Cruikshanks Nov 9, 2023
9665770
Fixed????
Cruikshanks Nov 9, 2023
fe09fc8
alan was hacking
Cruikshanks Nov 9, 2023
0aad413
WIP
Jozzey Nov 9, 2023
70ced68
Start of fetch-charge-versions unit test
Beckyrose200 Nov 10, 2023
8e53b84
Fix broken require
Cruikshanks Nov 12, 2023
3ee400d
Fix merge issues
Cruikshanks Nov 12, 2023
45d545f
Add tests for `fetch-returns-for-licence` service
Jozzey Nov 13, 2023
f20ea5b
Co-authored-by: Jason Claxton <[email protected]>
Cruikshanks Nov 15, 2023
7b70b53
WIP start of allocation functions
Cruikshanks Nov 16, 2023
59b674f
Merge remote-tracking branch 'origin/main' into alternate-2pt-engine
Jozzey Nov 16, 2023
5e86308
WIP
Jozzey Nov 16, 2023
0ed7e34
Fetch charge versions tests
Beckyrose200 Nov 17, 2023
c9253d3
Merge branch 'main' into alternate-2pt-engine
Jozzey Nov 20, 2023
b9a261b
Merge branch 'main' into alternate-2pt-engine
Cruikshanks Nov 21, 2023
8de73b0
Refactoring to FetchLicencesService
Cruikshanks Nov 21, 2023
e6e802b
Refactor to services
Cruikshanks Nov 21, 2023
b3d4479
Moved test migration into legacy folder
Beckyrose200 Nov 22, 2023
ea9f3ca
Creating the tables to persist engine data
Beckyrose200 Nov 22, 2023
afb09bd
WIP
Beckyrose200 Nov 22, 2023
7a11c89
WIP
Jozzey Nov 23, 2023
c6d07d6
Merge branch 'main' into alternate-2pt-engine
Jozzey Nov 23, 2023
85b298d
Include extra return dates in old check endpoint
Cruikshanks Nov 23, 2023
9ccf3ac
Remove console.logs and add aggregate to output
Cruikshanks Nov 23, 2023
4abf3c5
IGNORE ME!
Cruikshanks Nov 23, 2023
14a4598
KEEP IGNORING THIS
Cruikshanks Nov 24, 2023
170548d
WIP on issues logic
Cruikshanks Nov 24, 2023
f47c9c1
WIP
Jozzey Nov 24, 2023
2bcda90
Remove sillyness
Jozzey Nov 24, 2023
62736fd
Do what I'm sure Becky was telling me to do :grin:
Jozzey Nov 24, 2023
4bd8354
WIP Some more issues and start of statuses
Cruikshanks Nov 25, 2023
f23f5a4
Issue fixes
Cruikshanks Nov 26, 2023
a84b9d6
ANOTHER IGNORE ME
Cruikshanks Nov 26, 2023
731fb1c
ONE MORE IGNORE ME
Cruikshanks Nov 26, 2023
59496eb
ANOTHER ONE TO BE IGNORED!
Cruikshanks Nov 26, 2023
21e140c
WIP transforming to new models
Cruikshanks Nov 27, 2023
45a79f9
WIP finished the transforming of data
Cruikshanks Nov 27, 2023
6ed9351
Hack for Return split over charge references
Cruikshanks Nov 27, 2023
d849ade
Move DetermineIssuesService to /check
Cruikshanks Nov 27, 2023
dfc2b0a
Set abs outside period & charge dates overlap
Cruikshanks Nov 27, 2023
fdfe841
Update matchAndAllocate in check to follow 2pt v.
Cruikshanks Nov 27, 2023
cd59554
Simplify getting the nil return value
Cruikshanks Nov 27, 2023
be6034c
One more tweak for nil return
Cruikshanks Nov 27, 2023
0b0b69b
Also make same nil return tweaks to check version
Cruikshanks Nov 27, 2023
a837869
Format the output to match the scenario spreadsheet
Cruikshanks Nov 27, 2023
4dd0e97
Merge remote-tracking branch 'origin/main' into alternate-2pt-engine
Cruikshanks Nov 28, 2023
b4a4e40
Add authorised to element output
Cruikshanks Nov 28, 2023
4613552
Fix return split over charge version checker
Cruikshanks Nov 28, 2023
17a13db
Fix nil return flag
Cruikshanks Nov 28, 2023
97ce342
Update property to match table column name
Cruikshanks Nov 28, 2023
2040aba
Add status to returns in scenario formatter
Cruikshanks Nov 28, 2023
5b335a5
Fix Overlap of charge dates not going into review
Cruikshanks Nov 29, 2023
e2e1bb4
Debugging anglian falling over
Cruikshanks Nov 29, 2023
62b40e8
Fix issues with CE's with no returns property
Cruikshanks Nov 29, 2023
8cd727b
Fix the other places!
Cruikshanks Nov 29, 2023
e3aa7c6
Doh! Rushing things now
Cruikshanks Nov 29, 2023
c170bd7
Now got to deal with this issue in the formatter
Cruikshanks Nov 29, 2023
9fb844d
And another one
Cruikshanks Nov 29, 2023
c03833c
Merge branch 'main' into alternate-2pt-engine
Cruikshanks Dec 1, 2023
76fa2ca
Merge branch 'main' into alternate-2pt-engine
Cruikshanks Dec 4, 2023
c96a298
Fix wrong link in download button
Cruikshanks Dec 4, 2023
a12cd61
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 7, 2023
c1a5682
Fix all the merge issues
Jozzey Dec 7, 2023
302130f
Fix `fetch-charge-versions` service test
Jozzey Dec 7, 2023
698de27
Remove console.log
Jozzey Dec 7, 2023
1d5e9a2
Fix Alan's dodgy test
Jozzey Dec 7, 2023
f00bf48
Fix linting
Jozzey Dec 7, 2023
71e7eef
Adding charge reference allocation
Jozzey Dec 11, 2023
9f004c9
persisting data wip
Beckyrose200 Dec 15, 2023
aec2cba
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 15, 2023
59b0de6
Put `licenceId` back in `fetch-licences` service while we are hacking
Jozzey Dec 15, 2023
5e296ba
Update `fetch-charge-versions` test to avoid issues when merging new …
Jozzey Dec 15, 2023
3b4f41c
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 15, 2023
218a6bb
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 15, 2023
f6694ab
Merge remote-tracking branch 'origin/main' into alternate-2pt-engine
Cruikshanks Dec 15, 2023
0bf3167
Remove check services no longer used
Cruikshanks Dec 15, 2023
60df1da
Update to use completed service
Cruikshanks Dec 15, 2023
896583b
Merge branch 'main' into alternate-2pt-engine
Cruikshanks Dec 15, 2023
10020e0
Merge branch 'main' into alternate-2pt-engine
Cruikshanks Dec 15, 2023
f0c1d05
Merge remote-tracking branch 'origin/main' into alternate-2pt-engine
Cruikshanks Dec 15, 2023
4f0aed7
Merge remote-tracking branch 'origin/main' into alternate-2pt-engine
Cruikshanks Dec 16, 2023
534008a
Fix merge conflict not completed
Cruikshanks Dec 16, 2023
f35af51
Merge branch 'main' into alternate-2pt-engine
Beckyrose200 Dec 18, 2023
6836353
WIP
Jozzey Dec 18, 2023
6ffedc1
WIP
Jozzey Dec 18, 2023
02af93e
Tidy up
Jozzey Dec 18, 2023
e6d36c2
Fix `transform-allocated-licences...` service
Jozzey Dec 19, 2023
b712417
Add addional data items to `TransformAllocatedLicencesToResultsService`
Jozzey Dec 19, 2023
f4b1790
WIP
Jozzey Dec 19, 2023
5ef217c
Add Rebecca's persisting stuff
Jozzey Dec 20, 2023
1e85dbc
Fix element allocated volume
Jozzey Dec 20, 2023
a7fc071
Update migration to increase decimal places
Jozzey Dec 20, 2023
f024f93
Rename persisting service
Jozzey Dec 20, 2023
723c94d
WIP
Jozzey Dec 20, 2023
38204d6
Fix name of persist service
Jozzey Dec 20, 2023
7a70461
Refactoring persistAllocatedLicenceToResults service
Beckyrose200 Dec 20, 2023
d483a87
Stop things breaking
Jozzey Dec 20, 2023
79bebac
Make `chargeVersionId` & `chargeReferenceId` nullable
Jozzey Dec 21, 2023
53fe594
Get the data persisting properly
Jozzey Dec 21, 2023
2e3a55a
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 21, 2023
14d5d62
Refactor the persisting of the data
Jozzey Dec 21, 2023
bf16ae7
Refactor to generate `reviewReturnResultId` in prep service
Jozzey Dec 22, 2023
561f549
Fix typo
Jozzey Dec 22, 2023
7cb00f1
Prep before merging in changes
Jozzey Dec 22, 2023
afa4529
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 22, 2023
160e162
Update `prepare-licences` with `reviewReturnResultId` following merge
Jozzey Dec 22, 2023
417f8bc
Remove unused service
Jozzey Dec 28, 2023
624565c
Sort out temp endpoints for alt branch
Jozzey Dec 28, 2023
7421c14
Add public schema to DB helper
Jozzey Dec 28, 2023
548419f
WIP
Jozzey Dec 28, 2023
21d3ad1
Create unit tests for persisting service
Jozzey Dec 28, 2023
6a51e93
Tidy up
Jozzey Dec 28, 2023
1474745
Add comments to `allocate-returns-to-licence` service
Jozzey Dec 29, 2023
6446b1d
Remove `async` from service
Jozzey Dec 29, 2023
d2e1af6
Tidy up
Jozzey Dec 29, 2023
312297c
WIP create unit tests for `allocate-returns` service
Jozzey Dec 29, 2023
0e05930
Merge branch 'main' into alternate-2pt-engine
Jozzey Dec 29, 2023
467d086
Move the return log maching into its own service
Beckyrose200 Jan 5, 2024
e35eb23
Merge branch 'main' into alternate-2pt-engine
Jozzey Jan 8, 2024
f00132b
Get ready for the merge
Jozzey Jan 8, 2024
5bf004d
Merge branch 'main' into alternate-2pt-engine
Jozzey Jan 8, 2024
2b0a9b0
WIP
Jozzey Jan 9, 2024
5f04c1c
Remove `reviewReturnResultId` from service
Jozzey Jan 9, 2024
4730709
Fix for allocatedQuantity
Beckyrose200 Jan 9, 2024
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
26 changes: 25 additions & 1 deletion app/controllers/check.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const CheckTwoPartService = require('../services/check/two-part.service.js')
const TwoPartTariffMatchAndAllocateService = require('../services/bill-runs/two-part-tariff/match-and-allocate.service.js')

async function twoPart (request, h) {
const result = await CheckTwoPartService.go(request.params.naldRegionId, 'region')
Expand All @@ -19,7 +20,30 @@ async function twoPartLicence (request, h) {
return h.response(result).code(200)
}

// Used on alternate branch
async function twoPartReview (_request, h) {
const result = await TwoPartTariffMatchAndAllocateService.go(
{ billingBatchId: '41be6d72-701b-4252-90d5-2d38614b6282', regionId: 'ffea25c2-e577-4969-8667-b0eed899230d' },
[{ startDate: new Date('2022-04-01'), endDate: new Date('2023-03-31') }]
)

return h.response(result).code(200)
}

// Used on alternate branch
async function twoPartReviewLicence (request, h) {
const result = await TwoPartTariffMatchAndAllocateService.go(
{ billingBatchId: '41be6d72-701b-4252-90d5-2d38614b6282', regionId: 'ffea25c2-e577-4969-8667-b0eed899230d' },
[{ startDate: new Date('2022-04-01'), endDate: new Date('2023-03-31') }],
request.params.licenceId
)

return h.response(result).code(200)
}

module.exports = {
twoPart,
twoPartLicence
twoPartLicence,
twoPartReview,
twoPartReviewLicence
}
28 changes: 28 additions & 0 deletions app/routes/check.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ const routes = [
auth: false,
description: 'Used by the delivery team to check the SROC 2PT billing logic for a single licence'
}
},
{
// Used on the alternate branch
method: 'GET',
path: '/check/two-part-review',
handler: CheckController.twoPartReview,
options: {
app: {
excludeFromProd: true,
plainOutput: true
},
auth: false,
description: 'Used by the delivery team to check the SROC 2PT billing logic for a region'
}
},
{
// Used on the alternate branch
method: 'GET',
path: '/check/two-part-review/{licenceId}',
handler: CheckController.twoPartReviewLicence,
options: {
app: {
excludeFromProd: true,
plainOutput: true
},
auth: false,
description: 'Used by the delivery team to check the SROC 2PT billing logic for a single licence'
}
}
]

Expand Down
4 changes: 2 additions & 2 deletions app/services/bill-runs/start-bill-run-process.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DetermineBillingPeriodsService = require('./determine-billing-periods.serv
const CreateBillRunPresenter = require('../../presenters/bill-runs/create-bill-run.presenter.js')
const InitiateBillRunService = require('./initiate-bill-run.service.js')
const SupplementaryProcessBillRunService = require('./supplementary/process-bill-run.service.js')
const TwoPartTariffProcessBillRunService = require('./two-part-tariff/process-bill-run.service.js')
const TwoPartTariffMatchAndAllocateService = require('./two-part-tariff/match-and-allocate.service.js')

/**
* Manages the creation of a new bill run
Expand Down Expand Up @@ -46,7 +46,7 @@ function _processBillRun (billRun, billingPeriods) {
SupplementaryProcessBillRunService.go(billRun, billingPeriods)
break
case 'two_part_tariff':
TwoPartTariffProcessBillRunService.go(billRun, billingPeriods)
TwoPartTariffMatchAndAllocateService.go(billRun, billingPeriods)
break
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict'

/**
* Matches and allocates where applicable the abstracted volumes on the return log with the appropriate charge element
* @module AllocateReturnsToLicenceService
*/

const { periodsOverlap } = require('../../../lib/general.lib.js')
const MatchReturnsToChargeElementService = require('./match-returns-to-charge-element.service.js')

/**
* For each licence the service attempts to match the return logs to the charge element(s). It does this by matching the
* `elementCode` of the charge element with the return logs, also checking that the abstraction periods for both the
* element and return log overlap.
*
* If a match is found any abstracted volume recorded on the return log will be allocated to the charge element up to a
* maximum of the charge elements authorised volume, or the remaining authorised volume on the charge reference,
* whichever is lower.
*
* @param {Object[]} licences - The licences, associated charging data, and return logs to process
*/
function go (licences) {
licences.forEach((licence) => {
const { chargeVersions, returnLogs } = licence

chargeVersions.forEach((chargeVersion) => {
const { chargeReferences } = chargeVersion

chargeReferences.forEach((chargeReference) => {
chargeReference.allocatedQuantity = 0

const { chargeElements } = chargeReference

chargeElements.forEach((chargeElement) => {
_matchAndAllocate(chargeElement, returnLogs, chargeVersion.chargePeriod, chargeReference)
})
})
})
})
}

function _chargeDatesOverlap (matchedLine, chargePeriod) {
const { startDate: chargePeriodStartDate, endDate: chargePeriodEndDate } = chargePeriod
const { startDate: lineStartDate, endDate: lineEndDate } = matchedLine

if (lineStartDate < chargePeriodEndDate && lineEndDate > chargePeriodEndDate) {
return true
}

if (lineStartDate < chargePeriodStartDate && lineEndDate > chargePeriodStartDate) {
return true
}

return false
}

function _matchAndAllocate (chargeElement, returnLogs, chargePeriod, chargeReference) {
const matchedReturns = MatchReturnsToChargeElementService.go(chargeElement, returnLogs)

if (matchedReturns.length === 0) {
return
}

let i = 0
matchedReturns.forEach((matchedReturn) => {
if (chargeElement.allocatedQuantity < chargeElement.authorisedAnnualQuantity && chargeReference.allocatedQuantity < chargeReference.volume) {
if (matchedReturn.issues === true) {
return
}

const matchedLines = _matchLines(chargeElement, matchedReturn.returnSubmissions[0].returnSubmissionLines)

if (matchedLines.length === 0) {
return
}

matchedLines.forEach((matchedLine) => {
const remainingAllocation = chargeElement.authorisedAnnualQuantity - chargeElement.allocatedQuantity
if (remainingAllocation > 0) {
// We default how much to allocate to what is unallocated on the line i.e. remaining >= line.unallocated
let qtyToAllocate = matchedLine.unallocated

// If what remains is actually less than the line we instead set qtyToAllocate to what remains. We check this
// on both the chargeReference and the element
const chargeReferenceRemainingAllocation = chargeReference.volume - chargeReference.allocatedQuantity

if (qtyToAllocate > chargeReferenceRemainingAllocation) {
qtyToAllocate = chargeReferenceRemainingAllocation
} else if (remainingAllocation < matchedLine.unallocated) {
qtyToAllocate = remainingAllocation
}

chargeElement.chargeDatesOverlap = _chargeDatesOverlap(matchedLine, chargePeriod)
chargeElement.allocatedQuantity += qtyToAllocate
console.log('Charge Element :', chargeElement)
console.log('Matched Return :', matchedReturn)
chargeElement.returnLogs[i].allocatedQuantity += qtyToAllocate

matchedLine.unallocated -= qtyToAllocate
matchedReturn.allocatedQuantity += qtyToAllocate
chargeReference.allocatedQuantity += qtyToAllocate
}
})
}
i++
})
}

function _matchLines (chargeElement, returnSubmissionLines) {
return returnSubmissionLines.filter((returnSubmissionLine) => {
if (returnSubmissionLine.unallocated === 0) {
return false
}

const { startDate, endDate } = returnSubmissionLine
return periodsOverlap(chargeElement.abstractionPeriods, [{ startDate, endDate }])
})
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ const Workflow = require('../../../models/workflow.model.js')
*
* @returns {Object} Contains an array of SROC charge versions with linked licences, charge references, charge elements and related purpose
*/
async function go (regionId, billingPeriod) {
const regionCode = await _regionCode(regionId)
async function go (regionId, billingPeriod, licenceId) {
let regionCode = null

return _fetch(regionCode, billingPeriod)
if (!licenceId) {
regionCode = await _regionCode(regionId)
}

return _fetch(regionCode, billingPeriod, licenceId)
}

async function _fetch (regionCode, billingPeriod) {
async function _fetch (regionCode, billingPeriod, licenceId) {
const whereClause = {
field: licenceId ? 'licenceId' : 'regionCode',
value: licenceId ?? regionCode
}

const chargeVersions = await ChargeVersionModel.query()
.select([
'chargeVersions.id',
Expand All @@ -48,7 +57,7 @@ async function _fetch (regionCode, billingPeriod) {
.where('chargeVersions.scheme', 'sroc')
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.where('chargeVersions.status', 'current')
.where('chargeVersions.regionCode', regionCode)
.where(`chargeVersions.${whereClause.field}`, whereClause.value)
.where((builder) => {
builder
.whereNull('licence.expiredDate')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const FetchChargeVersionsService = require('./fetch-charge-versions.service.js')
*
* @returns {Object[]} the licences to be matched, each containing an array of charge versions applicable for two-part tariff
*/
async function go (regionId, billingPeriod) {
const chargeVersions = await FetchChargeVersionsService.go(regionId, billingPeriod)
async function go (regionId, billingPeriod, licenceId) {
const chargeVersions = await FetchChargeVersionsService.go(regionId, billingPeriod, licenceId)

const uniqueLicenceIds = _extractUniqueLicenceIds(chargeVersions)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

/**
* Match and allocate licences to returns for a two-part tariff bill run for the given billing periods
* @module MatchAndAllocateService
*/

const AllocateReturnsToLicenceService = require('./allocate-returns-to-licence.service.js')
const FetchLicencesService = require('./fetch-licences.service.js')
const PrepareLicencesForAllocationService = require('./prepare-licences-for-allocation.service.js')
const PersistAllocatedLicencesToResultsService = require('./persist-allocated-licences-to-results.service.js')
/**
* Functionality not yet implemented
*/
async function go (billRun, billingPeriods, licenceId) {
const startTime = process.hrtime.bigint()

const licences = await FetchLicencesService.go(billRun.regionId, billingPeriods[0], licenceId)

await PrepareLicencesForAllocationService.go(licences, billingPeriods[0])

AllocateReturnsToLicenceService.go(licences, billRun.billingBatchId)

await PersistAllocatedLicencesToResultsService.go(billRun.billingBatchId, licences)

_calculateAndLogTime(startTime)

return licences
}

function _calculateAndLogTime (startTime) {
const endTime = process.hrtime.bigint()
const timeTakenNs = endTime - startTime
const timeTakenMs = timeTakenNs / 1000000n

global.GlobalNotifier.omg('Two part tariff matching complete', { timeTakenMs })
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

/**
* Matches the return logs to the charge element
* @module MatchReturnsToChargeElementService
*/

const { periodsOverlap } = require('../../../lib/general.lib.js')

/**
* Matches return logs to a charge element and adds the matching returns to the charge element
*
* @param {module:ChargeElementModel} chargeElement - The charge element to match return logs against
* @param {module:ReturnLogModel[]} returnLogs - All return logs from the charge elements licence
*
* @returns {module:ReturnLogModel[]} Return logs that matched the charge element
*/
function go (chargeElement, returnLogs) {
const matchingReturns = _matchReturns(chargeElement, returnLogs)
_addReturnToElement(matchingReturns, chargeElement)

return matchingReturns
}

function _addReturnToElement (matchingReturns, chargeElement) {
if (matchingReturns.length === 0) {
return
}

matchingReturns.forEach((matchedReturn) => {
const matchedReturnResult = {
allocatedQuantity: 0,
returnId: matchedReturn.id
}

chargeElement.returnLogs.push(matchedReturnResult)
matchedReturn.matched = true
})
}

/**
* Filters and returns logs that match a given charge element based on legacy ID and abstraction periods
*/
function _matchReturns (chargeElement, returnLogs) {
const elementCode = chargeElement.purpose.legacyId
const elementPeriods = chargeElement.abstractionPeriods

return returnLogs.filter((returnLog) => {
const returnPeriods = returnLog.abstractionPeriods

const matchFound = returnLog.purposes.some((purpose) => {
return purpose.tertiary.code === elementCode
})

if (!matchFound) {
return false
}

return periodsOverlap(elementPeriods, returnPeriods)
})
}

module.exports = {
go
}
Loading
Loading