Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonsueur/809ceb8b042816d764fe108ad95a1f77 to your computer and use it in GitHub Desktop.
Save brandonsueur/809ceb8b042816d764fe108ad95a1f77 to your computer and use it in GitHub Desktop.
FixationsControl.js
const moment = require('moment')
const GRANULARITY_MONTH = 'month'
const GRANULARITY_QUARTER = 'quarter'
const GRANULARITY_YEAR = 'year'
/**
* getContractYearTabs
*
* @param contractStart moment
* @param contractEnd moment
* @param fixationGranularity GRANULARITY_MONTH or GRANULARITY_QUARTER or GRANULARITY_YEAR
* @param energy gas or power
* @returns {[]}
*/
export const getContractYearTabs = (
contractStart,
contractEnd,
fixationGranularity,
energy,
) => {
const controls = []
const isGasYearlyFixation =
energy === 'gas' && fixationGranularity === GRANULARITY_YEAR
const startMonth = moment(contractStart).startOf('month')
const endMonth = moment(contractEnd).startOf('month')
if (isGasYearlyFixation) {
for (
let currentDate = moment(startMonth);
currentDate.isSameOrBefore(endMonth);
currentDate.add(1, 'year')
) {
const year = currentDate.year()
const label = `${year}/${year + 1}`
controls.push({
year: year,
label: label,
})
}
} else {
for (let year = startMonth.year(); year <= endMonth.year(); year++) {
controls.push({
year: year,
label: `${year}`,
})
}
}
return controls
}
/**
* getContractFixationsControls
*
* @param contractStart moment
* @param contractEnd moment
* @param year Number
* @param fixationGranularity GRANULARITY_MONTH || GRANULARITY_QUARTER || GRANULARITY_YEAR
* @param energy gas || power
* @returns {[]}
*/
export const getContractFixationsControls = (
contractStart,
contractEnd,
year,
fixationGranularity,
energy,
) => {
const controls = []
const isGasYearlyFixation =
energy === 'gas' && fixationGranularity === GRANULARITY_YEAR
let startDate = moment(contractStart).startOf('month')
let yearFirstMonth = moment(`${year}-01-01`)
if (startDate.isBefore(yearFirstMonth)) startDate = yearFirstMonth
let endDate = moment(contractEnd).startOf('month')
let yearLastMonth = moment(`${year}-12-01`)
if (endDate.isAfter(yearLastMonth)) endDate = yearLastMonth
if (isGasYearlyFixation) {
const yearsDiffFromContractStart = year - contractStart.year()
controls.push({
date: moment(contractStart).add(yearsDiffFromContractStart, 'year'),
months: 12,
label: `${year}/${year + 1}`,
})
} else {
let previousDate = null
let currentDate = moment(startDate)
while (currentDate.isSameOrBefore(endDate)) {
let createControl = false
let label = ''
let months = 0
switch (fixationGranularity) {
case GRANULARITY_MONTH:
createControl = true
label = currentDate.format('MMMM')
months = 1
break
case GRANULARITY_QUARTER:
let currentMonthQuarter = currentDate.quarter()
let lastMonthQuarter = previousDate ? previousDate.quarter() : -1
if (currentMonthQuarter !== lastMonthQuarter) {
createControl = true
label = `${currentDate.format('MMM')} - ${moment(currentDate)
.add(2, 'month')
.format('MMM')}`
months = 3
}
break
case GRANULARITY_YEAR:
let currentMonthYear = currentDate.year()
let lastMonthYear = previousDate ? currentDate.year() : -1
if (currentMonthYear !== lastMonthYear) {
createControl = true
label = `${currentMonthYear}`
months = 12
}
break
}
if (createControl) {
controls.push({
date: moment(currentDate),
months: months,
label: label,
})
// console.log(
// `Date: ${currentDate.format(
// 'MMM YYYY',
// )} - Months: ${months} - Label: ${label}`,
// )
}
previousDate = moment(currentDate)
currentDate.add(1, 'month')
}
}
return controls
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment