Files
rentman-clone/backend/app/models/equipment.py
T

73 lines
2.9 KiB
Python

"""Equipment model for the rental inventory catalog."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Equipment(Base):
"""Represents a piece of rental equipment within a tenant account."""
__tablename__ = "equipment"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
account_id: Mapped[str] = mapped_column(
String(36), ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
category: Mapped[str | None] = mapped_column(String(100), nullable=True)
brand: Mapped[str | None] = mapped_column(String(255), nullable=True)
serial_number: Mapped[str | None] = mapped_column(
String(100), nullable=True, unique=True
)
barcode: Mapped[str | None] = mapped_column(String(100), nullable=True, unique=True)
qr_code: Mapped[str | None] = mapped_column(String(500), nullable=True)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default="available"
) # available, rented, maintenance, retired
purchase_price: Mapped[float | None] = mapped_column(Float, nullable=True)
current_value: Mapped[float | None] = mapped_column(Float, nullable=True)
weight_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
dimensions: Mapped[str | None] = mapped_column(String(255), nullable=True)
power_watt: Mapped[float | None] = mapped_column(Float, nullable=True)
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
custom_fields: Mapped[str | None] = mapped_column(
Text, nullable=True
) # JSON string for flexible custom fields
# Location reference
location_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("stock_locations.id", ondelete="SET NULL"), nullable=True
)
# Supplier reference (optional contact)
supplier_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("contacts.id", ondelete="SET NULL"), nullable=True
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
onupdate=func.now(),
nullable=False,
)
# Relationships
account: Mapped["Account"] = relationship("Account", back_populates="equipment")
location: Mapped["StockLocation"] = relationship(
"StockLocation", back_populates="equipment_items"
)
supplier: Mapped["Contact"] = relationship("Contact", foreign_keys=[supplier_id])
def __repr__(self) -> str:
return f"<Equipment {self.name} ({self.status})>"