61 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from enum import IntEnum, unique
 | 
						|
 | 
						|
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean
 | 
						|
from sqlalchemy.orm import relationship, backref
 | 
						|
 | 
						|
from models.base import BaseModel
 | 
						|
 | 
						|
 | 
						|
@unique
 | 
						|
class DealStatus(IntEnum):
 | 
						|
    CREATED = 0
 | 
						|
    AWAITING_ACCEPTANCE = 1
 | 
						|
    PACKAGING = 2
 | 
						|
    AWAITING_SHIPMENT = 3
 | 
						|
    AWAITING_PAYMENT = 4
 | 
						|
    COMPLETED = 5
 | 
						|
    CANCELLED = 6
 | 
						|
 | 
						|
 | 
						|
class Deal(BaseModel):
 | 
						|
    __tablename__ = 'deals'
 | 
						|
    id = Column(Integer, autoincrement=True, primary_key=True, index=True)
 | 
						|
    name = Column(String, nullable=False, comment='Название сделки')
 | 
						|
    created_at = Column(DateTime, nullable=False, comment='Дата создания')
 | 
						|
    current_status = Column(Integer, nullable=False, comment='Текущий статус')
 | 
						|
 | 
						|
    client_id = Column(Integer, ForeignKey('clients.id', ondelete='CASCADE'), nullable=False, comment='ID клиента')
 | 
						|
    client = relationship('Client', backref=backref('deals', cascade="all, delete-orphan"))
 | 
						|
 | 
						|
    status_history = relationship('DealStatusHistory', back_populates='deal', cascade="all, delete-orphan")
 | 
						|
 | 
						|
    is_deleted = Column(Boolean, nullable=False, server_default='0', default=False, comment='Удалена')
 | 
						|
    is_completed = Column(Boolean, nullable=False, server_default='0', default=False, comment='Завершена')
 | 
						|
 | 
						|
    services = relationship('DealService', back_populates='deal', cascade="all, delete-orphan")
 | 
						|
    products = relationship('DealProduct', back_populates='deal', cascade="all, delete-orphan")
 | 
						|
 | 
						|
    # TODO remake with sequence
 | 
						|
    lexorank = Column(String, nullable=False, comment='Lexorank', index=True)
 | 
						|
 | 
						|
    comment = Column(String, nullable=False, server_default='', comment='Коментарий к заданию')
 | 
						|
 | 
						|
 | 
						|
class DealStatusHistory(BaseModel):
 | 
						|
    __tablename__ = 'deals_status_history'
 | 
						|
    id = Column(Integer, autoincrement=True, primary_key=True, index=True)
 | 
						|
 | 
						|
    deal_id = Column(Integer, ForeignKey('deals.id'), nullable=False, comment='ID сделки')
 | 
						|
    deal = relationship('Deal', back_populates='status_history')
 | 
						|
 | 
						|
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
 | 
						|
    user = relationship('User')
 | 
						|
 | 
						|
    changed_at = Column(DateTime, nullable=False, comment='Дата и время когда произошла смена статуса')
 | 
						|
    from_status = Column(Integer, nullable=False, comment='Предыдущий статус')
 | 
						|
    to_status = Column(Integer, nullable=False, comment='Новый статус')
 | 
						|
 | 
						|
    next_status_deadline = Column(DateTime,
 | 
						|
                                  comment='Дедлайн до которого сделку нужно перевести на следующий этап')
 | 
						|
    comment = Column(String, nullable=False, comment='Коментарий', server_default='')
 |