demo seed change
This commit is contained in:
@@ -98,127 +98,160 @@ async def clone_demo_data(
|
||||
# Customer ID mapping (old -> new)
|
||||
customer_id_map = {}
|
||||
|
||||
# Clone Customers
|
||||
result = await db.execute(
|
||||
select(Customer).where(Customer.tenant_id == base_uuid)
|
||||
)
|
||||
base_customers = result.scalars().all()
|
||||
# Load Customers from seed data
|
||||
try:
|
||||
from shared.utils.seed_data_paths import get_seed_data_path
|
||||
|
||||
if demo_account_type == "professional":
|
||||
json_file = get_seed_data_path("professional", "08-orders.json")
|
||||
elif demo_account_type == "enterprise":
|
||||
json_file = get_seed_data_path("enterprise", "08-orders.json")
|
||||
else:
|
||||
raise ValueError(f"Invalid demo account type: {demo_account_type}")
|
||||
|
||||
except ImportError:
|
||||
# Fallback to original path
|
||||
seed_data_dir = Path(__file__).parent.parent.parent.parent / "infrastructure" / "seed-data"
|
||||
if demo_account_type == "professional":
|
||||
json_file = seed_data_dir / "professional" / "08-orders.json"
|
||||
elif demo_account_type == "enterprise":
|
||||
json_file = seed_data_dir / "enterprise" / "parent" / "08-orders.json"
|
||||
else:
|
||||
raise ValueError(f"Invalid demo account type: {demo_account_type}")
|
||||
|
||||
if not json_file.exists():
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Seed data file not found: {json_file}"
|
||||
)
|
||||
|
||||
# Load JSON data
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
seed_data = json.load(f)
|
||||
|
||||
logger.info(
|
||||
"Found customers to clone",
|
||||
count=len(base_customers),
|
||||
base_tenant=str(base_uuid)
|
||||
"Loaded orders seed data",
|
||||
customers=len(seed_data.get('customers', [])),
|
||||
orders=len(seed_data.get('orders', []))
|
||||
)
|
||||
|
||||
for customer in base_customers:
|
||||
new_customer_id = uuid.uuid4()
|
||||
customer_id_map[customer.id] = new_customer_id
|
||||
# Load Customers from seed data
|
||||
for customer_data in seed_data.get('customers', []):
|
||||
# Transform IDs using XOR
|
||||
from shared.utils.demo_id_transformer import transform_id
|
||||
try:
|
||||
customer_uuid = uuid.UUID(customer_data['id'])
|
||||
transformed_id = transform_id(customer_data['id'], virtual_uuid)
|
||||
except ValueError as e:
|
||||
logger.error("Failed to parse customer UUID",
|
||||
customer_id=customer_data['id'],
|
||||
error=str(e))
|
||||
continue
|
||||
|
||||
customer_id_map[uuid.UUID(customer_data['id'])] = transformed_id
|
||||
|
||||
new_customer = Customer(
|
||||
id=new_customer_id,
|
||||
id=transformed_id,
|
||||
tenant_id=virtual_uuid,
|
||||
customer_code=customer.customer_code,
|
||||
name=customer.name,
|
||||
business_name=customer.business_name,
|
||||
customer_type=customer.customer_type,
|
||||
tax_id=customer.tax_id,
|
||||
email=customer.email,
|
||||
phone=customer.phone,
|
||||
address_line1=customer.address_line1,
|
||||
address_line2=customer.address_line2,
|
||||
city=customer.city,
|
||||
state=customer.state,
|
||||
postal_code=customer.postal_code,
|
||||
country=customer.country,
|
||||
business_license=customer.business_license,
|
||||
is_active=customer.is_active,
|
||||
preferred_delivery_method=customer.preferred_delivery_method,
|
||||
payment_terms=customer.payment_terms,
|
||||
credit_limit=customer.credit_limit,
|
||||
discount_percentage=customer.discount_percentage,
|
||||
customer_segment=customer.customer_segment,
|
||||
priority_level=customer.priority_level,
|
||||
special_instructions=customer.special_instructions,
|
||||
delivery_preferences=customer.delivery_preferences,
|
||||
product_preferences=customer.product_preferences,
|
||||
total_orders=customer.total_orders,
|
||||
total_spent=customer.total_spent,
|
||||
average_order_value=customer.average_order_value,
|
||||
last_order_date=customer.last_order_date,
|
||||
customer_code=customer_data.get('customer_code'),
|
||||
name=customer_data.get('name'),
|
||||
business_name=customer_data.get('business_name'),
|
||||
customer_type=customer_data.get('customer_type'),
|
||||
tax_id=customer_data.get('tax_id'),
|
||||
email=customer_data.get('email'),
|
||||
phone=customer_data.get('phone'),
|
||||
address_line1=customer_data.get('address_line1'),
|
||||
address_line2=customer_data.get('address_line2'),
|
||||
city=customer_data.get('city'),
|
||||
state=customer_data.get('state'),
|
||||
postal_code=customer_data.get('postal_code'),
|
||||
country=customer_data.get('country'),
|
||||
business_license=customer_data.get('business_license'),
|
||||
is_active=customer_data.get('is_active', True),
|
||||
preferred_delivery_method=customer_data.get('preferred_delivery_method'),
|
||||
payment_terms=customer_data.get('payment_terms'),
|
||||
credit_limit=customer_data.get('credit_limit', 0.0),
|
||||
discount_percentage=customer_data.get('discount_percentage', 0.0),
|
||||
customer_segment=customer_data.get('customer_segment'),
|
||||
priority_level=customer_data.get('priority_level'),
|
||||
special_instructions=customer_data.get('special_instructions'),
|
||||
delivery_preferences=customer_data.get('delivery_preferences'),
|
||||
product_preferences=customer_data.get('product_preferences'),
|
||||
total_orders=customer_data.get('total_orders', 0),
|
||||
total_spent=customer_data.get('total_spent', 0.0),
|
||||
average_order_value=customer_data.get('average_order_value', 0.0),
|
||||
last_order_date=adjust_date_for_demo(
|
||||
datetime.fromisoformat(customer_data['last_order_date'].replace('Z', '+00:00')),
|
||||
session_time,
|
||||
BASE_REFERENCE_DATE
|
||||
) if customer_data.get('last_order_date') else None,
|
||||
created_at=session_time,
|
||||
updated_at=session_time
|
||||
)
|
||||
db.add(new_customer)
|
||||
stats["customers"] += 1
|
||||
|
||||
# Clone Customer Orders with Line Items
|
||||
result = await db.execute(
|
||||
select(CustomerOrder).where(CustomerOrder.tenant_id == base_uuid)
|
||||
)
|
||||
base_orders = result.scalars().all()
|
||||
|
||||
logger.info(
|
||||
"Found customer orders to clone",
|
||||
count=len(base_orders),
|
||||
base_tenant=str(base_uuid)
|
||||
)
|
||||
|
||||
# Load Customer Orders from seed data
|
||||
order_id_map = {}
|
||||
|
||||
for order in base_orders:
|
||||
new_order_id = uuid.uuid4()
|
||||
order_id_map[order.id] = new_order_id
|
||||
for order_data in seed_data.get('orders', []):
|
||||
# Transform IDs using XOR
|
||||
from shared.utils.demo_id_transformer import transform_id
|
||||
try:
|
||||
order_uuid = uuid.UUID(order_data['id'])
|
||||
transformed_id = transform_id(order_data['id'], virtual_uuid)
|
||||
except ValueError as e:
|
||||
logger.error("Failed to parse order UUID",
|
||||
order_id=order_data['id'],
|
||||
error=str(e))
|
||||
continue
|
||||
|
||||
order_id_map[uuid.UUID(order_data['id'])] = transformed_id
|
||||
|
||||
# Map customer_id if it exists in our map
|
||||
customer_id_value = order_data.get('customer_id')
|
||||
if customer_id_value:
|
||||
customer_id_value = customer_id_map.get(uuid.UUID(customer_id_value), uuid.UUID(customer_id_value))
|
||||
|
||||
# Adjust dates using demo_dates utility
|
||||
adjusted_order_date = adjust_date_for_demo(
|
||||
order.order_date, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
adjusted_requested_delivery = adjust_date_for_demo(
|
||||
order.requested_delivery_date, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
adjusted_confirmed_delivery = adjust_date_for_demo(
|
||||
order.confirmed_delivery_date, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
adjusted_actual_delivery = adjust_date_for_demo(
|
||||
order.actual_delivery_date, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
adjusted_window_start = adjust_date_for_demo(
|
||||
order.delivery_window_start, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
adjusted_window_end = adjust_date_for_demo(
|
||||
order.delivery_window_end, session_time, BASE_REFERENCE_DATE
|
||||
)
|
||||
datetime.fromisoformat(order_data['order_date'].replace('Z', '+00:00')),
|
||||
session_time,
|
||||
BASE_REFERENCE_DATE
|
||||
) if order_data.get('order_date') else session_time
|
||||
|
||||
adjusted_requested_delivery = adjust_date_for_demo(
|
||||
datetime.fromisoformat(order_data['requested_delivery_date'].replace('Z', '+00:00')),
|
||||
session_time,
|
||||
BASE_REFERENCE_DATE
|
||||
) if order_data.get('requested_delivery_date') else None
|
||||
|
||||
# Create new order from seed data
|
||||
new_order = CustomerOrder(
|
||||
id=new_order_id,
|
||||
id=str(transformed_id),
|
||||
tenant_id=virtual_uuid,
|
||||
order_number=f"ORD-{uuid.uuid4().hex[:8].upper()}", # New order number
|
||||
customer_id=customer_id_map.get(order.customer_id, order.customer_id),
|
||||
status=order.status,
|
||||
order_type=order.order_type,
|
||||
priority=order.priority,
|
||||
order_number=order_data.get('order_number', f"ORD-{uuid.uuid4().hex[:8].upper()}"),
|
||||
customer_id=str(customer_id_value) if customer_id_value else None,
|
||||
status=order_data.get('status', 'pending'),
|
||||
order_type=order_data.get('order_type', 'standard'),
|
||||
priority=order_data.get('priority', 'normal'),
|
||||
order_date=adjusted_order_date,
|
||||
requested_delivery_date=adjusted_requested_delivery,
|
||||
confirmed_delivery_date=adjusted_confirmed_delivery,
|
||||
actual_delivery_date=adjusted_actual_delivery,
|
||||
delivery_method=order.delivery_method,
|
||||
delivery_address=order.delivery_address,
|
||||
delivery_instructions=order.delivery_instructions,
|
||||
delivery_window_start=adjusted_window_start,
|
||||
delivery_window_end=adjusted_window_end,
|
||||
subtotal=order.subtotal,
|
||||
tax_amount=order.tax_amount,
|
||||
discount_amount=order.discount_amount,
|
||||
discount_percentage=order.discount_percentage,
|
||||
delivery_fee=order.delivery_fee,
|
||||
total_amount=order.total_amount,
|
||||
payment_status=order.payment_status,
|
||||
payment_method=order.payment_method,
|
||||
payment_terms=order.payment_terms,
|
||||
payment_due_date=order.payment_due_date,
|
||||
special_instructions=order.special_instructions,
|
||||
order_source=order.order_source,
|
||||
sales_channel=order.sales_channel,
|
||||
delivery_method=order_data.get('delivery_method'),
|
||||
delivery_address=order_data.get('delivery_address'),
|
||||
delivery_instructions=order_data.get('delivery_instructions'),
|
||||
subtotal=order_data.get('subtotal', 0.0),
|
||||
tax_amount=order_data.get('tax_amount', 0.0),
|
||||
discount_amount=order_data.get('discount_amount', 0.0),
|
||||
discount_percentage=order_data.get('discount_percentage', 0.0),
|
||||
delivery_fee=order_data.get('delivery_fee', 0.0),
|
||||
total_amount=order_data.get('total_amount', 0.0),
|
||||
payment_status=order_data.get('payment_status', 'pending'),
|
||||
payment_method=order_data.get('payment_method'),
|
||||
payment_terms=order_data.get('payment_terms'),
|
||||
special_instructions=order_data.get('special_instructions'),
|
||||
order_source=order_data.get('order_source', 'demo'),
|
||||
sales_channel=order_data.get('sales_channel', 'direct'),
|
||||
created_at=session_time,
|
||||
updated_at=session_time
|
||||
)
|
||||
|
||||
@@ -13,7 +13,7 @@ from app.core.database import database_manager
|
||||
from app.api.orders import router as orders_router
|
||||
from app.api.customers import router as customers_router
|
||||
from app.api.order_operations import router as order_operations_router
|
||||
from app.api import internal_demo, audit
|
||||
from app.api import audit, internal_demo
|
||||
from shared.service_base import StandardFastAPIService
|
||||
|
||||
|
||||
@@ -104,8 +104,8 @@ service.add_router(orders_router)
|
||||
# BUSINESS: Complex operations and workflows
|
||||
service.add_router(order_operations_router)
|
||||
|
||||
# INTERNAL: Service-to-service endpoints
|
||||
service.add_router(internal_demo.router)
|
||||
# INTERNAL: Service-to-service endpoints - DEPRECATED: Replaced by script-based seed data loading
|
||||
service.add_router(internal_demo.router, tags=["internal-demo"])
|
||||
|
||||
# REMOVED: test_procurement_scheduler endpoint
|
||||
# Procurement scheduling is now triggered by the Orchestrator Service
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user