def transition_check(self):
        Party = Pool().get('party.party')

        if not HAS_VATNUMBER or not hasattr(vatnumber, 'check_vies'):
            return 'no_result'

        parties_succeed = []
        parties_failed = []
        parties = Party.browse(Transaction().context.get('active_ids'))
        for party in parties:
            if not party.vat_code:
                continue
            try:
                if not vatnumber.check_vies(party.vat_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
Exemple #2
0
    def transition_check(self):
        Party = Pool().get('party.party')

        if not HAS_VATNUMBER or not hasattr(vatnumber, 'check_vies'):
            return 'no_result'

        parties_succeed = []
        parties_failed = []
        parties = Party.browse(Transaction().context.get('active_ids'))
        for party in parties:
            if not party.vat_code:
                continue
            try:
                if not vatnumber.check_vies(party.vat_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 _vat_check(self):
     if self.vat:
         try:
             self.vat_check = vatnumber.check_vies(self.vat)
         #~ except MethodNotFound,e:
         #~ raise Warning('Method missing %s' % e)
         except Exception, e:
             self.vat_check = e
 def _vat_check(self):
     if self.vat:
         try:
             self.vat_check =  vatnumber.check_vies(self.vat)
         #~ except MethodNotFound,e:
             #~ raise Warning('Method missing %s' % e)
         except Exception,e:
             self.vat_check = e
Exemple #5
0
 def vies_vat_check(self, country_code, vat_number):
     country_code = country_code.upper() if country_code else False
     try:
         # Validate against  VAT Information Exchange System (VIES)
         # see also http://ec.europa.eu/taxation_customs/vies/
         vat = country_code + vat_number
         res = vatnumber.check_vies(vat)
     except Exception:
         return False
     return res
Exemple #6
0
    def get_tax_rate(cls, vat_id, customer_country, supplier_country=None):
        if not supplier_country:
            supplier_country = cls.get_supplier_country_code()
        if not cls.is_in_EU(supplier_country):
            raise ImproperlyConfigured(
                "EUTaxationPolicy requires that supplier country is in EU")

        if not vat_id and not customer_country:
            # We don't know VAT ID or country
            return cls.get_default_tax()

        elif not vat_id and customer_country:
            # Customer is not a company, we know his country

            if cls.is_in_EU(customer_country):
                # Customer (private person) is from a EU
                # He must pay full VAT of our country
                return cls.get_default_tax()
            else:
                # Customer (private person) is not from EU
                # charge back
                return None
        else:
            # Customer is company, we now country and VAT ID

            if customer_country.upper() == supplier_country.upper():
                # Company is from the same country as supplier
                # Normal tax
                return cls.get_default_tax()

            if cls.is_in_EU(customer_country):
                # Company is from other EU country
                try:
                    use_vies_validator = getattr(
                        settings, 'INVOICING_USE_VIES_VALIDATOR', True)
                    vies_valid = vatnumber.check_vies(
                        vat_id) if use_vies_validator else True

                    if vat_id and vies_valid:
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return cls.get_default_tax()
                except ImportError as e:
                    raise e
                except Exception as e:
                    # If we could not connect to VIES
                    return cls.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #7
0
 def vies_vat_check(self, cr, uid, country_code, vat_number, context=None):
     try:
         # Validate against  VAT Information Exchange System (VIES)
         # see also http://ec.europa.eu/taxation_customs/vies/
         return vatnumber.check_vies(country_code.upper()+vat_number)
     except Exception:
         # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
         # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE,
         # TIMEOUT or SERVER_BUSY. There is no way we can validate the input
         # with VIES if any of these arise, including the first one (it means invalid
         # country code or empty VAT number), so we fall back to the simple check.
         return self.simple_vat_check(cr, uid, country_code, vat_number, context=context)
Exemple #8
0
 def vies_vat_check(self, cr, uid, country_code, vat_number, context=None):
     try:
         # Validate against  VAT Information Exchange System (VIES)
         # see also http://ec.europa.eu/taxation_customs/vies/
         return vatnumber.check_vies(country_code.upper()+vat_number)
     except Exception:
         # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
         # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE,
         # TIMEOUT or SERVER_BUSY. There is no way we can validate the input
         # with VIES if any of these arise, including the first one (it means invalid
         # country code or empty VAT number), so we fall back to the simple check.
         return self.simple_vat_check(cr, uid, country_code, vat_number, context=context)
    def __call__(self, value):
        # check country code
        country_code = str(value[:2])
        country = Country(code=country_code.upper(), flag_url=None)
        if not country:
            raise ValidationError(_('{0} is not a valid country code.').format(country_code))

        if not vatnumber.check_vat(value):
            raise ValidationError(_('{0} is not a valid VAT number').format(value))

        if self.use_vies_validation and EUTaxationPolicy.is_in_EU(country_code):
            if not vatnumber.check_vies(value):
                raise ValidationError(_('{0} is not a valid VAT number').format(value))
Exemple #10
0
    def get_tax_rate(cls, vat_id, customer_country, supplier_country=None):
        if not supplier_country:
            supplier_country = cls.get_supplier_country_code()
        if not cls.is_in_EU(supplier_country):
            raise ImproperlyConfigured("EUTaxationPolicy requires that supplier country is in EU")

        if not vat_id and not customer_country:
            # We don't know VAT ID or country
            return cls.get_default_tax()

        elif not vat_id and customer_country:
            # Customer is not a company, we know his country

            if cls.is_in_EU(customer_country):
                # Customer (private person) is from a EU
                # He must pay full VAT of our country
                return cls.get_default_tax()
            else:
                # Customer (private person) is not from EU
                # charge back
                return None
        else:
            # Customer is company, we now country and VAT ID

            if customer_country.upper() == supplier_country.upper():
                # Company is from the same country as supplier
                # Normal tax
                return cls.get_default_tax()

            if cls.is_in_EU(customer_country):
                # Company is from other EU country
                try:
                    use_vies_validator = getattr(settings, 'INVOICING_USE_VIES_VALIDATOR', True)
                    vies_valid = vatnumber.check_vies(vat_id) if use_vies_validator else True

                    if vat_id and vies_valid:
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return cls.get_default_tax()
                except ImportError as e:
                    raise e
                except Exception as e:
                    # If we could not connect to VIES
                    return cls.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #11
0
    def get_tax_rate(cls, tax_id, country_code):
        issuer_country_code = cls.get_issuer_country_code()
        if not cls.is_in_EU(issuer_country_code):
            raise ImproperlyConfigured(
                "EUTaxationPolicy requires that issuer country is in EU")

        if not tax_id and not country_code:
            # No vat id, no country
            return cls.get_default_tax()

        elif not tax_id and country_code:
            # Customer is not a company, we know his country

            if cls.is_in_EU(country_code):
                # Customer (private person) is from a EU
                # Customer pays his VAT rate
                return cls.EU_COUNTRIES_VAT[country_code]
            else:
                # Customer (private person) not from EU
                # VAT n/a
                return None

        else:
            # Customer is company, we now country and vat id

            if country_code.upper() == issuer_country_code.upper():
                # Company is from the same country as issuer
                # Normal tax
                return cls.get_default_tax()
            if cls.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    vies_result = vatnumber.check_vies(tax_id)
                    logger.info("TAX_ID=%s RESULT=%s" % (tax_id, vies_result))
                    if tax_id and vies_result:
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return cls.EU_COUNTRIES_VAT[country_code]
                except (WebFault, TransportError,
                        stdnum.exceptions.InvalidComponent):
                    # If we could not connect to VIES or the VAT ID is incorrect
                    logger.exception("TAX_ID=%s" % (tax_id))
                    return cls.EU_COUNTRIES_VAT[country_code]
            else:
                # Company is not from EU
                # VAT n/a
                return None
Exemple #12
0
    def get_tax_rate(cls, tax_id, country_code):
        issuer_country_code = cls.get_issuer_country_code()
        if not cls.is_in_EU(issuer_country_code):
            raise ImproperlyConfigured("EUTaxationPolicy requires that issuer country is in EU")

        if not tax_id and not country_code:
            # No vat id, no country
            return cls.get_default_tax()

        elif not tax_id and country_code:
            # Customer is not a company, we know his country

            if cls.is_in_EU(country_code):
                # Customer (private person) is from a EU
                # Customer pays his VAT rate
                return cls.EU_COUNTRIES_VAT[country_code]
            else:
                # Customer (private person) not from EU
                # VAT n/a
                return None

        else:
            # Customer is company, we now country and vat id

            if country_code.upper() == issuer_country_code.upper():
                # Company is from the same country as issuer
                # Normal tax
                return cls.get_default_tax()
            if cls.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    vies_result = vatnumber.check_vies(tax_id)
                    logger.info("TAX_ID=%s RESULT=%s" % (tax_id, vies_result))
                    if tax_id and vies_result:
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return cls.EU_COUNTRIES_VAT[country_code]
                except (WebFault, TransportError, stdnum.exceptions.InvalidComponent):
                    # If we could not connect to VIES or the VAT ID is incorrect
                    logger.exception("TAX_ID=%s" % (tax_id))
                    return cls.EU_COUNTRIES_VAT[country_code]
            else:
                # Company is not from EU
                # VAT n/a
                return None
Exemple #13
0
    def __call__(self, value):
        # check country code
        country_code = str(value[:2])
        country = Country(code=country_code.upper(), flag_url=None)
        if not country:
            raise ValidationError(
                _('{0} is not a valid country code.').format(country_code))

        if not vatnumber.check_vat(value):
            raise ValidationError(
                _('{0} is not a valid VAT number').format(value))

        if self.use_vies_validation and EUTaxationPolicy.is_in_EU(
                country_code):
            if not vatnumber.check_vies(value):
                raise ValidationError(
                    _('{0} is not a valid VAT number').format(value))
Exemple #14
0
 def validate_vies(self, cr, uid, ids, context=None):
     if context is None:
         context = {}
     values = {}
     partner_obj = self.pool.get('res.partner')
     for partner in partner_obj.browse(cr, uid, context.get('active_ids'), context=context):
         values['valid_vies_vat'] = False
         if partner.vat:
             vat = self._split_vat(partner.vat)
             if self.check_customer_vat(cr, uid, vat[0], vat[1]):
                 if vatnumber.check_vies(partner.vat):
                     values['valid_vies_vat'] = True
             else:
                 raise osv.except_osv(_("Error"), _("Client Vat Number not have a valid format"))
                 return {'type': 'ir.actions.act_window_close'}                    
         partner_obj.write(cr, uid, [partner.id], values)
     return {'type': 'ir.actions.act_window_close'}
Exemple #15
0
    def check_vat_vies(self, cr, uid, ids, context):
        if context is None:
            context = {}
        print "Vat vies"
        vat = False
        vat_ok = False
        european_countries = [
            'at', 'be', 'bg', 'cy', 'cz', 'de', 'dk', 'ee', 'el', 'es', 'fi',
            'fr', 'gb', 'hu', 'ie', 'it', 'lt', 'lu', 'lv', 'mt', 'nl', 'pl',
            'pt', 'ro', 'se', 'si', 'sk'
        ]
        fiscal_position = None

        partner_obj = self.pool.get('res.partner')
        values = {}
        partners = partner_obj.browse(cr, uid, ids, context)
        for partner in partners:
            fiscal_position = partner.property_account_position.id or None
            if partner.mag_vat:
                company = partner.company_id
                vat = partner.mag_vat
                country_code = partner.country.code
                if country_code.lower(
                ) == company.partner_id.country.code.lower():
                    if vat[:2].lower() != country_code.lower():
                        vat = country_code.upper() + vat
                    fiscal_position = company.national_fiscal_position.id or None
                else:
                    if country_code == 'GR':
                        country_code = 'EL'  #Fix Greece tax code
                    if country_code.lower() in european_countries:
                        if vat[:2].lower() != country_code.lower():
                            vat = country_code.upper() + vat
                        if vatnumber.check_vies(vat):
                            fiscal_position = company.valid_vies_fiscal_position.id or None
                        else:
                            fiscal_position = company.non_valid_vies_fiscal_position.id or None
                    else:
                        fiscal_position = company.non_european_fiscal_position.id or None
                print partner.vat
                values = {'property_account_position': fiscal_position}
                print values
                self.write(cr, uid, [partner.id], values, context)
                print "Hecho"

        return True
Exemple #16
0
    def get_tax_rate(self, tax_id, country_code):
        issuer_country = self.get_issuer_country_code()
        if not self.is_in_EU(issuer_country):
            raise ImproperlyConfigured(
                "EUTaxationPolicy requires that issuer country is in EU")

        if not tax_id and not country_code:
            # No vat id, no country
            return self.get_default_tax()

        elif tax_id and not country_code:
            # Customer is not a company, we know his country

            if self.is_in_EU(country_code):
                # Customer (private person) is from a EU
                # He must pay full VAT of our country
                return self.get_default_tax()
            else:
                # Customer (private person) not from EU
                # charge back
                return None

        else:
            # Customer is company, we now country and vat id

            if country_code.upper() == issuer_country.upper():
                # Company is from the same country as issuer
                # Normal tax
                return self.get_default_tax()
            if self.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    if tax_id and vatnumber.check_vies(tax_id):
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return self.get_default_tax()
                except suds.WebFault:
                    # If we could not connect to VIES
                    return self.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #17
0
    def get_tax_rate(cls, vat_id, country_code):
        supplier_country = cls.get_supplier_country_code()
        if not cls.is_in_EU(supplier_country):
            raise ImproperlyConfigured(
                "EUTaxationPolicy requires that supplier country is in EU")

        if not vat_id and not country_code:
            # We don't know VAT ID or country
            return cls.get_default_tax()

        elif not vat_id and country_code:
            # Customer is not a company, we know his country

            if cls.is_in_EU(country_code):
                # Customer (private person) is from a EU
                # He must pay full VAT of our country
                return cls.get_default_tax()
            else:
                # Customer (private person) is not from EU
                # charge back
                return None
        else:
            # Customer is company, we now country and VAT ID

            if country_code.upper() == supplier_country.upper():
                # Company is from the same country as supplier
                # Normal tax
                return cls.get_default_tax()

            if cls.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    if vat_id and vatnumber.check_vies(vat_id):
                        # Company is registered in VIES
                        # Charge back
                        return None
                    else:
                        return cls.get_default_tax()
                except Exception:
                    # If we could not connect to VIES
                    return cls.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #18
0
    def get_tax_rate(cls, tax_id, country_code):
        issuer_country = cls.get_issuer_country_code()
        if not cls.is_in_EU(issuer_country):
            raise ImproperlyConfigured("EUTaxationPolicy requires that issuer country is in EU")

        if not tax_id and not country_code:
            # No vat id, no country
            return cls.get_default_tax()

        elif tax_id and not country_code:
            # Customer is not a company, we know his country

           if cls.is_in_EU(country_code):
               # Customer (private person) is from a EU
               # He must pay full VAT of our country
               return cls.get_default_tax()
           else:
               # Customer (private person) not from EU
               # charge back
               return None

        else:
            # Customer is company, we now country and vat id

            if country_code.upper() == issuer_country.upper():
               # Company is from the same country as issuer
               # Normal tax
               return cls.get_default_tax()
            if cls.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    if tax_id and vatnumber.check_vies(tax_id):
                    # Company is registered in VIES
                    # Charge back
                        return None
                    else:
                        return cls.get_default_tax()
                except (WebFault, TransportError):
                    # If we could not connect to VIES
                    return cls.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #19
0
    def get_tax_rate(cls, vat_id, country_code):
        supplier_country = cls.get_supplier_country_code()
        if not cls.is_in_EU(supplier_country):
            raise ImproperlyConfigured("EUTaxationPolicy requires that supplier country is in EU")

        if not vat_id and not country_code:
            # We don't know VAT ID or country
            return cls.get_default_tax()

        elif not vat_id and country_code:
            # Customer is not a company, we know his country

            if cls.is_in_EU(country_code):
                # Customer (private person) is from a EU
                # He must pay full VAT of our country
                return cls.get_default_tax()
            else:
                # Customer (private person) is not from EU
                # charge back
                return None
        else:
            # Customer is company, we now country and VAT ID

            if country_code.upper() == supplier_country.upper():
                # Company is from the same country as supplier
                # Normal tax
                return cls.get_default_tax()

            if cls.is_in_EU(country_code):
                # Company is from other EU country
                try:
                    if vat_id and vatnumber.check_vies(vat_id):
                    # Company is registered in VIES
                    # Charge back
                        return None
                    else:
                        return cls.get_default_tax()
                except Exception:
                    # If we could not connect to VIES
                    return cls.get_default_tax()
            else:
                # Company is not from EU
                # Charge back
                return None
Exemple #20
0
            # No VAT validation available, default to check that the country code exists
            if country_code.upper() == 'EU':
                # Foreign companies that trade with non-enterprises in the EU
                # may have a VATIN starting with "EU" instead of a country code.
                return True
            country_code = _eu_country_vat_inverse.get(country_code, country_code)
            return bool(self.env['res.country'].search([('code', '=ilike', country_code)]))
        return check_func(vat_number)

    @api.model
    @tools.ormcache('vat')
    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.
<<<<<<< HEAD
        return vatnumber.check_vies(vat)
=======
        return stdnum.eu.vat.check_vies(vat)
>>>>>>> f0a66d05e70e432d35dc68c9fb1e1cc6e51b40b8

    @api.model
    def vies_vat_check(self, country_code, vat_number):
        try:
            # Validate against  VAT Information Exchange System (VIES)
            # see also http://ec.europa.eu/taxation_customs/vies/
            return self._check_vies(country_code.upper() + vat_number)
        except Exception:
            # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
            # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE,
            # TIMEOUT or SERVER_BUSY. There is no way we can validate the input
            # with VIES if any of these arise, including the first one (it means invalid
Exemple #21
0
import sys
import csv
import vatnumber

if vatnumber.check_vies(sys.argv[1]) == True:
	print sys.argv[1] + "\tZweryfikowane"
else:
	print sys.argv[1] + "\tNie zweryfikowane"
Exemple #22
0
 def test_vies(self):
     '''
     Test vies
     '''
     for vat in VIES_NUMBERS:
         self.assert_(vatnumber.check_vies(vat))
Exemple #23
0
def check_vies(vat):
    try:
        return vatnumber.check_vies(vat)
    except:
        return None
    def magento_create_partner(self,
                               cr,
                               uid,
                               magento_app,
                               values,
                               mapping=True,
                               context=None):
        """Create Partner from Magento Values
        Transform dicc by Base External Mapping
        :return partner_id
        """
        if context is None:
            context = {}

        vat = False
        vat_ok = False
        magento_vat = False
        if 'taxvat' in values:
            magento_vat = values['taxvat']

        logger = netsvc.Logger()

        external_referential_obj = self.pool.get(
            'magento.external.referential')
        res_partner_vals_obj = self.pool.get('base.external.mapping')
        sale_shop_obj = self.pool.get('sale.shop')
        partner_address_obj = self.pool.get('res.partner.address')
        sale_shop_id = sale_shop_obj.search(
            cr,
            uid, [('magento_website', '=', int(values['website_id']))],
            context=context)
        sale_shop = sale_shop_obj.browse(cr,
                                         uid,
                                         sale_shop_id[0],
                                         context=context)
        european_countries = [
            'at', 'be', 'bg', 'cy', 'cz', 'de', 'dk', 'ee', 'el', 'es', 'fi',
            'fr', 'gb', 'hu', 'ie', 'it', 'lt', 'lu', 'lv', 'mt', 'nl', 'pl',
            'pt', 'ro', 'se', 'si', 'sk'
        ]
        fiscal_position = sale_shop.non_vat_fiscal_position.id or None

        if magento_vat:
            country_obj = self.pool.get('res.country')
            country_code_search = magento_vat[:2]
            if country_code_search == 'EL':
                country_code_search = 'GR'  #Fix Greece tax code
            country_id = country_obj.search(
                cr,
                uid, [('code', 'ilike', country_code_search)],
                context=context)

            if len(country_id) == 0:  # The VAT has not a valid country code
                country_code = partner_address_obj.magento_get_customer_address_country_code(
                    cr, uid, magento_app, values, context)
                if country_code == 'GR':
                    country_code = 'EL'  #Fix Greece tax code
                vat = '%s%s' % (country_code, magento_vat)
            else:  # The VAT has a valid country code
                vat = magento_vat
            print vat
            if hasattr(self, 'check_vat_' + vat[:2].lower()):
                check = getattr(self, 'check_vat_' + vat[:2].lower())
                vat_ok = check(vat[2:])

            if vat_ok:
                values['vat'] = vat.upper()
                """If already exist a partner with the same VAT:
                Create External Referential
                Return partner_id
                """
                partner_id = self.search(cr,
                                         uid, [('vat', '=', values['vat'])],
                                         context=context)
                if len(partner_id) > 0:
                    external_referential_obj.create_external_referential(
                        cr, uid, magento_app, 'res.partner', partner_id[0],
                        values['customer_id'])
                    return partner_id[0]

            if vat[:2].lower(
            ) == sale_shop.company_id.partner_id.vat[:2].lower():
                fiscal_position = sale_shop.national_fiscal_position.id or None
            elif vat[:2].lower() in european_countries:
                if vatnumber.check_vies(vat):
                    fiscal_position = sale_shop.valid_vies_fiscal_position.id or None
                else:
                    fiscal_position = sale_shop.non_valid_vies_fiscal_position.id or None
            else:
                fiscal_position = sale_shop.non_european_fiscal_position.id or None

        else:
            print values
            if 'country_id' in values:
                country_code = values['country_id']
            else:
                country_code = partner_address_obj.magento_get_customer_address_country_code(
                    cr, uid, magento_app, values, context)
            if country_code == 'GR': country_code = 'EL'  #Fix Greece tax code
            print country_code
            if country_code.lower() in european_countries:
                fiscal_position = sale_shop.non_vat_fiscal_position.id or None
            else:
                fiscal_position = sale_shop.non_european_fiscal_position.id or None

        context['magento_app'] = magento_app
        values['name'] = '%s %s' % (values['firstname'].capitalize(),
                                    values['lastname'].capitalize())
        res_partner_vals = res_partner_vals_obj.get_external_to_oerp(
            cr, uid, 'magento.res.partner', False, values, context)
        res_partner_vals['customer'] = True  #fix this partner is customer
        res_partner_vals['property_account_position'] = fiscal_position
        partner_id = self.create(cr, uid, res_partner_vals, context)

        if mapping and ('customer_id' in values):
            external_referential_obj.create_external_referential(
                cr, uid, magento_app, 'res.partner', partner_id,
                values['customer_id'])
            self.pool.get('magento.log').create_log(
                cr, uid, magento_app, 'res.partner', partner_id,
                values['customer_id'], 'done',
                _('Successfully create partner: %s') % (values['name']))
        else:
            self.pool.get('magento.log').create_log(
                cr, uid, magento_app, 'res.partner', partner_id, '', 'done',
                _('Successfully create partner: %s') % (values['name']))
        if values.get('group_id', magento_app.customer_default_group.id):
            magento_app_customer_ids = self.pool.get(
                'magento.app.customer').magento_app_customer_create(
                    cr, uid, magento_app, partner_id, values, context)

        logger.notifyChannel(
            'Magento Sync Partner', netsvc.LOG_INFO,
            "Create Partner: magento %s, openerp id %s, magento id %s" %
            (magento_app.name, partner_id, values['customer_id']))

        return partner_id
 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 vatnumber.check_vies(vat)
Exemple #26
0
 def test_vies(self):
     '''
     Test vies
     '''
     for vat in VIES_NUMBERS:
         self.assert_(vatnumber.check_vies(vat))
Exemple #27
0
    def magento_create_partner(self, cr, uid, magento_app, values, mapping = True, context = None):
        """Create Partner from Magento Values
        Transform dicc by Base External Mapping
        :return partner_id
        """
        if context is None:
            context = {}

        vat = False
        vat_ok = False
        magento_vat = False
        if 'taxvat' in values:
            magento_vat = values['taxvat']

        logger = netsvc.Logger()

        external_referential_obj = self.pool.get('magento.external.referential')
        res_partner_vals_obj = self.pool.get('base.external.mapping')
        sale_shop_obj = self.pool.get('sale.shop')
        partner_address_obj = self.pool.get('res.partner.address')
        sale_shop_id = sale_shop_obj.search(cr,uid,[('magento_website','=',int(values['website_id']))],context=context)
        sale_shop = sale_shop_obj.browse(cr,uid,sale_shop_id[0],context=context)
        european_countries = ['at','be','bg','cy','cz','de','dk','ee','el','es','fi','fr','gb','hu','ie','it','lt','lu','lv','mt','nl','pl','pt','ro','se','si','sk']
        fiscal_position = sale_shop.non_vat_fiscal_position.id or None
        
        if magento_vat:
            country_obj = self.pool.get('res.country')
            country_code_search = magento_vat[:2]
            if country_code_search == 'EL': country_code_search = 'GR' #Fix Greece tax code
            country_id = country_obj.search(cr, uid, [('code', 'ilike', country_code_search)], context = context)
            
            if len(country_id) == 0: # The VAT has not a valid country code
                country_code = partner_address_obj.magento_get_customer_address_country_code(cr, uid, magento_app, values, context)
                if country_code == 'GR': country_code = 'EL' #Fix Greece tax code
                vat = '%s%s' % (country_code, magento_vat)
            else: # The VAT has a valid country code
                vat = magento_vat
            print vat
            if hasattr(self, 'check_vat_' + vat[:2].lower()):
                check = getattr(self, 'check_vat_' + vat[:2].lower())
                vat_ok = check(vat[2:])
            
            if vat_ok:
                values['vat'] = vat.upper()
                """If already exist a partner with the same VAT:
                Create External Referential
                Return partner_id
                """
                partner_id = self.search(cr, uid, [('vat', '=', values['vat'] )], context = context)
                if len(partner_id) > 0:
                    external_referential_obj.create_external_referential(cr, uid, magento_app, 'res.partner', partner_id[0], values['customer_id'])
                    return partner_id[0]

            if vat[:2].lower() == sale_shop.company_id.partner_id.vat[:2].lower():
                fiscal_position = sale_shop.national_fiscal_position.id or None
            elif vat[:2].lower() in european_countries:
                if vatnumber.check_vies(vat):
                    fiscal_position = sale_shop.valid_vies_fiscal_position.id or None
                else:
                    fiscal_position = sale_shop.non_valid_vies_fiscal_position.id or None
            else:
                fiscal_position = sale_shop.non_european_fiscal_position.id or None
                    
        else:
            print values
            if 'country_id' in values:
                country_code = values['country_id']
            else:
                country_code = partner_address_obj.magento_get_customer_address_country_code(cr, uid, magento_app, values, context)
            if country_code == 'GR': country_code = 'EL' #Fix Greece tax code
            print country_code
            if country_code.lower() in european_countries:
                fiscal_position = sale_shop.non_vat_fiscal_position.id or None
            else: 
                fiscal_position = sale_shop.non_european_fiscal_position.id or None

        context['magento_app'] = magento_app
        values['name'] = '%s %s' % (values['firstname'].capitalize(), values['lastname'].capitalize())
        res_partner_vals = res_partner_vals_obj.get_external_to_oerp(cr, uid, 'magento.res.partner', False, values, context)
        res_partner_vals['customer'] = True #fix this partner is customer
        res_partner_vals['property_account_position'] = fiscal_position
        partner_id = self.create(cr, uid, res_partner_vals, context)

        if mapping and ('customer_id' in values):
            external_referential_obj.create_external_referential(cr, uid, magento_app, 'res.partner', partner_id, values['customer_id'])
            self.pool.get('magento.log').create_log(cr, uid, magento_app, 'res.partner', partner_id, values['customer_id'], 'done', _('Successfully create partner: %s') % (values['name']) )
        else:
            self.pool.get('magento.log').create_log(cr, uid, magento_app, 'res.partner', partner_id, '', 'done', _('Successfully create partner: %s') % (values['name']) )
        if values.get('group_id', magento_app.customer_default_group.id):
            magento_app_customer_ids = self.pool.get('magento.app.customer').magento_app_customer_create(cr, uid, magento_app, partner_id, values, context)

        logger.notifyChannel('Magento Sync Partner', netsvc.LOG_INFO, "Create Partner: magento %s, openerp id %s, magento id %s" % (magento_app.name, partner_id, values['customer_id']))

        return partner_id