
Features: - JWT authentication with user registration and login - Customer management with full CRUD operations - Invoice management with automatic calculations - Multi-tenant data isolation by user - SQLite database with Alembic migrations - RESTful API with comprehensive documentation - Tax calculations and invoice status tracking - Code formatted with Ruff linting
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
from sqlalchemy import (
|
|
Column,
|
|
Integer,
|
|
String,
|
|
DateTime,
|
|
ForeignKey,
|
|
Numeric,
|
|
Text,
|
|
Enum,
|
|
)
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
from app.db.base import Base
|
|
import enum
|
|
|
|
|
|
class InvoiceStatus(enum.Enum):
|
|
DRAFT = "draft"
|
|
SENT = "sent"
|
|
PAID = "paid"
|
|
OVERDUE = "overdue"
|
|
CANCELLED = "cancelled"
|
|
|
|
|
|
class Invoice(Base):
|
|
__tablename__ = "invoices"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
invoice_number = Column(String, unique=True, nullable=False)
|
|
customer_id = Column(Integer, ForeignKey("customers.id"), nullable=False)
|
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
issue_date = Column(DateTime(timezone=True), server_default=func.now())
|
|
due_date = Column(DateTime(timezone=True), nullable=False)
|
|
status = Column(Enum(InvoiceStatus), default=InvoiceStatus.DRAFT)
|
|
subtotal = Column(Numeric(10, 2), default=0)
|
|
tax_rate = Column(Numeric(5, 2), default=0)
|
|
tax_amount = Column(Numeric(10, 2), default=0)
|
|
total = Column(Numeric(10, 2), default=0)
|
|
notes = Column(Text, nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
customer = relationship("Customer", back_populates="invoices")
|
|
user = relationship("User", back_populates="invoices")
|
|
items = relationship(
|
|
"InvoiceItem", back_populates="invoice", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class InvoiceItem(Base):
|
|
__tablename__ = "invoice_items"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
invoice_id = Column(Integer, ForeignKey("invoices.id"), nullable=False)
|
|
description = Column(String, nullable=False)
|
|
quantity = Column(Numeric(10, 2), nullable=False)
|
|
unit_price = Column(Numeric(10, 2), nullable=False)
|
|
total_price = Column(Numeric(10, 2), nullable=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
invoice = relationship("Invoice", back_populates="items")
|