fcsc_ipi_backend/app/services/quarterService.js

73 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// services/quarterService.js
/**
* Get Quarter and Month details based on current quarter and financial 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 financial year (AprMar)
const quarters = {
Q1: ["Apr", "May", "Jun"],
Q2: ["Jul", "Aug", "Sep"],
Q3: ["Oct", "Nov", "Dec"],
Q4: ["Jan", "Feb", "Mar"],
};
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 current quarter is Q1, previous year is same financial year → currentYear
// But Q1 (Apr-Jun) comes after Mar, so same financial year
if (currentQuarter === "Q1") {
previousYear = currentYear - 1;
} else if (currentQuarter === "Q4") {
previousYear = currentYear; // Q4 (JanMar) belongs to same financial year end
}
// --- 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;
};