190 lines
5.3 KiB
Python
Executable File
190 lines
5.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Example script demonstrating the use of the business models.
|
|
"""
|
|
import datetime
|
|
from typing import List
|
|
|
|
from sqlmodel import Session, SQLModel, create_engine, select
|
|
|
|
from models import (
|
|
Currency,
|
|
Customer,
|
|
Product,
|
|
ProductComponent,
|
|
ProductStatus,
|
|
ProductType,
|
|
Sale,
|
|
SaleItem,
|
|
SaleStatus,
|
|
)
|
|
|
|
|
|
def create_tables(engine):
|
|
"""Create all tables in the database"""
|
|
SQLModel.metadata.create_all(engine)
|
|
|
|
|
|
def create_sample_data(session: Session) -> None:
|
|
"""Create sample data for demonstration"""
|
|
# Create currencies
|
|
usd = Currency(currency_code="USD", amount=0.0)
|
|
eur = Currency(currency_code="EUR", amount=0.0)
|
|
session.add(usd)
|
|
session.add(eur)
|
|
session.commit()
|
|
|
|
# Create a customer
|
|
customer = Customer.new(
|
|
name="Acme Corporation",
|
|
description="A fictional company",
|
|
pubkey="acme123456",
|
|
contact_sids=["circle1_contact123", "circle2_contact456"]
|
|
)
|
|
session.add(customer)
|
|
session.commit()
|
|
|
|
# Create product components
|
|
cpu_component = ProductComponent.new(
|
|
name="CPU",
|
|
description="Central Processing Unit",
|
|
quantity=1,
|
|
)
|
|
ram_component = ProductComponent.new(
|
|
name="RAM",
|
|
description="Random Access Memory",
|
|
quantity=2,
|
|
)
|
|
session.add(cpu_component)
|
|
session.add(ram_component)
|
|
session.commit()
|
|
|
|
# Create products
|
|
laptop_price = Currency(currency_code="USD", amount=1200.0)
|
|
session.add(laptop_price)
|
|
session.commit()
|
|
|
|
laptop = Product.new(
|
|
name="Laptop",
|
|
description="High-performance laptop",
|
|
price=laptop_price,
|
|
type_=ProductType.PRODUCT,
|
|
category="Electronics",
|
|
status=ProductStatus.AVAILABLE,
|
|
max_amount=100,
|
|
validity_days=365,
|
|
istemplate=False,
|
|
)
|
|
laptop.add_component(cpu_component)
|
|
laptop.add_component(ram_component)
|
|
session.add(laptop)
|
|
session.commit()
|
|
|
|
support_price = Currency(currency_code="USD", amount=50.0)
|
|
session.add(support_price)
|
|
session.commit()
|
|
|
|
support = Product.new(
|
|
name="Technical Support",
|
|
description="24/7 technical support",
|
|
price=support_price,
|
|
type_=ProductType.SERVICE,
|
|
category="Support",
|
|
status=ProductStatus.AVAILABLE,
|
|
max_amount=1000,
|
|
validity_days=30,
|
|
istemplate=True, # This is a template product
|
|
)
|
|
session.add(support)
|
|
session.commit()
|
|
|
|
# Create a sale
|
|
sale = Sale.new(
|
|
customer=customer,
|
|
currency_code="USD",
|
|
)
|
|
session.add(sale)
|
|
session.commit()
|
|
|
|
# Create sale items
|
|
laptop_unit_price = Currency(currency_code="USD", amount=1200.0)
|
|
session.add(laptop_unit_price)
|
|
session.commit()
|
|
|
|
laptop_item = SaleItem.new(
|
|
product=laptop,
|
|
quantity=1,
|
|
unit_price=laptop_unit_price,
|
|
active_till=datetime.datetime.utcnow() + datetime.timedelta(days=365),
|
|
)
|
|
sale.add_item(laptop_item)
|
|
|
|
support_unit_price = Currency(currency_code="USD", amount=50.0)
|
|
session.add(support_unit_price)
|
|
session.commit()
|
|
|
|
support_item = SaleItem.new(
|
|
product=support,
|
|
quantity=2,
|
|
unit_price=support_unit_price,
|
|
active_till=datetime.datetime.utcnow() + datetime.timedelta(days=30),
|
|
)
|
|
sale.add_item(support_item)
|
|
|
|
# Complete the sale
|
|
sale.update_status(SaleStatus.COMPLETED)
|
|
session.commit()
|
|
|
|
|
|
def query_data(session: Session) -> None:
|
|
"""Query and display data from the database"""
|
|
print("\n=== Customers ===")
|
|
customers = session.exec(select(Customer)).all()
|
|
for customer in customers:
|
|
print(f"Customer: {customer.name} ({customer.pubkey})")
|
|
print(f" Description: {customer.description}")
|
|
print(f" Contact SIDs: {', '.join(customer.contact_sids)}")
|
|
print(f" Created at: {customer.created_at}")
|
|
|
|
print("\n=== Products ===")
|
|
products = session.exec(select(Product)).all()
|
|
for product in products:
|
|
print(f"Product: {product.name} ({product.type_.value})")
|
|
print(f" Description: {product.description}")
|
|
print(f" Price: {product.price.amount} {product.price.currency_code}")
|
|
print(f" Status: {product.status.value}")
|
|
print(f" Is Template: {product.istemplate}")
|
|
print(f" Components:")
|
|
for component in product.components:
|
|
print(f" - {component.name}: {component.quantity}")
|
|
|
|
print("\n=== Sales ===")
|
|
sales = session.exec(select(Sale)).all()
|
|
for sale in sales:
|
|
print(f"Sale to: {sale.customer.name}")
|
|
print(f" Status: {sale.status.value}")
|
|
print(f" Total: {sale.total_amount.amount} {sale.total_amount.currency_code}")
|
|
print(f" Items:")
|
|
for item in sale.items:
|
|
print(f" - {item.name}: {item.quantity} x {item.unit_price.amount} = {item.subtotal.amount} {item.subtotal.currency_code}")
|
|
|
|
|
|
def main():
|
|
"""Main function"""
|
|
print("Creating in-memory SQLite database...")
|
|
engine = create_engine("sqlite:///business.db", echo=False)
|
|
|
|
print("Creating tables...")
|
|
create_tables(engine)
|
|
|
|
print("Creating sample data...")
|
|
with Session(engine) as session:
|
|
create_sample_data(session)
|
|
|
|
print("Querying data...")
|
|
with Session(engine) as session:
|
|
query_data(session)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |