"""Payment Processor.""" from typing import Dict, List from config import app_logger as logger from src.connectors.ows_account import get_eligible_accounts from src.connectors.ows_royalties import get_payment_entities from src.constants import ( NO_COUNTRY_OF_TAX_RESIDENCE_ERR, NO_ELIGIBLE_ACCOUNTS_ERR, NO_PAYMENT_ENTITY_ERR, NO_PAYMENT_ENTITY_TAX_POLICY_MAPPING_ERR, ) from src.models import Account, Event, PaymentGroup from src.processors.base.payment_batch_processor import PaymentBatchProcessor from src.processors.base.processor import Processor from src.processors.models import EligibleAccountLevelData from src.utils import create_payment_entity_to_country_of_tax_reporting_mapping class PayoneerProcessor(Processor): """Processor to calculate worksheets for all eligible accounts in a payment paid by payoneer.""" def __init__(self, payment_group: PaymentGroup, abacus_event: Event) -> None: """Init.""" super().__init__(payment_group, abacus_event) self._all_eligible_accounts: List[EligibleAccountLevelData] = [] self._payment_entity_policy_country_mapping: Dict[int, str] = {} def _set_total_count(self) -> None: """Set total number of accounts to be processed""" self._total_count = len(self._all_eligible_accounts) def _set_all_eligible_accounts(self) -> None: """Set all eligible accounts to be processed""" self._all_eligible_accounts = self._get_payment_group_accounts() def _set_payment_entity_policy_country_mapping(self) -> None: """Set mapping payment_entity_id to country_of_tax_policy""" self._payment_entity_policy_country_mapping = self._get_payment_entity_mapping() def _get_eligible_account_batch_data( self, offset: int = 0 ) -> List[EligibleAccountLevelData]: """Will contain logic for fetching eligible accounts.""" return self._all_eligible_accounts[offset : offset + self._limit] def _process_batch(self, offset: int = 0) -> None: """Fetch the given batch of accounts and create worksheets for them.""" eligible_accounts = self._get_eligible_account_batch_data(offset) PaymentBatchProcessor( self._abacus_event, eligible_accounts, payment_entity_policy_country_mapping=self._payment_entity_policy_country_mapping, ).process() def process(self) -> None: """Process all worksheets for eligible accounts in payment group.""" logger.info('Process with new refactored PayoneerProcessor') self._set_payment_entity_policy_country_mapping() if not self._payment_entity_policy_country_mapping: logger.info( NO_PAYMENT_ENTITY_TAX_POLICY_MAPPING_ERR.format( self._payment_group.payment_group_id ) ) return self._set_all_eligible_accounts() self._set_total_count() if self._total_count == 0: logger.info( NO_ELIGIBLE_ACCOUNTS_ERR.format(self._payment_group.payment_group_id) ) return for offset in range(0, self._total_count, self._limit): self._process_batch(offset) def _get_payment_group_accounts(self) -> List[EligibleAccountLevelData]: """Fetch all eligible accounts to be processed""" accounts = get_eligible_accounts(self._payment_group.payment_group_id) return [ EligibleAccountLevelData( account_id=account.account_id, country_of_tax_residence=account.country_of_tax_residence, country_of_tax_policy=None, currency_code=account.currency_code, ) for account in accounts if self._is_account_eligible(account) ] def _get_payment_entity_mapping(self) -> Dict[int, str]: """Get mapping of payment_entity_id to country_of_tax_policy for all eligible accounts.""" ref_payment_entities = get_payment_entities() payment_entity_policy_country_mapping = ( create_payment_entity_to_country_of_tax_reporting_mapping( # noqa: E501 ref_payment_entities ) ) return payment_entity_policy_country_mapping def _is_account_eligible(self, account: Account) -> bool: if not account.country_of_tax_residence: logger.error(NO_COUNTRY_OF_TAX_RESIDENCE_ERR.format(account.account_id)) return False return True