diff --git a/card_attributes/handlers/card_attributes_command_handler.py b/card_attributes/handlers/card_attributes_command_handler.py index 5a2274f..4dec0c8 100644 --- a/card_attributes/handlers/card_attributes_command_handler.py +++ b/card_attributes/handlers/card_attributes_command_handler.py @@ -78,11 +78,13 @@ class CardAttributesCommandHandler(BaseHandler): try: for attr_name, attr_value in attributes.items(): - attr = next(attr for attr in project_attrs if attr.name == attr_name) - if attr: + try: + attr = next(attr for attr in project_attrs if attr.name == attr_name) if attr.is_applicable_to_group and card.group: await self.set_attr_for_each_in_group(card.group.id, attr_name, attr_value) else: await self._set_card_attribute(card.id, attr_name, attr_value) + except StopIteration: + pass except CardAttributeException: raise diff --git a/models/card.py b/models/card.py index ce8999b..69a8e34 100644 --- a/models/card.py +++ b/models/card.py @@ -62,6 +62,7 @@ class Card(BaseModel): # module servicesAndProducts is_locked: Mapped[bool] = mapped_column(default=False, server_default='0') + is_services_profit_accounted: Mapped[bool] = mapped_column(default=True, server_default='1') shipping_warehouse_id: Mapped[int] = mapped_column(ForeignKey('shipping_warehouses.id'), nullable=True) shipping_warehouse: Mapped["ShippingWarehouse"] = relationship() @@ -86,9 +87,9 @@ class Card(BaseModel): bill_request: Mapped[Optional['CardBillRequest']] = relationship(back_populates='card', lazy='joined') # module client - client_id: Mapped[int] = mapped_column( + client_id: Mapped[Optional[int]] = mapped_column( ForeignKey('clients.id', ondelete='CASCADE'), - nullable=False, + nullable=True, comment='ID клиента', ) client: Mapped['Client'] = relationship('Client', backref=backref('cards', cascade="all, delete-orphan")) diff --git a/schemas/card.py b/schemas/card.py index c76214f..e041890 100644 --- a/schemas/card.py +++ b/schemas/card.py @@ -33,7 +33,7 @@ class BaseSchemaWithAttributes(BaseSchema): class CardSummary(BaseSchema): id: int name: str - client_name: str + client_name: Optional[str] created_at: datetime status: StatusSchema board: BoardSchema @@ -87,11 +87,12 @@ class BaseCardSchema(BaseSchema): is_deleted: bool is_completed: bool + is_services_profit_accounted: bool is_locked: bool services: List[CardServiceSchema] products: List[CardProductSchema] - client_id: int - client: ClientSchema + client_id: Optional[int] + client: Optional[ClientSchema] shipping_warehouse: Optional[Union[ShippingWarehouseSchema, str]] = None bill_request: Optional[CardBillRequestSchema] = None group: Optional[CardGroupSchema] = None @@ -114,6 +115,7 @@ class CardGeneralInfoSchema(BaseSchemaWithAttributes): manager: Optional[UserSchema] = None board_id: int status_id: int + is_services_profit_accounted: bool class OptionalShippingWarehouseSchema(BaseShippingWarehouseSchema): @@ -158,18 +160,14 @@ class CardCreateRequest(BaseSchema): class CardQuickCreateRequest(BaseSchema): name: constr(strip_whitespace=True) - client_name: constr(strip_whitespace=True) + client_name: Optional[constr(strip_whitespace=True)] comment: str acceptance_date: datetime - shipping_warehouse: constr(strip_whitespace=True) - base_marketplace: BaseMarketplaceSchema + shipping_warehouse: Optional[constr(strip_whitespace=True)] + base_marketplace: Optional[BaseMarketplaceSchema] status_id: int -class CardSummaryRequest(BaseSchema): - pass - - class CardAddServicesRequest(BaseSchema): card_id: int services: list[CardServiceSchema] diff --git a/services/card.py b/services/card.py index 34da0da..16c1e5d 100644 --- a/services/card.py +++ b/services/card.py @@ -1,5 +1,4 @@ from collections import defaultdict -from typing import Union import lexorank from fastapi import HTTPException @@ -48,12 +47,15 @@ class CardsService(BaseService): return str(prev.next()) return str(lexorank.parse(card.lexorank).next()) - async def change_status(self, card: Card, - status_id: int, - user: User, - deadline: datetime = None, - rank=None, - comment: str = ''): + async def change_status( + self, + card: Card, + status_id: int, + user: User, + deadline: datetime = None, + rank=None, + comment: str = '' + ): if not card.current_status_id == status_id: deadline = deadline status_change = CardStatusHistory( @@ -92,7 +94,7 @@ class CardsService(BaseService): client_service = ClientService(self.session) client = await client_service.get_by_name(request.client_name) - if not client: + if not client and request.client_name: client = await client_service.create_client_raw( user, request.client_name, @@ -101,19 +103,19 @@ class CardsService(BaseService): shipping_warehouse_service = ShippingWarehouseService(self.session) shipping_warehouse = await shipping_warehouse_service.get_by_name(name=request.shipping_warehouse) - if not shipping_warehouse: + if not shipping_warehouse and request.shipping_warehouse: shipping_warehouse = await shipping_warehouse_service.create_by_name(name=request.shipping_warehouse) rank = await self._get_rank_for_card(request.status_id) card = Card( name=request.name, created_at=datetime.now(), - client_id=client.id, + client_id=client.id if client else None, current_status_id=request.status_id, board_id=card_status.board_id, lexorank=rank, - shipping_warehouse_id=shipping_warehouse.id, - base_marketplace_key=request.base_marketplace.key + shipping_warehouse_id=shipping_warehouse.id if shipping_warehouse else None, + base_marketplace_key=request.base_marketplace.key if request.base_marketplace else None ) self.session.add(card) await self.session.flush() @@ -225,10 +227,11 @@ class CardsService(BaseService): summaries.append( CardSummary( id=card.id, - client_name=card.client.name, + client_name=card.client.name if card.client else None, name=card.name, status=card.status, board=card.board, + group=card.group, total_price=total_price, rank=rank, base_marketplace=base_marketplace, @@ -237,7 +240,6 @@ class CardsService(BaseService): shipment_warehouse_name=shipment_warehouse_name, total_products=products_count, bill_request=card.bill_request, - group=card.group ) ) return CardSummaryResponse(summaries=summaries) @@ -334,8 +336,11 @@ class CardsService(BaseService): return CardSchema.model_validate(card) - async def update_general_info(self, request: CardUpdateGeneralInfoRequest, - user: User) -> CardUpdateGeneralInfoResponse: + async def update_general_info( + self, + request: CardUpdateGeneralInfoRequest, + user: User + ) -> CardUpdateGeneralInfoResponse: try: card: Card = await self.session.scalar( select(Card) diff --git a/services/statistics/expenses_statistics.py b/services/statistics/expenses_statistics.py index 6d4f161..5ebeee9 100644 --- a/services/statistics/expenses_statistics.py +++ b/services/statistics/expenses_statistics.py @@ -41,23 +41,23 @@ class PaymentStatisticsService(BaseService): return expenses_with_filled_gaps @staticmethod - def _apply_payments(deals_by_dates: Subquery, expenses_subquery: Subquery): + def _apply_payments(cards_by_dates: Subquery, expenses_subquery: Subquery): return ( select( - deals_by_dates.c.date, - deals_by_dates.c.deals_count, - deals_by_dates.c.revenue, - (func.coalesce(deals_by_dates.c.profit, 0) - func.coalesce(expenses_subquery.c.expenses, 0)) + cards_by_dates.c.date, + cards_by_dates.c.cards_count, + cards_by_dates.c.revenue, + (func.coalesce(cards_by_dates.c.profit, 0) - func.coalesce(expenses_subquery.c.expenses, 0)) .label("profit"), - (deals_by_dates.c.expenses + expenses_subquery.c.expenses).label("expenses"), + (cards_by_dates.c.expenses + expenses_subquery.c.expenses).label("expenses"), ) - .join(expenses_subquery, expenses_subquery.c.date == deals_by_dates.c.date) + .join(expenses_subquery, expenses_subquery.c.date == cards_by_dates.c.date) ) - def apply_payments(self, filters: CommonProfitFilters, deals_by_dates: Subquery): + def apply_payments(self, filters: CommonProfitFilters, cards_by_dates: Subquery): self.date_from, self.date_to = filters.date_range salary_expenses = self._get_payment_records_sub() - deals_by_dates = self._apply_payments(deals_by_dates, salary_expenses) + cards_by_dates = self._apply_payments(cards_by_dates, salary_expenses) - return deals_by_dates + return cards_by_dates diff --git a/services/statistics/profit_statistics.py b/services/statistics/profit_statistics.py index 3a22ebc..9f4fa34 100644 --- a/services/statistics/profit_statistics.py +++ b/services/statistics/profit_statistics.py @@ -16,10 +16,10 @@ from services.statistics.transactions_statistics import TransactionsStatisticsSe class ProfitStatisticsService(BaseService): @staticmethod - def _get_sub_deals_created_at(date_from: datetime.date, date_to: datetime.date): - deals_created_at = ( + def _get_sub_cards_created_at(date_from: datetime.date, date_to: datetime.date): + cards_created_at = ( select( - Card.id.label('deal_id'), + Card.id.label('card_id'), func.date_trunc( 'day', Card.created_at, @@ -29,8 +29,8 @@ class ProfitStatisticsService(BaseService): .subquery() ) return ( - select(deals_created_at) - .where(deals_created_at.c.date.between(date_from, date_to)) + select(cards_created_at) + .where(cards_created_at.c.date.between(date_from, date_to)) .subquery() ) @@ -46,14 +46,14 @@ class ProfitStatisticsService(BaseService): ) return ( select( - Card.id.label('deal_id'), + Card.id.label('card_id'), func.date_trunc( 'day', last_statuses.c.changed_at, ).label('date'), Card.current_status_id, ) - .join(last_statuses, last_statuses.c.deal_id == Card.id) + .join(last_statuses, last_statuses.c.card_id == Card.id) .subquery() ) @@ -66,9 +66,9 @@ class ProfitStatisticsService(BaseService): .subquery() ) - def _get_deals_dates(self, deal_status_id: int): - if deal_status_id == -1: - return ProfitStatisticsService._get_sub_deals_created_at(self.date_from, self.date_to) + def _get_cards_dates(self, card_status_id: int): + if card_status_id == -1: + return ProfitStatisticsService._get_sub_cards_created_at(self.date_from, self.date_to) return ProfitStatisticsService._get_filtered_sub_status_history(self.date_from, self.date_to) @staticmethod @@ -80,7 +80,7 @@ class ProfitStatisticsService(BaseService): if is_chart: data_item = ProfitChartDataItem( date=row.date.date(), - deals_count=row.cards_count, + cards_count=row.cards_count, profit=row.profit, revenue=row.revenue, expenses=row.expenses, @@ -88,7 +88,7 @@ class ProfitStatisticsService(BaseService): else: data_item = ProfitTableDataItem( grouped_value=row.date.date(), - deals_count=row.cards_count, + cards_count=row.cards_count, profit=row.profit, revenue=row.revenue, expenses=row.expenses, @@ -97,10 +97,10 @@ class ProfitStatisticsService(BaseService): return data - def _get_stmt_deal_services(self, sub_filtered_status_history: Subquery): + def _get_stmt_card_services(self, sub_filtered_status_history: Subquery): return ( select( - Card.id.label("deal_id"), + Card.id.label("card_id"), func.date_trunc( "day", sub_filtered_status_history.c.date, @@ -110,11 +110,11 @@ class ProfitStatisticsService(BaseService): ) .join(CardService, Card.id == CardService.card_id) .join(Service, CardService.service_id == Service.id) - .join(sub_filtered_status_history, Card.id == sub_filtered_status_history.c.deal_id) + .join(sub_filtered_status_history, Card.id == sub_filtered_status_history.c.card_id) .where( and_( Card.is_deleted == False, - Card.is_accounted == True, + Card.is_services_profit_accounted == True, Card.is_completed == True if self.is_completed_only else True ) ) @@ -130,42 +130,42 @@ class ProfitStatisticsService(BaseService): return stmt.where(Card.board_id.in_(board_ids_stmt)) @staticmethod - def _apply_filters(request: CommonProfitFilters, stmt_deal_services, stmt_deal_product_services): + def _apply_filters(request: CommonProfitFilters, stmt_card_services, stmt_card_product_services): if request.client_id != -1: - stmt_deal_services = stmt_deal_services.where(Card.client_id == request.client_id) - stmt_deal_product_services = stmt_deal_product_services.where(Card.client_id == request.client_id) + stmt_card_services = stmt_card_services.where(Card.client_id == request.client_id) + stmt_card_product_services = stmt_card_product_services.where(Card.client_id == request.client_id) if request.base_marketplace_key != "all": - stmt_deal_services = stmt_deal_services.where(Card.base_marketplace_key == request.base_marketplace_key) - stmt_deal_product_services = stmt_deal_product_services.where( + stmt_card_services = stmt_card_services.where(Card.base_marketplace_key == request.base_marketplace_key) + stmt_card_product_services = stmt_card_product_services.where( Card.base_marketplace_key == request.base_marketplace_key) if request.project_id != -1: - stmt_deal_services = ProfitStatisticsService._board_ids_for_project(request.project_id, stmt_deal_services) - stmt_deal_product_services = ProfitStatisticsService._board_ids_for_project( + stmt_card_services = ProfitStatisticsService._board_ids_for_project(request.project_id, stmt_card_services) + stmt_card_product_services = ProfitStatisticsService._board_ids_for_project( request.project_id, - stmt_deal_product_services, + stmt_card_product_services, ) if request.board_id != -1: - stmt_deal_services = stmt_deal_services.where(Card.board_id == request.board_id) - stmt_deal_product_services = stmt_deal_product_services.where(Card.board_id == request.board_id) + stmt_card_services = stmt_card_services.where(Card.board_id == request.board_id) + stmt_card_product_services = stmt_card_product_services.where(Card.board_id == request.board_id) if request.card_status_id != -1: - stmt_deal_services = stmt_deal_services.where(Card.current_status_id == request.card_status_id) - stmt_deal_product_services = stmt_deal_product_services.where( + stmt_card_services = stmt_card_services.where(Card.current_status_id == request.card_status_id) + stmt_card_product_services = stmt_card_product_services.where( Card.current_status_id == request.card_status_id) if request.manager_id != -1: - stmt_deal_services = stmt_deal_services.where(Card.manager_id == request.manager_id) - stmt_deal_product_services = stmt_deal_product_services.where(Card.manager_id == request.manager_id) + stmt_card_services = stmt_card_services.where(Card.manager_id == request.manager_id) + stmt_card_product_services = stmt_card_product_services.where(Card.manager_id == request.manager_id) - return stmt_deal_services, stmt_deal_product_services + return stmt_card_services, stmt_card_product_services def _get_stmt_product_services(self): return ( select( - Card.id.label("deal_id"), + Card.id.label("card_id"), func.sum(CardProductService.price * CardProduct.quantity).label("revenue"), func.sum(CardProductService.price * CardProduct.quantity).label("profit"), ) @@ -181,7 +181,7 @@ class ProfitStatisticsService(BaseService): .where( and_( Card.is_deleted == False, - Card.is_accounted == True, + Card.is_services_profit_accounted == True, Card.is_completed == True if self.is_completed_only else True, ) ) @@ -189,55 +189,55 @@ class ProfitStatisticsService(BaseService): ) @staticmethod - def _get_joined_deals_and_statuses(sub_deal_product_services: Subquery, sub_deals_dates: Subquery): + def _get_joined_cards_and_statuses(sub_card_product_services: Subquery, sub_cards_dates: Subquery): return ( select( - sub_deal_product_services.c.deal_id, + sub_card_product_services.c.card_id, func.date_trunc( "day", - sub_deals_dates.c.date + sub_cards_dates.c.date ).label("date"), - sub_deal_product_services.c.revenue.label("revenue"), - sub_deal_product_services.c.profit.label("profit"), + sub_card_product_services.c.revenue.label("revenue"), + sub_card_product_services.c.profit.label("profit"), ) - .join(sub_deals_dates, sub_deal_product_services.c.deal_id == sub_deals_dates.c.deal_id) + .join(sub_cards_dates, sub_card_product_services.c.card_id == sub_cards_dates.c.card_id) ) def _group_by_date(self, stmt): - all_dates = generate_date_range(self.date_from, self.date_to, ["deals_count", "revenue", "profit", "expenses"]) - deals = ( + all_dates = generate_date_range(self.date_from, self.date_to, ["cards_count", "revenue", "profit", "expenses"]) + cards = ( select( stmt.c.date, - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) .group_by(stmt.c.date) .subquery() ) - deals_with_filled_gaps = ( + cards_with_filled_gaps = ( select( all_dates.c.date, - (all_dates.c.deals_count + func.coalesce(deals.c.deals_count, 0)).label("deals_count"), - (all_dates.c.revenue + func.coalesce(deals.c.revenue, 0)).label("revenue"), - (all_dates.c.profit + func.coalesce(deals.c.profit, 0)).label("profit"), + (all_dates.c.cards_count + func.coalesce(cards.c.cards_count, 0)).label("cards_count"), + (all_dates.c.revenue + func.coalesce(cards.c.revenue, 0)).label("revenue"), + (all_dates.c.profit + func.coalesce(cards.c.profit, 0)).label("profit"), literal(0).label("expenses"), ) - .join(deals, all_dates.c.date == deals.c.date, isouter=True) + .join(cards, all_dates.c.date == cards.c.date, isouter=True) .order_by(all_dates.c.date.asc()) ) - return deals_with_filled_gaps + return cards_with_filled_gaps @staticmethod - def _group_by_deals(stmt_union: Subquery): + def _group_by_cards(stmt_union: Subquery): return ( select( - stmt_union.c.deal_id, + stmt_union.c.card_id, stmt_union.c.date, func.sum(stmt_union.c.profit).label("profit"), func.sum(stmt_union.c.revenue).label("revenue"), ) - .group_by(stmt_union.c.deal_id, stmt_union.c.date) + .group_by(stmt_union.c.card_id, stmt_union.c.date) ) @staticmethod @@ -246,7 +246,7 @@ class ProfitStatisticsService(BaseService): select( Client.id, Client.name.label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -261,7 +261,7 @@ class ProfitStatisticsService(BaseService): select( Project.id, Project.name.label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -277,7 +277,7 @@ class ProfitStatisticsService(BaseService): select( Board.id, Board.name.label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -291,7 +291,7 @@ class ProfitStatisticsService(BaseService): return ( select( Card.current_status_id.label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -306,7 +306,7 @@ class ProfitStatisticsService(BaseService): ShippingWarehouse.id, ShippingWarehouse.name.label("grouped_value"), ShippingWarehouse.is_deleted, - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -321,7 +321,7 @@ class ProfitStatisticsService(BaseService): return ( select( BaseMarketplace.name.label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -341,7 +341,7 @@ class ProfitStatisticsService(BaseService): select( managers.c.id, (managers.c.first_name + " " + managers.c.second_name).label("grouped_value"), - func.count(stmt.c.card_id).label("deals_count"), + func.count(stmt.c.card_id).label("cards_count"), func.sum(stmt.c.revenue).label("revenue"), func.sum(stmt.c.profit).label("profit"), ) @@ -352,30 +352,30 @@ class ProfitStatisticsService(BaseService): async def _get_data_rows_grouped_by_date( self, - stmt_deal_services, - stmt_deal_product_services, - sub_deals_dates: Subquery, + stmt_card_services, + stmt_card_product_services, + sub_cards_dates: Subquery, ): - sub_deal_product_services = stmt_deal_product_services.subquery() - stmt_join_deals_statuses = self._get_joined_deals_and_statuses(sub_deal_product_services, sub_deals_dates) + sub_card_product_services = stmt_card_product_services.subquery() + stmt_join_cards_statuses = self._get_joined_cards_and_statuses(sub_card_product_services, sub_cards_dates) - sub_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery() + sub_union = union_all(stmt_card_services, stmt_join_cards_statuses).subquery() - sub_grouped_by_deals = self._group_by_deals(sub_union) - sub_deals_grouped_by_date = self._group_by_date(sub_grouped_by_deals).subquery() + sub_grouped_by_cards = self._group_by_cards(sub_union) + sub_cards_grouped_by_date = self._group_by_date(sub_grouped_by_cards).subquery() expenses_statistics_service = PaymentStatisticsService(self.session) - stmt_deals_applied_expenses = expenses_statistics_service.apply_payments( + stmt_cards_applied_expenses = expenses_statistics_service.apply_payments( self.filters, - sub_deals_grouped_by_date + sub_cards_grouped_by_date ) transactions_statistics_service = TransactionsStatisticsService(self.session) - stmt_deals_applied_transactions = transactions_statistics_service.apply_transactions( + stmt_cards_applied_transactions = transactions_statistics_service.apply_transactions( self.filters, - stmt_deals_applied_expenses.subquery() + stmt_cards_applied_expenses.subquery() ) - result = await self.session.execute(stmt_deals_applied_transactions) + result = await self.session.execute(stmt_cards_applied_transactions) rows = result.all() return rows @@ -384,18 +384,18 @@ class ProfitStatisticsService(BaseService): self.is_completed_only = request.is_completed_only self.filters = request - sub_deals_dates = self._get_deals_dates(request.card_status_id) + sub_cards_dates = self._get_cards_dates(request.card_status_id) - stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates) - stmt_deal_product_services = self._get_stmt_product_services() - stmt_deal_services, stmt_deal_product_services = self._apply_filters( + stmt_card_services = self._get_stmt_card_services(sub_cards_dates) + stmt_card_product_services = self._get_stmt_product_services() + stmt_card_services, stmt_card_product_services = self._apply_filters( request, - stmt_deal_services, - stmt_deal_product_services + stmt_card_services, + stmt_card_product_services ) rows = await self._get_data_rows_grouped_by_date( - stmt_deal_services, stmt_deal_product_services, sub_deals_dates + stmt_card_services, stmt_card_product_services, sub_cards_dates ) return self._to_schema(rows, is_chart) @@ -417,103 +417,104 @@ class ProfitStatisticsService(BaseService): grouped_value=row.grouped_value, revenue=row.revenue, profit=row.profit, - deals_count=row.deals_count, + cards_count=row.cards_count, )) return GetProfitTableDataResponse(data=data) def _get_common_table_grouped(self, request: GetProfitTableDataRequest): self.date_from, self.date_to = request.date_range + self.is_completed_only = request.is_completed_only self.filters = request - sub_deals_dates = self._get_deals_dates(request.card_status_id) + sub_cards_dates = self._get_cards_dates(request.card_status_id) - stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates) + stmt_card_services = self._get_stmt_card_services(sub_cards_dates) - stmt_deal_product_services = self._get_stmt_product_services() + stmt_card_product_services = self._get_stmt_product_services() - stmt_deal_product_services = ( + stmt_card_product_services = ( select( - stmt_deal_product_services.c.deal_id, + stmt_card_product_services.c.card_id, func.date_trunc( "day", Card.created_at ).label("date"), - stmt_deal_product_services.c.revenue.label("revenue"), - stmt_deal_product_services.c.profit.label("profit"), + stmt_card_product_services.c.revenue.label("revenue"), + stmt_card_product_services.c.profit.label("profit"), ) - .join(Card, Card.id == stmt_deal_product_services.c.deal_id) + .join(Card, Card.id == stmt_card_product_services.c.card_id) ) - stmt_deal_services, stmt_deal_product_services = self._apply_filters( + stmt_card_services, stmt_card_product_services = self._apply_filters( request, - stmt_deal_services, - stmt_deal_product_services + stmt_card_services, + stmt_card_product_services ) - sub_union = union_all(stmt_deal_services, stmt_deal_product_services).subquery() + sub_union = union_all(stmt_card_services, stmt_card_product_services).subquery() - sub_grouped_by_deals = self._group_by_deals(sub_union) - return sub_grouped_by_deals + sub_grouped_by_cards = self._group_by_cards(sub_union) + return sub_grouped_by_cards async def _get_table_grouped_by_clients(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_clients = self._join_and_group_by_clients(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_clients = self._join_and_group_by_clients(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_clients) async def _get_table_grouped_by_projects(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_projects = self._join_and_group_by_projects(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_projects = self._join_and_group_by_projects(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_projects) async def _get_table_grouped_by_boards(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_boards = self._join_and_group_by_boards(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_boards = self._join_and_group_by_boards(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_boards) async def _get_table_grouped_by_statuses(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: date_from, date_to = request.date_range - sub_deals_dates = self._get_filtered_sub_status_history(date_from, date_to) + sub_cards_dates = self._get_filtered_sub_status_history(date_from, date_to) - stmt_deal_services = self._get_stmt_deal_services(sub_deals_dates) - stmt_deal_product_services = self._get_stmt_product_services() + stmt_card_services = self._get_stmt_card_services(sub_cards_dates) + stmt_card_product_services = self._get_stmt_product_services() - stmt_deal_services, stmt_deal_product_services = self._apply_filters( + stmt_card_services, stmt_card_product_services = self._apply_filters( request, - stmt_deal_services, - stmt_deal_product_services + stmt_card_services, + stmt_card_product_services ) - stmt_join_deals_statuses = self._get_joined_deals_and_statuses( - stmt_deal_product_services.subquery(), - sub_deals_dates + stmt_join_cards_statuses = self._get_joined_cards_and_statuses( + stmt_card_product_services.subquery(), + sub_cards_dates ) - sub_union = union_all(stmt_deal_services, stmt_join_deals_statuses).subquery() + sub_union = union_all(stmt_card_services, stmt_join_cards_statuses).subquery() - sub_grouped_by_deals = self._group_by_deals(sub_union) - stmt_grouped_by_date = self._join_and_group_by_statuses(sub_grouped_by_deals) + sub_grouped_by_cards = self._group_by_cards(sub_union) + stmt_grouped_by_date = self._join_and_group_by_statuses(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_date) async def _get_table_grouped_by_warehouses(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_warehouses = self._join_and_group_by_warehouses(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_warehouses = self._join_and_group_by_warehouses(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_warehouses) async def _get_table_grouped_by_marketplace(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_marketplaces = self._join_and_group_by_marketplaces(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_marketplaces = self._join_and_group_by_marketplaces(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_marketplaces) async def _get_table_grouped_by_managers(self, request: GetProfitTableDataRequest) -> GetProfitTableDataResponse: - sub_grouped_by_deals = self._get_common_table_grouped(request) - stmt_grouped_by_managers = self._join_and_group_by_managers(sub_grouped_by_deals) + sub_grouped_by_cards = self._get_common_table_grouped(request) + stmt_grouped_by_managers = self._join_and_group_by_managers(sub_grouped_by_cards) return await self._table_data_from_stmt(stmt_grouped_by_managers) diff --git a/services/statistics/transactions_statistics.py b/services/statistics/transactions_statistics.py index cfbd539..a8b2578 100644 --- a/services/statistics/transactions_statistics.py +++ b/services/statistics/transactions_statistics.py @@ -70,24 +70,24 @@ class TransactionsStatisticsService(BaseService): return expenses_with_filled_gaps @staticmethod - def _apply_transactions(deals_by_dates: Subquery, transactions: Subquery): + def _apply_transactions(cards_by_dates: Subquery, transactions: Subquery): return ( select( - deals_by_dates.c.date, - deals_by_dates.c.deals_count, - (deals_by_dates.c.revenue + transactions.c.revenue).label("revenue"), - (func.coalesce(deals_by_dates.c.profit, 0) - func.coalesce(transactions.c.expenses, 0) + func.coalesce( + cards_by_dates.c.date, + cards_by_dates.c.cards_count, + (cards_by_dates.c.revenue + transactions.c.revenue).label("revenue"), + (func.coalesce(cards_by_dates.c.profit, 0) - func.coalesce(transactions.c.expenses, 0) + func.coalesce( transactions.c.revenue, 0)) .label("profit"), - (deals_by_dates.c.expenses + transactions.c.expenses).label("expenses"), + (cards_by_dates.c.expenses + transactions.c.expenses).label("expenses"), ) - .join(transactions, transactions.c.date == deals_by_dates.c.date) + .join(transactions, transactions.c.date == cards_by_dates.c.date) ) - def apply_transactions(self, filters: CommonProfitFilters, deals_by_dates: Subquery): + def apply_transactions(self, filters: CommonProfitFilters, cards_by_dates: Subquery): self.date_from, self.date_to = filters.date_range additional_expenses = self._get_additional_transactions_sub(filters.income_tag_id, filters.expense_tag_id) - deals_by_dates = self._apply_transactions(deals_by_dates, additional_expenses) + cards_by_dates = self._apply_transactions(cards_by_dates, additional_expenses) - return deals_by_dates + return cards_by_dates