"""Generate a neightbouring rights perfomer revenue report.""" from decimal import Decimal from os import path import config from src.connectors import snowflake from src.connectors.ows_abacus_account import OwsAbacusAccount from src.connectors.ows_royalties import OwsRoyalties from src.documents.utils import create_report_file from src.documents.utils import sanitize_filename from src.documents.utils import sanitize_value from src.utils.constants import CONTRIBUTOR_ONLY_SCHEDULE_MSG # Mapping of column headers to database columns _COLUMN_MAPPING = { 'Statement': 'STATEMENT_PERIOD_NAME', 'Contract ID': 'CONTRACT_ID', 'Contracting Party': 'account_name', 'Contributor Name': 'CONTRIBUTOR_NAME', 'ISRC': 'ISRC', 'Recording Title': 'SOUND_RECORDING_NAME', 'Recording Version': 'VERSION', 'Recording Artist': 'MAIN_ARTIST', 'Collection Society': 'CUSTOMER_NAME', 'Country': 'COUNTRYNAME', 'Transaction Type': 'TRANSACTIONTYPEDESC', 'Transaction Subtype': 'TRANSACTION_SUBTYPE_DESCRIPTION', 'Usage Start Date': 'START_DATE', 'Usage End Date': 'END_DATE', 'Pre-WHT Amount': 'GROSS_REVENUE_PAYEE_CURRENCY', 'WHT Amount': 'WITHHOLDING_TAX_PAYEE_CURRENCY', 'Gross Revenue': 'GROSS_REVENUE_AFTER_WITHHOLDING_TAX_PAYEE_CURRENCY', 'Client Share %': 'ROYALTY_RATE', 'Commission': 'commission', 'Net Revenue': 'NET_SHARE_PAYEE_CURRENCY', 'Currency': 'ACCOUNT_PAYEE_CURRENCY', 'Schedule Name': 'SCHEDULE_NAME', 'Royalty Type': 'ABACUS_SALE_TYPE', 'Original Statement Period': 'ORIGINAL_STATEMENT_PERIOD', } def _process_row(row: dict, account: dict) -> list: """Process a raw fact_sales row into a row for the CSV file. Args: row (dict): Raw fact_sales row. account (dict): Account the row belongs to. Returns: list: Row for the CSV file. """ row['account_name'] = account['account_name'] row['commission'] = Decimal( row['GROSS_REVENUE_AFTER_WITHHOLDING_TAX_PAYEE_CURRENCY'] ) - Decimal(row['NET_SHARE_PAYEE_CURRENCY']) # noqa: E501 # Fall back to old proxy value if schedule name not present or contribution if not row.get('SCHEDULE_NAME') or row['TERM_TYPE'] == 'contribution': row['SCHEDULE_NAME'] = row['CONTRACT_TERM_NAME'] # Custom mapping required for contributor-only schedules if row['TERM_TYPE'] == 'contributor_schedule': row['SCHEDULE_NAME'] = CONTRIBUTOR_ONLY_SCHEDULE_MSG return [sanitize_value(row[column], True) for column in _COLUMN_MAPPING.values()] def build_document(account_id: int, contract_id: int, statement_period_id: int) -> str: """Generate the neighbouring rights revenue detail report document. Args: account_id (int): Account to generate the report for. contract_id (int): Contract to generate the report for. statement_period_id (int): Statement period ID. Returns: str: Local path to the generated file. """ account = OwsAbacusAccount.get_account(account_id) account_name = sanitize_filename(account['account_name']) account_name = account_name if len(account_name) <= 50 else account_name[:50] statement_period_name = OwsRoyalties.get_statement_period(statement_period_id)[ 'statement_period_name' ] statement_period_name = sanitize_filename(statement_period_name) destination_path = path.join( config.FILE_OUTPUT_PATH, f'{account_name}_{contract_id}_{statement_period_name}_nr_revenue_details.csv', ) fact_sales, total_rows = snowflake.get_neighbouring_rights_performer_fact_sales( statement_period_id, account_id, contract_id ) return create_report_file( list(_COLUMN_MAPPING.keys()), fact_sales, total_rows, destination_path, lambda row: _process_row(row, account), )