"""Payment Group Payment Account Detail model.""" from dataclasses import dataclass from decimal import Decimal import typing from abacus_common_logic.models.base import BaseModel, db from abacus_common_logic.utils.users import get_flask_user_id from sqlalchemy import delete, select, update from payment.models.payment_group_payment_account import PaymentGroupPaymentAccount class PaymentGroupPaymentAccountDetail(BaseModel): """Payment Group Payment Account Detail model.""" __tablename__ = 'payment_group_payment_account_detail' payment_group_payment_account_detail_id = db.Column(db.Integer, primary_key=True) payment_group_payment_account_id = db.Column( db.Integer, db.ForeignKey('payment_group_payment_account.payment_group_payment_account_id'), nullable=False, ) worksheet_account_contract_payable_after_tax_id = db.Column( db.Integer, db.ForeignKey( 'worksheet_account_contract_payable_after_tax.' 'worksheet_account_contract_payable_after_tax_id' ), nullable=False, ) account_id = db.Column(db.Integer, nullable=False) contract_id = db.Column(db.Integer, nullable=False) payable_amount_pre_tax = db.Column(db.Numeric(20, 2), nullable=False, default=0) tax_withholding_amount = db.Column(db.Numeric(20, 2), nullable=True) vat_amount = db.Column(db.Numeric(20, 2), nullable=True) payable_amount_post_tax = db.Column(db.Numeric(20, 2), nullable=False, default=0) currency_code = db.Column(db.String(3), nullable=False) deleted_at = db.Column(db.DateTime, nullable=True) deleted_by = db.Column(db.String(255), nullable=True) @classmethod def soft_delete_by_payment_group_payment( cls, payment_group_payment_id, commit: bool = True ): """Delete items by payment_group_payment_id. Args: payment_group_payment_id: ID of the payment group payment commit: whether to commit the transaction """ db.session.execute( update(cls) .where( cls.payment_group_payment_account_id.in_( select( PaymentGroupPaymentAccount.payment_group_payment_account_id ).where( PaymentGroupPaymentAccount.payment_group_payment_id == payment_group_payment_id ) ) ) .values( deleted_at=cls.current_timestamp(), deleted_by=get_flask_user_id(), ) .execution_options(synchronize_session=False) ) if commit: db.session.commit() @classmethod def soft_delete_by_payment_group_payment_account( cls, payment_group_payment_account_id ): """Delete items by payment_group_payment_account_id.""" db.session.execute( update(cls) .where( cls.payment_group_payment_account_id == payment_group_payment_account_id ) .values( deleted_at=cls.current_timestamp(), deleted_by=get_flask_user_id(), ) .execution_options(synchronize_session=False) ) db.session.commit() @classmethod def hard_delete_by_payment_group_payment( cls, payment_group_payment_id, commit: bool = True ): """Hard delete items by payment_group_payment_id. Args: payment_group_payment_id: ID of the payment group payment commit: whether to commit the transaction """ db.session.execute( delete(cls) .where( cls.payment_group_payment_account_id.in_( select( PaymentGroupPaymentAccount.payment_group_payment_account_id ).where( PaymentGroupPaymentAccount.payment_group_payment_id == payment_group_payment_id ) ) ) .execution_options(synchronize_session=False) ) if commit: db.session.commit() @classmethod def hard_delete_by_payment_group_payment_account( cls, payment_group_payment_account_id ): """Hard delete items by payment_group_payment_account_id.""" db.session.execute( delete(cls) .where( cls.payment_group_payment_account_id == payment_group_payment_account_id ) .execution_options(synchronize_session=False) ) db.session.commit() @classmethod def get_by_payment_group_payment_account( cls, payment_group_payment_account_id: int ) -> typing.Iterable[typing.Self]: """Get items by payment_group_payment_account_id.""" return ( db.session.execute( select(cls).where( cls.payment_group_payment_account_id == payment_group_payment_account_id ) ) .scalars() .all() ) @dataclass class PaymentDebitCreditDataEntry: """Data class for Debit/Credit data entries.""" account_id: int contract_id: int currency_code: str amount: Decimal worksheet_account_contract_payable_after_tax_id: int