Improve the frontend 3
This commit is contained in:
678
services/procurement/scripts/demo/seed_demo_procurement_plans.py
Normal file
678
services/procurement/scripts/demo/seed_demo_procurement_plans.py
Normal file
@@ -0,0 +1,678 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Demo Procurement Plans Seeding Script for Procurement Service
|
||||
Creates realistic procurement plans for demo template tenants using pre-defined UUIDs
|
||||
|
||||
This script runs as a Kubernetes init job inside the procurement-service container.
|
||||
It populates the template tenants with comprehensive procurement plans.
|
||||
|
||||
Usage:
|
||||
python /app/scripts/demo/seed_demo_procurement_plans.py
|
||||
|
||||
Environment Variables Required:
|
||||
PROCUREMENT_DATABASE_URL - PostgreSQL connection string for procurement database
|
||||
DEMO_MODE - Set to 'production' for production seeding
|
||||
LOG_LEVEL - Logging level (default: INFO)
|
||||
|
||||
Note: No database lookups needed - all IDs are pre-defined in the JSON file
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from datetime import datetime, timezone, timedelta, date
|
||||
from pathlib import Path
|
||||
import random
|
||||
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, text
|
||||
import structlog
|
||||
|
||||
from app.models.procurement_plan import ProcurementPlan, ProcurementRequirement
|
||||
|
||||
# Configure logging
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.stdlib.add_log_level,
|
||||
structlog.processors.TimeStamper(fmt="iso"),
|
||||
structlog.dev.ConsoleRenderer()
|
||||
]
|
||||
)
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
# Fixed Demo Tenant IDs (must match tenant service)
|
||||
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)
|
||||
|
||||
# Hardcoded SKU to Ingredient ID mapping (no database lookups needed!)
|
||||
INGREDIENT_ID_MAP = {
|
||||
"HAR-T55-001": "10000000-0000-0000-0000-000000000001",
|
||||
"HAR-T65-002": "10000000-0000-0000-0000-000000000002",
|
||||
"HAR-FUE-003": "10000000-0000-0000-0000-000000000003",
|
||||
"HAR-INT-004": "10000000-0000-0000-0000-000000000004",
|
||||
"HAR-CEN-005": "10000000-0000-0000-0000-000000000005",
|
||||
"HAR-ESP-006": "10000000-0000-0000-0000-000000000006",
|
||||
"LAC-MAN-001": "10000000-0000-0000-0000-000000000011",
|
||||
"LAC-LEC-002": "10000000-0000-0000-0000-000000000012",
|
||||
"LAC-NAT-003": "10000000-0000-0000-0000-000000000013",
|
||||
"LAC-HUE-004": "10000000-0000-0000-0000-000000000014",
|
||||
"LEV-FRE-001": "10000000-0000-0000-0000-000000000021",
|
||||
"LEV-SEC-002": "10000000-0000-0000-0000-000000000022",
|
||||
"BAS-SAL-001": "10000000-0000-0000-0000-000000000031",
|
||||
"BAS-AZU-002": "10000000-0000-0000-0000-000000000032",
|
||||
"ESP-CHO-001": "10000000-0000-0000-0000-000000000041",
|
||||
"ESP-ALM-002": "10000000-0000-0000-0000-000000000042",
|
||||
"ESP-VAI-004": "10000000-0000-0000-0000-000000000044",
|
||||
"ESP-CRE-005": "10000000-0000-0000-0000-000000000045",
|
||||
}
|
||||
|
||||
# Ingredient costs (for requirement generation)
|
||||
INGREDIENT_COSTS = {
|
||||
"HAR-T55-001": 0.85,
|
||||
"HAR-T65-002": 0.95,
|
||||
"HAR-FUE-003": 1.15,
|
||||
"HAR-INT-004": 1.20,
|
||||
"HAR-CEN-005": 1.30,
|
||||
"HAR-ESP-006": 2.45,
|
||||
"LAC-MAN-001": 6.50,
|
||||
"LAC-LEC-002": 0.95,
|
||||
"LAC-NAT-003": 3.20,
|
||||
"LAC-HUE-004": 0.25,
|
||||
"LEV-FRE-001": 4.80,
|
||||
"LEV-SEC-002": 12.50,
|
||||
"BAS-SAL-001": 0.60,
|
||||
"BAS-AZU-002": 0.90,
|
||||
"ESP-CHO-001": 15.50,
|
||||
"ESP-ALM-002": 8.90,
|
||||
"ESP-VAI-004": 3.50,
|
||||
"ESP-CRE-005": 7.20,
|
||||
}
|
||||
|
||||
|
||||
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 'weight' key"""
|
||||
total_weight = sum(c.get("weight", 1.0) for c in choices)
|
||||
r = random.uniform(0, total_weight)
|
||||
|
||||
cumulative = 0
|
||||
for choice in choices:
|
||||
cumulative += choice.get("weight", 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
|
||||
) -> dict:
|
||||
"""Generate procurement plans and requirements for a specific tenant"""
|
||||
logger.info("─" * 80)
|
||||
logger.info(f"Generating procurement data for: {tenant_name}")
|
||||
logger.info(f"Tenant ID: {tenant_id}")
|
||||
logger.info("─" * 80)
|
||||
|
||||
# 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["procurement_config"]
|
||||
total_plans = proc_config["plans_per_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["temporal_distribution"].items():
|
||||
cumulative += details["percentage"]
|
||||
if rand_temporal <= cumulative:
|
||||
temporal_category = details
|
||||
break
|
||||
|
||||
if not temporal_category:
|
||||
temporal_category = proc_config["temporal_distribution"]["completed"]
|
||||
|
||||
# Calculate plan date
|
||||
offset_days = random.randint(
|
||||
temporal_category["offset_days_min"],
|
||||
temporal_category["offset_days_max"]
|
||||
)
|
||||
plan_date = calculate_date_from_offset(offset_days)
|
||||
|
||||
# Select status
|
||||
status = random.choice(temporal_category["statuses"])
|
||||
|
||||
# Select plan type
|
||||
plan_type_choice = weighted_choice(proc_config["plan_types"])
|
||||
plan_type = plan_type_choice["type"]
|
||||
|
||||
# Select priority
|
||||
priority_rand = random.random()
|
||||
cumulative_priority = 0
|
||||
priority = "normal"
|
||||
for p, weight in proc_config["priorities"].items():
|
||||
cumulative_priority += weight
|
||||
if priority_rand <= cumulative_priority:
|
||||
priority = p
|
||||
break
|
||||
|
||||
# Select procurement strategy
|
||||
strategy_choice = weighted_choice(proc_config["procurement_strategies"])
|
||||
procurement_strategy = strategy_choice["strategy"]
|
||||
|
||||
# Select supply risk level
|
||||
risk_rand = random.random()
|
||||
cumulative_risk = 0
|
||||
supply_risk_level = "low"
|
||||
for risk, weight in proc_config["risk_levels"].items():
|
||||
cumulative_risk += weight
|
||||
if risk_rand <= cumulative_risk:
|
||||
supply_risk_level = risk
|
||||
break
|
||||
|
||||
# Calculate planning horizon
|
||||
planning_horizon = proc_config["planning_horizon_days"][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["safety_stock_percentage"]["min"],
|
||||
proc_config["safety_stock_percentage"]["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["performance_metrics"]
|
||||
fulfillment_rate = Decimal(str(random.uniform(
|
||||
metrics["fulfillment_rate"]["min"],
|
||||
metrics["fulfillment_rate"]["max"]
|
||||
)))
|
||||
on_time_delivery_rate = Decimal(str(random.uniform(
|
||||
metrics["on_time_delivery"]["min"],
|
||||
metrics["on_time_delivery"]["max"]
|
||||
)))
|
||||
cost_accuracy = Decimal(str(random.uniform(
|
||||
metrics["cost_accuracy"]["min"],
|
||||
metrics["cost_accuracy"]["max"]
|
||||
)))
|
||||
quality_score = Decimal(str(random.uniform(
|
||||
metrics["quality_score"]["min"],
|
||||
metrics["quality_score"]["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["requirements_per_plan"]["min"],
|
||||
proc_config["requirements_per_plan"]["max"]
|
||||
)
|
||||
|
||||
# Select random ingredients
|
||||
selected_ingredients = random.sample(
|
||||
list(INGREDIENT_ID_MAP.keys()),
|
||||
min(num_requirements, len(INGREDIENT_ID_MAP))
|
||||
)
|
||||
|
||||
total_estimated_cost = Decimal("0.00")
|
||||
|
||||
for req_num, ingredient_sku in enumerate(selected_ingredients, 1):
|
||||
# Get ingredient ID from hardcoded mapping
|
||||
ingredient_id_str = INGREDIENT_ID_MAP.get(ingredient_sku)
|
||||
if not ingredient_id_str:
|
||||
logger.warning(f" ⚠️ Ingredient SKU not in mapping: {ingredient_sku}")
|
||||
continue
|
||||
|
||||
# Generate tenant-specific ingredient ID
|
||||
base_ingredient_id = uuid.UUID(ingredient_id_str)
|
||||
tenant_int = int(tenant_id.hex, 16)
|
||||
ingredient_id = uuid.UUID(int=tenant_int ^ int(base_ingredient_id.hex, 16))
|
||||
|
||||
# Get quantity range for category
|
||||
category = ingredient_sku.split("-")[0] # HAR, LAC, LEV, BAS, ESP
|
||||
cantidad_range = proc_config["quantity_ranges"].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_COSTS.get(ingredient_sku, 1.0))) * Decimal(str(random.uniform(0.95, 1.05)))
|
||||
estimated_total_cost = estimated_unit_cost * net_requirement
|
||||
|
||||
# Timing
|
||||
lead_time_days = random.randint(1, 5)
|
||||
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=ingredient_id,
|
||||
product_name=f"Ingrediente {ingredient_sku}",
|
||||
product_sku=ingredient_sku,
|
||||
product_category=category,
|
||||
product_type="ingredient",
|
||||
required_quantity=required_quantity,
|
||||
unit_of_measure="kg",
|
||||
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(random.choice([1, 5, 10, 25]))),
|
||||
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=random.choice([30, 60, 90, 180, 365]),
|
||||
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}")
|
||||
logger.info("")
|
||||
|
||||
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("=" * 80)
|
||||
logger.info("🚚 Starting Demo Procurement Plans Seeding")
|
||||
logger.info("=" * 80)
|
||||
|
||||
# Load configuration
|
||||
config = {
|
||||
"procurement_config": {
|
||||
"plans_per_tenant": 8,
|
||||
"requirements_per_plan": {"min": 3, "max": 8},
|
||||
"planning_horizon_days": {
|
||||
"individual_bakery": 30,
|
||||
"central_bakery": 45
|
||||
},
|
||||
"safety_stock_percentage": {"min": 15.0, "max": 25.0},
|
||||
"temporal_distribution": {
|
||||
"completed": {
|
||||
"percentage": 0.3,
|
||||
"offset_days_min": -15,
|
||||
"offset_days_max": -1,
|
||||
"statuses": ["completed"]
|
||||
},
|
||||
"in_execution": {
|
||||
"percentage": 0.2,
|
||||
"offset_days_min": -5,
|
||||
"offset_days_max": 2,
|
||||
"statuses": ["in_execution", "partially_received"]
|
||||
},
|
||||
"approved": {
|
||||
"percentage": 0.2,
|
||||
"offset_days_min": -2,
|
||||
"offset_days_max": 1,
|
||||
"statuses": ["approved"]
|
||||
},
|
||||
"pending_approval": {
|
||||
"percentage": 0.15,
|
||||
"offset_days_min": 0,
|
||||
"offset_days_max": 3,
|
||||
"statuses": ["pending_approval"]
|
||||
},
|
||||
"draft": {
|
||||
"percentage": 0.15,
|
||||
"offset_days_min": 0,
|
||||
"offset_days_max": 5,
|
||||
"statuses": ["draft"]
|
||||
}
|
||||
},
|
||||
"plan_types": [
|
||||
{"type": "regular", "weight": 0.7},
|
||||
{"type": "seasonal", "weight": 0.2},
|
||||
{"type": "emergency", "weight": 0.1}
|
||||
],
|
||||
"priorities": {
|
||||
"normal": 0.7,
|
||||
"high": 0.25,
|
||||
"critical": 0.05
|
||||
},
|
||||
"procurement_strategies": [
|
||||
{"strategy": "just_in_time", "weight": 0.6},
|
||||
{"strategy": "bulk", "weight": 0.3},
|
||||
{"strategy": "mixed", "weight": 0.1}
|
||||
],
|
||||
"risk_levels": {
|
||||
"low": 0.6,
|
||||
"medium": 0.3,
|
||||
"high": 0.08,
|
||||
"critical": 0.02
|
||||
},
|
||||
"quantity_ranges": {
|
||||
"HAR": {"min": 50.0, "max": 500.0}, # Harinas
|
||||
"LAC": {"min": 20.0, "max": 200.0}, # Lácteos
|
||||
"LEV": {"min": 5.0, "max": 50.0}, # Levaduras
|
||||
"BAS": {"min": 10.0, "max": 100.0}, # Básicos
|
||||
"ESP": {"min": 1.0, "max": 20.0} # Especiales
|
||||
},
|
||||
"performance_metrics": {
|
||||
"fulfillment_rate": {"min": 85.0, "max": 98.0},
|
||||
"on_time_delivery": {"min": 80.0, "max": 95.0},
|
||||
"cost_accuracy": {"min": 90.0, "max": 99.0},
|
||||
"quality_score": {"min": 7.0, "max": 9.5}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results = []
|
||||
|
||||
# Seed San Pablo (Individual Bakery)
|
||||
result_san_pablo = await generate_procurement_for_tenant(
|
||||
db,
|
||||
DEMO_TENANT_SAN_PABLO,
|
||||
"Panadería 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,
|
||||
"Panadería 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)
|
||||
|
||||
logger.info("=" * 80)
|
||||
logger.info("✅ Demo Procurement Plans Seeding Completed")
|
||||
logger.info("=" * 80)
|
||||
|
||||
return {
|
||||
"results": results,
|
||||
"total_plans_created": total_plans,
|
||||
"total_requirements_created": total_requirements,
|
||||
"status": "completed"
|
||||
}
|
||||
|
||||
|
||||
async def main():
|
||||
"""Main execution function"""
|
||||
logger.info("Demo Procurement Plans Seeding Script Starting")
|
||||
logger.info("Mode: %s", os.getenv("DEMO_MODE", "development"))
|
||||
logger.info("Log Level: %s", os.getenv("LOG_LEVEL", "INFO"))
|
||||
|
||||
# Get database URL from environment
|
||||
database_url = os.getenv("PROCUREMENT_DATABASE_URL") or os.getenv("DATABASE_URL")
|
||||
if not database_url:
|
||||
logger.error("❌ PROCUREMENT_DATABASE_URL or 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)
|
||||
|
||||
logger.info("Connecting to procurement database")
|
||||
|
||||
# Create async engine
|
||||
engine = create_async_engine(
|
||||
database_url,
|
||||
echo=False,
|
||||
pool_pre_ping=True,
|
||||
pool_size=5,
|
||||
max_overflow=10
|
||||
)
|
||||
|
||||
async_session = sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False
|
||||
)
|
||||
|
||||
try:
|
||||
async with async_session() as session:
|
||||
result = await seed_all(session)
|
||||
|
||||
logger.info("")
|
||||
logger.info("📊 Seeding Summary:")
|
||||
logger.info(f" ✅ Total Plans: {result['total_plans_created']}")
|
||||
logger.info(f" ✅ Total Requirements: {result['total_requirements_created']}")
|
||||
logger.info(f" ✅ Status: {result['status']}")
|
||||
logger.info("")
|
||||
|
||||
# Print per-tenant details
|
||||
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"
|
||||
logger.info(f" Tenant {tenant_id}: {status}")
|
||||
|
||||
logger.info("")
|
||||
logger.info("🎉 Success! Procurement plans are ready for demo sessions.")
|
||||
logger.info("")
|
||||
logger.info("Plans created:")
|
||||
logger.info(" • 8 Regular procurement plans per tenant")
|
||||
logger.info(" • 3-8 Requirements per plan")
|
||||
logger.info(" • Various statuses: draft, pending, approved, in execution, completed")
|
||||
logger.info(" • Different priorities and risk levels")
|
||||
logger.info("")
|
||||
logger.info("Note: All IDs are pre-defined and hardcoded for cross-service consistency")
|
||||
logger.info("")
|
||||
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
logger.error("=" * 80)
|
||||
logger.error("❌ Demo Procurement Plans Seeding Failed")
|
||||
logger.error("=" * 80)
|
||||
logger.error("Error: %s", str(e))
|
||||
logger.error("", exc_info=True)
|
||||
return 1
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
430
services/procurement/scripts/demo/seed_demo_purchase_orders.py
Normal file
430
services/procurement/scripts/demo/seed_demo_purchase_orders.py
Normal file
@@ -0,0 +1,430 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Demo Purchase Orders Seeding Script for Procurement Service
|
||||
Creates realistic PO scenarios in various states for demo purposes
|
||||
|
||||
This script creates:
|
||||
- 3 PENDING_APPROVAL POs (created today, need user action)
|
||||
- 2 APPROVED POs (approved yesterday, in progress)
|
||||
- 1 AUTO_APPROVED PO (small amount, trusted supplier)
|
||||
- 2 COMPLETED POs (delivered last week)
|
||||
- 1 REJECTED PO (quality concerns)
|
||||
- 1 CANCELLED PO (supplier unavailable)
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
import sys
|
||||
import os
|
||||
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.purchase_order import (
|
||||
PurchaseOrder, PurchaseOrderItem, PurchaseOrderStatus
|
||||
)
|
||||
|
||||
# Configure logging
|
||||
logger = structlog.get_logger()
|
||||
|
||||
# Demo tenant IDs (match those from orders service)
|
||||
DEMO_TENANT_IDS = [
|
||||
uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"), # San Pablo
|
||||
uuid.UUID("b2c3d4e5-f6a7-48b9-c0d1-e2f3a4b5c6d7") # La Espiga
|
||||
]
|
||||
|
||||
# System user ID for auto-approvals
|
||||
SYSTEM_USER_ID = uuid.UUID("50000000-0000-0000-0000-000000000004")
|
||||
|
||||
# Hardcoded base supplier IDs (must match those in suppliers seed script)
|
||||
BASE_SUPPLIER_IDS = [
|
||||
uuid.UUID("40000000-0000-0000-0000-000000000001"), # Molinos San José S.L. (high trust)
|
||||
uuid.UUID("40000000-0000-0000-0000-000000000002"), # Lácteos del Valle S.A. (medium trust)
|
||||
uuid.UUID("40000000-0000-0000-0000-000000000005"), # Lesaffre Ibérica (low trust)
|
||||
]
|
||||
|
||||
def get_demo_supplier_ids(tenant_id: uuid.UUID):
|
||||
"""
|
||||
Generate tenant-specific supplier IDs using XOR strategy with hardcoded base IDs.
|
||||
|
||||
This maintains consistency across services without cross-database access.
|
||||
"""
|
||||
# Generate tenant-specific supplier IDs using XOR with tenant ID
|
||||
tenant_int = int(tenant_id.hex, 16)
|
||||
|
||||
class SupplierRef:
|
||||
def __init__(self, supplier_id, supplier_name, trust_level):
|
||||
self.id = supplier_id
|
||||
self.name = supplier_name
|
||||
self.trust_score = trust_level
|
||||
|
||||
suppliers = []
|
||||
trust_scores = [0.92, 0.75, 0.65] # High, medium, low trust
|
||||
supplier_names = [
|
||||
"Molinos San José S.L.",
|
||||
"Lácteos del Valle S.A.",
|
||||
"Lesaffre Ibérica"
|
||||
]
|
||||
|
||||
for i, base_id in enumerate(BASE_SUPPLIER_IDS):
|
||||
base_int = int(base_id.hex, 16)
|
||||
supplier_id = uuid.UUID(int=tenant_int ^ base_int)
|
||||
|
||||
suppliers.append(SupplierRef(
|
||||
supplier_id,
|
||||
supplier_names[i],
|
||||
trust_scores[i] if i < len(trust_scores) else 0.5
|
||||
))
|
||||
|
||||
return suppliers
|
||||
|
||||
|
||||
async def create_purchase_order(
|
||||
db: AsyncSession,
|
||||
tenant_id: uuid.UUID,
|
||||
supplier,
|
||||
status: PurchaseOrderStatus,
|
||||
total_amount: Decimal,
|
||||
created_offset_days: int = 0,
|
||||
auto_approved: bool = False,
|
||||
priority: str = "normal",
|
||||
items_data: list = None
|
||||
) -> PurchaseOrder:
|
||||
"""Create a purchase order with items"""
|
||||
|
||||
created_at = datetime.now(timezone.utc) + timedelta(days=created_offset_days)
|
||||
required_delivery = created_at + timedelta(days=random.randint(3, 7))
|
||||
|
||||
# Generate PO number
|
||||
po_number = f"PO-{datetime.now().year}-{random.randint(100, 999)}"
|
||||
|
||||
# Calculate amounts
|
||||
subtotal = total_amount
|
||||
tax_amount = subtotal * Decimal("0.10") # 10% IVA
|
||||
shipping_cost = Decimal(str(random.uniform(0, 20)))
|
||||
total = subtotal + tax_amount + shipping_cost
|
||||
|
||||
# Create PO
|
||||
po = PurchaseOrder(
|
||||
id=uuid.uuid4(),
|
||||
tenant_id=tenant_id,
|
||||
supplier_id=supplier.id,
|
||||
po_number=po_number,
|
||||
status=status,
|
||||
priority=priority,
|
||||
order_date=created_at,
|
||||
required_delivery_date=required_delivery,
|
||||
subtotal=subtotal,
|
||||
tax_amount=tax_amount,
|
||||
shipping_cost=shipping_cost,
|
||||
discount_amount=Decimal("0.00"),
|
||||
total_amount=total,
|
||||
notes=f"Auto-generated demo PO from procurement plan" if not auto_approved else f"Auto-approved: Amount €{subtotal:.2f} within threshold",
|
||||
created_at=created_at,
|
||||
updated_at=created_at,
|
||||
created_by=SYSTEM_USER_ID,
|
||||
updated_by=SYSTEM_USER_ID
|
||||
)
|
||||
|
||||
# Set approval data if approved
|
||||
if status in [PurchaseOrderStatus.approved, PurchaseOrderStatus.sent_to_supplier,
|
||||
PurchaseOrderStatus.confirmed, PurchaseOrderStatus.completed]:
|
||||
po.approved_at = created_at + timedelta(hours=random.randint(1, 6))
|
||||
po.approved_by = SYSTEM_USER_ID if auto_approved else uuid.uuid4()
|
||||
if auto_approved:
|
||||
po.notes = f"{po.notes}\nAuto-approved by system based on trust score and amount"
|
||||
|
||||
# Set sent/confirmed dates
|
||||
if status in [PurchaseOrderStatus.sent_to_supplier, PurchaseOrderStatus.confirmed,
|
||||
PurchaseOrderStatus.completed]:
|
||||
po.sent_to_supplier_at = po.approved_at + timedelta(hours=2)
|
||||
|
||||
if status in [PurchaseOrderStatus.confirmed, PurchaseOrderStatus.completed]:
|
||||
po.supplier_confirmation_date = po.sent_to_supplier_at + timedelta(hours=random.randint(4, 24))
|
||||
|
||||
db.add(po)
|
||||
await db.flush()
|
||||
|
||||
# Create items
|
||||
if not items_data:
|
||||
items_data = [
|
||||
{"name": "Harina de Trigo T55", "quantity": 100, "unit_price": 0.85, "uom": "kg"},
|
||||
{"name": "Levadura Fresca", "quantity": 5, "unit_price": 4.50, "uom": "kg"},
|
||||
{"name": "Sal Marina", "quantity": 10, "unit_price": 1.20, "uom": "kg"}
|
||||
]
|
||||
|
||||
for idx, item_data in enumerate(items_data, 1):
|
||||
ordered_qty = int(item_data["quantity"])
|
||||
unit_price = Decimal(str(item_data["unit_price"]))
|
||||
line_total = Decimal(str(ordered_qty)) * unit_price
|
||||
|
||||
item = PurchaseOrderItem(
|
||||
id=uuid.uuid4(),
|
||||
purchase_order_id=po.id,
|
||||
tenant_id=tenant_id,
|
||||
inventory_product_id=uuid.uuid4(), # Would link to actual inventory items
|
||||
product_code=f"PROD-{item_data['name'][:3].upper()}",
|
||||
product_name=item_data['name'],
|
||||
ordered_quantity=ordered_qty,
|
||||
received_quantity=ordered_qty if status == PurchaseOrderStatus.completed else 0,
|
||||
remaining_quantity=0 if status == PurchaseOrderStatus.completed else ordered_qty,
|
||||
unit_price=unit_price,
|
||||
line_total=line_total,
|
||||
unit_of_measure=item_data["uom"],
|
||||
item_notes=f"Demo item: {item_data['name']}"
|
||||
)
|
||||
db.add(item)
|
||||
|
||||
logger.info(f"Created PO: {po_number}", po_id=str(po.id), status=status.value, amount=float(total))
|
||||
return po
|
||||
|
||||
|
||||
async def seed_purchase_orders_for_tenant(db: AsyncSession, tenant_id: uuid.UUID):
|
||||
"""Seed purchase orders for a specific tenant"""
|
||||
logger.info("Seeding purchase orders", tenant_id=str(tenant_id))
|
||||
|
||||
# Get demo supplier IDs (suppliers exist in the suppliers service)
|
||||
suppliers = get_demo_supplier_ids(tenant_id)
|
||||
|
||||
# Group suppliers by trust level for easier access
|
||||
high_trust_suppliers = [s for s in suppliers if s.trust_score >= 0.85]
|
||||
medium_trust_suppliers = [s for s in suppliers if 0.6 <= s.trust_score < 0.85]
|
||||
low_trust_suppliers = [s for s in suppliers if s.trust_score < 0.6]
|
||||
|
||||
# Use first supplier of each type if available
|
||||
supplier_high_trust = high_trust_suppliers[0] if high_trust_suppliers else suppliers[0]
|
||||
supplier_medium_trust = medium_trust_suppliers[0] if medium_trust_suppliers else suppliers[1] if len(suppliers) > 1 else suppliers[0]
|
||||
supplier_low_trust = low_trust_suppliers[0] if low_trust_suppliers else suppliers[-1]
|
||||
|
||||
pos_created = []
|
||||
|
||||
# 1. PENDING_APPROVAL - Critical/Urgent (created today)
|
||||
po1 = await create_purchase_order(
|
||||
db, tenant_id, supplier_medium_trust,
|
||||
PurchaseOrderStatus.pending_approval,
|
||||
Decimal("1234.56"),
|
||||
created_offset_days=0,
|
||||
priority="high",
|
||||
items_data=[
|
||||
{"name": "Harina Integral Ecológica", "quantity": 150, "unit_price": 1.20, "uom": "kg"},
|
||||
{"name": "Semillas de Girasol", "quantity": 20, "unit_price": 3.50, "uom": "kg"},
|
||||
{"name": "Miel de Azahar", "quantity": 10, "unit_price": 8.90, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po1)
|
||||
|
||||
# 2. PENDING_APPROVAL - Medium amount, new supplier (created today)
|
||||
po2 = await create_purchase_order(
|
||||
db, tenant_id, supplier_low_trust,
|
||||
PurchaseOrderStatus.pending_approval,
|
||||
Decimal("789.00"),
|
||||
created_offset_days=0,
|
||||
items_data=[
|
||||
{"name": "Aceite de Oliva Virgen", "quantity": 30, "unit_price": 8.50, "uom": "l"},
|
||||
{"name": "Azúcar Moreno", "quantity": 50, "unit_price": 1.80, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po2)
|
||||
|
||||
# 3. PENDING_APPROVAL - Large amount (created yesterday)
|
||||
po3 = await create_purchase_order(
|
||||
db, tenant_id, supplier_medium_trust,
|
||||
PurchaseOrderStatus.pending_approval,
|
||||
Decimal("250.00"),
|
||||
created_offset_days=-1,
|
||||
priority="normal",
|
||||
items_data=[
|
||||
{"name": "Harina de Fuerza T65", "quantity": 500, "unit_price": 0.95, "uom": "kg"},
|
||||
{"name": "Mantequilla Premium", "quantity": 80, "unit_price": 5.20, "uom": "kg"},
|
||||
{"name": "Huevos Categoría A", "quantity": 600, "unit_price": 0.22, "uom": "unidad"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po3)
|
||||
|
||||
# 4. APPROVED (auto-approved, small amount, trusted supplier)
|
||||
po4 = await create_purchase_order(
|
||||
db, tenant_id, supplier_high_trust,
|
||||
PurchaseOrderStatus.approved,
|
||||
Decimal("234.50"),
|
||||
created_offset_days=0,
|
||||
auto_approved=True,
|
||||
items_data=[
|
||||
{"name": "Levadura Seca", "quantity": 5, "unit_price": 6.90, "uom": "kg"},
|
||||
{"name": "Sal Fina", "quantity": 25, "unit_price": 0.85, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po4)
|
||||
|
||||
# 5. APPROVED (manually approved yesterday)
|
||||
po5 = await create_purchase_order(
|
||||
db, tenant_id, supplier_high_trust,
|
||||
PurchaseOrderStatus.approved,
|
||||
Decimal("456.78"),
|
||||
created_offset_days=-1,
|
||||
items_data=[
|
||||
{"name": "Bolsas de Papel Kraft", "quantity": 1000, "unit_price": 0.12, "uom": "unidad"},
|
||||
{"name": "Cajas de Cartón Grande", "quantity": 200, "unit_price": 0.45, "uom": "unidad"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po5)
|
||||
|
||||
# 6. COMPLETED (delivered last week)
|
||||
po6 = await create_purchase_order(
|
||||
db, tenant_id, supplier_high_trust,
|
||||
PurchaseOrderStatus.completed,
|
||||
Decimal("1567.80"),
|
||||
created_offset_days=-7,
|
||||
items_data=[
|
||||
{"name": "Harina T55 Premium", "quantity": 300, "unit_price": 0.90, "uom": "kg"},
|
||||
{"name": "Chocolate Negro 70%", "quantity": 40, "unit_price": 7.80, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po6)
|
||||
|
||||
# 7. COMPLETED (delivered 5 days ago)
|
||||
po7 = await create_purchase_order(
|
||||
db, tenant_id, supplier_medium_trust,
|
||||
PurchaseOrderStatus.completed,
|
||||
Decimal("890.45"),
|
||||
created_offset_days=-5,
|
||||
items_data=[
|
||||
{"name": "Nueces Peladas", "quantity": 20, "unit_price": 12.50, "uom": "kg"},
|
||||
{"name": "Pasas Sultanas", "quantity": 15, "unit_price": 4.30, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
pos_created.append(po7)
|
||||
|
||||
# 8. CANCELLED (supplier unavailable)
|
||||
po8 = await create_purchase_order(
|
||||
db, tenant_id, supplier_low_trust,
|
||||
PurchaseOrderStatus.cancelled,
|
||||
Decimal("345.00"),
|
||||
created_offset_days=-3,
|
||||
items_data=[
|
||||
{"name": "Avellanas Tostadas", "quantity": 25, "unit_price": 11.80, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
po8.rejection_reason = "Supplier unable to deliver - stock unavailable"
|
||||
po8.notes = "Cancelled: Supplier stock unavailable at required delivery date"
|
||||
pos_created.append(po8)
|
||||
|
||||
# 9. DISPUTED (quality issues)
|
||||
po9 = await create_purchase_order(
|
||||
db, tenant_id, supplier_medium_trust,
|
||||
PurchaseOrderStatus.disputed,
|
||||
Decimal("678.90"),
|
||||
created_offset_days=-4,
|
||||
priority="high",
|
||||
items_data=[
|
||||
{"name": "Cacao en Polvo", "quantity": 30, "unit_price": 18.50, "uom": "kg"},
|
||||
{"name": "Vainilla en Rama", "quantity": 2, "unit_price": 45.20, "uom": "kg"}
|
||||
]
|
||||
)
|
||||
po9.rejection_reason = "Quality below specifications - requesting replacement"
|
||||
po9.notes = "DISPUTED: Quality issue reported - batch rejected, requesting replacement or refund"
|
||||
pos_created.append(po9)
|
||||
|
||||
await db.commit()
|
||||
|
||||
logger.info(
|
||||
f"Successfully created {len(pos_created)} purchase orders for tenant",
|
||||
tenant_id=str(tenant_id),
|
||||
pending_approval=3,
|
||||
approved=2,
|
||||
completed=2,
|
||||
cancelled=1,
|
||||
disputed=1
|
||||
)
|
||||
|
||||
return pos_created
|
||||
|
||||
|
||||
async def seed_all(db: AsyncSession):
|
||||
"""Seed all demo tenants with purchase orders"""
|
||||
logger.info("Starting demo purchase orders seed process")
|
||||
|
||||
all_pos = []
|
||||
|
||||
for tenant_id in DEMO_TENANT_IDS:
|
||||
# Check if POs already exist
|
||||
result = await db.execute(
|
||||
select(PurchaseOrder).where(PurchaseOrder.tenant_id == tenant_id).limit(1)
|
||||
)
|
||||
existing = result.scalar_one_or_none()
|
||||
|
||||
if existing:
|
||||
logger.info(f"Purchase orders already exist for tenant {tenant_id}, skipping")
|
||||
continue
|
||||
|
||||
pos = await seed_purchase_orders_for_tenant(db, tenant_id)
|
||||
all_pos.extend(pos)
|
||||
|
||||
return {
|
||||
"total_pos_created": len(all_pos),
|
||||
"tenants_seeded": len(DEMO_TENANT_IDS),
|
||||
"status": "completed"
|
||||
}
|
||||
|
||||
|
||||
async def main():
|
||||
"""Main execution function"""
|
||||
# Get database URL from environment
|
||||
database_url = os.getenv("PROCUREMENT_DATABASE_URL")
|
||||
if not database_url:
|
||||
logger.error("PROCUREMENT_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(
|
||||
"Purchase orders seed completed successfully!",
|
||||
total_pos=result["total_pos_created"],
|
||||
tenants=result["tenants_seeded"]
|
||||
)
|
||||
|
||||
# Print summary
|
||||
print("\n" + "="*60)
|
||||
print("DEMO PURCHASE ORDERS SEED SUMMARY")
|
||||
print("="*60)
|
||||
print(f"Total POs Created: {result['total_pos_created']}")
|
||||
print(f"Tenants Seeded: {result['tenants_seeded']}")
|
||||
print("\nPO Distribution:")
|
||||
print(" - 3 PENDING_APPROVAL (need user action)")
|
||||
print(" - 2 APPROVED (in progress)")
|
||||
print(" - 2 COMPLETED (delivered)")
|
||||
print(" - 1 CANCELLED (supplier issue)")
|
||||
print(" - 1 DISPUTED (quality issue)")
|
||||
print("="*60 + "\n")
|
||||
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Purchase orders 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)
|
||||
Reference in New Issue
Block a user