def _validate_vat_id_EU(vat_id, country_code): if vat_id[:2] != cc_to_vat_prefix(country_code): raise VATIDFinalError( _('Your VAT ID does not match the selected country.')) try: result = vat_moss.id.validate(vat_id) if result: country_code, normalized_id, company_name = result return normalized_id except (vat_moss.errors.InvalidError, ValueError): raise VATIDFinalError( _('This VAT ID is not valid. Please re-check your input.')) except vat_moss.errors.WebServiceUnavailableError: logger.exception( 'VAT ID checking failed for country {}'.format(country_code)) raise VATIDTemporaryError( _('Your VAT ID could not be checked, as the VAT checking service of ' 'your country is currently not available. We will therefore ' 'need to charge VAT on your invoice. You can get the tax amount ' 'back via the VAT reimbursement process.')) except (vat_moss.errors.WebServiceError, HTTPError): logger.exception( 'VAT ID checking failed for country {}'.format(country_code)) raise VATIDTemporaryError( _('Your VAT ID could not be checked, as the VAT checking service of ' 'your country returned an incorrect result. We will therefore ' 'need to charge VAT on your invoice. Please contact support to ' 'resolve this manually.'))
def clean(self): data = self.cleaned_data if not data.get('is_business'): data['company'] = '' data['vat_id'] = '' if data.get('is_business') and not is_eu_country(data.get('country')): data['vat_id'] = '' if self.event.settings.invoice_address_required: if data.get('is_business') and not data.get('company'): raise ValidationError(_('You need to provide a company name.')) if not data.get('is_business') and not data.get('name_parts'): raise ValidationError(_('You need to provide your name.')) if 'vat_id' in self.changed_data or not data.get('vat_id'): self.instance.vat_id_validated = False if data.get('city') and data.get('country') and str(data['country']) in COUNTRIES_WITH_STATE_IN_ADDRESS: if not data.get('state'): self.add_error('state', _('This field is required.')) self.instance.name_parts = data.get('name_parts') if all( not v for k, v in data.items() if k not in ('is_business', 'country', 'name_parts') ) and len(data.get('name_parts', {})) == 1: # Do not save the country if it is the only field set -- we don't know the user even checked it! self.cleaned_data['country'] = '' if self.validate_vat_id and self.instance.vat_id_validated and 'vat_id' not in self.changed_data: pass elif self.validate_vat_id and data.get('is_business') and is_eu_country(data.get('country')) and data.get('vat_id'): if data.get('vat_id')[:2] != cc_to_vat_prefix(str(data.get('country'))): raise ValidationError(_('Your VAT ID does not match the selected country.')) try: result = vat_moss.id.validate(data.get('vat_id')) if result: country_code, normalized_id, company_name = result self.instance.vat_id_validated = True self.instance.vat_id = normalized_id except (vat_moss.errors.InvalidError, ValueError): raise ValidationError(_('This VAT ID is not valid. Please re-check your input.')) except vat_moss.errors.WebServiceUnavailableError: logger.exception('VAT ID checking failed for country {}'.format(data.get('country'))) self.instance.vat_id_validated = False if self.request and self.vat_warning: messages.warning(self.request, _('Your VAT ID could not be checked, as the VAT checking service of ' 'your country is currently not available. We will therefore ' 'need to charge VAT on your invoice. You can get the tax amount ' 'back via the VAT reimbursement process.')) except (vat_moss.errors.WebServiceError, HTTPError): logger.exception('VAT ID checking failed for country {}'.format(data.get('country'))) self.instance.vat_id_validated = False if self.request and self.vat_warning: messages.warning(self.request, _('Your VAT ID could not be checked, as the VAT checking service of ' 'your country returned an incorrect result. We will therefore ' 'need to charge VAT on your invoice. Please contact support to ' 'resolve this manually.')) else: self.instance.vat_id_validated = False