demo seed change

This commit is contained in:
Urtzi Alfaro
2025-12-13 23:57:54 +01:00
parent f3688dfb04
commit ff830a3415
299 changed files with 20328 additions and 19485 deletions

View File

@@ -1,221 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Customer Seeding Script for Orders Service
Creates customers 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
from datetime import datetime, timezone, timedelta
from pathlib import Path
# 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.customer import Customer
# Add shared path for demo utilities
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from shared.utils.demo_dates import BASE_REFERENCE_DATE
# Configure logging
logger = structlog.get_logger()
# Base demo tenant IDs
DEMO_TENANT_PROFESSIONAL = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6") # Individual bakery
def load_customer_data():
"""Load customer data from JSON file"""
data_file = Path(__file__).parent / "clientes_es.json"
if not data_file.exists():
raise FileNotFoundError(f"Customer data file not found: {data_file}")
with open(data_file, 'r', encoding='utf-8') as f:
return json.load(f)
def calculate_date_from_offset(offset_days: int) -> datetime:
"""Calculate a date based on offset from BASE_REFERENCE_DATE"""
return BASE_REFERENCE_DATE + timedelta(days=offset_days)
async def seed_customers_for_tenant(
db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
customer_list: list
):
"""Seed customers for a specific tenant"""
logger.info(f"Seeding customers for: {tenant_name}", tenant_id=str(tenant_id))
# Check if customers already exist
result = await db.execute(
select(Customer).where(Customer.tenant_id == tenant_id).limit(1)
)
existing = result.scalar_one_or_none()
if existing:
logger.info(f"Customers already exist for {tenant_name}, skipping seed")
return {"tenant_id": str(tenant_id), "customers_created": 0, "skipped": True}
count = 0
for customer_data in customer_list:
# Calculate dates from offsets
first_order_date = None
if "first_order_offset_days" in customer_data:
first_order_date = calculate_date_from_offset(customer_data["first_order_offset_days"])
last_order_date = None
if "last_order_offset_days" in customer_data:
last_order_date = calculate_date_from_offset(customer_data["last_order_offset_days"])
# Use strings directly (model doesn't use enums)
customer_type = customer_data.get("customer_type", "business")
customer_segment = customer_data.get("customer_segment", "regular")
is_active = customer_data.get("status", "active") == "active"
# Create customer (using actual model fields)
# For San Pablo, use original IDs. For La Espiga, generate new UUIDs
if tenant_id == DEMO_TENANT_PROFESSIONAL:
customer_id = uuid.UUID(customer_data["id"])
else:
# Generate deterministic UUID for La Espiga based on original ID
base_uuid = uuid.UUID(customer_data["id"])
# Add a fixed offset to create a unique but deterministic ID
customer_id = uuid.UUID(int=base_uuid.int + 0x10000000000000000000000000000000)
customer = Customer(
id=customer_id,
tenant_id=tenant_id,
customer_code=customer_data["customer_code"],
name=customer_data["name"],
business_name=customer_data.get("business_name"),
customer_type=customer_type,
tax_id=customer_data.get("tax_id"),
email=customer_data.get("email"),
phone=customer_data.get("phone"),
address_line1=customer_data.get("billing_address"),
city=customer_data.get("billing_city"),
state=customer_data.get("billing_state"),
postal_code=customer_data.get("billing_postal_code"),
country=customer_data.get("billing_country", "España"),
is_active=is_active,
preferred_delivery_method=customer_data.get("preferred_delivery_method", "delivery"),
payment_terms=customer_data.get("payment_terms", "immediate"),
credit_limit=customer_data.get("credit_limit"),
discount_percentage=customer_data.get("discount_percentage", 0.0),
customer_segment=customer_segment,
priority_level=customer_data.get("priority_level", "normal"),
special_instructions=customer_data.get("special_instructions"),
total_orders=customer_data.get("total_orders", 0),
total_spent=customer_data.get("total_revenue", 0.0),
average_order_value=customer_data.get("average_order_value", 0.0),
last_order_date=last_order_date,
created_at=BASE_REFERENCE_DATE,
updated_at=BASE_REFERENCE_DATE
)
db.add(customer)
count += 1
logger.debug(f"Created customer: {customer.name}", customer_id=str(customer.id))
await db.commit()
logger.info(f"Successfully created {count} customers for {tenant_name}")
return {
"tenant_id": str(tenant_id),
"customers_created": count,
"skipped": False
}
async def seed_all(db: AsyncSession):
"""Seed all demo tenants with customers"""
logger.info("Starting demo customer seed process")
# Load customer data
data = load_customer_data()
results = []
# Seed Professional Bakery with customer base (merged from San Pablo + La Espiga)
result_professional = await seed_customers_for_tenant(
db,
DEMO_TENANT_PROFESSIONAL,
"Professional Bakery",
data["clientes"]
)
results.append(result_professional)
total_created = sum(r["customers_created"] for r in results)
return {
"results": results,
"total_customers_created": total_created,
"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(
"Customer seed completed successfully!",
total_customers=result["total_customers_created"],
status=result["status"]
)
# Print summary
print("\n" + "="*60)
print("DEMO CUSTOMER SEED SUMMARY")
print("="*60)
for tenant_result in result["results"]:
tenant_id = tenant_result["tenant_id"]
count = tenant_result["customers_created"]
skipped = tenant_result.get("skipped", False)
status = "SKIPPED (already exists)" if skipped else f"CREATED {count} customers"
print(f"Tenant {tenant_id}: {status}")
print(f"\nTotal Customers Created: {result['total_customers_created']}")
print("="*60 + "\n")
return 0
except Exception as e:
logger.error(f"Customer 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)

View File

@@ -1,396 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Retail Customer Seeding Script for Orders Service
Creates walk-in customers for child retail outlets
This script runs as a Kubernetes init job inside the orders-service container.
It populates child retail tenants with realistic customer profiles.
Usage:
python /app/scripts/demo/seed_demo_customers_retail.py
Environment Variables Required:
ORDERS_DATABASE_URL - PostgreSQL connection string for orders database
DEMO_MODE - Set to 'production' for production seeding
LOG_LEVEL - Logging level (default: INFO)
"""
import asyncio
import uuid
import sys
import os
import random
from datetime import datetime, timezone, timedelta
from pathlib import Path
# Add app to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
# Add shared to path for demo utilities
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent.parent))
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
import structlog
from shared.utils.demo_dates import BASE_REFERENCE_DATE
from app.models.customer import Customer
# 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_CHILD_1 = uuid.UUID("d4e5f6a7-b8c9-40d1-e2f3-a4b5c6d7e8f9") # Madrid Centro
DEMO_TENANT_CHILD_2 = uuid.UUID("e5f6a7b8-c9d0-41e2-f3a4-b5c6d7e8f9a0") # Barcelona Gràcia
DEMO_TENANT_CHILD_3 = uuid.UUID("f6a7b8c9-d0e1-42f3-a4b5-c6d7e8f9a0b1") # Valencia Ruzafa
# Spanish first names and surnames for realistic customer generation
FIRST_NAMES = [
"Carmen", "María", "José", "Antonio", "Ana", "Manuel", "Francisca", "David",
"Laura", "Daniel", "Marta", "Carlos", "Isabel", "Javier", "Lucía", "Miguel",
"Sofía", "Francisco", "Elena", "Rafael", "Paula", "Pedro", "Cristina", "Luis",
"Sara", "Fernando", "Raquel", "Alberto", "Beatriz", "Alejandro", "Natalia",
"Pablo", "Silvia", "Jorge", "Mónica", "Sergio", "Andrea", "Rubén", "Virginia",
"Diego", "Pilar", "Iván", "Teresa", "Adrián", "Nuria", "Óscar", "Patricia"
]
SURNAMES = [
"García", "Rodríguez", "González", "Fernández", "López", "Martínez", "Sánchez",
"Pérez", "Gómez", "Martín", "Jiménez", "Ruiz", "Hernández", "Díaz", "Moreno",
"Muñoz", "Álvarez", "Romero", "Alonso", "Gutiérrez", "Navarro", "Torres",
"Domínguez", "Vázquez", "Ramos", "Gil", "Ramírez", "Serrano", "Blanco", "Suárez",
"Molina", "Castro", "Ortega", "Delgado", "Ortiz", "Morales", "Jiménez", "Núñez",
"Medina", "Aguilar"
]
# Customer segment distribution for retail
CUSTOMER_SEGMENTS = [
("regular", 0.60), # 60% regular customers
("loyal", 0.25), # 25% loyal customers
("occasional", 0.15) # 15% occasional customers
]
def generate_spanish_name():
"""Generate a realistic Spanish name"""
first_name = random.choice(FIRST_NAMES)
surname1 = random.choice(SURNAMES)
surname2 = random.choice(SURNAMES)
return f"{first_name} {surname1} {surname2}"
def generate_customer_email(name: str, customer_code: str):
"""Generate a realistic email address"""
# Create email-safe version of name
parts = name.lower().split()
if len(parts) >= 2:
email_name = f"{parts[0]}.{parts[1]}"
else:
email_name = parts[0]
# Remove accents
email_name = email_name.replace('á', 'a').replace('é', 'e').replace('í', 'i')
email_name = email_name.replace('ó', 'o').replace('ú', 'u').replace('ñ', 'n')
domains = ["gmail.com", "hotmail.es", "yahoo.es", "outlook.es", "protonmail.com"]
domain = random.choice(domains)
return f"{email_name}{random.randint(1, 99)}@{domain}"
def generate_spanish_phone():
"""Generate a realistic Spanish mobile phone number"""
# Spanish mobile numbers start with 6 or 7
prefix = random.choice(['6', '7'])
number = ''.join([str(random.randint(0, 9)) for _ in range(8)])
return f"+34 {prefix}{number[0:2]} {number[2:5]} {number[5:8]}"
def select_customer_segment():
"""Select customer segment based on distribution"""
rand = random.random()
cumulative = 0.0
for segment, probability in CUSTOMER_SEGMENTS:
cumulative += probability
if rand <= cumulative:
return segment
return "regular"
async def seed_retail_customers_for_tenant(
db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
num_customers: int,
city: str
) -> dict:
"""
Seed walk-in customers for a retail outlet
Args:
db: Database session
tenant_id: UUID of the child tenant
tenant_name: Name of the tenant (for logging)
num_customers: Number of customers to generate
city: City name for address generation
Returns:
Dict with seeding statistics
"""
logger.info("" * 80)
logger.info(f"Seeding retail customers for: {tenant_name}")
logger.info(f"Tenant ID: {tenant_id}")
logger.info(f"Number of customers: {num_customers}")
logger.info("" * 80)
# Check if customers already exist
result = await db.execute(
select(Customer).where(Customer.tenant_id == tenant_id).limit(1)
)
existing = result.scalar_one_or_none()
if existing:
logger.info(f"Customers already exist for {tenant_name}, skipping seed")
return {"tenant_id": str(tenant_id), "customers_created": 0, "skipped": True}
created_count = 0
for i in range(num_customers):
# Generate customer details
name = generate_spanish_name()
customer_code = f"RET-{str(tenant_id).split('-')[0].upper()[:4]}-{i+1:04d}"
email = generate_customer_email(name, customer_code) if random.random() > 0.2 else None # 80% have email
phone = generate_spanish_phone() if random.random() > 0.1 else None # 90% have phone
# Customer segment determines behavior
segment = select_customer_segment()
# Determine order history based on segment
if segment == "loyal":
total_orders = random.randint(15, 40)
avg_order_value = random.uniform(15.0, 35.0)
days_since_last_order = random.randint(1, 7)
elif segment == "regular":
total_orders = random.randint(5, 15)
avg_order_value = random.uniform(8.0, 20.0)
days_since_last_order = random.randint(3, 14)
else: # occasional
total_orders = random.randint(1, 5)
avg_order_value = random.uniform(5.0, 15.0)
days_since_last_order = random.randint(14, 60)
total_spent = total_orders * avg_order_value
last_order_date = BASE_REFERENCE_DATE - timedelta(days=days_since_last_order)
first_order_date = BASE_REFERENCE_DATE - timedelta(days=random.randint(30, 365))
# Most retail customers are individuals (not businesses)
is_business = random.random() < 0.05 # 5% are small businesses (cafes, hotels, etc.)
if is_business:
business_name = f"{name.split()[0]} {random.choice(['Cafetería', 'Restaurante', 'Hotel', 'Catering'])}"
customer_type = "business"
tax_id = f"B{random.randint(10000000, 99999999)}" # Spanish NIF for businesses
else:
business_name = None
customer_type = "individual"
tax_id = None
# Create customer
customer = Customer(
id=uuid.uuid4(),
tenant_id=tenant_id,
customer_code=customer_code,
name=name,
business_name=business_name,
customer_type=customer_type,
tax_id=tax_id,
email=email,
phone=phone,
address_line1=None, # Walk-in customers don't always provide full address
city=city if random.random() > 0.3 else None, # 70% have city info
state=None,
postal_code=None,
country="España",
is_active=True,
preferred_delivery_method="pickup", # Retail customers typically pick up
payment_terms="immediate", # Retail is always immediate payment
credit_limit=None, # No credit for retail
discount_percentage=5.0 if segment == "loyal" else 0.0, # Loyal customers get 5% discount
customer_segment=segment,
priority_level="normal",
special_instructions=None,
total_orders=total_orders,
total_spent=total_spent,
average_order_value=avg_order_value,
last_order_date=last_order_date,
created_at=first_order_date,
updated_at=BASE_REFERENCE_DATE
)
db.add(customer)
created_count += 1
if created_count % 20 == 0:
logger.debug(f" Created {created_count}/{num_customers} customers...")
# Commit all changes
await db.commit()
logger.info(f" 📊 Customers created: {created_count}")
logger.info("")
return {
"tenant_id": str(tenant_id),
"tenant_name": tenant_name,
"customers_created": created_count,
"skipped": False
}
async def seed_retail_customers(db: AsyncSession):
"""
Seed retail customers for all child tenant templates
Args:
db: Database session
Returns:
Dict with overall seeding statistics
"""
logger.info("=" * 80)
logger.info("👥 Starting Demo Retail Customers Seeding")
logger.info("=" * 80)
logger.info("Creating walk-in customer profiles for retail outlets")
logger.info("")
results = []
# Seed customers for each retail outlet
# Larger stores have more customers
retail_configs = [
(DEMO_TENANT_CHILD_1, "Madrid Centro", 100, "Madrid"), # Large urban store
(DEMO_TENANT_CHILD_2, "Barcelona Gràcia", 75, "Barcelona"), # Medium store
(DEMO_TENANT_CHILD_3, "Valencia Ruzafa", 60, "Valencia") # Smaller boutique store
]
for tenant_id, tenant_name, num_customers, city in retail_configs:
logger.info("")
result = await seed_retail_customers_for_tenant(
db,
tenant_id,
f"{tenant_name} (Retail Outlet)",
num_customers,
city
)
results.append(result)
# Calculate totals
total_customers = sum(r["customers_created"] for r in results)
logger.info("=" * 80)
logger.info("✅ Demo Retail Customers Seeding Completed")
logger.info("=" * 80)
return {
"service": "customers_retail",
"tenants_seeded": len(results),
"total_customers_created": total_customers,
"results": results
}
async def main():
"""Main execution function"""
logger.info("Demo Retail Customers 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("ORDERS_DATABASE_URL") or os.getenv("DATABASE_URL")
if not database_url:
logger.error("❌ ORDERS_DATABASE_URL or DATABASE_URL environment variable must be set")
return 1
# Convert to async URL if needed
if database_url.startswith("postgresql://"):
database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
logger.info("Connecting to orders database")
# Create engine and session
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_retail_customers(session)
logger.info("")
logger.info("📊 Retail Customers Seeding Summary:")
logger.info(f" ✅ Retail outlets seeded: {result['tenants_seeded']}")
logger.info(f" ✅ Total customers created: {result['total_customers_created']}")
logger.info("")
# Print per-tenant details
for tenant_result in result['results']:
if not tenant_result['skipped']:
logger.info(
f" {tenant_result['tenant_name']}: "
f"{tenant_result['customers_created']} customers"
)
logger.info("")
logger.info("🎉 Success! Retail customer base is ready for cloning.")
logger.info("")
logger.info("Customer characteristics:")
logger.info(" ✓ Realistic Spanish names and contact info")
logger.info(" ✓ Segmentation: 60% regular, 25% loyal, 15% occasional")
logger.info(" ✓ 95% individual customers, 5% small businesses")
logger.info(" ✓ Order history and spending patterns")
logger.info(" ✓ Loyal customers receive 5% discount")
logger.info("")
logger.info("Next steps:")
logger.info(" 1. Seed retail orders (internal transfers from parent)")
logger.info(" 2. Seed POS configurations")
logger.info(" 3. Test customer analytics and segmentation")
logger.info("")
return 0
except Exception as e:
logger.error("=" * 80)
logger.error("❌ Demo Retail Customers 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)

View File

@@ -1,386 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Orders Seeding Script for Orders Service
Creates realistic orders with order lines 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
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.order import CustomerOrder, OrderItem
from app.models.customer import Customer
from app.models.enums import OrderStatus, PaymentMethod, PaymentStatus, DeliveryMethod
# Add shared path for demo utilities
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from shared.utils.demo_dates import BASE_REFERENCE_DATE
# Configure logging
logger = structlog.get_logger()
# Base demo tenant IDs
DEMO_TENANT_PROFESSIONAL = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6") # Individual bakery
def load_orders_config():
"""Load orders configuration from JSON file"""
config_file = Path(__file__).parent / "pedidos_config_es.json"
if not config_file.exists():
raise FileNotFoundError(f"Orders config file not found: {config_file}")
with open(config_file, 'r', encoding='utf-8') as f:
return json.load(f)
def load_customers_data():
"""Load customers data from JSON file"""
customers_file = Path(__file__).parent / "clientes_es.json"
if not customers_file.exists():
raise FileNotFoundError(f"Customers file not found: {customers_file}")
with open(customers_file, 'r', encoding='utf-8') as f:
data = json.load(f)
return data.get("clientes", [])
def calculate_date_from_offset(offset_days: int) -> datetime:
"""Calculate a date based on offset from BASE_REFERENCE_DATE"""
return BASE_REFERENCE_DATE + timedelta(days=offset_days)
# Model uses simple strings, no need for enum mapping functions
# (OrderPriority, DeliveryType don't exist in enums.py)
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_order_number(tenant_id: uuid.UUID, index: int) -> str:
"""Generate a unique order number"""
tenant_prefix = "SP" if tenant_id == DEMO_TENANT_PROFESSIONAL else "LE"
return f"ORD-{tenant_prefix}-{BASE_REFERENCE_DATE.year}-{index:04d}"
async def generate_orders_for_tenant(
db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
config: dict,
customers_data: list
):
"""Generate orders for a specific tenant"""
logger.info(f"Generating orders for: {tenant_name}", tenant_id=str(tenant_id))
# Check if orders already exist
result = await db.execute(
select(CustomerOrder).where(CustomerOrder.tenant_id == tenant_id).limit(1)
)
existing = result.scalar_one_or_none()
if existing:
logger.info(f"Orders already exist for {tenant_name}, skipping seed")
return {"tenant_id": str(tenant_id), "orders_created": 0, "order_lines_created": 0, "skipped": True}
# Get customers for this tenant
result = await db.execute(
select(Customer).where(Customer.tenant_id == tenant_id)
)
customers = list(result.scalars().all())
if not customers:
logger.warning(f"No customers found for {tenant_name}, cannot generate orders")
return {"tenant_id": str(tenant_id), "orders_created": 0, "order_lines_created": 0, "error": "no_customers"}
orders_config = config["configuracion_pedidos"]
total_orders = orders_config["total_pedidos_por_tenant"]
orders_created = 0
lines_created = 0
for i in range(total_orders):
# Select random customer
customer = random.choice(customers)
# Determine temporal distribution
rand_temporal = random.random()
cumulative = 0
temporal_category = None
for category, details in orders_config["distribucion_temporal"].items():
cumulative += details["porcentaje"]
if rand_temporal <= cumulative:
temporal_category = details
break
if not temporal_category:
temporal_category = orders_config["distribucion_temporal"]["completados_antiguos"]
# Calculate order date
offset_days = random.randint(
temporal_category["offset_dias_min"],
temporal_category["offset_dias_max"]
)
order_date = calculate_date_from_offset(offset_days)
# Select status based on temporal category (use strings directly)
status = random.choice(temporal_category["estados"])
# Select priority (use strings directly)
priority_rand = random.random()
cumulative_priority = 0
priority = "normal"
for p, weight in orders_config["distribucion_prioridad"].items():
cumulative_priority += weight
if priority_rand <= cumulative_priority:
priority = p
break
# Select payment method (use strings directly)
payment_method_choice = weighted_choice(orders_config["metodos_pago"])
payment_method = payment_method_choice["metodo"]
# Select delivery type (use strings directly)
delivery_type_choice = weighted_choice(orders_config["tipos_entrega"])
delivery_method = delivery_type_choice["tipo"]
# Calculate delivery date (1-7 days after order date typically)
delivery_offset = random.randint(1, 7)
delivery_date = order_date + timedelta(days=delivery_offset)
# Select delivery time
delivery_time = random.choice(orders_config["horarios_entrega"])
# Generate order number
order_number = generate_order_number(tenant_id, i + 1)
# Select notes
notes = random.choice(orders_config["notas_pedido"]) if random.random() < 0.6 else None
# Create order (using only actual model fields)
order = CustomerOrder(
id=uuid.uuid4(),
tenant_id=tenant_id,
order_number=order_number,
customer_id=customer.id,
status=status,
order_type="standard",
priority=priority,
order_date=order_date,
requested_delivery_date=delivery_date,
confirmed_delivery_date=delivery_date if status != "pending" else None,
actual_delivery_date=delivery_date if status in ["delivered", "completed"] else None,
delivery_method=delivery_method,
delivery_address={"address": customer.address_line1, "city": customer.city, "postal_code": customer.postal_code} if customer.address_line1 else None,
payment_method=payment_method,
payment_status="paid" if status in ["delivered", "completed"] else "pending",
payment_terms="immediate",
subtotal=Decimal("0.00"), # Will calculate
discount_percentage=Decimal("0.00"), # Will set
discount_amount=Decimal("0.00"), # Will calculate
tax_amount=Decimal("0.00"), # Will calculate
delivery_fee=Decimal("0.00"),
total_amount=Decimal("0.00"), # Will calculate
special_instructions=notes,
order_source="manual",
sales_channel="direct",
created_at=order_date,
updated_at=order_date
)
db.add(order)
await db.flush() # Get order ID
# Generate order lines
num_lines = random.randint(
orders_config["lineas_por_pedido"]["min"],
orders_config["lineas_por_pedido"]["max"]
)
# Select random products
selected_products = random.sample(
orders_config["productos_demo"],
min(num_lines, len(orders_config["productos_demo"]))
)
subtotal = Decimal("0.00")
for line_num, product in enumerate(selected_products, 1):
quantity = random.randint(
orders_config["cantidad_por_linea"]["min"],
orders_config["cantidad_por_linea"]["max"]
)
# Use base price with some variation
unit_price = Decimal(str(product["precio_base"])) * Decimal(str(random.uniform(0.95, 1.05)))
unit_price = unit_price.quantize(Decimal("0.01"))
line_total = unit_price * quantity
order_line = OrderItem(
id=uuid.uuid4(),
order_id=order.id,
product_id=uuid.uuid4(), # Generate placeholder product ID
product_name=product["nombre"],
product_sku=product["codigo"],
quantity=Decimal(str(quantity)),
unit_of_measure="each",
unit_price=unit_price,
line_discount=Decimal("0.00"),
line_total=line_total,
status="pending"
)
db.add(order_line)
subtotal += line_total
lines_created += 1
# Apply order-level discount
discount_rand = random.random()
if discount_rand < 0.70:
discount_percentage = Decimal("0.00")
elif discount_rand < 0.85:
discount_percentage = Decimal("5.00")
elif discount_rand < 0.95:
discount_percentage = Decimal("10.00")
else:
discount_percentage = Decimal("15.00")
discount_amount = (subtotal * discount_percentage / 100).quantize(Decimal("0.01"))
amount_after_discount = subtotal - discount_amount
tax_amount = (amount_after_discount * Decimal("0.10")).quantize(Decimal("0.01"))
total_amount = amount_after_discount + tax_amount
# Update order totals
order.subtotal = subtotal
order.discount_percentage = discount_percentage
order.discount_amount = discount_amount
order.tax_amount = tax_amount
order.total_amount = total_amount
orders_created += 1
await db.commit()
logger.info(f"Successfully created {orders_created} orders with {lines_created} lines for {tenant_name}")
return {
"tenant_id": str(tenant_id),
"orders_created": orders_created,
"order_lines_created": lines_created,
"skipped": False
}
async def seed_all(db: AsyncSession):
"""Seed all demo tenants with orders"""
logger.info("Starting demo orders seed process")
# Load configuration
config = load_orders_config()
customers_data = load_customers_data()
results = []
# Seed Professional Bakery (merged from San Pablo + La Espiga)
result_professional = await generate_orders_for_tenant(
db,
DEMO_TENANT_PROFESSIONAL,
"Professional Bakery",
config,
customers_data
)
results.append(result_professional)
total_orders = sum(r["orders_created"] for r in results)
total_lines = sum(r["order_lines_created"] for r in results)
return {
"results": results,
"total_orders_created": total_orders,
"total_lines_created": total_lines,
"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(
"Orders seed completed successfully!",
total_orders=result["total_orders_created"],
total_lines=result["total_lines_created"],
status=result["status"]
)
# Print summary
print("\n" + "="*60)
print("DEMO ORDERS SEED SUMMARY")
print("="*60)
for tenant_result in result["results"]:
tenant_id = tenant_result["tenant_id"]
orders = tenant_result["orders_created"]
lines = tenant_result["order_lines_created"]
skipped = tenant_result.get("skipped", False)
status = "SKIPPED (already exists)" if skipped else f"CREATED {orders} orders, {lines} lines"
print(f"Tenant {tenant_id}: {status}")
print(f"\nTotal Orders: {result['total_orders_created']}")
print(f"Total Order Lines: {result['total_lines_created']}")
print("="*60 + "\n")
return 0
except Exception as e:
logger.error(f"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)