Files
2026-09-27 20:22:13 +03:00

109 lines
3.9 KiB
Python
Raw Permalink 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, 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)
document_number = Column(String)
document_date = Column(Date)
contract = 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])
_TECHNICAL_PREFIXES = (
"BOQ Import - ",
"BOQ Import — ",
"BOQ Import —",
"BOQ Import -",
"Импорт ведомости — ",
"Импорт ведомости - ",
"Импорт ведомости —",
"Импорт ведомости -",
)
@property
def display_name(self) -> str:
name = (self.name or "").strip()
for prefix in self._TECHNICAL_PREFIXES:
if name.startswith(prefix):
name = name[len(prefix):].strip(" —-–")
break
return name or f"Ведомость #{self.id}"
def __repr__(self):
return f"<BoqDocument(id={self.id}, name='{self.name}')>"
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"<System(id={self.id}, name='{self.name}')>"
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"<Section(id={self.id}, name='{self.name}')>"
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"<WorkItem(id={self.id}, name='{self.name}')>"