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

Resolve values with too many decimal places #1069

Merged
merged 8 commits into from
Jun 4, 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 @@ -5,6 +5,8 @@
* @module ChargeReferenceDetailsPresenter
*/

const Big = require('big.js')

const { formatLongDate, formatFinancialYear } = require('../../base.presenter.js')

/**
Expand Down Expand Up @@ -113,7 +115,7 @@ function _prepareDate (startDate, endDate) {

function _totalBillableReturns (reviewChargeElements) {
return reviewChargeElements.reduce((total, reviewChargeElement) => {
total += reviewChargeElement.amendedAllocated
total = Big(total).plus(reviewChargeElement.amendedAllocated).toNumber()

return total
}, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module ReviewLicencePresenter
*/

const Big = require('big.js')

const DetermineAbstractionPeriodService = require('../../../services/bill-runs/determine-abstraction-periods.service.js')
const { formatLongDate } = require('../../base.presenter.js')

Expand Down Expand Up @@ -277,7 +279,7 @@ function _totalBillableReturns (reviewChargeReference) {
let totalBillableReturns = 0

reviewChargeReference.reviewChargeElements.forEach((reviewChargeElement) => {
totalBillableReturns += reviewChargeElement.amendedAllocated
totalBillableReturns = Big(totalBillableReturns).plus(reviewChargeElement.amendedAllocated).toNumber()
})

return `${totalBillableReturns} ML / ${reviewChargeReference.amendedAuthorisedVolume} ML`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module AllocateReturnsToChargeElementService
*/

const Big = require('big.js')

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

/**
Expand Down Expand Up @@ -34,8 +36,12 @@ function go (chargeElement, matchingReturns, chargePeriod, chargeReference) {

function _allocateReturns (chargeElement, matchedReturn, chargePeriod, chargeReference, i, matchedLines) {
matchedLines.forEach((matchedLine) => {
const chargeElementRemainingAllocation = chargeElement.authorisedAnnualQuantity - chargeElement.allocatedQuantity
const chargeReferenceRemainingAllocation = chargeReference.volume - chargeReference.allocatedQuantity
const chargeElementRemainingAllocation = Big(chargeElement.authorisedAnnualQuantity)
.minus(chargeElement.allocatedQuantity)
.toNumber()
const chargeReferenceRemainingAllocation = Big(chargeReference.volume)
.minus(chargeReference.allocatedQuantity)
.toNumber()

const remainingAllocation = Math.min(chargeElementRemainingAllocation, chargeReferenceRemainingAllocation)

Expand All @@ -56,33 +62,41 @@ function _allocateReturns (chargeElement, matchedReturn, chargePeriod, chargeRef
chargeElement.chargeDatesOverlap = _chargeDatesOverlap(matchedLine, chargePeriod)
}

chargeElement.allocatedQuantity += qtyToAllocate
chargeElement.returnLogs[i].allocatedQuantity += qtyToAllocate
matchedLine.unallocated -= qtyToAllocate
matchedReturn.allocatedQuantity += qtyToAllocate
chargeReference.allocatedQuantity += qtyToAllocate
chargeElement.allocatedQuantity = Big(chargeElement.allocatedQuantity).plus(qtyToAllocate).toNumber()
chargeElement.returnLogs[i].allocatedQuantity = Big(chargeElement.returnLogs[i].allocatedQuantity)
.plus(qtyToAllocate)
.toNumber()
matchedLine.unallocated = Big(matchedLine.unallocated).minus(qtyToAllocate).toNumber()
matchedReturn.allocatedQuantity = Big(matchedReturn.allocatedQuantity).plus(qtyToAllocate).toNumber()
chargeReference.allocatedQuantity = Big(chargeReference.allocatedQuantity).plus(qtyToAllocate).toNumber()
}
})
}

function _allocateDueReturns (chargeElement, matchedReturn, chargeReference, i) {
const chargeElementRemainingAllocation = chargeElement.authorisedAnnualQuantity - chargeElement.allocatedQuantity
const chargeElementRemainingAllocation = Big(chargeElement.authorisedAnnualQuantity)
.minus(chargeElement.allocatedQuantity)
.toNumber()

if (chargeElementRemainingAllocation > 0) {
let qtyToAllocate = chargeElementRemainingAllocation

// If the unallocated volume on the element is greater than that remaining on the reference the `qtyToAllocate` will
// be set to whatever volume remains unallocated on the reference
const chargeReferenceRemainingAllocation = chargeReference.volume - chargeReference.allocatedQuantity
const chargeReferenceRemainingAllocation = Big(chargeReference.volume)
.minus(chargeReference.allocatedQuantity)
.toNumber()

if (chargeElementRemainingAllocation > chargeReferenceRemainingAllocation) {
qtyToAllocate = chargeReferenceRemainingAllocation
}

chargeElement.allocatedQuantity += qtyToAllocate
chargeElement.returnLogs[i].allocatedQuantity += qtyToAllocate
matchedReturn.allocatedQuantity += qtyToAllocate
chargeReference.allocatedQuantity += qtyToAllocate
chargeElement.allocatedQuantity = Big(chargeElement.allocatedQuantity).plus(qtyToAllocate).toNumber()
chargeElement.returnLogs[i].allocatedQuantity = Big(chargeElement.returnLogs[i].allocatedQuantity)
.plus(qtyToAllocate)
.toNumber()
matchedReturn.allocatedQuantity = Big(matchedReturn.allocatedQuantity).plus(qtyToAllocate).toNumber()
chargeReference.allocatedQuantity = Big(chargeReference.allocatedQuantity).plus(qtyToAllocate).toNumber()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module PrepareReturnLogsService
*/

const Big = require('big.js')

const DetermineAbstractionPeriodService = require('../determine-abstraction-periods.service.js')
const FetchReturnLogsForLicenceService = require('./fetch-return-logs-for-licence.service.js')
const { periodsOverlap } = require('../../../lib/general.lib.js')
Expand Down Expand Up @@ -53,8 +55,8 @@ function _prepReturnsForMatching (returnLogs, billingPeriod) {
if (!abstractionOutsidePeriod) {
abstractionOutsidePeriod = _abstractionOutsidePeriod(abstractionPeriods, returnSubmissionLine)
}
returnSubmissionLine.unallocated = returnSubmissionLine.quantity / 1000
quantity += returnSubmissionLine.unallocated
returnSubmissionLine.unallocated = Big(returnSubmissionLine.quantity).div(1000).toNumber()
quantity = Big(quantity).plus(returnSubmissionLine.unallocated).toNumber()
})

returnLog.nilReturn = returnSubmissions[0]?.nilReturn ?? false
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@joi/date": "^2.1.0",
"@smithy/node-http-handler": "^2.0.4",
"bcryptjs": "^2.4.3",
"big.js": "^6.2.1",
"blipp": "^4.0.2",
"dotenv": "^16.3.1",
"got": "^12.5.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function _fetchReviewLicenceResults () {
reviewChargeReferenceId: '7759e0f9-5763-4b94-8d45-0621aea3edc1',
chargeElementId: 'b1cd4f98-ad96-4901-9e21-4432f032492a',
allocated: 0,
amendedAllocated: 0,
chargeDatesOverlap: false,
issues: '',
status: 'ready'
Expand Down Expand Up @@ -152,6 +153,7 @@ function _fetchReviewLicenceResults () {
reviewChargeReferenceId: '2210bb45-1efc-4e69-85cb-c8cc6e75c4fd',
chargeElementId: 'b1001716-cfb4-43c6-91f0-1863f4529223',
allocated: 0,
amendedAllocated: 0,
chargeDatesOverlap: false,
issues: '',
status: 'ready',
Expand Down
Loading