diff --git a/partner_data_vies_populator/README.rst b/partner_data_vies_populator/README.rst new file mode 100644 index 000000000..d9f7957bc --- /dev/null +++ b/partner_data_vies_populator/README.rst @@ -0,0 +1 @@ +Will be regenerated from readme subdir diff --git a/partner_data_vies_populator/__init__.py b/partner_data_vies_populator/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/partner_data_vies_populator/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/partner_data_vies_populator/__manifest__.py b/partner_data_vies_populator/__manifest__.py new file mode 100644 index 000000000..3ed7de8ce --- /dev/null +++ b/partner_data_vies_populator/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright (C) 2015 Forest and Biomass Romania +# Copyright (C) 2020 NextERP Romania +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Partner Data VIES Populator", + "summary": "Using VIES webservice, name and address information will " + "be fetched and added to the partner.", + "version": "14.0.1.0.0", + "category": "Customer Relationship Management", + "author": "NextERP Romania," + "Forest and Biomass Romania," + "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/partner-contact", + "license": "AGPL-3", + "depends": ["base_vat"], + "external_dependencies": {"python": ["python-stdnum"]}, + "installable": True, +} diff --git a/partner_data_vies_populator/i18n/partner_data_vies_populator.pot b/partner_data_vies_populator/i18n/partner_data_vies_populator.pot new file mode 100644 index 000000000..9625dbffd --- /dev/null +++ b/partner_data_vies_populator/i18n/partner_data_vies_populator.pot @@ -0,0 +1,19 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_data_vies_populator +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: partner_data_vies_populator +#: model:ir.model,name:partner_data_vies_populator.model_res_partner +msgid "Contact" +msgstr "" diff --git a/partner_data_vies_populator/models/__init__.py b/partner_data_vies_populator/models/__init__.py new file mode 100644 index 000000000..91fed54d4 --- /dev/null +++ b/partner_data_vies_populator/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/partner_data_vies_populator/models/res_partner.py b/partner_data_vies_populator/models/res_partner.py new file mode 100644 index 000000000..59fba4054 --- /dev/null +++ b/partner_data_vies_populator/models/res_partner.py @@ -0,0 +1,64 @@ +# Copyright (C) 2015 Forest and Biomass Romania +# Copyright (C) 2020 NextERP Romania +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging + +from odoo import _, api, models +from odoo.exceptions import UserError + +_logger = logging.getLogger(__name__) + +try: + from stdnum.eu.vat import check_vies +except ImportError: + _logger.debug("Cannot import check_vies method from python stdnum.") + + +class ResPartner(models.Model): + _inherit = "res.partner" + + @api.model + def _get_vies_data(self, vat, raise_if_fail=False): + res = {} + try: + result = check_vies(vat) + except Exception as e: + _logger.warning("Failed to query VIES: %s" % e) + if raise_if_fail: + raise UserError(_("Failed to query VIES.\nTechnical error: %s.") % e) + return res + if result.valid and 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", result.countryCode)] + ) + if country: + res["country_id"] = country[0].id + return res + + @api.onchange("vat") + def vies_vat_change(self): + eu_group = self.env.ref("base.europe", raise_if_not_found=False) + if eu_group: + for partner in self: + if not partner.vat or not partner.is_company: + continue + vat = partner.vat.strip().upper() + vat_country, vat_number = self._split_vat(vat) + vat_country = vat_country.upper() + eu_countries = eu_group.country_ids.mapped("code") + if vat_country and vat_country not in eu_countries: + continue + result = self._get_vies_data(vat) + if result: + partner.update(result) diff --git a/partner_data_vies_populator/readme/CONTRIBUTORS.rst b/partner_data_vies_populator/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..0a77744a4 --- /dev/null +++ b/partner_data_vies_populator/readme/CONTRIBUTORS.rst @@ -0,0 +1,9 @@ +* `NextERP Romania `_: + + * Fekete Mihai + +* `Forest and Biomass Romania `_ + +* `Akretion `_ : + + * Alexis de Lattre diff --git a/partner_data_vies_populator/readme/DESCRIPTION.rst b/partner_data_vies_populator/readme/DESCRIPTION.rst new file mode 100644 index 000000000..30490fd06 --- /dev/null +++ b/partner_data_vies_populator/readme/DESCRIPTION.rst @@ -0,0 +1,7 @@ +This module allows you to create the partners (companies) based on their VAT number. +Name and address of the partner will automatically be completed via VIES Webservice. + +VIES Service (based on stdnum python) +http://ec.europa.eu/taxation_customs/vies/vieshome.do + +Unfortunately, VIES doesn't return a structured address but just a one-line address that aggregate street, zip and city. So, when you use this module to create a partner, the *City* and *Zip* fields will be left empty ; the *Street* field will contain the one-line address. diff --git a/partner_data_vies_populator/readme/INSTALL.rst b/partner_data_vies_populator/readme/INSTALL.rst new file mode 100644 index 000000000..0f872545f --- /dev/null +++ b/partner_data_vies_populator/readme/INSTALL.rst @@ -0,0 +1 @@ +This module require the `python-stdnum `_ librairy. As Odoo itself depend on this librairy, it should already be installed on your system. diff --git a/partner_data_vies_populator/readme/USAGE.rst b/partner_data_vies_populator/readme/USAGE.rst new file mode 100644 index 000000000..4e84b2b13 --- /dev/null +++ b/partner_data_vies_populator/readme/USAGE.rst @@ -0,0 +1 @@ +When changing a company partner VAT number, this module will try to fetch the partner data from VIES webservice, if available on VIES will update the name, address and country. diff --git a/partner_data_vies_populator/static/description/icon.png b/partner_data_vies_populator/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/partner_data_vies_populator/static/description/icon.png differ diff --git a/partner_data_vies_populator/static/description/index.html b/partner_data_vies_populator/static/description/index.html new file mode 100644 index 000000000..7b33a9295 --- /dev/null +++ b/partner_data_vies_populator/static/description/index.html @@ -0,0 +1,443 @@ + + + + + + +Partner Data VIES Populator + + + +
+

