-
Notifications
You must be signed in to change notification settings - Fork 0
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 logic 2 determine billable abstraction periods #81
Merged
Jozzey
merged 7 commits into
main
from
add-logic-to-determine-billable-abstraction-periods
Jan 19, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
833819b
Add logic 2 determine billable abstraction periods
Cruikshanks 9ee3ef4
First iteration of the service
Cruikshanks 5deb766
Merge branch 'main' into add-logic-to-determine-billable-abstraction-…
Cruikshanks 8c067bc
Merge branch 'main' into add-logic-to-determine-billable-abstraction-…
Jozzey e9fb0b0
Merge branch 'main' into add-logic-to-determine-billable-abstraction-…
Jozzey ee1a147
added billable days calculation
Jozzey 03aa810
added billableDays to unit tests
Jozzey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
app/services/supplementary-billing/abstraction-billing-period.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict' | ||
|
||
/** | ||
* Calculates billing periods needed when generating a supplementary bill run | ||
* @module AbstractionBillingPeriodService | ||
*/ | ||
|
||
function go (billingPeriod, chargePurpose) { | ||
const abstractionPeriods = _abstractionPeriods(billingPeriod, chargePurpose) | ||
|
||
_flagPeriodsForConsideration(billingPeriod, abstractionPeriods) | ||
|
||
_calculateBillableDays(abstractionPeriods) | ||
|
||
return abstractionPeriods | ||
} | ||
|
||
function _abstractionPeriods (billingPeriod, chargePurpose) { | ||
const billingPeriodStartYear = billingPeriod.startDate.getFullYear() | ||
const billingPeriodEndYear = billingPeriod.endDate.getFullYear() | ||
const { | ||
abstractionPeriodStartDay: startDay, | ||
abstractionPeriodStartMonth: startMonth, | ||
abstractionPeriodEndDay: endDay, | ||
abstractionPeriodEndMonth: endMonth | ||
} = chargePurpose | ||
const firstPeriod = {} | ||
|
||
if (endMonth === startMonth) { | ||
if (endDay >= startDay) { | ||
firstPeriod.startDate = new Date(billingPeriodEndYear, startMonth - 1, startDay) | ||
firstPeriod.endDate = new Date(billingPeriodEndYear, endMonth - 1, endDay) | ||
} else { | ||
firstPeriod.startDate = new Date(billingPeriodStartYear, startMonth - 1, startDay) | ||
firstPeriod.endDate = new Date(billingPeriodEndYear, endMonth - 1, endDay) | ||
} | ||
} else if (endMonth >= startMonth) { | ||
firstPeriod.startDate = new Date(billingPeriodEndYear, startMonth - 1, startDay) | ||
firstPeriod.endDate = new Date(billingPeriodEndYear, endMonth - 1, endDay) | ||
} else { | ||
firstPeriod.startDate = new Date(billingPeriodStartYear, startMonth - 1, startDay) | ||
firstPeriod.endDate = new Date(billingPeriodEndYear, endMonth - 1, endDay) | ||
} | ||
|
||
const previousPeriod = { | ||
startDate: _subtractOneYear(firstPeriod.startDate), | ||
endDate: _subtractOneYear(firstPeriod.endDate) | ||
} | ||
|
||
return [previousPeriod, firstPeriod] | ||
} | ||
|
||
function _calculateBillableDays (abstractionPeriods) { | ||
let difference | ||
let billableDays | ||
for (const abstractionPeriod of abstractionPeriods) { | ||
difference = abstractionPeriod.endDate.getTime() - abstractionPeriod.startDate.getTime() // difference in msecs | ||
billableDays = Math.ceil(difference / (1000 * 3600 * 24)) + 1 // (1000 msecs * (60 secs * 60 mins) * 24 hrs) | ||
abstractionPeriod.billableDays = billableDays | ||
} | ||
} | ||
|
||
function _flagPeriodsForConsideration (billingPeriod, abstractionPeriods) { | ||
for (const abstractionPeriod of abstractionPeriods) { | ||
if (abstractionPeriod.startDate > billingPeriod.endDate) { | ||
abstractionPeriod.consider = false | ||
} else if (abstractionPeriod.endDate < billingPeriod.startDate) { | ||
abstractionPeriod.consider = false | ||
} else { | ||
abstractionPeriod.consider = true | ||
} | ||
} | ||
} | ||
|
||
function _subtractOneYear (date) { | ||
return new Date(date.getFullYear() - 1, date.getMonth(), date.getDate()) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I look at this function and go 🤮 . But I have a stinkin' cold and it's the end of the day, so don't know how I'd go about improving it.
But I definitely need to add some documentation to give some context as to what on earth it is trying to achieve.
Let me at least do that and then we're all good IMHO (though any suggestions for improvement I'm open to!)