"""Payment Minimum Model.""" from abacus_common_data.currency import get_currency_object_from_code from abacus_common_logic.constants import error from abacus_common_logic.models.base import BaseModel, db from abacus_common_logic.utils.dates import current_timestamp from flask import abort, g from sqlalchemy import func, select, update from payment.constants import constants class PaymentMinimum(BaseModel): """Payment Minimum model.""" __tablename__ = 'payment_minimum' payment_minimum_id = db.Column(db.Integer, primary_key=True) currency_code = db.Column(db.String(3), nullable=False) check_amount = db.Column(db.Numeric(6, 2), nullable=False) wire_transfer_amount = db.Column(db.Numeric(6, 2), nullable=False) western_union_amount = db.Column(db.Numeric(6, 2), nullable=True) @property def currency_name(self): """Class property to represent currency_name.""" return get_currency_object_from_code(self.currency_code)['currency_name'] @classmethod def bulk_update(cls, minimums): """Update one or more payment minimums.""" last_modified = current_timestamp() for minimum in minimums: pk = minimum['payment_minimum_id'] values = {k: v for k, v in minimum.items() if k != 'payment_minimum_id'} values['last_modified'] = last_modified values['last_modified_by'] = g.user_details.get('id') db.session.execute( update(cls).where(cls.payment_minimum_id == pk).values(**values) ) db.session.commit() @classmethod def default_order(cls): """Override default ordering.""" return func.lower(cls.payment_minimum_id) @classmethod def get_all(cls): """Get all payment minimums.""" return ( db.session.execute(select(cls).order_by(cls.default_order())) .scalars() .all() ) @classmethod def get_all_by_currency(cls): """Retrieve all existing payment minimum amounts.""" return { item.currency_code: { constants.PAYMENT_METHODS.CHECK: item.check_amount, constants.PAYMENT_METHODS.WESTERN_UNION: item.western_union_amount, constants.PAYMENT_METHODS.WIRE_TRANSFER: item.wire_transfer_amount, } for item in cls.get_all() } @classmethod def get_by_currency_code(cls, currency_code) -> 'PaymentMinimum | None': """Find object by currency_code.""" return ( db.session.execute(select(cls).where(cls.currency_code == currency_code)) .scalars() .first() )