"""Accounting Run model.""" from abacus_common_logic.connectors.database import db from abacus_common_logic.models import BaseModel, NormalizedDateTime from sqlalchemy import Enum from sqlalchemy.orm import backref from royalties.constants import constants from royalties.models.accounting_period import AccountingPeriod from royalties.models.run_controller import RunController class AccountingRun(BaseModel): """Accounting Run model.""" __tablename__ = 'accounting_run' accounting_run_id = db.Column(db.Integer, primary_key=True) accounting_period_id = db.Column( db.Integer, db.ForeignKey('accounting_period.accounting_period_id'), nullable=False, ) accounting_period = db.relationship( 'AccountingPeriod', backref=backref('accounting_runs', cascade='all, delete-orphan'), lazy='joined', ) run_controller_id = db.Column( db.Integer, db.ForeignKey('run_controller.run_controller_id'), nullable=False ) run_controller = db.relationship( 'RunController', backref='accounting_run', lazy='joined' ) run_status = db.Column( Enum(*constants.ACCOUNTING_RUN_STATUSES, name='run_status', create_type=False), nullable=False, server_default=constants.ACCOUNTING_RUN_STATUSES.NO_ACTION_TAKEN, ) start_date = db.Column(NormalizedDateTime(), nullable=True) end_date = db.Column(NormalizedDateTime(), nullable=True) summary_export_url = db.Column(db.String(255), nullable=True) @classmethod def get_page(cls, accounting_period_id): """Get page of accounting run's.""" return ( db.session.query(AccountingRun) .join(RunController) .join(AccountingPeriod) .filter_by(accounting_period_id=accounting_period_id) .order_by(RunController.run_controller_name) ) @classmethod def get_by_accounting_period_and_run_controller( cls, accounting_period_id, run_controller_id ): """Get accounting runs by accounting_period_id and run_controller_id.""" return cls.query.filter( cls.accounting_period_id == accounting_period_id, cls.run_controller_id == run_controller_id, ).all()