""" SQLAlchemy Models for Specs Accounting System V1 Consistent domain model according to 001-domain-model-v1.md specification. """ from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime, Numeric, UniqueConstraint, CheckConstraint, ForeignKeyConstraint from sqlalchemy.orm import relationship, validates from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import func from app.models.base import Base, TimestampMixin class Customer(Base, TimestampMixin): """Заказчик проекта.""" __tablename__ = 'customers' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) projects = relationship("Project", secondary="project_customers", back_populates="customers") class LegalEntity(Base, TimestampMixin): """Юридическое лицо проекта.""" __tablename__ = 'legal_entities' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) inn = Column(String(50)) kpp = Column(String(50)) address = Column(Text) customer_id = Column(Integer, ForeignKey('customers.id', ondelete='RESTRICT'), nullable=False) class Project(Base, TimestampMixin): """Корневой объект учета.""" __tablename__ = 'projects' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) customers = relationship("Customer", secondary="project_customers", back_populates="projects") class ProjectCustomer(Base): """Связующая таблица Project ↔ Customer.""" __tablename__ = 'project_customers' project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), primary_key=True) customer_id = Column(Integer, ForeignKey('customers.id', ondelete='CASCADE'), primary_key=True) description = Column(Text) class ProjectLegalEntity(Base): """Связующая таблица Project ↔ LegalEntity.""" __tablename__ = 'project_legal_entities' project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), primary_key=True) legal_entity_id = Column(Integer, ForeignKey('legal_entities.id', ondelete='CASCADE'), primary_key=True) description = Column(Text) class Module(Base, TimestampMixin): """Вид лицензируемого модуля ПО.""" __tablename__ = 'modules' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False, unique=True) description = Column(Text) software_id = Column(Integer, ForeignKey('softwares.id', ondelete='SET NULL'), nullable=True) class System(Base, TimestampMixin): """Основной объект лицензирования.""" __tablename__ = 'systems' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) customer_id = Column(Integer, ForeignKey('customers.id', ondelete='RESTRICT'), nullable=False) project = relationship("Project") customer = relationship("Customer") guardians = relationship("Guardian", back_populates="system") class Guardian(Base, TimestampMixin): """Физический ключ защиты ПО (Hardware key).""" __tablename__ = 'guardians' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) serial_number = Column(String(100)) location = Column(String(255)) system_id = Column(Integer, ForeignKey('systems.id', ondelete='CASCADE'), nullable=False) system = relationship("System", back_populates="guardians") __table_args__ = ( UniqueConstraint('system_id', 'serial_number', name='uq_guardian_system_serial'), ) class SystemModule(Base, TimestampMixin): """Использование модуля в системе через Guardian или Common part.""" __tablename__ = 'system_modules' id = Column(Integer, primary_key=True) system_id = Column(Integer, ForeignKey('systems.id', ondelete='CASCADE'), nullable=False) guardian_id = Column(Integer, ForeignKey('guardians.id', ondelete='CASCADE'), nullable=True) module_id = Column(Integer, ForeignKey('modules.id', ondelete='CASCADE'), nullable=False) quantity = Column(Integer, nullable=False) module = relationship("Module") __table_args__ = ( UniqueConstraint('system_id', 'guardian_id', 'module_id', name='uq_system_module_guardian'), CheckConstraint('quantity > 0', name='check_module_quantity_positive'), ) class Software(Base, TimestampMixin): """Программный продукт.""" __tablename__ = 'softwares' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) class Resource(Base, TimestampMixin): """Приобретенная партия Module.""" __tablename__ = 'resources' id = Column(Integer, primary_key=True) project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) module_id = Column(Integer, ForeignKey('modules.id', ondelete='RESTRICT'), nullable=False) quantity = Column(Integer, nullable=False) invoice_number = Column(String(100)) invoice_date = Column(DateTime) specification_number = Column(String(100)) specification_id = Column(Integer, ForeignKey('specifications.id', ondelete='SET NULL'), nullable=True) invoice_id = Column(Integer, ForeignKey('invoices.id', ondelete='RESTRICT'), nullable=False) description = Column(Text) __table_args__ = ( CheckConstraint('quantity > 0', name='check_resource_quantity_positive'), ) class ResourceAllocation(Base, TimestampMixin): """Распределение ресурса из общего пула в System/Guardian.""" __tablename__ = 'resource_allocations' id = Column(Integer, primary_key=True) resource_id = Column(Integer, ForeignKey('resources.id', ondelete='CASCADE'), nullable=False) system_id = Column(Integer, ForeignKey('systems.id', ondelete='CASCADE'), nullable=False) guardian_id = Column(Integer, ForeignKey('guardians.id', ondelete='SET NULL'), nullable=True) quantity = Column(Integer, nullable=False) allocation_date = Column(DateTime, default=func.now()) description = Column(Text) __table_args__ = ( CheckConstraint('quantity > 0', name='check_allocation_quantity_positive'), ) class Contract(Base, TimestampMixin): """Контракт проекта.""" __tablename__ = 'contracts' id = Column(Integer, primary_key=True) project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) contract_number = Column(String(100), nullable=False) contract_date = Column(DateTime) start_date = Column(DateTime) end_date = Column(DateTime) description = Column(Text) __table_args__ = ( UniqueConstraint('project_id', 'contract_number', name='uq_contract_project_number'), ) class ContractAddendum(Base, TimestampMixin): """Дополнение к контракту.""" __tablename__ = 'contract_addendums' id = Column(Integer, primary_key=True) contract_id = Column(Integer, ForeignKey('contracts.id', ondelete='CASCADE'), nullable=False) addendum_number = Column(String(100), nullable=False) addendum_date = Column(DateTime) description = Column(Text) class Specification(Base, TimestampMixin): """Спецификация (потребность заказчика).""" __tablename__ = 'specifications' id = Column(Integer, primary_key=True) project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) contract_id = Column(Integer, ForeignKey('contracts.id', ondelete='CASCADE'), nullable=False) specification_number = Column(String(100), nullable=False) specification_date = Column(DateTime) description = Column(Text) purchases = relationship("Purchase", secondary="purchase_specifications", back_populates="specifications") __table_args__ = ( UniqueConstraint('contract_id', 'specification_number', name='uq_specification_contract_number'), ) class SpecificationItem(Base, TimestampMixin): """Позиция спецификации.""" __tablename__ = 'specification_items' id = Column(Integer, primary_key=True) specification_id = Column(Integer, ForeignKey('specifications.id', ondelete='CASCADE'), nullable=False) module_id = Column(Integer, ForeignKey('modules.id', ondelete='RESTRICT'), nullable=False) quantity = Column(Integer, nullable=False) description = Column(Text) module = relationship("Module") class Vendor(Base, TimestampMixin): """Поставщик.""" __tablename__ = 'vendors' id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) description = Column(Text) contact_person = Column(String(255)) phone = Column(String(50)) email = Column(String(255)) address = Column(Text) class Purchase(Base, TimestampMixin): """Закупка у поставщика.""" __tablename__ = 'purchases' id = Column(Integer, primary_key=True) project_id = Column(Integer, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) vendor_id = Column(Integer, ForeignKey('vendors.id', ondelete='RESTRICT'), nullable=False) purchase_number = Column(String(100), nullable=False) purchase_date = Column(DateTime) description = Column(Text) specifications = relationship("Specification", secondary="purchase_specifications", back_populates="purchases") class PurchaseSpecification(Base): """Связующая таблица Purchase ↔ Specification (M:N).""" __tablename__ = 'purchase_specifications' purchase_id = Column(Integer, ForeignKey('purchases.id', ondelete='CASCADE'), primary_key=True) specification_id = Column(Integer, ForeignKey('specifications.id', ondelete='CASCADE'), primary_key=True) class PurchaseItem(Base, TimestampMixin): """Позиция закупки.""" __tablename__ = 'purchase_items' id = Column(Integer, primary_key=True) purchase_id = Column(Integer, ForeignKey('purchases.id', ondelete='CASCADE'), nullable=False) module_id = Column(Integer, ForeignKey('modules.id', ondelete='RESTRICT'), nullable=False) quantity = Column(Integer, nullable=False) unit_price = Column(Numeric(12, 2)) description = Column(Text) class Invoice(Base, TimestampMixin): """Счет поставщика.""" __tablename__ = 'invoices' id = Column(Integer, primary_key=True) purchase_id = Column(Integer, ForeignKey('purchases.id', ondelete='CASCADE'), nullable=False) vendor_id = Column(Integer, ForeignKey('vendors.id', ondelete='RESTRICT'), nullable=False) invoice_number = Column(String(100), nullable=False) invoice_date = Column(DateTime) amount = Column(Numeric(12, 2)) description = Column(Text) class Shipment(Base, TimestampMixin): """Фактическая поставка/отгрузка.""" __tablename__ = 'shipments' id = Column(Integer, primary_key=True) purchase_id = Column(Integer, ForeignKey('purchases.id', ondelete='CASCADE'), nullable=False) shipment_number = Column(String(100)) shipment_date = Column(DateTime) tracking_number = Column(String(100)) description = Column(Text) class Document(Base, TimestampMixin): """Логический документ.""" __tablename__ = 'documents' id = Column(Integer, primary_key=True) project_id = Column(Integer, ForeignKey('projects.id', ondelete='SET NULL'), nullable=True) document_type = Column(String(50)) document_number = Column(String(100), nullable=False) document_date = Column(DateTime) title = Column(String(255)) description = Column(Text) class DocumentFile(Base, TimestampMixin): """Файл документа.""" __tablename__ = 'document_files' id = Column(Integer, primary_key=True) document_id = Column(Integer, ForeignKey('documents.id', ondelete='CASCADE'), nullable=False) file_path = Column(String(500), nullable=False) file_name = Column(String(255), nullable=False) description = Column(Text) class DocumentDispatch(Base, TimestampMixin): """Факт отправки документа.""" __tablename__ = 'document_dispatches' id = Column(Integer, primary_key=True) document_id = Column(Integer, ForeignKey('documents.id', ondelete='CASCADE'), nullable=False) dispatch_date = Column(DateTime) recipient = Column(String(255)) package = Column(String(255)) channel = Column(String(50)) status = Column(String(50)) description = Column(Text)