feat: shipping warehouse and cost
This commit is contained in:
@@ -13,6 +13,7 @@ from schemas.client import ClientDetailsSchema
|
||||
from schemas.deal import *
|
||||
from services.base import BaseService
|
||||
from services.client import ClientService
|
||||
from services.shipping_warehouse import ShippingWarehouseService
|
||||
|
||||
|
||||
class DealService(BaseService):
|
||||
@@ -90,6 +91,10 @@ class DealService(BaseService):
|
||||
request.client_name,
|
||||
ClientDetailsSchema()
|
||||
)
|
||||
shipping_warehouse_service = ShippingWarehouseService(self.session)
|
||||
shipping_warehouse = await shipping_warehouse_service.get_by_name(name=request.shipping_warehouse)
|
||||
if not shipping_warehouse:
|
||||
shipping_warehouse = await shipping_warehouse_service.create_by_name(name=request.shipping_warehouse)
|
||||
|
||||
rank = await self._get_rank_for_deal(DealStatus.CREATED)
|
||||
deal = Deal(
|
||||
@@ -97,7 +102,8 @@ class DealService(BaseService):
|
||||
created_at=datetime.datetime.now(),
|
||||
client_id=client.id,
|
||||
current_status=DealStatus.CREATED,
|
||||
lexorank=rank
|
||||
lexorank=rank,
|
||||
shipping_warehouse_id=shipping_warehouse.id
|
||||
)
|
||||
self.session.add(deal)
|
||||
await self.session.flush()
|
||||
@@ -167,7 +173,8 @@ class DealService(BaseService):
|
||||
price_subquery, Deal.id == price_subquery.c.deal_id)
|
||||
.where(
|
||||
Deal.is_deleted == False,
|
||||
Deal.is_completed == False
|
||||
Deal.is_completed == False,
|
||||
Deal.current_status != DealStatus.COMPLETED
|
||||
)
|
||||
)
|
||||
deals_query = await self.session.execute(q)
|
||||
@@ -203,7 +210,9 @@ class DealService(BaseService):
|
||||
deal = await self.session.scalar(
|
||||
select(Deal)
|
||||
.options(
|
||||
joinedload(Deal.client).joinedload(Client.details),
|
||||
joinedload(Deal.shipping_warehouse),
|
||||
joinedload(Deal.client)
|
||||
.joinedload(Client.details),
|
||||
selectinload(Deal.services)
|
||||
.joinedload(models.secondary.DealService.service)
|
||||
.joinedload(Service.category),
|
||||
@@ -220,6 +229,7 @@ class DealService(BaseService):
|
||||
.joinedload(DealStatusHistory.user),
|
||||
selectinload(Deal.status_history)
|
||||
.noload(DealStatusHistory.deal),
|
||||
|
||||
)
|
||||
.where(Deal.id == deal_id)
|
||||
)
|
||||
|
||||
37
services/shipping_warehouse.py
Normal file
37
services/shipping_warehouse.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from typing import Union
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
import models
|
||||
from schemas.shipping_warehouse import GetAllShippingWarehousesResponse, ShippingWarehouseSchema
|
||||
from services.base import BaseService
|
||||
|
||||
|
||||
class ShippingWarehouseService(BaseService):
|
||||
async def get_all(self) -> GetAllShippingWarehousesResponse:
|
||||
stmt = (
|
||||
select(
|
||||
models.ShippingWarehouse
|
||||
)
|
||||
.order_by(
|
||||
models.ShippingWarehouse.id
|
||||
)
|
||||
)
|
||||
shipping_warehouses = (await self.session.scalars(stmt)).all()
|
||||
result = []
|
||||
for shipping_warehouse in shipping_warehouses:
|
||||
result.append(ShippingWarehouseSchema.model_validate(shipping_warehouse))
|
||||
return GetAllShippingWarehousesResponse(shipping_warehouses=result)
|
||||
|
||||
async def get_by_name(self, name: str) -> Union[models.ShippingWarehouse, None]:
|
||||
stmt = select(models.ShippingWarehouse).where(models.ShippingWarehouse.name == name)
|
||||
shipping_warehouse = await self.session.scalar(stmt)
|
||||
return shipping_warehouse
|
||||
|
||||
async def create_by_name(self, name: str) -> models.ShippingWarehouse:
|
||||
shipping_warehouse = models.ShippingWarehouse(
|
||||
name=name
|
||||
)
|
||||
self.session.add(shipping_warehouse)
|
||||
await self.session.flush()
|
||||
return shipping_warehouse
|
||||
Reference in New Issue
Block a user