"""Payments Model.""" from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import Numeric from sqlalchemy import String from moneyhub.constants.constants import ActionStatus from moneyhub.models.snowflake_base import BaseModel class Payments(BaseModel): """Payments model.""" __tablename__ = 'payments_dbt' ledger_account_id = Column(Numeric(12, 0), nullable=False, primary_key=True) 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=False) 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) @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()