"""Schema for Ledger Adjustment serialization.""" from abacus_common_logic.marshalling.custom_fields import ma from marshmallow import ValidationError, validates_schema from ledger.constants.constants import DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET from ledger.constants.error import ERROR_INVALID_STATEMENT_PERIOD_RANGE class LedgerAdjustmentDetailSchema(ma.Schema): """Ledger Adjustment Detail schema.""" ledger_adjustment_detail_id = ma.NonNegativeInteger(required=True) account_id = ma.NonNegativeInteger(required=True) contract_id = ma.NonNegativeInteger(required=True) activity_statement_period_id = ma.NonNegativeInteger(required=True) apply_to_statement_period_id = ma.NonNegativeInteger(required=True) reference_adjustment_type_id = ma.NonNegativeInteger(required=True) currency_code = ma.NonemptyString(required=True) amount = ma.Decimal(as_string=True, required=True) upc = ma.NonemptyString(required=True) distribution_type = ma.NonemptyString(required=True) note = ma.NonemptyString(required=True) class LedgerAdjustmentSchema(ma.Schema): """Ledger Adjustment schema.""" ledger_adjustment_id = ma.NonNegativeInteger(required=True) abacus_event_id = ma.NonNegativeInteger(required=True) account_id = ma.NonNegativeInteger(required=True) contract_id = ma.NonNegativeInteger(allow_none=True) activity_statement_period_id = ma.NonNegativeInteger(required=True) apply_to_statement_period_id = ma.NonNegativeInteger(required=True) reference_adjustment_type_id = ma.NonNegativeInteger(required=True) adjustment_amount = ma.Decimal(as_string=True, required=True) adjustment_currency_code = ma.NonemptyString(required=True) note = ma.NonemptyString(allow_none=True) details = ma.List(ma.Nested(LedgerAdjustmentDetailSchema), required=False) class LedgerAdjustmentExtendedSchema(LedgerAdjustmentSchema): """Ledger Adjustment schema for getting items by statement period. Adds an account_currency_code, derived from the related account_payment_term """ account_currency_code = ma.NonemptyString(required=True) class LedgerAdjustmentListSchema(LedgerAdjustmentSchema): """Ledger Adjustment List schema.""" adjustment_ledger_status = ma.NonemptyString(required=True) adjustment_type = ma.NonemptyString(required=True) class LedgerAdjustmentFilterSchema(ma.Schema): """Ledger Adjustment Filter schema.""" account_id = ma.NonNegativeInteger(allow_none=True) start_apply_to_statement_period_id = ma.NonNegativeInteger(allow_none=True) end_apply_to_statement_period_id = ma.NonNegativeInteger(allow_none=True) show_only_applied_adjustments = ma.Bool(allow_none=True) limit = ma.NonNegativeInteger(missing=DEFAULT_PAGE_LIMIT) offset = ma.NonNegativeInteger(missing=DEFAULT_PAGE_OFFSET) @validates_schema def validate_statement_period_range(self, data, **kwargs): """Validate the statement period range.""" start_apply_to_statement_period_id = data.get( 'start_apply_to_statement_period_id' ) end_apply_to_statement_period_id = data.get('end_apply_to_statement_period_id') if ( start_apply_to_statement_period_id and end_apply_to_statement_period_id ) and (start_apply_to_statement_period_id > end_apply_to_statement_period_id): raise ValidationError(ERROR_INVALID_STATEMENT_PERIOD_RANGE)