This commit is contained in:
Malini 2025-12-22 20:35:19 +05:30
parent d57d37c55a
commit 30360fe3f3

View File

@ -755,12 +755,12 @@ const location = useLocation();
const response = await getPreviousForecastData(
establishmentId,
formattedQuarter,
year,
quarterPeriods?.previous_quarter || formattedQuarter,
quarterPeriods?.previous_year || year,
productId
);
console.log("response",response)
if (!response) {
console.warn('Empty response from getPreviousForecastData');
return null;
@ -893,6 +893,7 @@ const location = useLocation();
// Handle product selection
if (field === 'product') {
const selectedProduct = options?.find(opt => opt.value === value);
console.log(selectedProduct,"selectedProduct")
const updatedProduct = {
...product,
product: selectedProduct?.value || value,
@ -910,6 +911,7 @@ const location = useLocation();
if (establishmentId && quarter && year) {
// Use the product_id directly as the value
const productId = selectedProduct.value;
console.log("productId",productId)
// Call handleProductSelect with the product ID
handleProductSelect(productId, establishmentId, id);
@ -1028,6 +1030,7 @@ const location = useLocation();
});
return;
}
console.log("handleProductSelect",handleProductSelect)
try {
const forecastData = await fetchPreviousForecastData(productId, establishmentId);
@ -1035,6 +1038,7 @@ const location = useLocation();
console.warn('No forecast data received for product:', productId);
return;
}
console.log("forecastData",forecastData)
// Check if the response has a data property (nested response) or is the data itself
const data = forecastData.data || forecastData;
@ -1042,17 +1046,18 @@ const location = useLocation();
if (product.id === id) {
return {
...product,
octQuantity: data.previous_quantity_period_one || '',
novQuantity: data.previous_quantity_period_two || '',
decQuantity: data.previous_quantity_period_three || '',
octCost: data.previous_cost_period_one || '',
novCost: data.previous_cost_period_two || '',
decCost: data.previous_cost_period_three || '',
octQuantity: data.current_quantity_period_one || '',
novQuantity: data.current_quantity_period_two || '',
decQuantity: data.current_quantity_period_three || '',
octCost: data.current_cost_period_one || '',
novCost: data.current_cost_period_two || '',
decCost: data.current_cost_period_three || '',
capacity: data.annual_installed_capacity || ''
};
}
return product;
});
console.log("updatedProducts",updatedProducts)
onProductsChange(updatedProducts);
} catch (error) {
console.error('Error in handleProductSelect:', {
@ -1275,8 +1280,18 @@ const location = useLocation();
}
// Find the selected product from options
const selectedProduct = productOptions.find(opt => opt.value === selectedValue || opt.productId?.toString() === selectedValue);
const selectedProduct = productOptions.find(opt => {
return opt.value === selectedValue ||
opt.productId?.toString() === selectedValue ||
opt.originalData?.product_id?.toString() === selectedValue;
});
console.log("Selected Product:", selectedProduct);
// Get the correct product ID (prefer originalData.product_id, then productId, then value)
const productId = selectedProduct?.originalData?.product_id ||
selectedProduct?.productId ||
selectedProduct?.value;
console.log("productId",productId)
// Get unit information from the selected product
const unitId = selectedProduct?.originalData?.['product.unit.id'] ||
selectedProduct?.unitId ||
@ -1352,19 +1367,20 @@ const location = useLocation();
localStorage.getItem('establishment_id');
if (establishmentId && quarter && year) {
const selectedProductId = selectedProduct.originalData?.id || selectedProduct.value;
const selectedProductId = selectedProduct.originalData?.product_id || selectedProduct.value;
console.log('Using product ID for API call:', selectedProductId);
const forecastData = await fetchPreviousForecastData(selectedProductId, establishmentId, p.id);
if (forecastData) {
const data = forecastData.data || forecastData;
const updatedWithForecast = {
...updatedProduct,
octQuantity: data.previous_quantity_period_one || '',
novQuantity: data.previous_quantity_period_two || '',
decQuantity: data.previous_quantity_period_three || '',
octCost: data.previous_cost_period_one || '',
novCost: data.previous_cost_period_two || '',
decCost: data.previous_cost_period_three || '',
octQuantity: data.current_quantity_period_one || '',
novQuantity: data.current_quantity_period_two || '',
decQuantity: data.current_quantity_period_three || '',
octCost: data.current_cost_period_one || '',
novCost: data.current_cost_period_two || '',
decCost: data.current_cost_period_three || '',
capacity: data.annual_installed_capacity || ''
};