1
from __future__ import annotations
3
from datetime import datetime
4
from typing import Optional, List
6
from fastapi_filter.contrib.sqlalchemy import Filter
7
from pydantic import BaseModel, Field, computed_field
8
from pydantic.types import UUID4
10
from app.inventory.order.enums.order_enum import OrderStatus
11
from app.inventory.order.models import Order
12
from app.inventory.order.schemas.move_schemas import MoveScheme, MoveCreateScheme, MoveUpdateScheme
13
from app.inventory.order.schemas.order_type_schemas import OrderTypeScheme
14
from core.schemas import BaseFilter
15
from core.schemas.filter_generic import CustomBaseModel
16
from core.schemas.list_schema import GenericListSchema
17
from core.schemas.timestamps import TimeStampScheme
18
from core.types import UUID
21
class OrderBaseScheme(BaseModel):
22
external_number: Optional[str] = Field(default=None, title='External ID', table=True, form=True)
23
order_type_id: UUID = Field(title='Order type', model='order_type')
24
store_id: UUID = Field(title='Store', table=True, form=True, model='store')
25
partner_id: Optional[UUID] = Field(default=None, title='Partner', table=True, form=True, model='partner', readonly=True)
26
lot_id: Optional[UUID] = Field(default=None, title='Lot', model='lot')
27
origin_type: Optional[str] = Field(default=None, title='Original Type', form=True)
28
origin_number: Optional[str] = Field(default=None, title='Original', table=True, form=True)
29
planned_datetime: Optional[datetime] = Field(default=None, title='Planned Date', table=True, form=True)
30
expiration_datetime: Optional[datetime] = Field(default=None, title='Expiration Date', table=True, form=True)
31
description: Optional[str] = Field(default=None, title='Description', table=True, form=True)
32
status: OrderStatus = Field(title='Status', table=True, form=True)
33
order_id: Optional[UUID] = Field(default=None, title='Parent', form=True)
38
from_attributes = True
42
class OrderUpdateScheme(OrderBaseScheme):
43
move_list_rel: Optional[list[MoveUpdateScheme]] = Field(default=[], title='Order Movements', form=True)
46
class OrderCreateScheme(OrderBaseScheme):
47
move_list_rel: Optional[list[MoveCreateScheme]] = Field(default=[], title='Order Movements', form=True)
50
class OrderScheme(OrderCreateScheme, TimeStampScheme, CustomBaseModel):
53
company_id: UUID = Field(title='Company', model='company')
54
vars: Optional[dict] = None
55
number: str = Field(title='Order #', table=True, form=True, description="Internal number of order")
56
actual_datetime: Optional[datetime] = Field(title='Actual Date', table=True, form=True)
57
created_by: UUID = Field(title='Created By', table=True, model='user')
58
edited_by: UUID = Field(title='Edit By', model='user')
59
user_ids: Optional[list[UUID]] = Field(default=[], title='Users', form=True, model='user')
60
order_type_rel: OrderTypeScheme = Field(title='Order Type', table=True, form=True)
61
move_list_rel: Optional[list[MoveScheme]] = Field(default=[], title='Order Movements', form=True)
63
def title(self) -> str:
64
return f'{self.order_type_rel.title}: [{self.number}]'
73
class OrderFilter(BaseFilter):
74
planned_datetime__gte: Optional[datetime] = Field(title="bigger or equal planned date", default=None)
75
planned_datetime__lt: Optional[datetime] = Field(title="less planned date", default=None)
76
status__in: Optional[List[OrderStatus]] = Field(default=None, title='Order Status')
77
store_id__in: Optional[List[UUID]] = Field(default=None, title='Store', model='store')
78
order_type_id__in: Optional[List[UUID]] = Field(default=None, title='Order Type', model='order_type')
80
class Constants(Filter.Constants):
82
ordering_field_name = "order_by"
83
search_model_fields = ["external_number", "origin_number", "number"]
86
class OrderListSchema(GenericListSchema):
87
data: Optional[List[OrderScheme]]