"""Account Logic.""" from moneyhub import models from moneyhub.constants.constants import ContractType from moneyhub.constants.constants import OrderDirection def get_account_activity( account_id: int, subaccount_id: int | None, is_subaccount: bool, ) -> dict: """Get accounting activity for a specified account. Args: account_id (int): The id of an account subaccount_id (int): the id of a subaccount to filter by is_subaccount (bool): Whether the request is made by a subaccount Returns: dict: activity info """ adjustments = bool(models.CombinedAdjustments.get_account_adjustments_activity(account_id)) expenses = bool(models.Expenses.get_account_adjustment_detail_activity(account_id)) advances = bool(models.CombinedAdvances.get_account_advance_activity(account_id)) has_distro_revenue = bool(models.RevenueDistro.get_by_account_id( account_id, subaccount_id, is_subaccount)) has_nr_revenue = bool(models.RevenueNr.get_by_account_id(account_id)) mechanicals = models.LedgerSummary.check_account_mechanicals(account_id) has_vat = bool(models.LedgerVatSummary.get_activity_by_account_id(account_id)) has_wht = bool(models.CombinedPayments.get_withholding_tax_payment_by_account(account_id)) has_publishing_revenue = ( not is_subaccount and bool(models.CombinedAdjustments.get_publishing_revenue_activity(account_id)) ) activity_info = { 'advances': advances, 'physical_reserves': bool(models.LedgerReserveReleaseSchedule.get_account_reserves_activity(account_id)), 'revenue': has_distro_revenue or has_nr_revenue, 'adjustments': adjustments, 'mechanicals': mechanicals, 'expenses': expenses, 'taxes': has_vat or has_wht, 'publishing_revenue': has_publishing_revenue, } return activity_info def get_activity_periods_by_account( account_id: int, subaccount_id: int | None, activity_period_ids: list[int] | None, ) -> list: """Get activity periods for a specified account. Args: account_id (int): The id of an account subaccount_id (int): Subaccount to filter by activity_period_ids: (list): Optional list of activity periods to filter for Returns: list: list of periods """ return models.RevenueByActivityMonth.get_activity_periods_by_account_id( account_id, subaccount_id, activity_period_ids) def get_contracts_info(account_id: int, contract_ids: list) -> list: """Get contracts info for a specified account. Args: account_id (int): The id of an account contract_ids (list): Contracts to get info for. Returns: list: list of contracts """ return models.Contracts.get_by_account_id(account_id, contract_ids) def get_statement_periods_by_account( account_id: int, contract_id: int | None, order_dir: OrderDirection = OrderDirection.ASC ) -> list: """Get active statement periods for a specified account. Args: account_id (int): The id of an account contract_id (int): Contract to filter by order_dir (OrderDirection): Sort direction order Returns: list: list of statement periods """ active_periods_items, _ = models.AccountStatementPeriods.get_by_account_id( account_id, contract_id, None, None, order_dir) return active_periods_items def get_stores_by_account_id( account_id: int, subaccount_id: int | None, ) -> list: """Get stores by account id and optionally subaccount. Args: account_id (int): ID of the account subaccount_id (int): ID of a subaccount Returns: list: list of stores """ return models.RevenueByStore.get_stores_by_account_id( account_id, subaccount_id) def get_transaction_types_by_account_id( account_id: int, subaccount_id: int | None, ): """Get transactions by account id and optionally subaccount. Args: account_id (int): ID of the account subaccount_id (int): ID of a subaccount Returns: TransactionTypeSchema: response schema """ return models.RevenueByTransactionType.get_transaction_types_by_account_id( account_id, subaccount_id) def get_imprints_by_account_id( account_id: int, limit: int | None, search_term: str | int | None, subaccount_id: int | None, imprint_ids: list[int] | None, ) -> list: """Get imprints by account id with optional search and limit. Args: account_id (int): ID of the account limit (int | None): Maximum number of imprints to return subaccount_id (int | None): ID of a subaccount search_term (str | None): Search term to filter imprints by name imprint_ids (list | None): Imprint IDs to filter by Returns: list: List of imprint options """ return models.RevenueByImprint.get_imprints_by_account_id( account_id, limit, search_term, subaccount_id, imprint_ids) def get_artists_by_account_id( account_id: int, limit: int, search_term: str | int | None, subaccount_id: int | None, ) -> list: """Get artists by account id with optional search and limit. Args: account_id (int): ID of the account limit (int): Maximum number of artists to return subaccount_id (int): ID of a subaccount search_term (str): Search term to filter artists by name Returns: list: List of artist options """ return models.RevenueByArtist.get_artists_by_account_id( account_id, limit, search_term, subaccount_id) def get_tracks_by_account_id( account_id: int, limit: int, search_term: str | int | None, subaccount_id: int | None, ) -> list: """Get tracks by account id with optional search and limit. Args: account_id (int): ID of the account limit (int): Maximum number of tracks to return search_term (str | int | None): Search term to filter tracks by name or ISRC subaccount_id (int | None): ID of a subaccount Returns: list: List of track options """ return models.RevenueByTrack.get_tracks_by_account_id( account_id, limit, search_term, subaccount_id) def get_projects_by_account_id( account_id: int, limit: int, search_term: str | int | None, subaccount_id: int | None, ) -> list: """Get projects by account id with optional search and limit. Args: account_id (int): ID of the account limit (int): Maximum number of projects to return subaccount_id (int): ID of a subaccount search_term (str): Search term to filter projects by name Returns: list: List of project options """ return models.RevenueByProject.get_projects_by_account_id( account_id, limit, search_term, subaccount_id) def get_products_by_account_id( account_id: int, limit: int, search_term: str | None, subaccount_id: int | None, ) -> list: """Get products by account id with optional search and limit. Args: account_id (int): ID of the account limit (int): Maximum number of products to return search_term (str | None): Search term to filter products by name, UPC, or exact ID subaccount_id (int | None): ID of a subaccount Returns: list: List of product options """ return models.RevenueByProductDistro.get_products_by_account_id( account_id, limit, search_term, subaccount_id) def get_artists_by_ids(artist_ids: list[int]): """Get artist names by IDs. Args: artist_ids (list[int]): List of artist IDs to look up Returns: list: List of artist id and name pairs """ return models.RevenueByArtist.get_artist_names_by_ids(artist_ids) def get_recording_names(account_id: int, recording_ids: list[str]) -> list: """Get recording names by IDs based on the account's contract type. Args: account_id (int): The id of an account recording_ids (list[str]): List of recording IDs to look up Returns: list: List of recording id, name and isrc pairs """ contract_type = models.Contract.get_contract_type_by_account( account_id).contract_type if contract_type == ContractType.NEIGHBOURING_RIGHTS: return models.PerformanceNrSoundRecording.get_recording_names_by_ids( recording_ids) else: return models.DimTrack.get_recording_names_by_ids(recording_ids) def get_recordings_by_account_id( account_id: int, limit: int, search_term: str | None, contract_id: int | None = None, ) -> list: """Get recordings by account id with optional search and limit. Args: account_id (int): ID of the account limit (int): Maximum number of recordings to return search_term (str | None): Search term to filter recordings by title contract_id (int | None): The id of a contract to filter by Returns: list: List of recording options """ contract_type = models.Contract.get_contract_type_by_account( account_id).contract_type if contract_type == ContractType.NEIGHBOURING_RIGHTS: return models.RevenueByRecordingNr.get_recordings_by_account_id( account_id, limit, search_term, contract_id) else: return models.RevenueByRecordingDistro.get_recordings_by_account_id( account_id, limit, search_term, contract_id)