"""LedgerContractFlowthrough model. Model for managing ledger entries for contract_flowthrough. """ from typing import Any, Iterable, Tuple from abacus_common_logic.connectors.database import db from abacus_common_logic.models.base import BaseModel from ledger.models.ledger_contract_flowthrough_current_balance import ( LedgerContractFlowthroughCurrentBalance, ) class LedgerContractFlowthrough(BaseModel): """LedgerContractFlowthrough model.""" __tablename__ = 'ledger_contract_flowthrough' ledger_contract_flowthrough_id = db.Column(db.Integer, primary_key=True) account_id = db.Column(db.Integer, nullable=False) contract_id = db.Column(db.Integer, nullable=False) abacus_event_id = db.Column(db.Integer, nullable=False) currency_code = db.Column(db.String(3), nullable=False) currency_amount = db.Column(db.Numeric(20, 2), nullable=False) previous_balance = db.Column(db.Numeric(20, 2), nullable=False) current_balance = db.Column(db.Numeric(20, 2), nullable=False) note = db.Column(db.String(255), nullable=True) ledger_contract_flowthrough_current_balance = db.relationship( 'LedgerContractFlowthroughCurrentBalance', backref='LedgerContractFlowthrough', cascade='all, delete-orphan', uselist=False, ) @classmethod def get_ledger_contract_flowthrough_balance_by_contracts( cls, contract_ids: Iterable[int] ): """Get contract flowthrough current balances by contract_ids. Args: contract_ids: a list of contract ids """ return cls.query.join( LedgerContractFlowthroughCurrentBalance, LedgerContractFlowthroughCurrentBalance.ledger_contract_flowthrough_id == cls.ledger_contract_flowthrough_id, ).filter(cls.contract_id.in_(contract_ids))