Files
xenia-pos-local/local_backend/models/expenses.py
T
bonaminandClaude Opus 5.5 9297cbcd3b feat(backend): expense lines (quantity, price before VAT, VAT %) and documents (PDF / photo)
An expense can list what it bought; the lines make its total (net, VAT half up per line), are
replaced on edit ([] removes them) and the total still can't drop below what was paid. Files kept
with an expense: POST/GET/DELETE /api/expenses/{id}/documents[/{doc}], PDF / JPG / PNG / WebP /
HEIC by their bytes, at most 15 MB, under MEDIA_DIR/expenses/<id>/ (removed with the expense).
Migration 0039.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 14:57:58 +02:00

96 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, Text
from core.timeutil import UTCDateTime, utcnow
from sqlalchemy.orm import relationship
from database import Base
class Contact(Base):
__tablename__ = "contacts"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
type = Column(String, default="supplier", nullable=False) # supplier|staff|utility|other
phone = Column(String, nullable=True)
email = Column(String, nullable=True)
address = Column(Text, nullable=True)
notes = Column(Text, nullable=True)
is_active = Column(Boolean, default=True, nullable=False)
created_at = Column(UTCDateTime, default=utcnow)
expenses = relationship("Expense", back_populates="contact")
class Expense(Base):
__tablename__ = "expenses"
id = Column(Integer, primary_key=True, index=True)
description = Column(String, nullable=False)
category = Column(String, nullable=False) # supplier|utilities|rent|maintenance|staff|equipment|other
contact_id = Column(Integer, ForeignKey("contacts.id"), nullable=True, index=True)
total_amount = Column(Float, nullable=False)
paid_amount = Column(Float, default=0.0, nullable=False)
due_date = Column(UTCDateTime, nullable=True)
business_day_id = Column(Integer, ForeignKey("business_days.id"), nullable=True, index=True)
created_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
created_at = Column(UTCDateTime, default=utcnow)
notes = Column(Text, nullable=True)
contact = relationship("Contact", back_populates="expenses")
created_by = relationship("User", foreign_keys=[created_by_id])
payments = relationship("ExpensePayment", back_populates="expense", cascade="all, delete-orphan")
# what was bought (2026-11-20): when an expense has lines, its total is theirs
lines = relationship("ExpenseLine", back_populates="expense", cascade="all, delete-orphan",
order_by="ExpenseLine.position")
documents = relationship("ExpenseDocument", back_populates="expense", cascade="all, delete-orphan",
order_by="ExpenseDocument.id")
class ExpensePayment(Base):
__tablename__ = "expense_payments"
id = Column(Integer, primary_key=True, index=True)
expense_id = Column(Integer, ForeignKey("expenses.id"), nullable=False, index=True)
amount = Column(Float, nullable=False)
paid_at = Column(UTCDateTime, default=utcnow)
paid_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
notes = Column(Text, nullable=True)
expense = relationship("Expense", back_populates="payments")
paid_by = relationship("User", foreign_keys=[paid_by_id])
class ExpenseLine(Base):
"""A line of an expense (2026-11-20): what was bought, how many, the price of one before VAT
and its VAT rate. Line total = quantity × unit price × (1 + VAT %)."""
__tablename__ = "expense_lines"
id = Column(Integer, primary_key=True, index=True)
expense_id = Column(Integer, ForeignKey("expenses.id"), nullable=False, index=True)
position = Column(Integer, default=0, nullable=False)
description = Column(String, nullable=False)
quantity = Column(Float, default=1.0, nullable=False)
unit_price = Column(Float, nullable=False) # one, before VAT
vat_rate = Column(Float, default=0.0, nullable=False) # %, e.g. 24
expense = relationship("Expense", back_populates="lines")
class ExpenseDocument(Base):
"""A file kept with an expense (2026-11-20): the invoice as a PDF or a photo of the receipt.
Stored under MEDIA_DIR/expenses/<expense id>/ (services/expense_docs.py)."""
__tablename__ = "expense_documents"
id = Column(Integer, primary_key=True, index=True)
expense_id = Column(Integer, ForeignKey("expenses.id"), nullable=False, index=True)
filename = Column(String, nullable=False) # as uploaded
stored_name = Column(String, nullable=False) # on disk
content_type = Column(String, nullable=False)
size = Column(Integer, nullable=False)
uploaded_by_id = Column(Integer, ForeignKey("users.id"), nullable=True)
uploaded_at = Column(UTCDateTime, default=utcnow)
expense = relationship("Expense", back_populates="documents")
uploaded_by = relationship("User", foreign_keys=[uploaded_by_id])