feat: processing of modules in card, renaming
This commit is contained in:
		@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user