249 lines
9.5 KiB
Python
249 lines
9.5 KiB
Python
"""Project models for event/rental project management."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Integer, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
|
|
|
|
class Project(Base):
|
|
"""Represents a rental/event project within a tenant account."""
|
|
|
|
__tablename__ = "projects"
|
|
|
|
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)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
status: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="draft"
|
|
) # draft, confirmed, in_progress, completed, cancelled
|
|
start_date: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
end_date: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
budget: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
total_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
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
|
|
custom_field_defs: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True
|
|
) # JSON string for custom field definitions
|
|
|
|
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="projects")
|
|
sub_projects: Mapped[list["SubProject"]] = relationship(
|
|
"SubProject", back_populates="project", cascade="all, delete-orphan"
|
|
)
|
|
function_groups: Mapped[list["ProjectFunctionGroup"]] = relationship(
|
|
"ProjectFunctionGroup", back_populates="project", cascade="all, delete-orphan"
|
|
)
|
|
equipment_groups: Mapped[list["ProjectEquipmentGroup"]] = relationship(
|
|
"ProjectEquipmentGroup", back_populates="project", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Project {self.name} ({self.status})>"
|
|
|
|
|
|
class SubProject(Base):
|
|
"""Represents a sub-project with self-referential parent hierarchy."""
|
|
|
|
__tablename__ = "sub_projects"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
project_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
parent_id: Mapped[str | None] = mapped_column(
|
|
String(36), ForeignKey("sub_projects.id", ondelete="CASCADE"), nullable=True
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
project: Mapped["Project"] = relationship("Project", back_populates="sub_projects")
|
|
parent: Mapped[Optional["SubProject"]] = relationship(
|
|
"SubProject", remote_side="SubProject.id", back_populates="children"
|
|
)
|
|
children: Mapped[list["SubProject"]] = relationship(
|
|
"SubProject", back_populates="parent", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<SubProject {self.name}>"
|
|
|
|
|
|
class ProjectFunctionGroup(Base):
|
|
"""Groups project functions under a project."""
|
|
|
|
__tablename__ = "project_function_groups"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
project_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
start_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
end_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
# Relationships
|
|
project: Mapped["Project"] = relationship(
|
|
"Project", back_populates="function_groups"
|
|
)
|
|
functions: Mapped[list["ProjectFunction"]] = relationship(
|
|
"ProjectFunction", back_populates="function_group", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectFunctionGroup {self.name}>"
|
|
|
|
|
|
class ProjectFunction(Base):
|
|
"""Individual function/line-item within a function group."""
|
|
|
|
__tablename__ = "project_functions"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
function_group_id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
ForeignKey("project_function_groups.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
function_type: Mapped[str] = mapped_column(String(50), nullable=False, default='crew')
|
|
quantity: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)
|
|
daily_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
total_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
function_group: Mapped["ProjectFunctionGroup"] = relationship(
|
|
"ProjectFunctionGroup", back_populates="functions"
|
|
)
|
|
crew_assignments: Mapped[list["ProjectCrew"]] = relationship(
|
|
"ProjectCrew", back_populates="project_function", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectFunction {self.name} x{self.quantity}>"
|
|
|
|
|
|
class ProjectEquipmentGroup(Base):
|
|
"""Groups project equipment under a project."""
|
|
|
|
__tablename__ = "project_equipment_groups"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
project_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
start_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
end_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
# Relationships
|
|
project: Mapped["Project"] = relationship("Project", back_populates="equipment_groups")
|
|
items: Mapped[list["ProjectEquipment"]] = relationship(
|
|
"ProjectEquipment", back_populates="group", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectEquipmentGroup {self.name}>"
|
|
|
|
|
|
class ProjectEquipment(Base):
|
|
"""Single equipment item within a project equipment group."""
|
|
|
|
__tablename__ = "project_equipment"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
project_equipment_group_id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
ForeignKey("project_equipment_groups.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
equipment_id: Mapped[str | None] = mapped_column(
|
|
String(36), ForeignKey("equipment.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
quantity: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)
|
|
price: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
discount: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
group: Mapped["ProjectEquipmentGroup"] = relationship(
|
|
"ProjectEquipmentGroup", back_populates="items"
|
|
)
|
|
equipment: Mapped[Optional["Equipment"]] = relationship(
|
|
"Equipment", foreign_keys=[equipment_id]
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectEquipment {self.name} x{self.quantity}>"
|
|
|
|
|
|
class ProjectCrew(Base):
|
|
"""Crew assignment to a project function."""
|
|
|
|
__tablename__ = "project_crew"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
project_function_id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
ForeignKey("project_functions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
crew_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("crew.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
start_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
end_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
# Relationships
|
|
project_function: Mapped["ProjectFunction"] = relationship(
|
|
"ProjectFunction", back_populates="crew_assignments"
|
|
)
|
|
crew_member: Mapped["Crew"] = relationship("Crew")
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectCrew {self.crew_id} -> {self.project_function_id}>"
|