439 lines
13 KiB
Python
439 lines
13 KiB
Python
"""
|
|
Seed data for UI prototype demonstration.
|
|
Creates a complete demo dataset that can be viewed manually.
|
|
"""
|
|
from sqlalchemy.orm import Session
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
|
|
from app.models import (
|
|
Customer, Project, ProjectCustomer, LegalEntity, System, Guardian,
|
|
Module, SystemModule, Contract, Specification, SpecificationItem,
|
|
Purchase, PurchaseSpecification, PurchaseItem, Invoice, Resource,
|
|
ResourceAllocation
|
|
)
|
|
|
|
|
|
def seed_demo_data(db: Session):
|
|
"""
|
|
Create demo dataset for UI prototype.
|
|
|
|
Structure:
|
|
- Project: Test Project
|
|
- Customers: Customer A, Customer B
|
|
- Systems: System 101 (Customer A), System 103 (Customer B)
|
|
- System 103 has 3 Guardians with Core + modules
|
|
- Common part with modules
|
|
- Modules: Core, Agent, Camera, Tracker
|
|
- Resources, Allocations, Specifications, Purchases
|
|
"""
|
|
|
|
# Clear existing data (optional - can remove if needed)
|
|
# db.query(ResourceAllocation).delete()
|
|
# db.query.ResourceAllocation.delete()
|
|
# ... (simpler to just add new data)
|
|
|
|
# === Create Modules ===
|
|
# Check if modules already exist
|
|
existing_modules = db.query(Module).all()
|
|
if not existing_modules:
|
|
core_module = Module(name="Core", description="Core module")
|
|
agent_module = Module(name="Agent", description="Agent module")
|
|
camera_module = Module(name="Camera", description="Camera module")
|
|
tracker_module = Module(name="Tracker", description="Tracker module")
|
|
|
|
db.add_all([core_module, agent_module, camera_module, tracker_module])
|
|
db.commit()
|
|
else:
|
|
print("Modules already exist, skipping...")
|
|
|
|
# === Create Project ===
|
|
project = Project(
|
|
name="Test Project",
|
|
description="Demo project for UI prototype"
|
|
)
|
|
db.add(project)
|
|
db.commit()
|
|
|
|
# === Create Customers ===
|
|
customer_a = Customer(name="Customer A LLC", description="First customer")
|
|
customer_b = Customer(name="Customer B Inc", description="Second customer")
|
|
db.add_all([customer_a, customer_b])
|
|
db.commit()
|
|
|
|
# Link customers to project
|
|
db.add(ProjectCustomer(project_id=project.id, customer_id=customer_a.id))
|
|
db.add(ProjectCustomer(project_id=project.id, customer_id=customer_b.id))
|
|
db.commit()
|
|
|
|
# === Create System 101 (Customer A) ===
|
|
system_101 = System(
|
|
name="System 101",
|
|
description="System for Customer A",
|
|
project_id=project.id,
|
|
customer_id=customer_a.id
|
|
)
|
|
db.add(system_101)
|
|
db.commit()
|
|
|
|
# System 101: Common part
|
|
system_module_101_common = SystemModule(
|
|
system_id=system_101.id,
|
|
guardian_id=None,
|
|
module_id=agent_module.id,
|
|
quantity=5
|
|
)
|
|
db.add(system_module_101_common)
|
|
db.commit()
|
|
|
|
# === Create System 103 (Customer B) ===
|
|
system_103 = System(
|
|
name="System 103",
|
|
description="System for Customer B",
|
|
project_id=project.id,
|
|
customer_id=customer_b.id
|
|
)
|
|
db.add(system_103)
|
|
db.commit()
|
|
|
|
# System 103 Common part
|
|
system_module_103_common_agent = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=None,
|
|
module_id=agent_module.id,
|
|
quantity=10
|
|
)
|
|
system_module_103_common_camera = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=None,
|
|
module_id=camera_module.id,
|
|
quantity=3002
|
|
)
|
|
db.add_all([system_module_103_common_agent, system_module_103_common_camera])
|
|
db.commit()
|
|
|
|
# === Create Guardians for System 103 ===
|
|
guardian_001 = Guardian(
|
|
name="G-001",
|
|
description="Guardian 001",
|
|
system_id=system_103.id,
|
|
serial_number="SER-001"
|
|
)
|
|
guardian_002 = Guardian(
|
|
name="G-002",
|
|
description="Guardian 002",
|
|
system_id=system_103.id,
|
|
serial_number="SER-002"
|
|
)
|
|
guardian_003 = Guardian(
|
|
name="G-003",
|
|
description="Guardian 003",
|
|
system_id=system_103.id,
|
|
serial_number="SER-003"
|
|
)
|
|
db.add_all([guardian_001, guardian_002, guardian_003])
|
|
db.commit()
|
|
|
|
# Guardian 001: Core + modules
|
|
system_module_001_core = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_001.id,
|
|
module_id=core_module.id,
|
|
quantity=1
|
|
)
|
|
system_module_001_agent = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_001.id,
|
|
module_id=agent_module.id,
|
|
quantity=1
|
|
)
|
|
system_module_001_camera = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_001.id,
|
|
module_id=camera_module.id,
|
|
quantity=60
|
|
)
|
|
db.add_all([system_module_001_core, system_module_001_agent, system_module_001_camera])
|
|
db.commit()
|
|
|
|
# Guardian 002: Core + modules
|
|
system_module_002_core = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_002.id,
|
|
module_id=core_module.id,
|
|
quantity=1
|
|
)
|
|
system_module_002_agent = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_002.id,
|
|
module_id=agent_module.id,
|
|
quantity=1
|
|
)
|
|
system_module_002_camera = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_002.id,
|
|
module_id=camera_module.id,
|
|
quantity=60
|
|
)
|
|
db.add_all([system_module_002_core, system_module_002_agent, system_module_002_camera])
|
|
db.commit()
|
|
|
|
# Guardian 003: Core + modules
|
|
system_module_003_core = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_003.id,
|
|
module_id=core_module.id,
|
|
quantity=1
|
|
)
|
|
system_module_003_camera = SystemModule(
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_003.id,
|
|
module_id=camera_module.id,
|
|
quantity=72
|
|
)
|
|
db.add_all([system_module_003_core, system_module_003_camera])
|
|
db.commit()
|
|
|
|
# === Create Contract ===
|
|
contract = Contract(
|
|
project_id=project.id,
|
|
contract_number="CTR-DEMO-001",
|
|
contract_date=date(2026, 1, 15),
|
|
description="Demo contract"
|
|
)
|
|
db.add(contract)
|
|
db.commit()
|
|
|
|
# === Create Specifications ===
|
|
spec_a = Specification(
|
|
project_id=project.id,
|
|
contract_id=contract.id,
|
|
specification_number="SPEC-A",
|
|
specification_date=date(2026, 2, 1),
|
|
description="Specification A"
|
|
)
|
|
spec_b = Specification(
|
|
project_id=project.id,
|
|
contract_id=contract.id,
|
|
specification_number="SPEC-B",
|
|
specification_date=date(2026, 2, 10),
|
|
description="Specification B"
|
|
)
|
|
spec_c = Specification(
|
|
project_id=project.id,
|
|
contract_id=contract.id,
|
|
specification_number="SPEC-C",
|
|
specification_date=date(2026, 2, 15),
|
|
description="Specification C"
|
|
)
|
|
db.add_all([spec_a, spec_b, spec_c])
|
|
db.commit()
|
|
|
|
# Specification items
|
|
spec_a_camera = SpecificationItem(
|
|
specification_id=spec_a.id,
|
|
module_id=camera_module.id,
|
|
quantity=2000,
|
|
description="Camera for Spec A"
|
|
)
|
|
spec_a_agent = SpecificationItem(
|
|
specification_id=spec_a.id,
|
|
module_id=agent_module.id,
|
|
quantity=50,
|
|
description="Agent for Spec A"
|
|
)
|
|
|
|
spec_b_camera = SpecificationItem(
|
|
specification_id=spec_b.id,
|
|
module_id=camera_module.id,
|
|
quantity=1500,
|
|
description="Camera for Spec B"
|
|
)
|
|
|
|
spec_c_agent = SpecificationItem(
|
|
specification_id=spec_c.id,
|
|
module_id=agent_module.id,
|
|
quantity=215,
|
|
description="Agent for Spec C"
|
|
)
|
|
db.add_all([spec_a_camera, spec_a_agent, spec_b_camera, spec_c_agent])
|
|
db.commit()
|
|
|
|
# === Create Purchase ===
|
|
from app.models import Vendor
|
|
vendor = db.query(Vendor).filter(
|
|
Vendor.name == "Demo Vendor"
|
|
).first()
|
|
if not vendor:
|
|
from app.models import Vendor
|
|
vendor = Vendor(
|
|
name="Demo Vendor",
|
|
description="Demo vendor"
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
|
|
purchase = Purchase(
|
|
project_id=project.id,
|
|
vendor_id=vendor.id,
|
|
purchase_number="PO-DEMO-001",
|
|
purchase_date=date(2026, 3, 1),
|
|
description="Demo purchase"
|
|
)
|
|
db.add(purchase)
|
|
db.commit()
|
|
|
|
# Purchase items
|
|
purchase_item_camera = PurchaseItem(
|
|
purchase_id=purchase.id,
|
|
module_id=camera_module.id,
|
|
quantity=3500,
|
|
unit_price=Decimal("100.00"),
|
|
description="Camera purchase"
|
|
)
|
|
purchase_item_agent = PurchaseItem(
|
|
purchase_id=purchase.id,
|
|
module_id=agent_module.id,
|
|
quantity=500,
|
|
unit_price=Decimal("50.00"),
|
|
description="Agent purchase"
|
|
)
|
|
db.add_all([purchase_item_camera, purchase_item_agent])
|
|
db.commit()
|
|
|
|
# Link Purchase to Specifications
|
|
db.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec_a.id))
|
|
db.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec_b.id))
|
|
db.commit()
|
|
|
|
# === Create Invoices ===
|
|
invoice_a = Invoice(
|
|
purchase_id=purchase.id,
|
|
vendor_id=vendor.id,
|
|
invoice_number="INV-A",
|
|
invoice_date=date(2026, 3, 5),
|
|
amount=Decimal("200000.00"),
|
|
description="Invoice A"
|
|
)
|
|
invoice_b = Invoice(
|
|
purchase_id=purchase.id,
|
|
vendor_id=vendor.id,
|
|
invoice_number="INV-B",
|
|
invoice_date=date(2026, 3, 10),
|
|
amount=Decimal("150000.00"),
|
|
description="Invoice B"
|
|
)
|
|
invoice_c = Invoice(
|
|
purchase_id=purchase.id,
|
|
vendor_id=vendor.id,
|
|
invoice_number="INV-C",
|
|
invoice_date=date(2026, 3, 15),
|
|
amount=Decimal("25000.00"),
|
|
description="Invoice C"
|
|
)
|
|
db.add_all([invoice_a, invoice_b, invoice_c])
|
|
db.commit()
|
|
|
|
# === Create Resources ===
|
|
resource_camera_2000 = Resource(
|
|
project_id=project.id,
|
|
module_id=camera_module.id,
|
|
quantity=2000,
|
|
invoice_id=invoice_a.id,
|
|
invoice_number="INV-A",
|
|
invoice_date=date(2026, 3, 5),
|
|
specification_id=spec_a.id,
|
|
specification_number="SPEC-A",
|
|
description="Camera received from Invoice A"
|
|
)
|
|
resource_camera_1500 = Resource(
|
|
project_id=project.id,
|
|
module_id=camera_module.id,
|
|
quantity=1500,
|
|
invoice_id=invoice_b.id,
|
|
invoice_number="INV-B",
|
|
invoice_date=date(2026, 3, 10),
|
|
specification_id=spec_b.id,
|
|
specification_number="SPEC-B",
|
|
description="Camera received from Invoice B"
|
|
)
|
|
resource_agent_215 = Resource(
|
|
project_id=project.id,
|
|
module_id=agent_module.id,
|
|
quantity=215,
|
|
invoice_id=invoice_c.id,
|
|
invoice_number="INV-C",
|
|
invoice_date=date(2026, 3, 15),
|
|
specification_id=spec_c.id,
|
|
specification_number="SPEC-C",
|
|
description="Agent received from Invoice C"
|
|
)
|
|
db.add_all([resource_camera_2000, resource_camera_1500, resource_agent_215])
|
|
db.commit()
|
|
|
|
# === Create ResourceAllocations ===
|
|
# Allocate some resources to System 103 Common
|
|
alloc_common_agent = ResourceAllocation(
|
|
resource_id=resource_agent_215.id,
|
|
system_id=system_103.id,
|
|
guardian_id=None,
|
|
quantity=65,
|
|
description="Allocated to Common"
|
|
)
|
|
|
|
# Allocate to Guardian 001
|
|
alloc_guardian_001 = ResourceAllocation(
|
|
resource_id=resource_camera_2000.id,
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_001.id,
|
|
quantity=300,
|
|
description="Allocated to G-001"
|
|
)
|
|
|
|
# Allocate to Guardian 002
|
|
alloc_guardian_002 = ResourceAllocation(
|
|
resource_id=resource_camera_2000.id,
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_002.id,
|
|
quantity=500,
|
|
description="Allocated to G-002"
|
|
)
|
|
|
|
# Allocate to Guardian 003
|
|
alloc_guardian_003 = ResourceAllocation(
|
|
resource_id=resource_camera_1500.id,
|
|
system_id=system_103.id,
|
|
guardian_id=guardian_003.id,
|
|
quantity=400,
|
|
description="Allocated to G-003"
|
|
)
|
|
|
|
# Allocate some to System 101
|
|
alloc_system_101 = ResourceAllocation(
|
|
resource_id=resource_agent_215.id,
|
|
system_id=system_101.id,
|
|
guardian_id=None,
|
|
quantity=10,
|
|
description="Allocated to System 101 Common"
|
|
)
|
|
|
|
db.add_all([alloc_common_agent, alloc_guardian_001, alloc_guardian_002,
|
|
alloc_guardian_003, alloc_system_101])
|
|
db.commit()
|
|
|
|
print("Demo data seeded successfully!")
|
|
print(f"Project: {project.name} (ID: {project.id})")
|
|
print(f"Systems: 101, 103")
|
|
print(f"Guardians: {guardian_001.name}, {guardian_002.name}, {guardian_003.name}")
|
|
print(f"Resources: {resource_camera_2000.quantity}, {resource_camera_1500.quantity}, {resource_agent_215.quantity}")
|
|
print(f"Allocations: 5 created")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from app.database import SessionLocal
|
|
db = SessionLocal()
|
|
try:
|
|
seed_demo_data(db)
|
|
finally:
|
|
db.close()
|