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

SRoC Supplementary Billing - Determine Abstraction Period #97

Merged
merged 18 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -46,6 +46,8 @@ function go (billingPeriod, chargePurpose) {

_flagPeriodsForConsideration(billingPeriod, abstractionPeriods)

_calculateBillablePeriods(abstractionPeriods, billingPeriod)

_calculateBillableDays(abstractionPeriods)
Jozzey marked this conversation as resolved.
Show resolved Hide resolved

return abstractionPeriods
Expand Down Expand Up @@ -137,23 +139,61 @@ function _abstractionPeriods (billingPeriod, chargePurpose) {
}

/**
* Calculates the 'billable days' for each abstraction billing period
* Calculates the 'billable days' for each abstraction billable period
*
* @param {Object[]} abstractionPeriods An array of abstraction billing periods
*
* @returns {Object[]} The array abstraction periods each with a new `billableDays` property
* @returns {Date} periods[].startDate
* @returns {Date} periods[].endDate
* @returns {boolean} periods[].consider
* @returns {Date} periods[].billableStartDate
* @returns {Date} periods[].billableEndDate
* @returns {number} periods[].billableDays
*/
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
if (abstractionPeriod.billableStartDate) {
const difference = abstractionPeriod.billableEndDate.getTime() - abstractionPeriod.billableStartDate.getTime() // difference in msecs
const billableDays = Math.ceil(difference / (1000 * 3600 * 24)) + 1 // (1000 msecs * (60 secs * 60 mins) * 24 hrs)
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
abstractionPeriod.billableDays = billableDays
}
}
}

/**
* Calculates the 'billable period' for each abstraction billing period
*
* @param {Object[]} abstractionPeriods An array of abstraction billing periods
* @param {Object} billingPeriod Object that has a `startDate` and `endDate` that defines the billing period
*
* @returns {Object[]} The array abstraction periods each with new `billableStartDate` & `billableEndDate` properties
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Date} periods[].startDate
* @returns {Date} periods[].endDate
* @returns {boolean} periods[].consider
* @returns {Date} periods[].billableStartDate
* @returns {Date} periods[].billableEndDate
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
*/
function _calculateBillablePeriods (abstractionPeriods, billingPeriod) {
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
let billableStartDate
let billableEndDate
for (const abstractionPeriod of abstractionPeriods) {
if (abstractionPeriod.startDate < billingPeriod.startDate) {
billableStartDate = billingPeriod.startDate
} else {
billableStartDate = abstractionPeriod.startDate
}

if (abstractionPeriod.endDate > billingPeriod.endDate) {
billableEndDate = billingPeriod.endDate
} else {
billableEndDate = abstractionPeriod.endDate
}

if (billableStartDate <= billableEndDate) {
abstractionPeriod.billableStartDate = billableStartDate
abstractionPeriod.billableEndDate = billableEndDate
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-01-01'))
expect(result[0].endDate).to.equal(new Date('2022-05-31'))
expect(result[0].billableDays).to.equal(151)
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-05-31'))
expect(result[0].billableDays).to.equal(61)

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2023-01-01'))
expect(result[1].endDate).to.equal(new Date('2023-05-31'))
expect(result[1].billableDays).to.equal(151)
expect(result[1].billableStartDate).to.equal(new Date('2023-01-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableDays).to.equal(90)
})
})

Expand All @@ -64,12 +68,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-05-01'))
expect(result[0].endDate).to.equal(new Date('2022-10-31'))
expect(result[0].billableStartDate).to.equal(new Date('2022-05-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-10-31'))
expect(result[0].billableDays).to.equal(184)

expect(result[1].consider).to.be.false()
expect(result[1].startDate).to.equal(new Date('2023-05-01'))
expect(result[1].endDate).to.equal(new Date('2023-10-31'))
expect(result[1].billableDays).to.equal(184)
expect(result[1].billableStartDate).to.be.undefined()
expect(result[1].billableEndDate).to.be.undefined()
expect(result[1].billableDays).to.be.undefined()
})
})

Expand All @@ -91,12 +99,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-04-01'))
expect(result[0].endDate).to.equal(new Date('2022-10-31'))
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-10-31'))
expect(result[0].billableDays).to.equal(214)

expect(result[1].consider).to.be.false()
expect(result[1].startDate).to.equal(new Date('2023-04-01'))
expect(result[1].endDate).to.equal(new Date('2023-10-31'))
expect(result[1].billableDays).to.equal(214)
expect(result[1].billableStartDate).to.be.undefined()
expect(result[1].billableEndDate).to.be.undefined()
expect(result[1].billableDays).to.be.undefined()
})
})
})
Expand All @@ -120,11 +132,15 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.false()
expect(result[0].startDate).to.equal(new Date('2022-03-01'))
expect(result[0].endDate).to.equal(new Date('2022-03-31'))
expect(result[0].billableDays).to.equal(31)
expect(result[0].billableStartDate).to.be.undefined()
expect(result[0].billableEndDate).to.be.undefined()
expect(result[0].billableDays).to.be.undefined()

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2023-03-01'))
expect(result[1].endDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableStartDate).to.equal(new Date('2023-03-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableDays).to.equal(31)
})
})
Expand All @@ -147,12 +163,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-05-01'))
expect(result[0].endDate).to.equal(new Date('2022-05-31'))
expect(result[0].billableStartDate).to.equal(new Date('2022-05-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-05-31'))
expect(result[0].billableDays).to.equal(31)

