"""Ledger Correction model.""" from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy import Numeric from sqlalchemy import String from sqlalchemy import Text from moneyhub.constants.constants import CorrectionType from moneyhub.models.mysql_base import BaseModel class LedgerCorrection(BaseModel): """Ledger correction model.""" __tablename__ = 'ledger_correction' ledger_correction_id = Column(Integer, primary_key=True) abacus_event_id = Column(Integer, nullable=False) worksheet_correction_id = Column(Integer, nullable=False) account_id = Column(Integer, nullable=False) contract_id = Column(Integer, nullable=True) statement_period_id = Column(Integer, nullable=False) correction_statement_period_id = Column(Integer, nullable=False) correction_type = Column( Enum( *CorrectionType, name='correction_type', create_type=False ), nullable=False ) currency_code = Column(String(3), nullable=False) gross_revenue = Column(Numeric(20, 2), nullable=False) distribution_fee = Column(Numeric(20, 2), nullable=False) mechanical_deduction_total = Column(Numeric(20, 2), nullable=True) mechanical_deduction_admin_fee_total = Column(Numeric(20, 2), nullable=True) net_revenue = Column(Numeric(20, 2), nullable=False) note = Column(Text, nullable=True) created_at = Column(DateTime, nullable=False) created_by = Column(String(255), nullable=False) last_modified = Column(DateTime, nullable=False) last_modified_by = Column(String(255), nullable=False) @classmethod def get_for_account( cls, account_id: int, contract_id: int | None = None, visible_periods: list[int] | None = None ) -> list: """Get the ledger corrections for account & statement period. Args: account_id (int): Account to get balance for. contract_id (int): Optional contract to filter results by. visible_periods (list): list of visible statement periods to filter by Returns: list: List of corrections. """ filters = [ cls.account_id == account_id ] if visible_periods: filters.append(cls.statement_period_id.in_(visible_periods)) if contract_id: filters.append(cls.contract_id == contract_id) return cls.query.filter(*filters).all()