497 lines
18 KiB
Python
Executable File
497 lines
18 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Demo Procurement Seeding Script for Orders Service
|
|
Creates procurement plans and requirements for demo template tenants
|
|
|
|
This script runs as a Kubernetes init job inside the orders-service container.
|
|
"""
|
|
|
|
import asyncio
|
|
import uuid
|
|
import sys
|
|
import os
|
|
import json
|
|
import random
|
|
from datetime import datetime, timezone, timedelta, date
|
|
from pathlib import Path
|
|
from decimal import Decimal
|
|
|
|
# Add app to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy import select
|
|
import structlog
|
|
|
|
from app.models.procurement import ProcurementPlan, ProcurementRequirement
|
|
|
|
# Configure logging
|
|
logger = structlog.get_logger()
|
|
|
|
# Base demo tenant IDs
|
|
DEMO_TENANT_SAN_PABLO = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6") # Individual bakery
|
|
DEMO_TENANT_LA_ESPIGA = uuid.UUID("b2c3d4e5-f6a7-48b9-c0d1-e2f3a4b5c6d7") # Central bakery
|
|
|
|
# Base reference date for date calculations
|
|
BASE_REFERENCE_DATE = datetime(2025, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
|
|
|
|
|
|
def load_procurement_config():
|
|
"""Load procurement configuration from JSON file"""
|
|
config_file = Path(__file__).parent / "compras_config_es.json"
|
|
if not config_file.exists():
|
|
raise FileNotFoundError(f"Procurement config file not found: {config_file}")
|
|
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
|
|
|
|
def calculate_date_from_offset(offset_days: int) -> date:
|
|
"""Calculate a date based on offset from BASE_REFERENCE_DATE"""
|
|
return (BASE_REFERENCE_DATE + timedelta(days=offset_days)).date()
|
|
|
|
|
|
def calculate_datetime_from_offset(offset_days: int) -> datetime:
|
|
"""Calculate a datetime based on offset from BASE_REFERENCE_DATE"""
|
|
return BASE_REFERENCE_DATE + timedelta(days=offset_days)
|
|
|
|
|
|
def weighted_choice(choices: list) -> dict:
|
|
"""Make a weighted random choice from list of dicts with 'peso' key"""
|
|
total_weight = sum(c.get("peso", 1.0) for c in choices)
|
|
r = random.uniform(0, total_weight)
|
|
|
|
cumulative = 0
|
|
for choice in choices:
|
|
cumulative += choice.get("peso", 1.0)
|
|
if r <= cumulative:
|
|
return choice
|
|
|
|
return choices[-1]
|
|
|
|
|
|
def generate_plan_number(tenant_id: uuid.UUID, index: int, plan_type: str) -> str:
|
|
"""Generate a unique plan number"""
|
|
tenant_prefix = "SP" if tenant_id == DEMO_TENANT_SAN_PABLO else "LE"
|
|
type_code = plan_type[0:3].upper()
|
|
return f"PROC-{tenant_prefix}-{type_code}-{BASE_REFERENCE_DATE.year}-{index:03d}"
|
|
|
|
|
|
async def generate_procurement_for_tenant(
|
|
db: AsyncSession,
|
|
tenant_id: uuid.UUID,
|
|
tenant_name: str,
|
|
business_model: str,
|
|
config: dict
|
|
):
|
|
"""Generate procurement plans and requirements for a specific tenant"""
|
|
logger.info(f"Generating procurement data for: {tenant_name}", tenant_id=str(tenant_id))
|
|
|
|
# Check if procurement plans already exist
|
|
result = await db.execute(
|
|
select(ProcurementPlan).where(ProcurementPlan.tenant_id == tenant_id).limit(1)
|
|
)
|
|
existing = result.scalar_one_or_none()
|
|
|
|
if existing:
|
|
logger.info(f"Procurement plans already exist for {tenant_name}, skipping seed")
|
|
return {"tenant_id": str(tenant_id), "plans_created": 0, "requirements_created": 0, "skipped": True}
|
|
|
|
proc_config = config["configuracion_compras"]
|
|
total_plans = proc_config["planes_por_tenant"]
|
|
|
|
plans_created = 0
|
|
requirements_created = 0
|
|
|
|
for i in range(total_plans):
|
|
# Determine temporal distribution
|
|
rand_temporal = random.random()
|
|
cumulative = 0
|
|
temporal_category = None
|
|
|
|
for category, details in proc_config["distribucion_temporal"].items():
|
|
cumulative += details["porcentaje"]
|
|
if rand_temporal <= cumulative:
|
|
temporal_category = details
|
|
break
|
|
|
|
if not temporal_category:
|
|
temporal_category = proc_config["distribucion_temporal"]["completados"]
|
|
|
|
# Calculate plan date
|
|
offset_days = random.randint(
|
|
temporal_category["offset_dias_min"],
|
|
temporal_category["offset_dias_max"]
|
|
)
|
|
plan_date = calculate_date_from_offset(offset_days)
|
|
|
|
# Select status
|
|
status = random.choice(temporal_category["estados"])
|
|
|
|
# Select plan type
|
|
plan_type_choice = weighted_choice(proc_config["tipos_plan"])
|
|
plan_type = plan_type_choice["tipo"]
|
|
|
|
# Select priority
|
|
priority_rand = random.random()
|
|
cumulative_priority = 0
|
|
priority = "normal"
|
|
for p, weight in proc_config["prioridades"].items():
|
|
cumulative_priority += weight
|
|
if priority_rand <= cumulative_priority:
|
|
priority = p
|
|
break
|
|
|
|
# Select procurement strategy
|
|
strategy_choice = weighted_choice(proc_config["estrategias_compra"])
|
|
procurement_strategy = strategy_choice["estrategia"]
|
|
|
|
# Select supply risk level
|
|
risk_rand = random.random()
|
|
cumulative_risk = 0
|
|
supply_risk_level = "low"
|
|
for risk, weight in proc_config["niveles_riesgo"].items():
|
|
cumulative_risk += weight
|
|
if risk_rand <= cumulative_risk:
|
|
supply_risk_level = risk
|
|
break
|
|
|
|
# Calculate planning horizon
|
|
planning_horizon = proc_config["horizonte_planificacion_dias"][business_model]
|
|
|
|
# Calculate period dates
|
|
period_start = plan_date
|
|
period_end = plan_date + timedelta(days=planning_horizon)
|
|
|
|
# Generate plan number
|
|
plan_number = generate_plan_number(tenant_id, i + 1, plan_type)
|
|
|
|
# Calculate safety stock buffer
|
|
safety_stock_buffer = Decimal(str(random.uniform(
|
|
proc_config["buffer_seguridad_porcentaje"]["min"],
|
|
proc_config["buffer_seguridad_porcentaje"]["max"]
|
|
)))
|
|
|
|
# Calculate approval/execution dates based on status
|
|
approved_at = None
|
|
execution_started_at = None
|
|
execution_completed_at = None
|
|
approved_by = None
|
|
|
|
if status in ["approved", "in_execution", "completed"]:
|
|
approved_at = calculate_datetime_from_offset(offset_days - 1)
|
|
approved_by = uuid.uuid4() # Would be actual user ID
|
|
|
|
if status in ["in_execution", "completed"]:
|
|
execution_started_at = calculate_datetime_from_offset(offset_days)
|
|
|
|
if status == "completed":
|
|
execution_completed_at = calculate_datetime_from_offset(offset_days + planning_horizon)
|
|
|
|
# Calculate performance metrics for completed plans
|
|
fulfillment_rate = None
|
|
on_time_delivery_rate = None
|
|
cost_accuracy = None
|
|
quality_score = None
|
|
|
|
if status == "completed":
|
|
metrics = proc_config["metricas_rendimiento"]
|
|
fulfillment_rate = Decimal(str(random.uniform(
|
|
metrics["tasa_cumplimiento"]["min"],
|
|
metrics["tasa_cumplimiento"]["max"]
|
|
)))
|
|
on_time_delivery_rate = Decimal(str(random.uniform(
|
|
metrics["entrega_puntual"]["min"],
|
|
metrics["entrega_puntual"]["max"]
|
|
)))
|
|
cost_accuracy = Decimal(str(random.uniform(
|
|
metrics["precision_costo"]["min"],
|
|
metrics["precision_costo"]["max"]
|
|
)))
|
|
quality_score = Decimal(str(random.uniform(
|
|
metrics["puntuacion_calidad"]["min"],
|
|
metrics["puntuacion_calidad"]["max"]
|
|
)))
|
|
|
|
# Create procurement plan
|
|
plan = ProcurementPlan(
|
|
id=uuid.uuid4(),
|
|
tenant_id=tenant_id,
|
|
plan_number=plan_number,
|
|
plan_date=plan_date,
|
|
plan_period_start=period_start,
|
|
plan_period_end=period_end,
|
|
planning_horizon_days=planning_horizon,
|
|
status=status,
|
|
plan_type=plan_type,
|
|
priority=priority,
|
|
business_model=business_model,
|
|
procurement_strategy=procurement_strategy,
|
|
total_requirements=0, # Will update after adding requirements
|
|
total_estimated_cost=Decimal("0.00"), # Will calculate
|
|
total_approved_cost=Decimal("0.00"),
|
|
safety_stock_buffer=safety_stock_buffer,
|
|
supply_risk_level=supply_risk_level,
|
|
demand_forecast_confidence=Decimal(str(random.uniform(7.0, 9.5))),
|
|
approved_at=approved_at,
|
|
approved_by=approved_by,
|
|
execution_started_at=execution_started_at,
|
|
execution_completed_at=execution_completed_at,
|
|
fulfillment_rate=fulfillment_rate,
|
|
on_time_delivery_rate=on_time_delivery_rate,
|
|
cost_accuracy=cost_accuracy,
|
|
quality_score=quality_score,
|
|
created_at=calculate_datetime_from_offset(offset_days - 2),
|
|
updated_at=calculate_datetime_from_offset(offset_days)
|
|
)
|
|
|
|
db.add(plan)
|
|
await db.flush() # Get plan ID
|
|
|
|
# Generate requirements for this plan
|
|
num_requirements = random.randint(
|
|
proc_config["requisitos_por_plan"]["min"],
|
|
proc_config["requisitos_por_plan"]["max"]
|
|
)
|
|
|
|
# Select random ingredients
|
|
selected_ingredients = random.sample(
|
|
proc_config["ingredientes_demo"],
|
|
min(num_requirements, len(proc_config["ingredientes_demo"]))
|
|
)
|
|
|
|
total_estimated_cost = Decimal("0.00")
|
|
|
|
for req_num, ingredient in enumerate(selected_ingredients, 1):
|
|
# Get quantity range for category
|
|
category = ingredient["categoria"]
|
|
cantidad_range = proc_config["rangos_cantidad"].get(
|
|
category,
|
|
{"min": 50.0, "max": 200.0}
|
|
)
|
|
|
|
# Calculate required quantity
|
|
required_quantity = Decimal(str(random.uniform(
|
|
cantidad_range["min"],
|
|
cantidad_range["max"]
|
|
)))
|
|
|
|
# Calculate safety stock
|
|
safety_stock_quantity = required_quantity * (safety_stock_buffer / 100)
|
|
|
|
# Total quantity needed
|
|
total_quantity_needed = required_quantity + safety_stock_quantity
|
|
|
|
# Current stock simulation
|
|
current_stock_level = required_quantity * Decimal(str(random.uniform(0.1, 0.4)))
|
|
reserved_stock = current_stock_level * Decimal(str(random.uniform(0.0, 0.3)))
|
|
available_stock = current_stock_level - reserved_stock
|
|
|
|
# Net requirement
|
|
net_requirement = total_quantity_needed - available_stock
|
|
|
|
# Demand breakdown
|
|
order_demand = required_quantity * Decimal(str(random.uniform(0.5, 0.7)))
|
|
production_demand = required_quantity * Decimal(str(random.uniform(0.2, 0.4)))
|
|
forecast_demand = required_quantity * Decimal(str(random.uniform(0.05, 0.15)))
|
|
buffer_demand = safety_stock_quantity
|
|
|
|
# Pricing
|
|
estimated_unit_cost = Decimal(str(ingredient["costo_unitario"])) * Decimal(str(random.uniform(0.95, 1.05)))
|
|
estimated_total_cost = estimated_unit_cost * net_requirement
|
|
|
|
# Timing
|
|
lead_time_days = ingredient["lead_time_dias"]
|
|
required_by_date = period_start + timedelta(days=random.randint(3, planning_horizon - 2))
|
|
lead_time_buffer_days = random.randint(1, 2)
|
|
suggested_order_date = required_by_date - timedelta(days=lead_time_days + lead_time_buffer_days)
|
|
latest_order_date = required_by_date - timedelta(days=lead_time_days)
|
|
|
|
# Requirement status based on plan status
|
|
if status == "draft":
|
|
req_status = "pending"
|
|
elif status == "pending_approval":
|
|
req_status = "pending"
|
|
elif status == "approved":
|
|
req_status = "approved"
|
|
elif status == "in_execution":
|
|
req_status = random.choice(["ordered", "partially_received"])
|
|
elif status == "completed":
|
|
req_status = "received"
|
|
else:
|
|
req_status = "pending"
|
|
|
|
# Requirement priority
|
|
if priority == "critical":
|
|
req_priority = "critical"
|
|
elif priority == "high":
|
|
req_priority = random.choice(["high", "critical"])
|
|
else:
|
|
req_priority = random.choice(["normal", "high"])
|
|
|
|
# Risk level
|
|
if supply_risk_level == "critical":
|
|
req_risk_level = random.choice(["high", "critical"])
|
|
elif supply_risk_level == "high":
|
|
req_risk_level = random.choice(["medium", "high"])
|
|
else:
|
|
req_risk_level = "low"
|
|
|
|
# Create requirement
|
|
requirement = ProcurementRequirement(
|
|
id=uuid.uuid4(),
|
|
plan_id=plan.id,
|
|
requirement_number=f"{plan_number}-REQ-{req_num:03d}",
|
|
product_id=uuid.UUID(ingredient["id"]),
|
|
product_name=ingredient["nombre"],
|
|
product_sku=ingredient["sku"],
|
|
product_category=ingredient["categoria"],
|
|
product_type=ingredient["tipo"],
|
|
required_quantity=required_quantity,
|
|
unit_of_measure=ingredient["unidad"],
|
|
safety_stock_quantity=safety_stock_quantity,
|
|
total_quantity_needed=total_quantity_needed,
|
|
current_stock_level=current_stock_level,
|
|
reserved_stock=reserved_stock,
|
|
available_stock=available_stock,
|
|
net_requirement=net_requirement,
|
|
order_demand=order_demand,
|
|
production_demand=production_demand,
|
|
forecast_demand=forecast_demand,
|
|
buffer_demand=buffer_demand,
|
|
supplier_lead_time_days=lead_time_days,
|
|
minimum_order_quantity=Decimal(str(ingredient["cantidad_minima"])),
|
|
estimated_unit_cost=estimated_unit_cost,
|
|
estimated_total_cost=estimated_total_cost,
|
|
required_by_date=required_by_date,
|
|
lead_time_buffer_days=lead_time_buffer_days,
|
|
suggested_order_date=suggested_order_date,
|
|
latest_order_date=latest_order_date,
|
|
shelf_life_days=ingredient["vida_util_dias"],
|
|
status=req_status,
|
|
priority=req_priority,
|
|
risk_level=req_risk_level,
|
|
created_at=plan.created_at,
|
|
updated_at=plan.updated_at
|
|
)
|
|
|
|
db.add(requirement)
|
|
total_estimated_cost += estimated_total_cost
|
|
requirements_created += 1
|
|
|
|
# Update plan totals
|
|
plan.total_requirements = num_requirements
|
|
plan.total_estimated_cost = total_estimated_cost
|
|
if status in ["approved", "in_execution", "completed"]:
|
|
plan.total_approved_cost = total_estimated_cost * Decimal(str(random.uniform(0.95, 1.05)))
|
|
|
|
plans_created += 1
|
|
|
|
await db.commit()
|
|
logger.info(f"Successfully created {plans_created} plans with {requirements_created} requirements for {tenant_name}")
|
|
|
|
return {
|
|
"tenant_id": str(tenant_id),
|
|
"plans_created": plans_created,
|
|
"requirements_created": requirements_created,
|
|
"skipped": False
|
|
}
|
|
|
|
|
|
async def seed_all(db: AsyncSession):
|
|
"""Seed all demo tenants with procurement data"""
|
|
logger.info("Starting demo procurement seed process")
|
|
|
|
# Load configuration
|
|
config = load_procurement_config()
|
|
|
|
results = []
|
|
|
|
# Seed San Pablo (Individual Bakery)
|
|
result_san_pablo = await generate_procurement_for_tenant(
|
|
db,
|
|
DEMO_TENANT_SAN_PABLO,
|
|
"San Pablo - Individual Bakery",
|
|
"individual_bakery",
|
|
config
|
|
)
|
|
results.append(result_san_pablo)
|
|
|
|
# Seed La Espiga (Central Bakery)
|
|
result_la_espiga = await generate_procurement_for_tenant(
|
|
db,
|
|
DEMO_TENANT_LA_ESPIGA,
|
|
"La Espiga - Central Bakery",
|
|
"central_bakery",
|
|
config
|
|
)
|
|
results.append(result_la_espiga)
|
|
|
|
total_plans = sum(r["plans_created"] for r in results)
|
|
total_requirements = sum(r["requirements_created"] for r in results)
|
|
|
|
return {
|
|
"results": results,
|
|
"total_plans_created": total_plans,
|
|
"total_requirements_created": total_requirements,
|
|
"status": "completed"
|
|
}
|
|
|
|
|
|
async def main():
|
|
"""Main execution function"""
|
|
# Get database URL from environment
|
|
database_url = os.getenv("ORDERS_DATABASE_URL")
|
|
if not database_url:
|
|
logger.error("ORDERS_DATABASE_URL environment variable must be set")
|
|
return 1
|
|
|
|
# Ensure asyncpg driver
|
|
if database_url.startswith("postgresql://"):
|
|
database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
|
|
|
# Create async engine
|
|
engine = create_async_engine(database_url, echo=False)
|
|
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
try:
|
|
async with async_session() as session:
|
|
result = await seed_all(session)
|
|
|
|
logger.info(
|
|
"Procurement seed completed successfully!",
|
|
total_plans=result["total_plans_created"],
|
|
total_requirements=result["total_requirements_created"],
|
|
status=result["status"]
|
|
)
|
|
|
|
# Print summary
|
|
print("\n" + "="*60)
|
|
print("DEMO PROCUREMENT SEED SUMMARY")
|
|
print("="*60)
|
|
for tenant_result in result["results"]:
|
|
tenant_id = tenant_result["tenant_id"]
|
|
plans = tenant_result["plans_created"]
|
|
requirements = tenant_result["requirements_created"]
|
|
skipped = tenant_result.get("skipped", False)
|
|
status = "SKIPPED (already exists)" if skipped else f"CREATED {plans} plans, {requirements} requirements"
|
|
print(f"Tenant {tenant_id}: {status}")
|
|
print(f"\nTotal Plans: {result['total_plans_created']}")
|
|
print(f"Total Requirements: {result['total_requirements_created']}")
|
|
print("="*60 + "\n")
|
|
|
|
return 0
|
|
|
|
except Exception as e:
|
|
logger.error(f"Procurement seed failed: {str(e)}", exc_info=True)
|
|
return 1
|
|
finally:
|
|
await engine.dispose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
exit_code = asyncio.run(main())
|
|
sys.exit(exit_code)
|