From 9645a64d0e3a055db8eaf624c18766df95febfaf Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Wed, 7 Feb 2018 10:18:08 +0100 Subject: [PATCH] [FIX] base_vat_autocomplete: address fields should not be conservered on change vat Bafore this commit, fields that were not written during on change were kept leading to having a wrong overall address when changing vat when the return of the API did not contain the same fields as before. Also, ZIP from Netherlands were not supported After this commit, when changing the VAT number, fields that were not set by this operation are reset to False Also, NL zip codes compute fine OPW 807666 --- .../models/res_partner.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/addons/base_vat_autocomplete/models/res_partner.py b/addons/base_vat_autocomplete/models/res_partner.py index 41b40ec9c1d..93d74c497f8 100644 --- a/addons/base_vat_autocomplete/models/res_partner.py +++ b/addons/base_vat_autocomplete/models/res_partner.py @@ -30,16 +30,23 @@ class ResPartner(models.Model): city = lines.pop() return (cp, city) else: - result = re.match('((?:L-|AT-)?[0-9\-]+) (.+)', lines[-1]) + result = re.match('((?:L-|AT-)?[0-9\-]+[A-Z]{,2}) (.+)', lines[-1]) if result: lines.pop() return (result.group(1), result.group(2)) return False + def _set_address_field(partner, field, value): + partner[field] = value + non_set_address_fields.remove(field) + if stdnum_vat is None: return {} for partner in self: + # If a field is non set in this algorithm + # wipe it anyway + non_set_address_fields = set(['street', 'street2', 'city', 'zip', 'state_id', 'country_id']) if not partner.vat: return {} if len(partner.vat) > 5 and partner.vat[:2].lower() in stdnum_vat.country_codes: @@ -68,14 +75,20 @@ class ResPartner(models.Model): 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) + + _set_address_field(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] + _set_address_field(partner, 'zip', res[0]) + _set_address_field(partner, 'city', res[1]) if len(lines) > 0: - partner.street2 = lines.pop(0) + _set_address_field(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 + _set_address_field(partner, 'country_id', country and country.id or False) + + for field in non_set_address_fields: + if partner[field]: + partner[field] = False