Imporve the i18 and frontend UI pages

This commit is contained in:
Urtzi Alfaro
2025-09-22 16:10:08 +02:00
parent ee36c45d25
commit 8d54202e91
32 changed files with 875 additions and 434 deletions

View File

@@ -30,7 +30,11 @@ class ProcurementSchedulerService(BaseAlertService, AlertServiceMixin):
"""Initialize scheduler and procurement service"""
# Initialize base alert service
await super().start()
# Initialize procurement service instance for reuse
from app.core.database import AsyncSessionLocal
self.db_session_factory = AsyncSessionLocal
logger.info("Procurement scheduler service started", service=self.config.SERVICE_NAME)
def setup_scheduled_checks(self):
@@ -45,112 +49,153 @@ class ProcurementSchedulerService(BaseAlertService, AlertServiceMixin):
coalesce=True,
max_instances=1
)
# Also add a test job that runs every 30 minutes for development/testing
# This will be disabled in production via environment variable
if getattr(self.config, 'DEBUG', False) or getattr(self.config, 'PROCUREMENT_TEST_MODE', False):
self.scheduler.add_job(
func=self.run_daily_procurement_planning,
trigger=CronTrigger(minute='*/30'), # Every 30 minutes
id="test_procurement_planning",
name="Test Procurement Planning (30min)",
misfire_grace_time=300,
coalesce=True,
max_instances=1
)
logger.info("⚡ Test procurement planning job added (every 30 minutes)")
# Weekly procurement optimization at 7:00 AM on Mondays
self.scheduler.add_job(
func=self.run_weekly_optimization,
trigger=CronTrigger(day_of_week=0, hour=7, minute=0),
id="weekly_procurement_optimization",
id="weekly_procurement_optimization",
name="Weekly Procurement Optimization",
misfire_grace_time=600,
coalesce=True,
max_instances=1
)
logger.info("Procurement scheduled jobs configured")
logger.info("📅 Procurement scheduled jobs configured",
jobs_count=len(self.scheduler.get_jobs()))
async def run_daily_procurement_planning(self):
"""Execute daily procurement planning for all active tenants"""
if not self.is_leader:
logger.debug("Skipping procurement planning - not leader")
return
try:
self._checks_performed += 1
logger.info("Starting daily procurement planning")
# Get active tenants
logger.info("🔄 Starting daily procurement planning execution",
timestamp=datetime.now().isoformat())
# Get active tenants from tenant service
active_tenants = await self.get_active_tenants()
if not active_tenants:
logger.info("No active tenants found for procurement planning")
return
# Process each tenant
processed_tenants = 0
failed_tenants = 0
for tenant_id in active_tenants:
try:
logger.info("Processing tenant procurement", tenant_id=str(tenant_id))
await self.process_tenant_procurement(tenant_id)
processed_tenants += 1
logger.info("✅ Successfully processed tenant", tenant_id=str(tenant_id))
except Exception as e:
logger.error("Error processing tenant procurement",
failed_tenants += 1
logger.error("❌ Error processing tenant procurement",
tenant_id=str(tenant_id),
error=str(e))
logger.info("Daily procurement planning completed",
logger.info("🎯 Daily procurement planning completed",
total_tenants=len(active_tenants),
processed_tenants=processed_tenants)
processed_tenants=processed_tenants,
failed_tenants=failed_tenants)
except Exception as e:
self._errors_count += 1
logger.error("Daily procurement planning failed", error=str(e))
logger.error("💥 Daily procurement planning failed completely", error=str(e))
async def get_active_tenants(self) -> List[UUID]:
"""Override to return test tenant since tenants table is not in orders DB"""
# For testing, return the known test tenant
return [UUID('c464fb3e-7af2-46e6-9e43-85318f34199a')]
"""Get active tenants from tenant service or base implementation"""
# Only use tenant service, no fallbacks
try:
return await super().get_active_tenants()
except Exception as e:
logger.error("Could not fetch tenants from base service", error=str(e))
return []
async def process_tenant_procurement(self, tenant_id: UUID):
"""Process procurement planning for a specific tenant"""
try:
# Use default configuration since tenants table is not in orders DB
planning_days = 7 # Default planning horizon
# Calculate planning date (tomorrow by default)
planning_date = datetime.now().date() + timedelta(days=1)
logger.info("Processing procurement for tenant",
tenant_id=str(tenant_id),
planning_date=str(planning_date),
planning_days=planning_days)
# Create procurement service instance and generate plan
from app.core.database import AsyncSessionLocal
from app.schemas.procurement_schemas import GeneratePlanRequest
from decimal import Decimal
async with AsyncSessionLocal() as session:
procurement_service = ProcurementService(session, self.config)
# Check if plan already exists for this date
existing_plan = await procurement_service.get_plan_by_date(
tenant_id, planning_date
)
if existing_plan:
logger.debug("Procurement plan already exists",
logger.info("📋 Procurement plan already exists, skipping",
tenant_id=str(tenant_id),
plan_date=str(planning_date))
plan_date=str(planning_date),
plan_id=str(existing_plan.id))
return
# Generate procurement plan
request = GeneratePlanRequest(
plan_date=planning_date,
planning_horizon_days=planning_days,
include_safety_stock=True,
safety_stock_percentage=Decimal('20.0')
safety_stock_percentage=Decimal('20.0'),
force_regenerate=False
)
logger.info("📊 Generating procurement plan",
tenant_id=str(tenant_id),
request_params=str(request.model_dump()))
result = await procurement_service.generate_procurement_plan(tenant_id, request)
plan = result.plan if result.success else None
if plan:
# Send notification about new plan
await self.send_procurement_notification(
tenant_id, plan, "plan_created"
)
logger.info("Procurement plan created successfully",
tenant_id=str(tenant_id),
plan_id=str(plan.id),
plan_date=str(planning_date))
if result.success and result.plan:
# Send notification about new plan
await self.send_procurement_notification(
tenant_id, result.plan, "plan_created"
)
logger.info("🎉 Procurement plan created successfully",
tenant_id=str(tenant_id),
plan_id=str(result.plan.id),
plan_date=str(planning_date),
total_requirements=result.plan.total_requirements)
else:
logger.warning("⚠️ Failed to generate procurement plan",
tenant_id=str(tenant_id),
errors=result.errors,
warnings=result.warnings)
except Exception as e:
logger.error("Error processing tenant procurement",
logger.error("💥 Error processing tenant procurement",
tenant_id=str(tenant_id),
error=str(e))
raise
@@ -237,10 +282,16 @@ class ProcurementSchedulerService(BaseAlertService, AlertServiceMixin):
error=str(e))
async def test_procurement_generation(self):
"""Test method to manually trigger procurement planning for testing"""
test_tenant_id = UUID('c464fb3e-7af2-46e6-9e43-85318f34199a')
"""Test method to manually trigger procurement planning"""
# Get the first available tenant for testing
active_tenants = await self.get_active_tenants()
if not active_tenants:
logger.error("No active tenants found for testing procurement generation")
return
test_tenant_id = active_tenants[0]
logger.info("Testing procurement plan generation", tenant_id=str(test_tenant_id))
try:
await self.process_tenant_procurement(test_tenant_id)
logger.info("Test procurement generation completed successfully")

View File

@@ -164,13 +164,22 @@ class ProcurementService:
if requirements_data:
await self.requirement_repo.create_requirements_batch(requirements_data)
# Update plan with correct total_requirements count
await self.plan_repo.update_plan(
plan.id,
tenant_id,
{"total_requirements": len(requirements_data)}
# Calculate total costs from requirements
total_estimated_cost = sum(
req_data.get('estimated_total_cost', Decimal('0'))
for req_data in requirements_data
)
# Update plan with correct totals
plan_updates = {
"total_requirements": len(requirements_data),
"total_estimated_cost": total_estimated_cost,
"total_approved_cost": Decimal('0'), # Will be updated during approval
"cost_variance": Decimal('0') - total_estimated_cost # Initial variance
}
await self.plan_repo.update_plan(plan.id, tenant_id, plan_updates)
await self.db.commit()
@@ -213,6 +222,13 @@ class ProcurementService:
if status == "approved":
updates["approved_at"] = datetime.utcnow()
updates["approved_by"] = updated_by
# When approving, set approved cost equal to estimated cost
# (In real system, this might be different based on actual approvals)
plan = await self.plan_repo.get_plan_by_id(plan_id, tenant_id)
if plan and plan.total_estimated_cost:
updates["total_approved_cost"] = plan.total_estimated_cost
updates["cost_variance"] = Decimal('0') # No variance initially
elif status == "in_execution":
updates["execution_started_at"] = datetime.utcnow()
elif status in ["completed", "cancelled"]:
@@ -497,25 +513,168 @@ class ProcurementService:
async def _get_procurement_summary(self, tenant_id: uuid.UUID) -> ProcurementSummary:
"""Get procurement summary for dashboard"""
# Implement summary calculation
return ProcurementSummary(
total_plans=0,
active_plans=0,
total_requirements=0,
pending_requirements=0,
critical_requirements=0,
total_estimated_cost=Decimal('0'),
total_approved_cost=Decimal('0'),
cost_variance=Decimal('0')
)
try:
# Get all plans for the tenant
all_plans = await self.plan_repo.list_plans(tenant_id, limit=1000)
# Debug logging
logger.info(f"Found {len(all_plans)} plans for tenant {tenant_id}")
for plan in all_plans[:3]: # Log first 3 plans for debugging
logger.info(f"Plan {plan.plan_number}: status={plan.status}, requirements={plan.total_requirements}, cost={plan.total_estimated_cost}")
# Calculate total and active plans
total_plans = len(all_plans)
active_statuses = ['draft', 'pending_approval', 'approved', 'in_execution']
active_plans = len([p for p in all_plans if p.status in active_statuses])
# Get all requirements for analysis
pending_requirements = []
critical_requirements = []
try:
pending_requirements = await self.requirement_repo.get_pending_requirements(tenant_id)
logger.info(f"Found {len(pending_requirements)} pending requirements")
except Exception as req_err:
logger.warning(f"Error getting pending requirements: {req_err}")
try:
critical_requirements = await self.requirement_repo.get_critical_requirements(tenant_id)
logger.info(f"Found {len(critical_requirements)} critical requirements")
except Exception as crit_err:
logger.warning(f"Error getting critical requirements: {crit_err}")
# Calculate total requirements across all plans
total_requirements = 0
total_estimated_cost = Decimal('0')
total_approved_cost = Decimal('0')
plans_to_fix = [] # Track plans that need recalculation
for plan in all_plans:
plan_reqs = plan.total_requirements or 0
plan_est_cost = plan.total_estimated_cost or Decimal('0')
plan_app_cost = plan.total_approved_cost or Decimal('0')
# If plan has requirements but zero costs, it needs recalculation
if plan_reqs > 0 and plan_est_cost == Decimal('0'):
plans_to_fix.append(plan.id)
logger.info(f"Plan {plan.plan_number} needs cost recalculation")
total_requirements += plan_reqs
total_estimated_cost += plan_est_cost
total_approved_cost += plan_app_cost
# Fix plans with missing totals (do this in background to avoid blocking dashboard)
if plans_to_fix:
logger.info(f"Found {len(plans_to_fix)} plans that need cost recalculation")
# For now, just log. In production, you might want to queue this for background processing
# Calculate cost variance
cost_variance = total_approved_cost - total_estimated_cost
logger.info(f"Summary totals: plans={total_plans}, active={active_plans}, requirements={total_requirements}, est_cost={total_estimated_cost}")
return ProcurementSummary(
total_plans=total_plans,
active_plans=active_plans,
total_requirements=total_requirements,
pending_requirements=len(pending_requirements),
critical_requirements=len(critical_requirements),
total_estimated_cost=total_estimated_cost,
total_approved_cost=total_approved_cost,
cost_variance=cost_variance
)
except Exception as e:
logger.error("Error calculating procurement summary", error=str(e), tenant_id=tenant_id)
# Return empty summary on error
return ProcurementSummary(
total_plans=0,
active_plans=0,
total_requirements=0,
pending_requirements=0,
critical_requirements=0,
total_estimated_cost=Decimal('0'),
total_approved_cost=Decimal('0'),
cost_variance=Decimal('0')
)
async def _get_upcoming_deliveries(self, tenant_id: uuid.UUID) -> List[Dict[str, Any]]:
"""Get upcoming deliveries"""
return []
try:
# Get requirements with expected delivery dates in the next 7 days
today = date.today()
upcoming_date = today + timedelta(days=7)
# Get all pending requirements that have expected delivery dates
pending_requirements = await self.requirement_repo.get_pending_requirements(tenant_id)
upcoming_deliveries = []
for req in pending_requirements:
if (req.expected_delivery_date and
today <= req.expected_delivery_date <= upcoming_date and
req.delivery_status in ['pending', 'in_transit']):
upcoming_deliveries.append({
"id": str(req.id),
"requirement_number": req.requirement_number,
"product_name": req.product_name,
"supplier_name": req.supplier_name or "Sin proveedor",
"expected_delivery_date": req.expected_delivery_date.isoformat(),
"ordered_quantity": float(req.ordered_quantity or 0),
"unit_of_measure": req.unit_of_measure,
"delivery_status": req.delivery_status,
"days_until_delivery": (req.expected_delivery_date - today).days
})
# Sort by delivery date
upcoming_deliveries.sort(key=lambda x: x["expected_delivery_date"])
return upcoming_deliveries[:10] # Return top 10 upcoming deliveries
except Exception as e:
logger.error("Error getting upcoming deliveries", error=str(e), tenant_id=tenant_id)
return []
async def _get_overdue_requirements(self, tenant_id: uuid.UUID) -> List[Dict[str, Any]]:
"""Get overdue requirements"""
return []
try:
today = date.today()
# Get all pending requirements
pending_requirements = await self.requirement_repo.get_pending_requirements(tenant_id)
overdue_requirements = []
for req in pending_requirements:
# Check if requirement is overdue based on required_by_date
if (req.required_by_date and req.required_by_date < today and
req.status in ['pending', 'approved']):
days_overdue = (today - req.required_by_date).days
overdue_requirements.append({
"id": str(req.id),
"requirement_number": req.requirement_number,
"product_name": req.product_name,
"supplier_name": req.supplier_name or "Sin proveedor",
"required_by_date": req.required_by_date.isoformat(),
"required_quantity": float(req.required_quantity),
"unit_of_measure": req.unit_of_measure,
"status": req.status,
"priority": req.priority,
"days_overdue": days_overdue,
"estimated_total_cost": float(req.estimated_total_cost or 0)
})
# Sort by days overdue (most overdue first)
overdue_requirements.sort(key=lambda x: x["days_overdue"], reverse=True)
return overdue_requirements[:10] # Return top 10 overdue requirements
except Exception as e:
logger.error("Error getting overdue requirements", error=str(e), tenant_id=tenant_id)
return []
async def _get_low_stock_alerts(self, tenant_id: uuid.UUID) -> List[Dict[str, Any]]:
"""Get low stock alerts from inventory service"""
@@ -527,4 +686,63 @@ class ProcurementService:
async def _get_performance_metrics(self, tenant_id: uuid.UUID) -> Dict[str, Any]:
"""Get performance metrics"""
return {}
try:
# Get completed and active plans for metrics calculation
all_plans = await self.plan_repo.list_plans(tenant_id, limit=1000)
completed_plans = [p for p in all_plans if p.status == 'completed']
if not completed_plans:
return {
"average_fulfillment_rate": 0.0,
"average_on_time_delivery": 0.0,
"cost_accuracy": 0.0,
"plan_completion_rate": 0.0,
"supplier_performance": 0.0
}
# Calculate fulfillment rate
total_fulfillment = sum(float(p.fulfillment_rate or 0) for p in completed_plans)
avg_fulfillment = total_fulfillment / len(completed_plans) if completed_plans else 0.0
# Calculate on-time delivery rate
total_on_time = sum(float(p.on_time_delivery_rate or 0) for p in completed_plans)
avg_on_time = total_on_time / len(completed_plans) if completed_plans else 0.0
# Calculate cost accuracy (how close approved costs were to estimated)
cost_accuracy_sum = 0.0
cost_plans_count = 0
for plan in completed_plans:
if plan.total_estimated_cost and plan.total_approved_cost and plan.total_estimated_cost > 0:
accuracy = min(100.0, (float(plan.total_approved_cost) / float(plan.total_estimated_cost)) * 100)
cost_accuracy_sum += accuracy
cost_plans_count += 1
avg_cost_accuracy = cost_accuracy_sum / cost_plans_count if cost_plans_count > 0 else 0.0
# Calculate plan completion rate
total_plans = len(all_plans)
completion_rate = (len(completed_plans) / total_plans * 100) if total_plans > 0 else 0.0
# Calculate supplier performance (average quality score)
quality_scores = [float(p.quality_score or 0) for p in completed_plans if p.quality_score]
avg_supplier_performance = sum(quality_scores) / len(quality_scores) if quality_scores else 0.0
return {
"average_fulfillment_rate": round(avg_fulfillment, 2),
"average_on_time_delivery": round(avg_on_time, 2),
"cost_accuracy": round(avg_cost_accuracy, 2),
"plan_completion_rate": round(completion_rate, 2),
"supplier_performance": round(avg_supplier_performance, 2),
"total_plans_analyzed": len(completed_plans),
"active_plans": len([p for p in all_plans if p.status in ['draft', 'pending_approval', 'approved', 'in_execution']])
}
except Exception as e:
logger.error("Error calculating performance metrics", error=str(e), tenant_id=tenant_id)
return {
"average_fulfillment_rate": 0.0,
"average_on_time_delivery": 0.0,
"cost_accuracy": 0.0,
"plan_completion_rate": 0.0,
"supplier_performance": 0.0
}