Example #1
0
 def on_change_with_code(self):
     if self.type == 'eu_vat':
         try:
             return vat.compact(self.code)
         except stdnum.exceptions.ValidationError:
             pass
     return self.code
Example #2
0
 def on_change_with_code(self):
     if self.type == 'eu_vat':
         try:
             return vat.compact(self.code)
         except stdnum.exceptions.ValidationError:
             pass
     return self.code
Example #3
0
    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:
                # Equivalent to stdnum_vat.check_vies(partner.vat).
                # However, we want to add a custom timeout to the suds.client
                # because by default, it's 120 seconds and this is to long.
                try:
                    client = Client(stdnum_vat.vies_wsdl, timeout=5)
                    partner_vat = stdnum_vat.compact(partner.vat)
                    result = client.service.checkVat(partner_vat[:2], partner_vat[2:])
                except:
                    # Avoid blocking the client when the service is unreachable/unavailable
                    return {}

                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
Example #4
0
    def __init__(self, invoice_number, invoice_date, ship_date, merchant_nip,
                 merchant_name, merchant_adr, country, codes):
        self.invoice_number = invoice_number
        self.invoice_date = invoice_date
        self.ship_date = ship_date

        self.merchant_name = merchant_name
        self.merchant_adr = merchant_adr
        self.country = country
        self.codes = codes

        if nip.is_valid(merchant_nip):
            self.merchant_nip = nip.compact(merchant_nip)
            self.is_eu_vat = False
        elif vat.is_valid(merchant_nip):
            self.merchant_nip = vat.compact(merchant_nip)
            self.is_eu_vat = True
        else:
            self.merchant_nip = merchant_nip
            self.is_eu_vat = None
Example #5
0
    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)
            elif country == 'SE':
                result = re.match('([0-9]{3}\s?[0-9]{2})\s?([A-Z]+)',
                                  lines[-1])
                if result:
                    lines.pop()
                    return (result.group(1), result.group(2))
            else:
                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(
                ['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:
                # Equivalent to stdnum_vat.check_vies(partner.vat).
                # However, we want to add a custom timeout to the suds.client
                # because by default, it's 120 seconds and this is to long.
                try:
                    client = Client(stdnum_vat.vies_wsdl, timeout=5)
                    partner_vat = stdnum_vat.compact(partner.vat)
                    result = client.service.checkVat(partner_vat[:2],
                                                     partner_vat[2:])
                except:
                    # Avoid blocking the client when the service is unreachable/unavailable
                    return {}

                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]

                vals = partner._split_street_with_params(
                    ', '.join(lines.pop(0).rsplit(' ', 1)),
                    '%(street_name)s, %(street_number)s/%(street_number2)s')
                partner.update(vals)

                if len(lines) > 0:
                    res = _check_city(lines, result['countryCode'])
                    if res:
                        _set_address_field(partner, 'zip', res[0])
                        _set_address_field(partner, 'city', res[1])
                if len(lines) > 0:
                    _set_address_field(partner, 'street2', lines.pop(0))

                country = self.env['res.country'].search(
                    [('code', '=', result['countryCode'])], limit=1)
                _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
Example #6
0
    def _get_partner_vals(self, vat):
        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)
            elif country == 'SE':
                result = re.match('([0-9]{3}\s?[0-9]{2})\s?([A-Z]+)', lines[-1])
                if result:
                    lines.pop()
                    return (result.group(1), result.group(2))
            else:
                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

        # Equivalent to stdnum_vat.check_vies(partner.vat).
        # However, we want to add a custom timeout to the suds.client
        # because by default, it's 120 seconds and this is to long.
        try:
            client = Client(stdnum_vat.vies_wsdl, timeout=5)
            partner_vat = stdnum_vat.compact(vat)
            result = client.service.checkVat(partner_vat[:2], partner_vat[2:])
        except:
            # Avoid blocking the client when the service is unreachable/unavailable
            return False, {}

        if not result['valid']:
            return False, {}

        partner_name = False
        partner_address = {}
        if result['name'] != '---':
            partner_name = result['name']

        #parse the address from VIES and fill the partner's data
        if result['address'] == '---': return partner_name, {}

        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_address['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_address['zip'] = res[0]
                partner_address['city'] = res[1]
                #_set_address_field(partner, 'zip', res[0])
                #_set_address_field(partner, 'city', res[1])
        if len(lines) > 0:
            partner_address['street2'] = lines.pop(0)
            #_set_address_field(partner, 'street2', lines.pop(0))

        country = self.env['res.country'].search([('code', '=', result['countryCode'])], limit=1)

        #_set_address_field(partner, 'country_id', country and country.id or False)
        partner_address['country_id'] = country and country.id or False
        return partner_name, partner_address
Example #7
0
    def _get_partner_vals(self, vat):
        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\-]+[A-Z]{,2}) (.+)',
                                  lines[-1])
                if result:
                    lines.pop()
                    return (result.group(1), result.group(2))
            return False

        # Equivalent to stdnum_vat.check_vies(partner.vat).
        # However, we want to add a custom timeout to the suds.client
        # because by default, it's 120 seconds and this is to long.
        try:
            client = Client(stdnum_vat.vies_wsdl, timeout=5)
            partner_vat = stdnum_vat.compact(vat)
            result = client.service.checkVat(partner_vat[:2], partner_vat[2:])
        except:
            # Avoid blocking the client when the service is unreachable/unavailable
            return False, {}

        if not result['valid']:
            return False, {}

        partner_name = False
        partner_address = {}
        if result['name'] != '---':
            partner_name = result['name']

        #parse the address from VIES and fill the partner's data
        if result['address'] == '---': return partner_name, {}

        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_address['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_address['zip'] = res[0]
                partner_address['city'] = res[1]
                #_set_address_field(partner, 'zip', res[0])
                #_set_address_field(partner, 'city', res[1])
        if len(lines) > 0:
            partner_address['street2'] = lines.pop(0)
            #_set_address_field(partner, 'street2', lines.pop(0))

        country = self.env['res.country'].search(
            [('code', '=', result['countryCode'])], limit=1)

        #_set_address_field(partner, 'country_id', country and country.id or False)
        partner_address['country_id'] = country and country.id or False
        return partner_name, partner_address
Example #8
0
    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:
                # Equivalent to stdnum_vat.check_vies(partner.vat).
                # However, we want to add a custom timeout to the suds.client
                # because by default, it's 120 seconds and this is to long.
                try:
                    client = Client(stdnum_vat.vies_wsdl, timeout=5)
                    partner_vat = stdnum_vat.compact(partner.vat)
                    result = client.service.checkVat(partner_vat[:2],
                                                     partner_vat[2:])
                except:
                    # Avoid blocking the client when the service is unreachable/unavailable
                    return {}

                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