from sqlalchemy import Column, Integer, String, Date, DateTime, Text, ForeignKey, Numeric, Boolean from sqlalchemy.sql import func from sqlalchemy.orm import relationship from app.database.database import Base from datetime import datetime from typing import Optional class BoqDocument(Base): __tablename__ = "boq_documents" id = Column(Integer, primary_key=True, index=True) project_id = Column(Integer, ForeignKey("projects.id"), nullable=False) agreement_id = Column(Integer, ForeignKey("agreements.id"), nullable=True, index=True) name = Column(String, nullable=False) version = Column(String) source_file = Column(String) created_at = Column(DateTime, default=func.now()) # Relationship to project project = relationship("Project", back_populates="boq_documents") agreement = relationship("Agreement", back_populates="boq_documents", foreign_keys=[agreement_id]) def __repr__(self): return f"" class System(Base): __tablename__ = "systems" id = Column(Integer, primary_key=True, index=True) project_id = Column(Integer, ForeignKey("projects.id"), nullable=False) code = Column(String, index=True) name = Column(String, nullable=False) sort_order = Column(Integer, default=0) created_at = Column(DateTime, default=func.now()) # Relationship to project project = relationship("Project", back_populates="systems") sections = relationship("Section", back_populates="system") def __repr__(self): return f"" class Section(Base): __tablename__ = "sections" id = Column(Integer, primary_key=True, index=True) system_id = Column(Integer, ForeignKey("systems.id"), nullable=False) name = Column(String, nullable=False) sort_order = Column(Integer, default=0) created_at = Column(DateTime, default=func.now()) # Relationship to system system = relationship("System", back_populates="sections") def __repr__(self): return f"" class WorkItem(Base): __tablename__ = "work_items" id = Column(Integer, primary_key=True, index=True) project_id = Column(Integer, ForeignKey("projects.id"), nullable=False) boq_document_id = Column(Integer, ForeignKey("boq_documents.id"), nullable=True) system_id = Column(Integer, ForeignKey("systems.id")) section_id = Column(Integer, ForeignKey("sections.id")) source_number = Column(String) name = Column(String, nullable=False) model = Column(String) manufacturer = Column(String) unit = Column(String) quantity = Column(Numeric(15, 4)) # Imported workbook values can contain fractional cents while displaying to 2 places. unit_price = Column(Numeric(18, 12)) amount = Column(Numeric(18, 12)) created_at = Column(DateTime, default=func.now()) updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) # Relationships project = relationship("Project") boq_document = relationship("BoqDocument") system = relationship("System") section = relationship("Section") def __repr__(self): return f""