Quarter calculation changed financial year to calander year

This commit is contained in:
Gowtham M 2025-11-06 17:14:02 +05:30
parent 9590409213
commit e472682224
2 changed files with 18 additions and 25 deletions

View File

@ -2,13 +2,7 @@
const { Sequelize, Op } = require("sequelize");
const { Establishment, Submission, Emirate, EstablishmentUser, SubmissionProduct, SubmissionDeadline, QuarterlyWindowsConfiguration } = require("../models");
function getQuarter(date) {
const month = date.getMonth() + 1;
if (month >= 4 && month <= 6) return "Q1";
if (month >= 7 && month <= 9) return "Q2";
if (month >= 10 && month <= 12) return "Q3";
return "Q4"; // Jan-Mar
}
function getQuarterDates(quarter, year) {
switch (quarter) {
@ -30,12 +24,12 @@ function getPrevCurrNextQuarter(date) {
const year = date.getFullYear();
const quarters = ["Q1", "Q2", "Q3", "Q4"];
// map month -> index: 0=Q1(Apr-Jun),1=Q2(Jul-Sep),2=Q3(Oct-Dec),3=Q4(Jan-Mar)
const getQuarterIndex = (m) => {
if (m >= 4 && m <= 6) return 0; // Apr-Jun -> Q1
if (m >= 7 && m <= 9) return 1; // Jul-Sep -> Q2
if (m >= 10 && m <= 12) return 2; // Oct-Dec -> Q3
return 3; // Jan-Mar -> Q4
if (m >= 1 && m <= 3) return 0; // Jan-Mar -> Q1
if (m >= 4 && m <= 6) return 1; // Apr-Jun -> Q2
if (m >= 7 && m <= 9) return 2; // Jul-Sep -> Q3
if (m >= 10 && m <=12) return 3; // Oct-Dec -> Q4
};
const currentIndex = getQuarterIndex(month);
@ -47,8 +41,8 @@ function getPrevCurrNextQuarter(date) {
const previousQuarter = quarters[previousIndex];
const nextQuarter = quarters[nextIndex];
const previousQuarterYear = (currentIndex === 3) ? year - 1 : year;
const nextQuarterYear = (currentIndex === 2) ? year + 1 : year;
const previousQuarterYear = (currentIndex === 1) ? year - 1 : year;
const nextQuarterYear = (currentIndex === 3) ? year + 1 : year;
return {
previous_quarter: previousQuarter,
@ -75,6 +69,8 @@ exports.getEstablishmentDashboard = async (req, res) => {
const submission_history = await Submission.findAll({
where: { establishment_id },
order: [["created_at", "DESC"]],

View File

@ -1,18 +1,18 @@
// services/quarterService.js
/**
* Get Quarter and Month details based on current quarter and financial year
* 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 financial year (AprMar)
// Define quarters based on Calander year (JanDec)
const quarters = {
Q1: ["Apr", "May", "Jun"],
Q2: ["Jul", "Aug", "Sep"],
Q3: ["Oct", "Nov", "Dec"],
Q4: ["Jan", "Feb", "Mar"],
Q1: ["Jan", "Feb", "Mar"],
Q2: ["Apr", "May", "Jun"],
Q3: ["Jul", "Aug", "Sep"],
Q4: ["Oct", "Nov", "Dec"],
};
const allQuarters = ["Q1", "Q2", "Q3", "Q4"];
@ -25,13 +25,10 @@ exports.getQuarterPeriods = (currentYear, currentQuarter) => {
// --- 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];