Partner Data VIES Populator

+ + +

Beta License: AGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runbot

+

This module allows you to create the partners (companies) based on their VAT number. +Name and address of the partner will automatically be completed via VIES Webservice.

+

VIES Service (based on stdnum python) +http://ec.europa.eu/taxation_customs/vies/vieshome.do

+

Table of contents

+ +
+

Installation

+

To install this module, you need to:

+
    +
  1. Clone the branch 13.0 of the repository https://github.com/OCA/partner-contact
  2. +
  3. Add the path to this repository in your configuration (addons-path)
  4. +
  5. Update the module list
  6. +
  7. Search for “Partner Data VIES Populator” in your addons
  8. +
  9. install the module
  10. +
+
+
+

Usage

+

When changing a company partner VAT number, this module will try to fetch the partner data from VIES webservice, if available on VIES will update the name, address and country.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • NextERP Romania
  • +
  • Forest and Biomass Romania
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/partner-contact project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/partner_data_vies_populator/tests/__init__.py b/partner_data_vies_populator/tests/__init__.py new file mode 100644 index 000000000..fc68d9282 --- /dev/null +++ b/partner_data_vies_populator/tests/__init__.py @@ -0,0 +1 @@ +from . import test_partner_data_vies_populator diff --git a/partner_data_vies_populator/tests/test_partner_data_vies_populator.py b/partner_data_vies_populator/tests/test_partner_data_vies_populator.py new file mode 100644 index 000000000..47b206891 --- /dev/null +++ b/partner_data_vies_populator/tests/test_partner_data_vies_populator.py @@ -0,0 +1,85 @@ +# Copyright (C) 2015 Forest and Biomass Romania +# Copyright (C) 2020 NextERP Romania +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo.tests.common import Form, TransactionCase + + +class TestPartnerCreateByVAT(TransactionCase): + def setUp(self): + super().setUp() + self.partner_model = self.env["res.partner"] + self.be_country_id = self.env.ref("base.be").id + self.sample_1 = { + "name": "SA ODOO", + "address": "Chaussée De Namur 40 1367 Ramillies", + "country_code": "BE", + } + + def test_create_from_vat1(self): + # Create an partner from VAT number field + with Form(self.partner_model) as partner_form: + partner_form.is_company = True + partner_form.vat = "be0477472701" + + # Check if the datas fetch correspond with the datas from VIES. + self.assertEqual(partner_form.name, self.sample_1["name"]) + self.assertEqual(partner_form.street, self.sample_1["address"]) + self.assertEqual( + partner_form.country_id.code, self.sample_1["country_code"] + ) + self.assertEqual(partner_form.vat, "BE0477472701") + + def test_company_vat_change(self): + # Change partner VAT number field + partner = self.partner_model.create({"name": "1", "is_company": True}) + with Form(partner) as partner_form: + # Check if the datas fetch correspond with the datas from VIES. + partner_form.vat = "be0477472701" + self.assertEqual(partner_form.name, self.sample_1["name"]) + self.assertEqual(partner_form.street, self.sample_1["address"]) + self.assertEqual( + partner_form.country_id.code, self.sample_1["country_code"] + ) + self.assertEqual(partner_form.vat, "BE0477472701") + + def test_empty_vat_change(self): + partner = self.partner_model.create( + { + "name": "2", + "is_company": True, + "country_id": self.be_country_id, + } + ) + with Form(partner) as partner_form: + partner_form.vat = False + self.assertEqual(partner_form.name, partner.name) + self.assertEqual(partner_form.street, False) + self.assertEqual(partner_form.country_id.id, partner.country_id.id) + + def test_individual_vat_change(self): + partner = self.partner_model.create( + { + "name": "3", + "is_company": False, + "country_id": self.be_country_id, + } + ) + with Form(partner) as partner_form: + partner_form.vat = "BE0477472701" + self.assertEqual(partner_form.name, partner.name) + self.assertEqual(partner_form.street, False) + self.assertEqual(partner_form.country_id.id, partner.country_id.id) + + def test_non_eu_vat_change(self): + non_eu_country_id = self.env.ref("base.sc").id + partner = self.partner_model.create( + { + "name": "4", + "is_company": True, + "country_id": non_eu_country_id, + } + ) + with Form(partner) as partner_form: + partner_form.vat = "GT1234567 - 1" + self.assertEqual(partner_form.name, partner.name) diff --git a/requirements.txt b/requirements.txt index 41b756f38..c35786a17 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ # generated from manifests external_dependencies email-validator phonenumbers +python-stdnum diff --git a/setup/partner_data_vies_populator/odoo/addons/partner_data_vies_populator b/setup/partner_data_vies_populator/odoo/addons/partner_data_vies_populator new file mode 120000 index 000000000..b9f44eac4 --- /dev/null +++ b/setup/partner_data_vies_populator/odoo/addons/partner_data_vies_populator @@ -0,0 +1 @@ +../../../../partner_data_vies_populator \ No newline at end of file diff --git a/setup/partner_data_vies_populator/setup.py b/setup/partner_data_vies_populator/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/partner_data_vies_populator/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)