feat: filters for profit statistics table
This commit is contained in:
@@ -23,15 +23,17 @@ class ProfitTableDataItem(BaseSchema):
|
|||||||
|
|
||||||
# region Requests
|
# region Requests
|
||||||
|
|
||||||
class GetProfitChartDataRequest(BaseSchema):
|
class CommonProfitFilters(BaseSchema):
|
||||||
date_range: Tuple[datetime.date, datetime.date]
|
date_range: Tuple[datetime.date, datetime.date]
|
||||||
client_id: int
|
client_id: int
|
||||||
base_marketplace_key: str
|
base_marketplace_key: str
|
||||||
deal_status_id: int
|
deal_status_id: int
|
||||||
|
|
||||||
|
class GetProfitChartDataRequest(CommonProfitFilters):
|
||||||
|
pass
|
||||||
|
|
||||||
class GetProfitTableDataRequest(BaseSchema):
|
|
||||||
date_range: Tuple[datetime.date, datetime.date]
|
class GetProfitTableDataRequest(CommonProfitFilters):
|
||||||
group_table_by: ProfitTableGroupBy
|
group_table_by: ProfitTableGroupBy
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from enums.profit_table_group_by import ProfitTableGroupBy
|
|||||||
from models import DealService, Deal, DealStatusHistory, DealProductService, DealProduct, Service, Client, \
|
from models import DealService, Deal, DealStatusHistory, DealProductService, DealProduct, Service, Client, \
|
||||||
ShippingWarehouse, BaseMarketplace
|
ShippingWarehouse, BaseMarketplace
|
||||||
from schemas.statistics import GetProfitChartDataResponse, GetProfitChartDataRequest, ProfitChartDataItem, \
|
from schemas.statistics import GetProfitChartDataResponse, GetProfitChartDataRequest, ProfitChartDataItem, \
|
||||||
GetProfitTableDataResponse, GetProfitTableDataRequest, ProfitTableDataItem
|
GetProfitTableDataResponse, GetProfitTableDataRequest, ProfitTableDataItem, CommonProfitFilters
|
||||||
from services.base import BaseService
|
from services.base import BaseService
|
||||||
|
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ class StatisticsService(BaseService):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _apply_filters(request: GetProfitChartDataRequest, stmt_deal_services, stmt_deal_product_services):
|
def _apply_chart_filters(request: CommonProfitFilters, stmt_deal_services, stmt_deal_product_services):
|
||||||
if request.client_id != -1:
|
if request.client_id != -1:
|
||||||
stmt_deal_services = stmt_deal_services.where(Deal.client_id == request.client_id)
|
stmt_deal_services = stmt_deal_services.where(Deal.client_id == request.client_id)
|
||||||
stmt_deal_product_services = stmt_deal_product_services.where(Deal.client_id == request.client_id)
|
stmt_deal_product_services = stmt_deal_product_services.where(Deal.client_id == request.client_id)
|
||||||
@@ -266,7 +266,7 @@ class StatisticsService(BaseService):
|
|||||||
rows = result.all()
|
rows = result.all()
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
async def get_profit_chart_data(self, request: GetProfitChartDataRequest) -> GetProfitChartDataResponse:
|
async def get_data_grouped_by_date(self, request: CommonProfitFilters, is_chart: bool = True):
|
||||||
date_from, date_to = request.date_range
|
date_from, date_to = request.date_range
|
||||||
date_to += timedelta(days=1)
|
date_to += timedelta(days=1)
|
||||||
|
|
||||||
@@ -274,7 +274,7 @@ class StatisticsService(BaseService):
|
|||||||
|
|
||||||
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
||||||
stmt_deal_product_services = self._get_stmt_product_services()
|
stmt_deal_product_services = self._get_stmt_product_services()
|
||||||
stmt_deal_services, stmt_deal_product_services = self._apply_filters(
|
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
|
||||||
request,
|
request,
|
||||||
stmt_deal_services,
|
stmt_deal_services,
|
||||||
stmt_deal_product_services
|
stmt_deal_product_services
|
||||||
@@ -283,26 +283,14 @@ class StatisticsService(BaseService):
|
|||||||
rows = await self._get_data_rows_grouped_by_date(
|
rows = await self._get_data_rows_grouped_by_date(
|
||||||
stmt_deal_services, stmt_deal_product_services, sub_deals_dates
|
stmt_deal_services, stmt_deal_product_services, sub_deals_dates
|
||||||
)
|
)
|
||||||
|
return self._fill_dates_gaps(rows, date_from, date_to, is_chart)
|
||||||
|
|
||||||
data = self._fill_dates_gaps(rows, date_from, date_to)
|
async def get_profit_chart_data(self, request: GetProfitChartDataRequest) -> GetProfitChartDataResponse:
|
||||||
|
data = await self.get_data_grouped_by_date(request)
|
||||||
return GetProfitChartDataResponse(data=data)
|
return GetProfitChartDataResponse(data=data)
|
||||||
|
|
||||||
async def _get_table_grouped_by_dates(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse:
|
async def _get_table_grouped_by_dates(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse:
|
||||||
date_from, date_to = request.date_range
|
data = await self.get_data_grouped_by_date(request, False)
|
||||||
date_to += timedelta(days=1)
|
|
||||||
|
|
||||||
sub_deals_dates = self._get_sub_deals_created_at(date_from, date_to)
|
|
||||||
|
|
||||||
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
|
||||||
|
|
||||||
stmt_deal_product_services = self._get_stmt_product_services()
|
|
||||||
rows = await self._get_data_rows_grouped_by_date(
|
|
||||||
stmt_deal_services, stmt_deal_product_services, sub_deals_dates
|
|
||||||
)
|
|
||||||
|
|
||||||
data = self._fill_dates_gaps(rows, date_from, date_to, False)
|
|
||||||
|
|
||||||
return GetProfitTableDataResponse(data=data)
|
return GetProfitTableDataResponse(data=data)
|
||||||
|
|
||||||
async def _table_data_from_stmt(self, stmt) -> GetProfitTableDataResponse:
|
async def _table_data_from_stmt(self, stmt) -> GetProfitTableDataResponse:
|
||||||
@@ -323,7 +311,7 @@ class StatisticsService(BaseService):
|
|||||||
date_from, date_to = request.date_range
|
date_from, date_to = request.date_range
|
||||||
date_to += timedelta(days=1)
|
date_to += timedelta(days=1)
|
||||||
|
|
||||||
sub_deals_dates = self._get_sub_deals_created_at(date_from, date_to)
|
sub_deals_dates = self._get_deals_dates(request.deal_status_id, date_from, date_to)
|
||||||
|
|
||||||
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
||||||
|
|
||||||
@@ -342,6 +330,12 @@ class StatisticsService(BaseService):
|
|||||||
.join(Deal, Deal.id == stmt_deal_product_services.c.deal_id)
|
.join(Deal, Deal.id == stmt_deal_product_services.c.deal_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
|
||||||
|
request,
|
||||||
|
stmt_deal_services,
|
||||||
|
stmt_deal_product_services
|
||||||
|
)
|
||||||
|
|
||||||
sub_union = union_all(stmt_deal_services, stmt_deal_product_services).subquery()
|
sub_union = union_all(stmt_deal_services, stmt_deal_product_services).subquery()
|
||||||
|
|
||||||
sub_grouped_by_deals = self._group_by_deals(sub_union)
|
sub_grouped_by_deals = self._group_by_deals(sub_union)
|
||||||
@@ -360,9 +354,18 @@ class StatisticsService(BaseService):
|
|||||||
sub_deals_dates = self._get_filtered_sub_status_history(date_from, date_to)
|
sub_deals_dates = self._get_filtered_sub_status_history(date_from, date_to)
|
||||||
|
|
||||||
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates)
|
||||||
sub_deal_product_services = self._get_stmt_product_services().subquery()
|
stmt_deal_product_services = self._get_stmt_product_services()
|
||||||
|
|
||||||
stmt_join_deals_statuses = self._get_joined_deals_and_statuses(sub_deal_product_services, sub_deals_dates)
|
stmt_deal_services, stmt_deal_product_services = self._apply_chart_filters(
|
||||||
|
request,
|
||||||
|
stmt_deal_services,
|
||||||
|
stmt_deal_product_services
|
||||||
|
)
|
||||||
|
|
||||||
|
stmt_join_deals_statuses = self._get_joined_deals_and_statuses(
|
||||||
|
stmt_deal_product_services.subquery(),
|
||||||
|
sub_deals_dates
|
||||||
|
)
|
||||||
|
|
||||||
sub_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery()
|
sub_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user