feat: cards, attributes and modules

This commit is contained in:
2025-02-19 14:46:31 +04:00
parent a509a3a586
commit 1af78ce08a
61 changed files with 3212 additions and 2795 deletions

View File

@@ -1,115 +1,138 @@
from sqlalchemy import Table, Column, Integer, ForeignKey, ForeignKeyConstraint, UniqueConstraint, String
from typing import TYPE_CHECKING
from sqlalchemy import Table, Column, ForeignKey, ForeignKeyConstraint, UniqueConstraint
from sqlalchemy.orm import relationship, mapped_column, Mapped
from models import Product
from models.base import BaseModel
deal_product_service_employees = Table(
'deal_product_service_employees',
if TYPE_CHECKING:
from models import Card, Service, User
card_product_service_employees = Table(
'card_product_service_employees',
BaseModel.metadata,
Column('deal_id', primary_key=True),
Column('card_id', primary_key=True),
Column('service_id', primary_key=True),
Column('product_id', primary_key=True),
Column('user_id', ForeignKey('users.id'), primary_key=True),
ForeignKeyConstraint(
['deal_id', 'product_id', 'service_id'],
['deal_product_services.deal_id', 'deal_product_services.product_id', 'deal_product_services.service_id']
['card_id', 'product_id', 'service_id'],
['card_product_services.card_id', 'card_product_services.product_id', 'card_product_services.service_id']
)
)
deal_service_employees = Table(
'deal_service_employees',
card_service_employees = Table(
'card_service_employees',
BaseModel.metadata,
Column('deal_id', primary_key=True),
Column('card_id', primary_key=True),
Column('service_id', primary_key=True),
Column('user_id', ForeignKey('users.id'), primary_key=True),
ForeignKeyConstraint(
['deal_id', 'service_id'],
['deal_services.deal_id', 'deal_services.service_id']
['card_id', 'service_id'],
['card_services.card_id', 'card_services.service_id']
)
)
class DealService(BaseModel):
__tablename__ = 'deal_services'
deal_id = Column(Integer, ForeignKey('deals.id'),
nullable=False,
comment='ID Сделки',
primary_key=True)
deal = relationship('Deal', back_populates='services')
class CardService(BaseModel):
__tablename__ = 'card_services'
card_id: Mapped[int] = mapped_column(
ForeignKey('cards.id'),
comment='ID Сделки',
primary_key=True,
)
card: Mapped['Card'] = relationship('Card', back_populates='services')
service_id = Column(Integer, ForeignKey('services.id'), nullable=False, comment='ID Услуги', primary_key=True)
service = relationship('Service')
service_id: Mapped[int] = mapped_column(ForeignKey('services.id'), nullable=False, comment='ID Услуги', primary_key=True)
service: Mapped['Service'] = relationship('Service')
quantity = Column(Integer, nullable=False, comment='Кол-во услуги')
price = Column(Integer, nullable=False, server_default='0', comment='Цена услуги')
quantity: Mapped[int] = mapped_column(nullable=False, comment='Кол-во услуги')
price: Mapped[int] = mapped_column(nullable=False, server_default='0', comment='Цена услуги')
is_fixed_price: Mapped[bool] = mapped_column(default=False, server_default='0', comment='Фиксированная цена')
employees = relationship('User', secondary=deal_service_employees)
employees: Mapped[list['User']] = relationship('User', secondary=card_service_employees)
__table_args__ = (
UniqueConstraint('deal_id', 'service_id', name='uix_deal_service'),
UniqueConstraint('card_id', 'service_id', name='uix_card_service'),
)
class DealProductService(BaseModel):
__tablename__ = 'deal_product_services'
deal_id = Column(Integer, primary_key=True, nullable=False, comment='ID Сделки')
class CardProductService(BaseModel):
__tablename__ = 'card_product_services'
card_id: Mapped[int] = mapped_column(primary_key=True, nullable=False, comment='ID Сделки')
product_id = Column(Integer, primary_key=True, nullable=False, comment='ID Продукта')
product_id: Mapped[int] = mapped_column(primary_key=True, nullable=False, comment='ID Продукта')
service_id = Column(Integer, ForeignKey('services.id'), primary_key=True, nullable=False, comment='ID Услуги')
service_id: Mapped[int] = mapped_column(
ForeignKey('services.id'),
primary_key=True,
nullable=False,
comment='ID Услуги',
)
price = Column(Integer, nullable=False, comment='Цена услуги')
price: Mapped[int] = mapped_column(nullable=False, comment='Цена услуги')
is_fixed_price: Mapped[bool] = mapped_column(default=False, server_default='0', comment='Фиксированная цена')
deal_product = relationship('DealProduct',
back_populates='services',
primaryjoin="and_(DealProductService.deal_id == DealProduct.deal_id, "
"DealProductService.product_id == DealProduct.product_id)",
foreign_keys=[deal_id, product_id])
card_product: Mapped['CardProduct'] = relationship(
'CardProduct',
back_populates='services',
primaryjoin="and_(CardProductService.card_id == CardProduct.card_id, "
"CardProductService.product_id == CardProduct.product_id)",
foreign_keys=[card_id, product_id],
)
service = relationship('Service',
foreign_keys=[service_id],
lazy='joined'
)
employees = relationship('User',
secondary=deal_product_service_employees,
)
service: Mapped['Service'] = relationship(
'Service',
foreign_keys=[service_id],
lazy='joined',
)
employees: Mapped[list['User']] = relationship(
'User',
secondary=card_product_service_employees,
)
__table_args__ = (
ForeignKeyConstraint(
['deal_id', 'product_id'],
['deal_products.deal_id', 'deal_products.product_id']
['card_id', 'product_id'],
['card_products.card_id', 'card_products.product_id']
),
)
class DealProduct(BaseModel):
__tablename__ = 'deal_products'
deal_id = Column(Integer, ForeignKey('deals.id'), primary_key=True, nullable=False, comment='ID Сделки')
product_id = Column(Integer, ForeignKey('products.id'), primary_key=True, nullable=False, comment='ID Продукта')
quantity = Column(Integer, nullable=False, comment='Кол-во продукта')
comment = Column(String, nullable=False, server_default='', comment='Комментарий к товару')
class CardProduct(BaseModel):
__tablename__ = 'card_products'
card_id: Mapped[int] = mapped_column(ForeignKey('cards.id'), primary_key=True, nullable=False, comment='ID карточки')
product_id: Mapped[int] = mapped_column(
ForeignKey('products.id'),
primary_key=True,
nullable=False,
comment='ID Продукта',
)
quantity: Mapped[int] = mapped_column(nullable=False, comment='Кол-во продукта')
comment: Mapped[str] = mapped_column(nullable=False, server_default='', comment='Комментарий к товару')
deal = relationship('Deal',
back_populates='products',
foreign_keys=[deal_id])
product = relationship(
card: Mapped['Card'] = relationship(
'Card',
back_populates='products',
foreign_keys=[card_id],
)
product: Mapped['Product'] = relationship(
'Product',
lazy='joined',
foreign_keys=[product_id],
)
services = relationship('DealProductService',
back_populates='deal_product',
cascade="all, delete-orphan",
primaryjoin="and_(DealProductService.deal_id == DealProduct.deal_id, "
"DealProductService.product_id == DealProduct.product_id)",
foreign_keys=[DealProductService.deal_id, DealProductService.product_id],
lazy='selectin',
order_by="desc(DealProductService.service_id)"
)
services: Mapped[list['CardProductService']] = relationship(
'CardProductService',
back_populates='card_product',
cascade="all, delete-orphan",
primaryjoin="and_(CardProductService.card_id == CardProduct.card_id, "
"CardProductService.product_id == CardProduct.product_id)",
foreign_keys=[CardProductService.card_id, CardProductService.product_id],
lazy='selectin',
order_by="desc(CardProductService.service_id)"
)
barcode_template_attribute_link = Table(