"""Schema for Ledger Account serialization.""" from abacus_common_data.currency import get_currency_object_from_code from abacus_common_logic.marshalling.custom_fields import ma from abacus_common_logic.marshalling.custom_validations import not_blank from marshmallow import fields class LedgerAccountSchema(ma.Schema): """Shared Ledger fields.""" ledger_account_id = ma.IntegerId(required=True) currency_amount = ma.Decimal(required=True, as_string=True) currency_code = ma.NonemptyString(required=True) current_balance = ma.Decimal(required=True, as_string=True) abacus_event_id = ma.NonNegativeInteger(required=True) note = ma.String(custom_validations=[not_blank]) account_id = ma.NonNegativeInteger(required=True) contract_id = ma.NonNegativeInteger() previous_balance = ma.Decimal(required=True, as_string=True) class LedgerAccountCurrentBalanceSchema(ma.Schema): """GET response of current balance.""" account_id = ma.NonNegativeInteger() current_balance = ma.Decimal(as_string=True) currency_code = ma.NonemptyString() currency_name = ma.NonemptyString() class LedgerAccountListSchema(ma.Schema): """Ledger Account list response.""" ledger_account_id = ma.IntegerId() account_id = ma.NonNegativeInteger() accounting_period_id = ma.NonNegativeInteger(allow_none=True) amount = ma.Decimal(as_string=True) contract_id = ma.NonNegativeInteger(allow_none=True) currency_code = ma.NonemptyString() currency_name = fields.Method('currency_data') date = ma.FormattedDate() ending_balance = ma.Decimal(as_string=True) opening_balance = ma.Decimal(as_string=True) statement_period_id = ma.NonNegativeInteger() transaction_type = ma.NonemptyString() @staticmethod def currency_data(obj): """Get Currency Name.""" return get_currency_object_from_code(obj['currency_code'])['currency_name']