"""RevenueByStore Model.""" from sqlalchemy import asc from sqlalchemy import Column from sqlalchemy import desc from sqlalchemy import func from sqlalchemy import Numeric from sqlalchemy import or_ from sqlalchemy import String from sqlalchemy.engine import Row from moneyhub.constants.constants import OrderDirection from moneyhub.models.revenue_base import apply_filters from moneyhub.models.revenue_base import apply_subaccount_revenue from moneyhub.models.snowflake_base import BaseModel class RevenueByStore(BaseModel): """Revenue By Store model.""" __tablename__ = 'revenue_by_store_dbt' store_id = Column(Numeric(32, 0), nullable=False, primary_key=True) artist_id = Column(Numeric(32, 0), nullable=False, primary_key=False) store_name = Column(String(255), nullable=True) account_id = Column(Numeric(12, 0), nullable=False, primary_key=True) contract_id = Column(Numeric(12, 0), nullable=False, primary_key=True) subaccount_id = Column(Numeric(32, 0), nullable=True, primary_key=True) product_id = Column(Numeric(32, 0), nullable=True) track_unique_id = Column(Numeric(32, 0), nullable=True) statement_period_id = Column(Numeric(38, 0), nullable=False, primary_key=True) activity_period_id = Column(Numeric(38, 0), nullable=True) country_code = Column(String(255), nullable=True) imprint_id = Column(Numeric(32, 0), nullable=True) project_id = Column(Numeric(32, 0), nullable=True) transaction_type_id = Column(Numeric(32, 0), nullable=True) account_payee_currency = Column(String(50), nullable=False) net_revenue_payee_currency = Column(Numeric(36, 12), nullable=False) gross_revenue_payee_currency = Column(Numeric(36, 12), nullable=False) @classmethod def get_by_account_id( cls, account_id: int, artist_id: int | None, subaccount_id: int | None, limit: int, offset: int, contract_id: int | None, product_id: int | None, track_unique_id: int | None, statement_period_id_start: int | None, statement_period_id_end: int | None, order_by: str, order_dir: OrderDirection, search_term: str | None, subaccount_info: dict | None, activity_period_id_start: int | None = None, activity_period_id_end: int | None = None, country_codes: list[str] | None = None, imprint_ids: list[int] | None = None, transaction_type_ids: list[int] | None = None, store_ids: list[int] | None = None, project_id: int | None = None, ) -> tuple[list, int]: """GET list of revenue by store for a specified account ID and contract ID. Args: account_id (int): the id of an account artist_id (int): the id of an artist to filter by subaccount_id (int): the id of a subaccount to filter by limit (int): how many entities to retrieve. offset (int): the offset (for pagination). contract_id (int): the id of a contract to filter by product_id (int): the id of a product to filter by track_unique_id (int): the id of a track to filter by statement_period_id_start (int): start of the period range statement_period_id_end (int): end of the period range order_by (str): field to sort by order_dir (str): direction to sort by (ASC or DESC) search_term (str): query to search by subaccount_info (dict): Subaccount information activity_period_id_start (int): start of the period range activity_period_id_end (int): end of the period range country_codes (list[str] | None): countries to filter by imprint_ids (list[int] | None): ids to filter by transaction_type_ids (list[int] | None): ids to filter by store_ids (list[int] | None): store ids to filter by project_id (int | None): project id to filter by Returns: list: list of revenue by store """ filters = [(cls.account_id == account_id)] group_by = [cls.store_id, cls.store_name, cls.account_payee_currency] with_entities = [ cls.store_id, cls.store_name, cls.account_payee_currency, func.sum(cls.net_revenue_payee_currency).label('net_revenue_payee_currency'), func.sum(cls.gross_revenue_payee_currency).label('gross_revenue_payee_currency'), func.count().over().label('total_records') ] if search_term: search_filter = cls.store_name.ilike(f'%{search_term}%') if search_term.isnumeric(): search_filter = or_(search_filter, cls.store_id == search_term) filters.append(search_filter) apply_filters( cls, filters, artist_id=artist_id, statement_period_id_start=statement_period_id_start, statement_period_id_end=statement_period_id_end, country_codes=country_codes, imprint_ids=imprint_ids, transaction_type_ids=transaction_type_ids, track_unique_id=track_unique_id, subaccount_id=subaccount_id, contract_id=contract_id, product_id=product_id, project_id=project_id, activity_period_id_start=activity_period_id_start, activity_period_id_end=activity_period_id_end, store_ids=store_ids ) apply_subaccount_revenue(subaccount_info, with_entities, cls) order_direction = desc if order_dir == OrderDirection.DESC else asc query = cls.query.with_entities(*with_entities).filter(*filters).group_by( *group_by).order_by(order_direction(order_by)) records = query.limit(limit).offset(offset).all() if limit else query.all() total_records = int(records[0].total_records) if records else 0 return records, total_records @classmethod def get_stores_totals_by_account_id( cls, account_id: int, artist_id: int | None, subaccount_id: int | None, contract_id: int | None, product_id: int | None, track_unique_id: int | None, statement_period_id_start: int | None, statement_period_id_end: int | None, subaccount_info: dict | None, activity_period_id_start: int | None = None, activity_period_id_end: int | None = None, country_codes: list[str] | None = None, imprint_ids: list[int] | None = None, transaction_type_ids: list[int] | None = None, store_ids: list[int] | None = None, project_id: int | None = None, ) -> Row: """GET total net and gross revenue for all stores for an account. Args: account_id (int): the id of an account artist_id (int): the id of an artist to filter by subaccount_id (int): the id of a subaccount to filter by contract_id (int): the id of a contract to filter by product_id (int): the id of a product to filter by track_unique_id (int): the id of a track to filter by statement_period_id_start (int): start of the period range statement_period_id_end (int): end of the period range subaccount_info (dict): Subaccount information activity_period_id_start (int): start of the period range activity_period_id_end (int): end of the period range country_codes (list[str] | None): countries to filter by imprint_ids (list[int] | None): ids to filter by transaction_type_ids (list[int] | None): ids to filter by store_ids (list[int] | None): store ids to filter by project_id (int | None): project id to filter by Returns: Row: Row containing the total net and gross revenue in the payee currency """ filters = [(cls.account_id == account_id)] with_entities = [ func.sum(cls.net_revenue_payee_currency).label('net_revenue_payee_currency'), func.sum(cls.gross_revenue_payee_currency).label('gross_revenue_payee_currency') ] apply_filters( cls, filters, artist_id=artist_id, statement_period_id_start=statement_period_id_start, statement_period_id_end=statement_period_id_end, country_codes=country_codes, imprint_ids=imprint_ids, transaction_type_ids=transaction_type_ids, track_unique_id=track_unique_id, subaccount_id=subaccount_id, contract_id=contract_id, product_id=product_id, project_id=project_id, activity_period_id_start=activity_period_id_start, activity_period_id_end=activity_period_id_end, store_ids=store_ids, ) apply_subaccount_revenue(subaccount_info, with_entities, cls) return cls.query.with_entities(*with_entities).filter(*filters).one() @classmethod def get_stores_by_account_id( cls, account_id: int, subaccount_id: int | None = None, ) -> list: """Get a list of stores for a specified account. Optionally filtered by subaccount. Args: account_id (int): ID of an account subaccount_id (int): Optional ID of a subaccount Returns: list: list of revenue by stores """ filters = [(cls.account_id == account_id)] with_entities = [ cls.store_id, cls.store_name, ] if subaccount_id: filters.append(cls.subaccount_id == subaccount_id) return cls.query \ .with_entities(*with_entities) \ .filter(*filters) \ .order_by(asc(func.lower(cls.store_name))) \ .distinct() \ .all()