70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
// services/quarterService.js
|
||
|
||
/**
|
||
* Get Quarter and Month details based on current quarter and Calander year
|
||
* @param {number} currentYear - e.g. 2025
|
||
* @param {string} currentQuarter - e.g. 'Q4'
|
||
* @returns {object} - structured period data
|
||
*/
|
||
exports.getQuarterPeriods = (currentYear, currentQuarter) => {
|
||
// Define quarters based on Calander year (Jan–Dec)
|
||
const quarters = {
|
||
Q1: ["Jan", "Feb", "Mar"],
|
||
Q2: ["Apr", "May", "Jun"],
|
||
Q3: ["Jul", "Aug", "Sep"],
|
||
Q4: ["Oct", "Nov", "Dec"],
|
||
};
|
||
|
||
const allQuarters = ["Q1", "Q2", "Q3", "Q4"];
|
||
const currentIndex = allQuarters.indexOf(currentQuarter);
|
||
|
||
if (currentIndex === -1) {
|
||
throw new Error("Invalid quarter. Use one of: Q1, Q2, Q3, Q4");
|
||
}
|
||
|
||
// --- Previous Quarter ---
|
||
let previousQuarter = allQuarters[(currentIndex - 1 + 4) % 4];
|
||
let previousYear = currentYear;
|
||
|
||
if (currentQuarter === "Q1") {
|
||
previousYear = currentYear - 1;
|
||
}
|
||
|
||
// --- Forecast Quarter ---
|
||
let forecastQuarter = allQuarters[(currentIndex + 1) % 4];
|
||
let forecastYear = currentYear;
|
||
// If next quarter is Q1, then financial year increments
|
||
if (forecastQuarter === "Q1") {
|
||
forecastYear = currentYear + 1;
|
||
}
|
||
|
||
// Construct Output Object
|
||
const output = {
|
||
previous_month: {
|
||
previous_period_one: quarters[previousQuarter][0],
|
||
previous_period_two: quarters[previousQuarter][1],
|
||
previous_period_three: quarters[previousQuarter][2],
|
||
},
|
||
previous_year: previousYear,
|
||
previous_quarter: previousQuarter,
|
||
|
||
current_month: {
|
||
current_period_one: quarters[currentQuarter][0],
|
||
current_period_two: quarters[currentQuarter][1],
|
||
current_period_three: quarters[currentQuarter][2],
|
||
},
|
||
current_year: currentYear,
|
||
current_quarter: currentQuarter,
|
||
|
||
forecast_month: {
|
||
forecast_period_one: quarters[forecastQuarter][0],
|
||
forecast_period_two: quarters[forecastQuarter][1],
|
||
forecast_period_three: quarters[forecastQuarter][2],
|
||
},
|
||
forecast_year: forecastYear,
|
||
forecast_quarter: forecastQuarter,
|
||
};
|
||
|
||
return output;
|
||
};
|
||
|