+
{
const checked = e.target.checked;
setShowPreviousNext(checked);
- if (checked) {
- try {
- setFetchingNextQuarter(true);
-
- const currentQuarter = quarterPeriods?.current_quarter || "Q4";
- const currentYear = quarterPeriods?.current_year || 2025;
-
- const establishmentId = submissionData?.establishment?.id;
- const productId = submissionData?.products?.[0]?.product?.id;
-
- if (!establishmentId || !currentQuarter || !currentYear || !productId) {
- console.error("Missing required parameters:", {
- establishmentId,
- currentQuarter,
- currentYear,
- productId,
- });
- return;
- }
-
- // API call
- const response = await getBeforePreviousData(
- establishmentId,
- currentQuarter,
- currentYear,
- productId
-);
-
- let mappedDataMap = {};
-
- if (response?.data) {
- submissionData.products.forEach((product) => {
- const pid = product?.product?.id;
- if (pid) {
- mappedDataMap[pid] = {
- forecast_quantity_period_one:
- response.data.current_quantity_period_one ?? 0,
- forecast_quantity_period_two:
- response.data.current_quantity_period_two ?? 0,
- forecast_quantity_period_three:
- response.data.current_quantity_period_three ?? 0,
-
- forecast_cost_period_one:
- response.data.current_cost_period_one ?? 0,
- forecast_cost_period_two:
- response.data.current_cost_period_two ?? 0,
- forecast_cost_period_three:
- response.data.current_cost_period_three ?? 0,
- };
- }
- });
- } else {
- // if no backend data
- submissionData.products.forEach((product) => {
- const pid = product?.product?.id;
- if (pid) {
- mappedDataMap[pid] = {
- forecast_quantity_period_one: 0,
- forecast_quantity_period_two: 0,
- forecast_quantity_period_three: 0,
- forecast_cost_period_one: 0,
- forecast_cost_period_two: 0,
- forecast_cost_period_three: 0,
- };
- }
- });
- }
-
- setNextQuarterData(mappedDataMap);
-
- // Calculate previous-previous quarter UI labels
- const q = Number(currentQuarter.replace("Q", ""));
- let historicalQuarter = `Q${q - 2}`;
- let historicalYear = currentYear;
-
- if (q - 2 <= 0) {
- historicalQuarter = `Q${4 + (q - 2)}`;
- historicalYear = currentYear - 1;
- }
-
- setAdditionalForecastQuarter(historicalQuarter);
- setAdditionalForecastYear(historicalYear);
- setAdditionalForecastMonths(
- getMonthsForQuarter(historicalQuarter, historicalYear)
- );
-
- } catch (error) {
- console.error("Error fetching before-previous data:", error);
- } finally {
- setFetchingNextQuarter(false);
- }
-
- } else {
- // On uncheck, clear values
+ // Uncheck → reset
+ if (!checked) {
setNextQuarterData({});
setAdditionalForecastQuarter("");
setAdditionalForecastYear("");
setAdditionalForecastMonths(["", "", ""]);
+ return;
+ }
+
+ try {
+ setFetchingNextQuarter(true);
+
+ const establishmentId = submissionData?.establishment?.id;
+ const productId = submissionData?.products?.[0]?.product?.id;
+
+ if (!establishmentId || !productId) {
+ console.error("Missing required params");
+ return;
+ }
+
+ // API Call
+ const response = await getBeforePreviousData(
+ establishmentId,
+ quarterPeriods?.current_quarter,
+ quarterPeriods?.current_year,
+ productId
+ );
+
+
+ let mappedDataMap = {};
+
+ submissionData?.products?.forEach((product) => {
+ const pid = product?.product?.id;
+ if (!pid) return;
+
+ mappedDataMap[pid] = {
+ forecast_quantity_period_one:
+ response?.data?.forecast_quantity_period_one ?? 0,
+ forecast_quantity_period_two:
+ response?.data?.forecast_quantity_period_two ?? 0,
+ forecast_quantity_period_three:
+ response?.data?.forecast_quantity_period_three ?? 0,
+
+ forecast_cost_period_one:
+ response?.data?.forecast_cost_period_one ?? 0,
+ forecast_cost_period_two:
+ response?.data?.forecast_cost_period_two ?? 0,
+ forecast_cost_period_three:
+ response?.data?.forecast_cost_period_three ?? 0,
+ };
+ });
+
+ setNextQuarterData(mappedDataMap);
+
+ // DIRECTLY FROM API – NO CALCULATION
+ const qp = response?.data?.quarter_periods;
+
+ setAdditionalForecastQuarter(qp?.forecast_quarter);
+ setAdditionalForecastYear(qp?.forecast_year);
+ setAdditionalForecastMonths([
+ qp?.forecast_month?.forecast_period_one,
+ qp?.forecast_month?.forecast_period_two,
+ qp?.forecast_month?.forecast_period_three,
+ ]);
+
+ } catch (error) {
+ console.error("Error fetching forecast data:", error);
+ } finally {
+ setFetchingNextQuarter(false);
}
};
+
+
// Helper to get forecast data for a specific product
const getCurrentYearForecastData = (productId) => {
if (!showPreviousNext || !productId) {
@@ -920,7 +893,7 @@ const getQuarterYearLabel = (quarter, year) => {
const previousYear = quarterPeriods?.previous_year || 2025;
const currentQuarter = quarterPeriods?.current_quarter || 'Q4';
const currentYear = quarterPeriods?.current_year || 2025;
- const forecastQuarter = quarterPeriods?.forecast_quarter || 'Q1';
+ const forecastQuarter = quarterPeriods?.forecast_quarter || '-';
const forecastYear = quarterPeriods?.forecast_year || 2026;
// Get dynamic month information from API response
@@ -962,8 +935,6 @@ const additionalForecastLabel =
? `${additionalForecastQuarter} ${additionalForecastYear}`
: '';
-
-
const establishmentDetails = [
{
label: 'Establishment Name',
@@ -1140,7 +1111,7 @@ const additionalForecastLabel =
)}
@@ -1175,11 +1147,14 @@ const additionalForecastLabel =