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 logic 2 determine billable abstraction periods #81

Merged
merged 7 commits into from
Jan 19, 2023
Merged
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]
}
Comment on lines +18 to +51
Copy link
Member Author

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!)


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
}
Loading