expect(result[1].consider).to.be.false()
expect(result[1].startDate).to.equal(new Date('2023-05-01'))
expect(result[1].endDate).to.equal(new Date('2023-05-31'))
expect(result[1].billableDays).to.equal(31)
expect(result[1].billableStartDate).to.be.undefined()
expect(result[1].billableEndDate).to.be.undefined()
expect(result[1].billableDays).to.be.undefined()
})
})

Expand All @@ -174,12 +194,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-04-01'))
expect(result[0].endDate).to.equal(new Date('2022-04-30'))
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-04-30'))
expect(result[0].billableDays).to.equal(30)

expect(result[1].consider).to.be.false()
expect(result[1].startDate).to.equal(new Date('2023-04-01'))
expect(result[1].endDate).to.equal(new Date('2023-04-30'))
expect(result[1].billableDays).to.equal(30)
expect(result[1].billableStartDate).to.be.undefined()
expect(result[1].billableEndDate).to.be.undefined()
expect(result[1].billableDays).to.be.undefined()
})
})
})
Expand All @@ -205,12 +229,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.false()
expect(result[0].startDate).to.equal(new Date('2021-03-01'))
expect(result[0].endDate).to.equal(new Date('2022-02-28'))
expect(result[0].billableDays).to.equal(365)
expect(result[0].billableStartDate).to.be.undefined()
expect(result[0].billableEndDate).to.be.undefined()
expect(result[0].billableDays).to.be.undefined()

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2022-03-01'))
expect(result[1].endDate).to.equal(new Date('2023-02-28'))
expect(result[1].billableDays).to.equal(365)
expect(result[1].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-02-28'))
expect(result[1].billableDays).to.equal(334)
})
})

Expand All @@ -232,12 +260,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2021-10-01'))
expect(result[0].endDate).to.equal(new Date('2022-09-30'))
expect(result[0].billableDays).to.equal(365)
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-09-30'))
expect(result[0].billableDays).to.equal(183)

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2022-10-01'))
expect(result[1].endDate).to.equal(new Date('2023-09-30'))
expect(result[1].billableDays).to.equal(365)
expect(result[1].billableStartDate).to.equal(new Date('2022-10-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableDays).to.equal(182)
})
})

Expand All @@ -259,11 +291,15 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.false()
expect(result[0].startDate).to.equal(new Date('2021-04-01'))
expect(result[0].endDate).to.equal(new Date('2022-03-31'))
expect(result[0].billableDays).to.equal(365)
expect(result[0].billableStartDate).to.be.undefined()
expect(result[0].billableEndDate).to.be.undefined()
expect(result[0].billableDays).to.be.undefined()

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2022-04-01'))
expect(result[1].endDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableDays).to.equal(365)
})
})
Expand All @@ -288,12 +324,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.false()
expect(result[0].startDate).to.equal(new Date('2021-03-31'))
expect(result[0].endDate).to.equal(new Date('2022-03-01'))
expect(result[0].billableDays).to.equal(336)
expect(result[0].billableStartDate).to.be.undefined()
expect(result[0].billableEndDate).to.be.undefined()
expect(result[0].billableDays).to.be.undefined()

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2022-03-31'))
expect(result[1].endDate).to.equal(new Date('2023-03-01'))
expect(result[1].billableDays).to.equal(336)
expect(result[1].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-01'))
expect(result[1].billableDays).to.equal(335)
})
})

Expand All @@ -315,12 +355,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2021-10-01'))
expect(result[0].endDate).to.equal(new Date('2022-09-30'))
expect(result[0].billableDays).to.equal(365)
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-09-30'))
expect(result[0].billableDays).to.equal(183)

expect(result[1].consider).to.be.true()
expect(result[1].startDate).to.equal(new Date('2022-10-01'))
expect(result[1].endDate).to.equal(new Date('2023-09-30'))
expect(result[1].billableDays).to.equal(365)
expect(result[1].billableStartDate).to.equal(new Date('2022-10-01'))
expect(result[1].billableEndDate).to.equal(new Date('2023-03-31'))
expect(result[1].billableDays).to.equal(182)
})
})
})
Expand All @@ -344,12 +388,16 @@ describe('Abstraction Billing Period service', () => {
expect(result[0].consider).to.be.true()
expect(result[0].startDate).to.equal(new Date('2022-04-01'))
expect(result[0].endDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableStartDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableEndDate).to.equal(new Date('2022-04-01'))
expect(result[0].billableDays).to.equal(1)

expect(result[1].consider).to.be.false()
expect(result[1].startDate).to.equal(new Date('2023-04-01'))
expect(result[1].endDate).to.equal(new Date('2023-04-01'))
expect(result[1].billableDays).to.equal(1)
expect(result[1].billableStartDate).to.be.undefined()
expect(result[1].billableEndDate).to.be.undefined()
expect(result[1].billableDays).to.be.undefined()
})
})
})