feat: Complete backend i18n implementation with error codes and demo data

Demo Seed Scripts:
- Updated seed_demo_purchase_orders.py to use structured reasoning_data
  * Imports create_po_reasoning_low_stock and create_po_reasoning_supplier_contract
  * Generates reasoning_data with product names, stock levels, and consequences
  * Removed deprecated reasoning/consequence TEXT fields
- Updated seed_demo_batches.py to use structured reasoning_data
  * Imports create_batch_reasoning_forecast_demand and create_batch_reasoning_regular_schedule
  * Generates intelligent reasoning based on batch priority and AI assistance
  * Adds reasoning_data to all production batches

Backend Services - Error Code Implementation:
- Updated safety_stock_calculator.py with error codes
  * Replaced "Lead time or demand std dev is zero or negative" with ERROR:LEAD_TIME_INVALID
  * Replaced "Insufficient historical demand data" with ERROR:INSUFFICIENT_DATA
- Updated replenishment_planning_service.py with error codes
  * Replaced "Insufficient data for safety stock calculation" with ERROR:INSUFFICIENT_DATA
  * Frontend can now translate error codes using i18n

Demo data will now display with translatable reasoning in EN/ES/EU languages.
Backend services return error codes that frontend translates for user's language.
This commit is contained in:
Claude
2025-11-07 18:40:44 +00:00
parent 28136cf198
commit ed7db4d4f2
4 changed files with 70 additions and 29 deletions

View File

@@ -25,6 +25,10 @@ import structlog
from app.models.production import ProductionBatch, ProductionStatus, ProductionPriority, ProcessStage
# Import reasoning helper functions for i18n support
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from shared.schemas.reasoning_types import create_batch_reasoning_forecast_demand, create_batch_reasoning_regular_schedule
# Configure logging
logger = structlog.get_logger()
@@ -161,6 +165,29 @@ async def seed_batches_for_tenant(
# For La Espiga, append tenant suffix to make batch number unique
batch_number = batch_data["batch_number"] + "-LE"
# Generate structured reasoning_data for i18n support
reasoning_data = None
try:
# Use forecast demand reasoning for most batches
if batch_data.get("is_ai_assisted") or priority in [ProductionPriority.HIGH, ProductionPriority.URGENT]:
reasoning_data = create_batch_reasoning_forecast_demand(
product_name=batch_data["product_name"],
predicted_demand=batch_data["planned_quantity"],
current_stock=int(batch_data["planned_quantity"] * 0.3), # Demo: assume 30% current stock
production_needed=batch_data["planned_quantity"],
target_date=planned_start.date().isoformat(),
confidence_score=0.85 if batch_data.get("is_ai_assisted") else 0.75
)
else:
# Regular schedule reasoning for standard batches
reasoning_data = create_batch_reasoning_regular_schedule(
product_name=batch_data["product_name"],
schedule_frequency="daily",
batch_size=batch_data["planned_quantity"]
)
except Exception as e:
logger.warning(f"Failed to generate reasoning_data for batch {batch_number}: {e}")
# Create production batch
batch = ProductionBatch(
id=batch_id,
@@ -197,6 +224,7 @@ async def seed_batches_for_tenant(
waste_defect_type=batch_data.get("waste_defect_type"),
production_notes=batch_data.get("production_notes"),
quality_notes=batch_data.get("quality_notes"),
reasoning_data=reasoning_data, # Structured reasoning for i18n support
created_at=BASE_REFERENCE_DATE,
updated_at=BASE_REFERENCE_DATE,
completed_at=completed_at