Improve te panel de control logic

This commit is contained in:
Urtzi Alfaro
2025-11-21 16:15:09 +01:00
parent 2ee94fb4b1
commit 3242c8d837
21 changed files with 2805 additions and 696 deletions

View File

@@ -417,14 +417,38 @@ class DashboardService:
# Get reasoning type and convert to i18n key
reasoning_type = reasoning_data.get('type', 'inventory_replenishment')
reasoning_type_i18n_key = self._get_reasoning_type_i18n_key(reasoning_type, context="purchaseOrder")
# Check if enhanced mode (has product_details with supply chain intelligence)
is_enhanced_mode = reasoning_data.get('metadata', {}).get('enhanced_mode', False)
# Use enhanced i18n key if available
if is_enhanced_mode and reasoning_type == 'low_stock_detection':
reasoning_type_i18n_key = "reasoning.purchaseOrder.low_stock_detection_detailed"
else:
reasoning_type_i18n_key = self._get_reasoning_type_i18n_key(reasoning_type, context="purchaseOrder")
# Preprocess parameters for i18n - MUST create a copy to avoid modifying immutable database objects
params = dict(reasoning_data.get('parameters', {}))
# Convert product_names array to product_names_joined string
if 'product_names' in params and isinstance(params['product_names'], list):
params['product_names_joined'] = ', '.join(params['product_names'])
# Convert critical_products array to indexed params and joined string for i18n
if 'critical_products' in params and isinstance(params['critical_products'], list):
critical_prods = params['critical_products']
# Add indexed params for select/plural statements
for i, prod in enumerate(critical_prods[:3]): # Limit to first 3
params[f'critical_products_{i}'] = prod
params['critical_products_joined'] = ', '.join(critical_prods)
# Convert affected_batches array to indexed params for i18n
if 'affected_batches' in params and isinstance(params['affected_batches'], list):
batches = params['affected_batches']
for i, batch in enumerate(batches[:3]): # Limit to first 3
params[f'affected_batches_{i}'] = batch
params['affected_batches_joined'] = ', '.join(batches)
actions.append({
"id": po["id"],
"type": ActionType.APPROVE_PO,
@@ -594,7 +618,8 @@ class DashboardService:
if actual_start and planned_end:
total_duration = (planned_end - actual_start).total_seconds()
elapsed = (now - actual_start).total_seconds()
progress = min(int((elapsed / total_duration) * 100), 99)
# Ensure progress is never negative (defensive programming)
progress = max(0, min(int((elapsed / total_duration) * 100), 99))
else:
progress = 50
status_icon = "🔄"
@@ -604,10 +629,12 @@ class DashboardService:
"params": {}
}
else:
# PENDING, SCHEDULED, or any other status
progress = 0
status_icon = ""
status_text = "PENDING"
status_text = status # Use actual status
status_i18n = {
"key": "production.status.pending",
"key": f"production.status.{status.lower()}",
"params": {}
}