def _get_vies_data(self, vat): res = {} vat = vat.strip().upper() vat_country, vat_number = self._split_vat(vat) result = check_vies(vat) # Raise error if partner is not listed on Vies if result.name is None: raise ValidationError( _("The partner is not listed on Vies " "Webservice.")) res['vat'] = vat res['vat_subjected'] = result.valid # Update partner name if listed on VIES if result.name != '---': res['name'] = result.name.upper() # Update partner address if listed on VIES if result.address != '---': res['street'] = \ result.address.replace('\n', ' ').replace('\r', '').title() # Get country by country code country = self.env['res.country'].search([('code', 'ilike', vat_country)]) if country: res['country_id'] = country[0].id return res
def transition_check(self): Party = Pool().get('party.party') parties_succeed = [] parties_failed = [] parties = Party.browse(Transaction().context.get('active_ids')) for party in parties: for identifier in party.identifiers: if identifier.type != 'eu_vat': continue try: if not vat.check_vies(identifier.code): parties_failed.append(party.id) else: parties_succeed.append(party.id) except Exception, e: if hasattr(e, 'faultstring') \ and hasattr(e.faultstring, 'find'): if e.faultstring.find('INVALID_INPUT'): parties_failed.append(party.id) continue if e.faultstring.find('SERVICE_UNAVAILABLE') \ or e.faultstring.find('MS_UNAVAILABLE') \ or e.faultstring.find('TIMEOUT') \ or e.faultstring.find('SERVER_BUSY'): self.raise_user_error('vies_unavailable') raise
def transition_check(self): Party = Pool().get('party.party') parties_succeed = [] parties_failed = [] parties = Party.browse(Transaction().context.get('active_ids')) for party in parties: for identifier in party.identifiers: if identifier.type != 'eu_vat': continue try: if not vat.check_vies(identifier.code): parties_failed.append(party.id) else: parties_succeed.append(party.id) except Exception, e: if hasattr(e, 'faultstring') \ and hasattr(e.faultstring, 'find'): if e.faultstring.find('INVALID_INPUT'): parties_failed.append(party.id) continue if e.faultstring.find('SERVICE_UNAVAILABLE') \ or e.faultstring.find('MS_UNAVAILABLE') \ or e.faultstring.find('TIMEOUT') \ or e.faultstring.find('SERVER_BUSY'): self.raise_user_error('vies_unavailable') raise
def vies_vat_change(self): def _check_city(lines, country='BE'): if country == 'GB': ukzip = '[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}' if re.match(ukzip, lines[-1]): cp = lines.pop() city = lines.pop() return (cp, city) else: result = re.match('((?:L-|AT-)?[0-9\-]+) (.+)', lines[-1]) if result: lines.pop() return (result.group(1), result.group(2)) return False if stdnum_vat is None: return {} for partner in self: if not partner.vat: return {} if len(partner.vat) > 5 and partner.vat[:2].lower( ) in stdnum_vat.country_codes: result = stdnum_vat.check_vies(partner.vat) if not result['valid']: return {} if (not partner.name) and (result['name'] != '---'): partner.name = result['name'] #parse the address from VIES and fill the partner's data if result['address'] == '---': return {} lines = [x for x in result['address'].split("\n") if x] if len(lines) == 1: lines = [x.strip() for x in lines[0].split(',') if x] if len(lines) == 1: lines = [x.strip() for x in lines[0].split(' ') if x] partner.street = lines.pop(0) if len(lines) > 0: res = _check_city(lines, result['countryCode']) if res: partner.zip = res[0] partner.city = res[1] if len(lines) > 0: partner.street2 = lines.pop(0) country = self.env['res.country'].search( [('code', '=', result['countryCode'])], limit=1) partner.country_id = country and country.id or False
def _siren2vat_vies(self, siren, raise_if_fail=False): vat = "FR%s" % siren_to_vat(siren) logger.info("VIES check of VAT %s" % vat) vies_res = False res = False try: stdnum_version_float = float(stdnum_version) except Exception: stdnum_version_float = 1.8 try: if stdnum_version_float < 1.9: vies_res = check_vies(vat) else: vies_res = check_vies(vat, timeout=5) logger.debug("VIES answer vies_res.valid=%s", vies_res.valid) except Exception as e: logger.error("VIES query failed: %s", e) if raise_if_fail: raise UserError(_("Failed to query VIES.\nTechnical error: %s.") % e) return None if vies_res and vies_res.valid: res = vat return res
def vies_vat_change(self): def _check_city(lines, country='BE'): if country == 'GB': ukzip = '[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}' if re.match(ukzip, lines[-1]): cp = lines.pop() city = lines.pop() return (cp, city) else: result = re.match('((?:L-|AT-)?[0-9\-]+) (.+)', lines[-1]) if result: lines.pop() return (result.group(1), result.group(2)) return False if stdnum_vat is None: return {} for partner in self: if not partner.vat: return {} if len(partner.vat) > 5 and partner.vat[:2].lower() in stdnum_vat.country_codes: result = stdnum_vat.check_vies(partner.vat) if not result['valid']: return {} if (not partner.name) and (result['name'] != '---'): partner.name = result['name'] #parse the address from VIES and fill the partner's data if result['address'] == '---': return {} lines = [x for x in result['address'].split("\n") if x] if len(lines) == 1: lines = [x.strip() for x in lines[0].split(',') if x] if len(lines) == 1: lines = [x.strip() for x in lines[0].split(' ') if x] partner.street = lines.pop(0) if len(lines) > 0: res = _check_city(lines, result['countryCode']) if res: partner.zip = res[0] partner.city = res[1] if len(lines) > 0: partner.street2 = lines.pop(0) country = self.env['res.country'].search([('code', '=', result['countryCode'])], limit=1) partner.country_id = country and country.id or False
def _get_vies_data(self, vat): res = {} vat_country, vat_number = self._split_vat(vat) result = check_vies(vat) if result.name: res["vat"] = vat # Update partner name if listed on VIES if result.name != "---": res["name"] = result.name.upper() # Update partner address if listed on VIES if result.address != "---": res["street"] = (result.address.replace("\n", " ").replace( "\r", "").title()) # Get country by country code country = self.env["res.country"].search([("code", "ilike", vat_country)]) if country: res["country_id"] = country[0].id return res
def button_get_partner_data(self): def _check_vat_ro(vat): return bool( len(part.name.strip()) > 2 and part.name.strip().upper()[:2] == 'RO' and part.name.strip()[2:].isdigit()) part = self[0] vat = part.vat if vat: self.write({'vat': part.vat.upper().replace(" ", "")}) elif part.name and len(part.name.strip()) > 2 and part.name.strip( ).upper()[:2] == 'RO' and part.name.strip()[2:].isdigit(): self.write({'vat': part.name.upper().replace(" ", "")}) if not part.vat and part.name: try: vat_country, vat_number = self._split_vat( part.name.upper().replace(" ", "")) valid = self.vies_vat_check(vat_country, vat_number) if valid: self.write({'vat': part.name.upper().replace(" ", "")}) except: raise Warning(_("No VAT number found")) vat_country, vat_number = self._split_vat(part.vat) if part.vat_subjected: self.write({'vat_subjected': False}) if vat_number and vat_country: self.write({ 'is_company': True, 'country_id': self.env['res.country'].search([('code', 'ilike', vat_country) ])[0].id }) if vat_country == 'ro': values = {} try: ''' result = self._get_Mfinante(vat_number) if result: values = self._Mfinante_to_Odoo(result) self.write(values) ''' result = self._get_Anaf(vat_number) if result: values = self._Anaf_to_Odoo(result) except Exception as e: print(str(e)) values = self._get_Openapi(vat_number) if values: self.write(values) else: try: result = check_vies(part.vat) if result.name and result.name != '---': self.write({ 'name': result.name.upper( ), #unicode(result.name).upper(), 'is_company': True, 'vat_subjected': True }) if (not part.street and result.address and result.address != '---'): self.write({'street': result.address.title() }) # unicode(result.address).title()}) self.write({'vat_subjected': result.valid}) except: self.write({ 'vat_subjected': self.vies_vat_check(vat_country, vat_number) })
def _check_vies(self, vat): # Store the VIES result in the cache. In case an exception is raised during the request # (e.g. service unavailable), the fallback on simple_vat_check is not kept in cache. return check_vies(vat)
def test_check_vies(self): """Test stdnum.eu.vat.check_vies()""" result = vat.check_vies('BE555445') self.assertEqual(result['countryCode'], 'BE') self.assertEqual(result['vatNumber'], '555445')
def test_check_vies(self): """Test stdnum.eu.vat.check_vies()""" result = vat.check_vies('BE555445') self.assertEqual(result['countryCode'], 'BE') self.assertEqual(result['vatNumber'], '555445')