def test_dk__country_code(self): """check_vat_number('..', country_code='..') """ for country_code, cases in VAT_NUMBER_CHECK_CASES.items(): for vat_number, expected in cases: self.assert_result_equals( expected, check_vat_number(vat_number, country_code)) self.assert_result_equals( expected, check_vat_number('%s%s' % (country_code, vat_number), country_code))
def test_dk__country_code(self): """check_vat_number('..', country_code='..') """ for country_code, cases in VAT_NUMBER_CHECK_CASES.items(): for vat_number, expected in cases: self.assert_result_equals( expected, check_vat_number(vat_number, country_code) ) self.assert_result_equals( expected, check_vat_number('%s%s' % (country_code, vat_number), country_code) )
def validate_vat_id(vat_id, country_code: str = None) -> [bool, str]: if not country_code: return False, _('Did not receive country code') if country_code == 'CH': # validate Swiss business number, e.g. 'CHE-107.787.577' # or VAT ID number, which is the business number with one of the suffixes: # IVA, TVA, MWST, TPV, e.g. 'CHE-107.787.577 TVA' if ch_uid.is_valid(vat_id) or ch_vat.is_valid(vat_id): return True, _('Valid VAT ID') else: return False, _('Invalid VAT ID') elif country_code not in getattr(settings, 'EU_COUNTRIES'): return True, 'Non EU country' else: try: if country_code != vat_id[0:2]: return False, _( 'VAT ID country prefix do not match your selected country') if vat_id and not check_vat_number( vat_number=vat_id, country_code=country_code).is_valid: return False, _('Invalid VAT ID format') else: return True, _('Valid VAT ID') except Exception as e: LOG.debug(e) return False, _('Invalid VAT ID format')
def is_valid_vies_vat(vat_num: str) -> bool: try: res = pyvat.check_vat_number(vat_num, None).is_valid if res == True: return True else: return False except Exception as e: return False
def validate(self, attrs): country = attrs.get('country') vat_code = attrs.get('vat_code') if vat_code: # Check VAT format if not pyvat.is_vat_number_format_valid(vat_code, country): raise serializers.ValidationError( {'vat_code': _('VAT number has invalid format.')} ) # Check VAT number in EU VAT Information Exchange System # if customer is new or either VAT number or country of the customer has changed if ( not self.instance or self.instance.vat_code != vat_code or self.instance.country != country ): check_result = pyvat.check_vat_number(vat_code, country) if check_result.is_valid: attrs['vat_name'] = check_result.business_name attrs['vat_address'] = check_result.business_address if not attrs.get('contact_details'): attrs['contact_details'] = attrs['vat_address'] elif check_result.is_valid is False: raise serializers.ValidationError( {'vat_code': _('VAT number is invalid.')} ) else: logger.debug( 'Unable to check VAT number %s for country %s. Error message: %s', vat_code, country, check_result.log_lines, ) raise serializers.ValidationError( {'vat_code': _('Unable to check VAT number.')} ) return attrs