"""AccountTaxInfo marshmallow schemas.""" from abacus_common_logic.marshalling.custom_fields import ma from marshmallow import ValidationError, validate, validates_schema from abacus_account.constants.constants import ( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, TAX_EMPLOYMENT_TYPES, ) from abacus_account.constants.error import ERROR_START_END_DATE class AccountTaxInfoPutSchema(ma.Schema): """AccountTaxInfo PUT body.""" country_of_tax_residence = ma.NonemptyString( validate=[validate.Length(equal=3)], allow_none=True ) is_sba_signed = ma.Boolean() is_vat_exempt = ma.Boolean() is_tax_treaty_claimed = ma.Boolean() tax_employment_type = ma.Enum(options=TAX_EMPLOYMENT_TYPES, allow_none=True) certificate_of_residence_expiration_date = ma.FormattedDate(allow_none=True) is_wht_applicable = ma.Boolean() is_resident_of_spanish_islands = ma.Boolean(allow_none=True) wht_rate_override = ma.Decimal(as_string=True, allow_none=True) class AccountTaxInfoDetailSchema(AccountTaxInfoPutSchema): """AccountTaxInfo fields.""" country_of_tax_residence = ma.NonemptyString( required=False, allow_none=True, validate=[validate.Length(equal=3)] ) account_tax_info_id = ma.IntegerId(required=True) account_id = ma.IntegerId(required=True) is_vat_exempt = ma.Boolean() class AccountTaxInfoListInputSchema(ma.Schema): """AccountTaxInfo list inout schema.""" certificate_of_residence_expiration_date_start = ma.FormattedDate(allow_none=True) certificate_of_residence_expiration_date_end = ma.FormattedDate(allow_none=True) limit = ma.Integer( load_default=DEFAULT_PAGE_LIMIT, validate=[validate.Range(min=1)] ) offset = ma.Integer( load_default=DEFAULT_PAGE_OFFSET, validate=[validate.Range(min=0)] ) account_ids = ma.List(ma.IntegerId(), allow_none=True) @validates_schema def validate_dates(self, data, **kwargs): """Validate that start date is before end date.""" start = data.get('certificate_of_residence_expiration_date_start') end = data.get('certificate_of_residence_expiration_date_end') if start and end and start > end: raise ValidationError(ERROR_START_END_DATE)