feat: residues accounting
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import datetime
|
||||
from typing import Union
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm import joinedload, selectinload, noload
|
||||
|
||||
from models import Client, ClientDetails, User
|
||||
from models import Client, ClientDetails, User, ResidualPallet, ResidualBox, ResidualProduct, Product
|
||||
from schemas.client import *
|
||||
from services.base import BaseService
|
||||
|
||||
@@ -15,9 +16,35 @@ class ClientService(BaseService):
|
||||
client = await self.session.scalar(select(Client).where(Client.name == name))
|
||||
return client
|
||||
|
||||
async def get_by_id(self, client_id: int) -> Union[Client, None]:
|
||||
async def _get_by_id(self, client_id: int) -> Union[Client, None]:
|
||||
return await self.session.get(Client, client_id)
|
||||
|
||||
async def get_by_id(self, client_id: int) -> ClientGetResponse:
|
||||
stmt = (
|
||||
select(Client)
|
||||
.options(
|
||||
selectinload(Client.pallets)
|
||||
.selectinload(ResidualPallet.residual_products)
|
||||
.selectinload(ResidualProduct.product)
|
||||
.noload(Product.barcodes),
|
||||
selectinload(Client.pallets)
|
||||
.selectinload(ResidualPallet.boxes)
|
||||
.selectinload(ResidualBox.residual_products)
|
||||
.selectinload(ResidualProduct.product)
|
||||
.noload(Product.barcodes),
|
||||
selectinload(Client.boxes)
|
||||
.selectinload(ResidualBox.residual_products)
|
||||
.selectinload(ResidualProduct.product)
|
||||
.noload(Product.barcodes),
|
||||
)
|
||||
.where(Client.id == client_id)
|
||||
)
|
||||
client = (await self.session.execute(stmt)).one_or_none()
|
||||
client = client[0] if client else None
|
||||
if not client:
|
||||
raise HTTPException(status_code=404, detail="Клиент не найден")
|
||||
return ClientGetResponse(client=client)
|
||||
|
||||
async def get_details_by_client_id(self, client_id: int) -> Union[ClientDetails, None]:
|
||||
details = await self.session.scalar(select(ClientDetails).where(ClientDetails.client_id == client_id))
|
||||
return details
|
||||
@@ -101,7 +128,7 @@ class ClientService(BaseService):
|
||||
|
||||
async def update(self, request: ClientUpdateRequest, user: User) -> ClientUpdateResponse:
|
||||
try:
|
||||
client = await self.get_by_id(request.data.id)
|
||||
client = await self._get_by_id(request.data.id)
|
||||
if not client:
|
||||
return ClientUpdateResponse(ok=False, message='Клиент не найден')
|
||||
request_dict = request.data.dict()
|
||||
@@ -124,7 +151,7 @@ class ClientService(BaseService):
|
||||
|
||||
async def delete(self, request: ClientDeleteRequest) -> ClientDeleteResponse:
|
||||
try:
|
||||
client = await self.get_by_id(request.client_id)
|
||||
client = await self._get_by_id(request.client_id)
|
||||
if not client:
|
||||
return ClientDeleteResponse(ok=False, message='Клиент не найден')
|
||||
await self.session.delete(client)
|
||||
|
||||
Reference in New Issue
Block a user