Fix issues 4
This commit is contained in:
@@ -547,6 +547,9 @@ export class InventoryService {
|
||||
*/
|
||||
async getProductsList(tenantId: string): Promise<ProductInfo[]> {
|
||||
try {
|
||||
console.log('🔍 Fetching products for forecasting...', { tenantId });
|
||||
|
||||
// First try to get finished products (preferred for forecasting)
|
||||
const response = await apiClient.get(`/tenants/${tenantId}/ingredients`, {
|
||||
params: {
|
||||
limit: 100,
|
||||
@@ -606,7 +609,47 @@ export class InventoryService {
|
||||
}))
|
||||
.filter(product => product.inventory_product_id && product.name);
|
||||
|
||||
console.log('📋 Processed inventory products:', products);
|
||||
console.log('📋 Processed finished products:', products);
|
||||
|
||||
// If no finished products found, try to get all products as fallback
|
||||
if (products.length === 0) {
|
||||
console.log('⚠️ No finished products found, trying to get all products as fallback...');
|
||||
|
||||
const fallbackResponse = await apiClient.get(`/tenants/${tenantId}/ingredients`, {
|
||||
params: {
|
||||
limit: 100,
|
||||
// No product_type filter to get all products
|
||||
},
|
||||
});
|
||||
|
||||
console.log('🔍 Fallback API Response:', fallbackResponse);
|
||||
|
||||
const fallbackDataToProcess = fallbackResponse?.data || fallbackResponse;
|
||||
let fallbackProductsArray: any[] = [];
|
||||
|
||||
if (Array.isArray(fallbackDataToProcess)) {
|
||||
fallbackProductsArray = fallbackDataToProcess;
|
||||
} else if (fallbackDataToProcess && typeof fallbackDataToProcess === 'object') {
|
||||
const keys = Object.keys(fallbackDataToProcess);
|
||||
if (keys.length > 0 && keys.every(key => !isNaN(Number(key)))) {
|
||||
fallbackProductsArray = Object.values(fallbackDataToProcess);
|
||||
}
|
||||
}
|
||||
|
||||
const fallbackProducts: ProductInfo[] = fallbackProductsArray
|
||||
.map((product: any) => ({
|
||||
inventory_product_id: product.id || product.inventory_product_id,
|
||||
name: product.name || product.product_name || `Product ${product.id || ''}`,
|
||||
category: product.category,
|
||||
current_stock: product.current_stock,
|
||||
unit: product.unit,
|
||||
cost_per_unit: product.cost_per_unit
|
||||
}))
|
||||
.filter(product => product.inventory_product_id && product.name);
|
||||
|
||||
console.log('📋 Processed fallback products (all inventory items):', fallbackProducts);
|
||||
return fallbackProducts;
|
||||
}
|
||||
|
||||
return products;
|
||||
|
||||
|
||||
@@ -87,12 +87,10 @@ const ForecastPage: React.FC = () => {
|
||||
try {
|
||||
const products = await getProductsList(tenantId);
|
||||
|
||||
// If no finished products found, use fallback products with trained models
|
||||
// Always use real inventory products - no hardcoded fallbacks
|
||||
if (products.length === 0) {
|
||||
setInventoryItems([
|
||||
{ id: '3ae0afca-3e75-4f93-b6af-2d24c24bfcd5', name: 'Croissants' },
|
||||
{ id: 'b2341c5d-db5d-418e-a978-6d24cd9f039e', name: 'Pan de molde' }
|
||||
]);
|
||||
console.warn('⚠️ No finished products found in inventory for forecasting');
|
||||
setInventoryItems([]);
|
||||
} else {
|
||||
// Map products to the expected format
|
||||
setInventoryItems(products.map(p => ({
|
||||
@@ -102,11 +100,8 @@ const ForecastPage: React.FC = () => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load products:', error);
|
||||
// Use fallback products with trained models
|
||||
setInventoryItems([
|
||||
{ id: '3ae0afca-3e75-4f93-b6af-2d24c24bfcd5', name: 'Croissants' },
|
||||
{ id: 'b2341c5d-db5d-418e-a978-6d24cd9f039e', name: 'Pan de molde' }
|
||||
]);
|
||||
// Don't use fake fallback products - show empty state instead
|
||||
setInventoryItems([]);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -462,7 +457,19 @@ const ForecastPage: React.FC = () => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{forecastData.length === 0 && !isLoading && !isGenerating && (
|
||||
{inventoryItems.length === 0 && !isLoading && !isGenerating && (
|
||||
<div className="mt-4 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
|
||||
<div className="flex items-center">
|
||||
<AlertTriangle className="h-5 w-5 text-yellow-600 mr-2" />
|
||||
<span className="text-yellow-800 text-sm">
|
||||
<strong>No hay productos disponibles para predicciones.</strong>
|
||||
<br />Para usar esta funcionalidad, necesitas crear productos terminados (como pan, croissants, etc.) en tu inventario con tipo "Producto terminado".
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{inventoryItems.length > 0 && forecastData.length === 0 && !isLoading && !isGenerating && (
|
||||
<div className="mt-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
|
||||
<div className="flex items-center">
|
||||
<Info className="h-5 w-5 text-blue-600 mr-2" />
|
||||
|
||||
Reference in New Issue
Block a user