"""Combined Payments Model.""" from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import Numeric from sqlalchemy import String from sqlalchemy.engine import Row from moneyhub.constants.constants import ActionStatus from moneyhub.models.snowflake_base import BaseModel class CombinedPayments(BaseModel): """Combined payments model.""" __tablename__ = 'combined_payments_dbt' account_id = Column(Numeric(12, 0), nullable=False) contract_id = Column(Numeric(12, 0), nullable=False, primary_key=True) currency_code = Column(String(50), nullable=False) currency_amount = Column(Numeric(36, 12), nullable=False) created_at = Column(DateTime, nullable=True) statement_period_id = Column(Numeric(38, 0), nullable=False) event_name = Column(String(32), nullable=False) action_status = Column( Enum( *ActionStatus, name='action_status', create_type=False ), nullable=False) withholding_tax_ledger_account_id = Column(Numeric(12, 0), nullable=True) withholding_tax_currency_code = Column(String(50), nullable=True) withholding_tax_currency_amount = Column(Numeric(36, 12), nullable=True) withholding_tax_created_at = Column(DateTime, nullable=True) unique_key = Column(String(32), nullable=False, primary_key=True) @classmethod def get_payments_by_account_and_statement_periods( cls, account_id: int, statement_period_ids: list[int], contract_id: int | None ) -> list: """Get a payee's applied payments by statement periods. Args: account_id (int): The id of an account contract_id (int): Optional id of the contract statement_period_ids (int): The id of the statement period Returns: dict: dict of applied payments """ filters = [ cls.account_id == account_id, cls.statement_period_id.in_(statement_period_ids), ] if contract_id: filters.append(cls.contract_id == contract_id) return cls.query.filter(*filters).all() @classmethod def get_withholding_tax_payment_by_account(cls, account_id: int) -> Row: """Get payment with non-null withholding_tax_currency_amount for an account. Args: account_id (int): The id of an account Returns: Row: non-null withholding_tax_currency_amount entity """ return ( cls.query .filter( cls.account_id == account_id, cls.withholding_tax_currency_amount.isnot(None) ) .first() )