Improve the sales import
This commit is contained in:
@@ -239,7 +239,7 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
|
||||
|
||||
const handleCreateInventory = async () => {
|
||||
const selectedItems = inventoryItems.filter(item => item.selected);
|
||||
|
||||
|
||||
if (selectedItems.length === 0) {
|
||||
setError('Por favor selecciona al menos un artículo de inventario para crear');
|
||||
return;
|
||||
@@ -254,22 +254,18 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
|
||||
setError('');
|
||||
|
||||
try {
|
||||
const createdIngredients = [];
|
||||
// Parallel inventory creation
|
||||
setProgressState({
|
||||
stage: 'creating_inventory',
|
||||
progress: 10,
|
||||
message: `Creando ${selectedItems.length} artículos de inventario...`
|
||||
});
|
||||
|
||||
for (const item of selectedItems) {
|
||||
// Ensure reorder_point > minimum_stock_level as required by backend validation
|
||||
const creationPromises = selectedItems.map(item => {
|
||||
const minimumStock = Math.max(1, Math.ceil(item.stock_quantity * 0.2));
|
||||
const calculatedReorderPoint = Math.ceil(item.stock_quantity * 0.3);
|
||||
const reorderPoint = Math.max(minimumStock + 2, calculatedReorderPoint, minimumStock + 1);
|
||||
|
||||
console.log(`📊 Inventory validation for "${item.suggested_name}":`, {
|
||||
stockQuantity: item.stock_quantity,
|
||||
minimumStock,
|
||||
calculatedReorderPoint,
|
||||
finalReorderPoint: reorderPoint,
|
||||
isValid: reorderPoint > minimumStock
|
||||
});
|
||||
|
||||
|
||||
const ingredientData = {
|
||||
name: item.suggested_name,
|
||||
category: item.category,
|
||||
@@ -285,18 +281,36 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
|
||||
notes: item.notes || `Creado durante onboarding - Confianza: ${Math.round(item.confidence_score * 100)}%`
|
||||
};
|
||||
|
||||
const created = await createIngredient.mutateAsync({
|
||||
return createIngredient.mutateAsync({
|
||||
tenantId: currentTenant.id,
|
||||
ingredientData
|
||||
});
|
||||
|
||||
createdIngredients.push({
|
||||
}).then(created => ({
|
||||
...created,
|
||||
initialStock: item.stock_quantity
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
const results = await Promise.allSettled(creationPromises);
|
||||
|
||||
const createdIngredients = results
|
||||
.filter(r => r.status === 'fulfilled')
|
||||
.map(r => (r as PromiseFulfilledResult<any>).value);
|
||||
|
||||
const failedCount = results.filter(r => r.status === 'rejected').length;
|
||||
|
||||
if (failedCount > 0) {
|
||||
console.warn(`${failedCount} items failed to create out of ${selectedItems.length}`);
|
||||
}
|
||||
|
||||
console.log(`Successfully created ${createdIngredients.length} inventory items in parallel`);
|
||||
|
||||
// After inventory creation, import the sales data
|
||||
setProgressState({
|
||||
stage: 'importing_sales',
|
||||
progress: 50,
|
||||
message: 'Importando datos de ventas...'
|
||||
});
|
||||
|
||||
console.log('Importing sales data after inventory creation...');
|
||||
let salesImportResult = null;
|
||||
try {
|
||||
@@ -305,29 +319,33 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
|
||||
tenantId: currentTenant.id,
|
||||
file: selectedFile
|
||||
});
|
||||
|
||||
|
||||
salesImportResult = result;
|
||||
if (result.success) {
|
||||
console.log('Sales data imported successfully');
|
||||
setProgressState({
|
||||
stage: 'completing',
|
||||
progress: 95,
|
||||
message: 'Finalizando configuración...'
|
||||
});
|
||||
} else {
|
||||
console.warn('Sales import completed with issues:', result.error);
|
||||
}
|
||||
}
|
||||
} catch (importError) {
|
||||
console.error('Error importing sales data:', importError);
|
||||
// Don't fail the entire process if import fails - the inventory has been created successfully
|
||||
}
|
||||
|
||||
setProgressState(null);
|
||||
onComplete({
|
||||
createdIngredients,
|
||||
totalItems: selectedItems.length,
|
||||
totalItems: createdIngredients.length,
|
||||
validationResult,
|
||||
file: selectedFile,
|
||||
salesImportResult,
|
||||
inventoryConfigured: true, // Flag for ML training dependency
|
||||
shouldAutoCompleteSuppliers: true, // Flag to trigger suppliers auto-completion after step completion
|
||||
userId: user?.id // Pass user ID for suppliers completion
|
||||
inventoryConfigured: true,
|
||||
shouldAutoCompleteSuppliers: true,
|
||||
userId: user?.id
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error creating inventory items:', err);
|
||||
|
||||
Reference in New Issue
Block a user