"""RevenueByProductDistro Model.""" from sqlalchemy import asc from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import desc from sqlalchemy import func from sqlalchemy import Numeric from sqlalchemy import or_ from sqlalchemy import String 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 RevenueByProductDistro(BaseModel): """Revenue By Product Distro model.""" __tablename__ = 'revenue_by_product_distro_dbt' product_id = Column(Numeric(32, 0), nullable=False, primary_key=True) product_name = Column(String(1400), nullable=False) display_upc = Column(String(255), nullable=False) release_date = Column(DateTime, nullable=False) account_id = Column(Numeric(12, 0), nullable=False, primary_key=True) contract_id = Column(Numeric(12, 0), nullable=False, primary_key=True) statement_period_id = Column(Numeric(38, 0), nullable=False, primary_key=True) artist_id = Column(Numeric(32, 0), nullable=True) artist_name = Column(String(2000), nullable=False) subaccount_id = Column(Numeric(32, 0), nullable=True) account_payee_currency = Column(String(50), nullable=False) mechanical_deduction_amount_payee_currency = Column(Numeric(36, 12), nullable=False) publisher_admin_fee_payee_currency = Column(Numeric(36, 12), nullable=False) net_share_payee_currency = Column(Numeric(36, 12), nullable=False) gross_revenue_payee_currency = Column(Numeric(36, 12), nullable=False) activity_period_id = Column(Numeric(38, 0), nullable=True) store_id = Column(Numeric(32, 0), nullable=True) country_code = Column(String(2), nullable=True) imprint_id = Column(Numeric(32, 0), nullable=True) transaction_type_id = Column(Numeric(32, 0), nullable=True) product_code = Column(String(50), nullable=True) project_id = Column(Numeric(32, 0), nullable=False) project_code = Column(String(50), nullable=True) project_name = Column(String(255), nullable=True) @classmethod def get_by_account_id( cls, account_id: int, limit: int, offset: int, contract_id: int | None, statement_period_id_start: int | None, statement_period_id_end: int | None, activity_period_id_start: int | None, activity_period_id_end: int | None, artist_id: int | None, subaccount_id: int | None, order_by: str, order_dir: OrderDirection, search_term: str | None, store_ids: list[int] | None, country_codes: list[str] | None, transaction_type_ids: list[int] | None, imprint_ids: list[int] | None, subaccount_info: dict | None, project_id: int | None = None, ) -> tuple[list, int]: """GET list of revenue by product for a specified account ID and contract ID. Args: account_id (int): the id of an account limit (int): how many entities to retrieve. offset (int): the offset (for pagination). contract_id (int): the id of a contract to filter by statement_period_id_start (int): Start of the period range statement_period_id_end (int): End of the period range activity_period_id_start (int): Start of the activity period range activity_period_id_end (int): End of the activity period range artist_id (int): the id of an artist to filter by subaccount_id (int): the id of a subaccount to filter by order_by (str): Key to sort by (defaults to no sorting) order_dir (str): Direction to sort by (ASC or DESC) search_term (str): query to search by store_ids (list[int]): list of store IDs to filter by country_codes (list[str]): list of country codes to filter by transaction_type_ids (list[int]): list of transaction type IDs to filter by imprint_ids (list[int]): list of imprint IDs to filter by subaccount_info (dict): Subaccount information project_id (int): the id of a project to filter by Returns: list: list of revenue by product """ filters = [(cls.account_id == account_id)] group_by = [ cls.artist_name, cls.product_id, cls.product_name, cls.account_payee_currency, cls.release_date, cls.product_code, cls.project_code, cls.project_name ] with_entities = [ cls.product_id, cls.account_payee_currency, cls.project_name, func.sum(cls.mechanical_deduction_amount_payee_currency).label( 'mechanical_deduction_amount_payee_currency'), func.sum(cls.publisher_admin_fee_payee_currency).label( 'publisher_admin_fee_payee_currency'), func.sum(cls.net_share_payee_currency).label('net_share_payee_currency'), func.sum(cls.gross_revenue_payee_currency).label('gross_revenue_payee_currency'), func.count().over().label('total_records'), ] apply_subaccount_revenue(subaccount_info, with_entities, cls) if contract_id: filters.append(cls.contract_id == contract_id) apply_filters( cls=cls, filters=filters, activity_period_id_start=activity_period_id_start, activity_period_id_end=activity_period_id_end, artist_id=artist_id, country_codes=country_codes, imprint_ids=imprint_ids, statement_period_id_start=statement_period_id_start, statement_period_id_end=statement_period_id_end, store_ids=store_ids, subaccount_id=subaccount_id, transaction_type_ids=transaction_type_ids, project_id=project_id, ) if search_term: search_filters = [ (cls.product_name.ilike(f'%{search_term}%')), (cls.display_upc.ilike(f'%{search_term}%')), ] if search_term.isnumeric(): search_filters.append((cls.product_id == search_term)) filters.append(or_(*search_filters)) 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() total_records = int(records[0].total_records) if records else 0 return records, total_records @classmethod def get_products_by_account_id( cls, account_id: int, limit: int, search_term: str | None, subaccount_id: int | None, ) -> list: """Get list of distinct products for a specified account. Args: account_id (int): the id of an account subaccount_id (int | None): the id of a subaccount to filter by limit (int): maximum number of products to return search_term (str | None): search term to filter products by name, UPC, or exact ID Returns: list: list of distinct products with product_id, product_name, and display_upc """ filters = [cls.account_id == account_id] if subaccount_id: filters.append(cls.subaccount_id == subaccount_id) if search_term: search_filters = [ cls.product_name.ilike(f'%{search_term}%'), cls.display_upc.ilike(f'%{search_term}%'), ] if search_term.isnumeric(): search_filters.append(cls.product_id == search_term) filters.append(or_(*search_filters)) with_entities = [ cls.product_id, cls.product_name, cls.display_upc, ] query = cls.query \ .with_entities(*with_entities) \ .filter(*filters) \ .distinct() \ .order_by(cls.product_name) records = query.limit(limit).all() return records