From f4ca23503d7289236b132c09dc52e4b32c047277 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ra=C3=BAl?= <raul.ruilova@trescloud.com>
Date: Wed, 10 May 2023 20:30:34 +0000
Subject: [PATCH] [REF] l10n_ec: Message validation for VAT

- We refactored the VAT validation from a validation error to a warning that is stored in l10n_ec_vat_error
- Remove the "-" and "EC" from the base_vat in the EC validation/example
- Only validate the length in the base_vat
- Add compute field with the warning message in the l10n_ec

closes odoo/odoo#121099

Signed-off-by: Josse Colpaert <jco@odoo.com>
---
 addons/base_vat/models/res_partner.py     | 13 ++-----
 addons/l10n_ec/__manifest__.py            |  3 +-
 addons/l10n_ec/i18n/es_EC.po              | 28 ++++++++++-----
 addons/l10n_ec/i18n/l10n_ec.pot           | 20 ++++++++---
 addons/l10n_ec/models/res_partner.py      | 42 ++++++++++++++---------
 addons/l10n_ec/views/res_partner_view.xml | 15 ++++++++
 6 files changed, 82 insertions(+), 39 deletions(-)
 create mode 100644 addons/l10n_ec/views/res_partner_view.xml

diff --git a/addons/base_vat/models/res_partner.py b/addons/base_vat/models/res_partner.py
index aeb05189c792..c877dc6660e2 100644
--- a/addons/base_vat/models/res_partner.py
+++ b/addons/base_vat/models/res_partner.py
@@ -39,7 +39,7 @@ _ref_vat = {
     'de': 'DE123456788',
     'dk': 'DK12345674',
     'do': 'DO1-01-85004-3 or 101850043',
-    'ec': 'EC1792060346-001',
+    'ec': '1792060346001 or 1792060346',
     'ee': 'EE123456780',
     'el': 'EL12345670',
     'es': 'ESA12345674',
@@ -328,15 +328,8 @@ class ResPartner(models.Model):
 
 
     def is_valid_ruc_ec(self, vat):
-        ci = stdnum.util.get_cc_module("ec", "ci")
-        ruc = stdnum.util.get_cc_module("ec", "ruc")
-        if len(vat) == 10:
-            return ci.is_valid(vat)
-        elif len(vat) == 13:
-            if vat[2] == "6" and ci.is_valid(vat[:10]):
-                return True
-            else:
-                return ruc.is_valid(vat)
+        if len(vat) in (10, 13) and vat.isdecimal():
+            return True
         return False
 
     def check_vat_ec(self, vat):
diff --git a/addons/l10n_ec/__manifest__.py b/addons/l10n_ec/__manifest__.py
index 478b5d2ffa3e..87174feb6287 100644
--- a/addons/l10n_ec/__manifest__.py
+++ b/addons/l10n_ec/__manifest__.py
@@ -2,7 +2,7 @@
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 {
     "name": "Ecuadorian Accounting",
-    "version": "3.4",
+    "version": "3.5",
     "description": """
 Functional
 ----------
@@ -73,6 +73,7 @@ Master Data:
         "views/l10n_latam_document_type_view.xml",
         "views/l10n_ec_sri_payment.xml",
         "views/account_journal_view.xml",
+        "views/res_partner_view.xml",
         # Try loading CoA
         "data/account_chart_template_configure_data.xml",
     ],
diff --git a/addons/l10n_ec/i18n/es_EC.po b/addons/l10n_ec/i18n/es_EC.po
index 55104c332852..44a4d82988e7 100644
--- a/addons/l10n_ec/i18n/es_EC.po
+++ b/addons/l10n_ec/i18n/es_EC.po
@@ -251,6 +251,12 @@ msgstr "Consumos Especiales (ICE)"
 msgid "Special Consumptions Tax (ICE)"
 msgstr "Impuesto a los Consumos Especiales (ICE)"
 
+#. module: l10n_ec
+#: model:ir.model.fields,help:l10n_ec.field_res_partner__l10n_ec_vat_validation
+#: model:ir.model.fields,help:l10n_ec.field_res_users__l10n_ec_vat_validation
+msgid "Error message when validating the Ecuadorian VAT"
+msgstr "Mensaje de error al validar la Cédula/RUC Ecuatoriana"
+
 #. module: l10n_ec
 #: model:ir.model,name:l10n_ec.model_account_tax
 msgid "Tax"
@@ -312,22 +318,22 @@ msgstr "Desconocido"
 #: code:addons/l10n_ec/models/res_partner.py:0
 #, python-format
 msgid ""
-"VAT %s is not valid for an Ecuadorian DNI, it must be like this form "
-"1234567897"
+"The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+"(could be an old VAT number)"
 msgstr ""
-"Cédula %s no es valida para el DNI de Ecuador, debe ser de esta forma "
-"1234567897"
+"La Cédula %s parece no ser válida ya que el décimo dígito no cumple con el algoritmo de validación "
+"(podría ser un número de cédula antiguo)"
 
 #. module: l10n_ec
 #. odoo-python
 #: code:addons/l10n_ec/models/res_partner.py:0
 #, python-format
 msgid ""
-"VAT %s is not valid for an Ecuadorian company, it must be like this form "
-"1234567897001"
+"The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+"(SRI has stated that this validation is not required anymore for some VAT numbers)"
 msgstr ""
-"RUC %s no es valida para una compañía ecuatoriana, debe ser de esta forma "
-"1234567897001"
+"El RUC %s parece no ser válido ya que el décimo dígito no cumple con el algoritmo de validación "
+"(SRI ha declarado que esta validación ya no es necesaria para algunos números de RUC)"
 
 #. module: l10n_ec
 #: model:account.tax.group,name:l10n_ec.tax_group_vat0
@@ -353,6 +359,12 @@ msgstr "IVA 14%"
 msgid "VAT 8%"
 msgstr ""
 
+#. module: l10n_ec
+#: model:ir.model.fields,field_description:l10n_ec.field_res_partner__l10n_ec_vat_validation
+#: model:ir.model.fields,field_description:l10n_ec.field_res_users__l10n_ec_vat_validation
+msgid "VAT Error message validation"
+msgstr "Mensaje de error de validación de Cédula/RUC"
+
 #. module: l10n_ec
 #: model:account.tax.group,name:l10n_ec.tax_group_vat_exempt
 #: model:ir.model.fields.selection,name:l10n_ec.selection__account_tax_group__l10n_ec_type__exempt_vat
diff --git a/addons/l10n_ec/i18n/l10n_ec.pot b/addons/l10n_ec/i18n/l10n_ec.pot
index 9a20a136f110..f591b4f0e7af 100644
--- a/addons/l10n_ec/i18n/l10n_ec.pot
+++ b/addons/l10n_ec/i18n/l10n_ec.pot
@@ -276,6 +276,12 @@ msgstr ""
 msgid "Special Consumptions Tax (ICE)"
 msgstr ""
 
+#. module: l10n_ec
+#: model:ir.model.fields,help:l10n_ec.field_res_partner__l10n_ec_vat_validation
+#: model:ir.model.fields,help:l10n_ec.field_res_users__l10n_ec_vat_validation
+msgid "Error message when validating the Ecuadorian VAT"
+msgstr ""
+
 #. module: l10n_ec
 #: model:ir.model,name:l10n_ec.model_account_tax
 msgid "Tax"
@@ -332,8 +338,8 @@ msgstr ""
 #: code:addons/l10n_ec/models/res_partner.py:0
 #, python-format
 msgid ""
-"VAT %s is not valid for an Ecuadorian DNI, it must be like this form "
-"1234567897"
+"The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+"(could be an old VAT number)"
 msgstr ""
 
 #. module: l10n_ec
@@ -341,8 +347,8 @@ msgstr ""
 #: code:addons/l10n_ec/models/res_partner.py:0
 #, python-format
 msgid ""
-"VAT %s is not valid for an Ecuadorian company, it must be like this form "
-"1234567897001"
+"The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+"(SRI has stated that this validation is not required anymore for some VAT numbers)"
 msgstr ""
 
 #. module: l10n_ec
@@ -369,6 +375,12 @@ msgstr ""
 msgid "VAT 8%"
 msgstr ""
 
+#. module: l10n_ec
+#: model:ir.model.fields,field_description:l10n_ec.field_res_partner__l10n_ec_vat_validation
+#: model:ir.model.fields,field_description:l10n_ec.field_res_users__l10n_ec_vat_validation
+msgid "VAT Error message validation"
+msgstr ""
+
 #. module: l10n_ec
 #: model:account.tax.group,name:l10n_ec.tax_group_vat_exempt
 #: model:ir.model.fields.selection,name:l10n_ec.selection__account_tax_group__l10n_ec_type__exempt_vat
diff --git a/addons/l10n_ec/models/res_partner.py b/addons/l10n_ec/models/res_partner.py
index 9a1dc68bbeec..2bad55eb33ad 100644
--- a/addons/l10n_ec/models/res_partner.py
+++ b/addons/l10n_ec/models/res_partner.py
@@ -1,8 +1,8 @@
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
 import enum
-
-from odoo import _, api, models
+import stdnum
+from odoo import _, api, fields, models
 from odoo.exceptions import ValidationError
 
 
@@ -50,6 +50,12 @@ class ResPartner(models.Model):
 
     _inherit = "res.partner"
 
+    l10n_ec_vat_validation = fields.Char(
+        string="VAT Error message validation",
+        compute="_compute_l10n_ec_vat_validation",
+        help="Error message when validating the Ecuadorian VAT",
+    )
+
     @api.constrains("vat", "country_id", "l10n_latam_identification_type_id")
     def check_vat(self):
         it_ruc = self.env.ref("l10n_ec.ec_ruc", False)
@@ -69,22 +75,26 @@ class ResPartner(models.Model):
                     if partner.l10n_latam_identification_type_id.id == it_ruc.id and len(partner.vat) != 13:
                         raise ValidationError(_('If your identification type is %s, it must be 13 digits')
                                               % it_ruc.display_name)
-                    final_consumer = verify_final_consumer(partner.vat)
-                    if final_consumer:
-                        valid = True
-                    else:
-                        valid = self.is_valid_ruc_ec(partner.vat)
-                    if not valid:
-                        error_message = ""
-                        if partner.l10n_latam_identification_type_id.id == it_dni.id:
-                            error_message = _("VAT %s is not valid for an Ecuadorian DNI, "
-                                              "it must be like this form 1234567897") % partner.vat
-                        if partner.l10n_latam_identification_type_id.id == it_ruc.id:
-                            error_message = _("VAT %s is not valid for an Ecuadorian company, "
-                                              "it must be like this form 1234567897001") % partner.vat
-                        raise ValidationError(error_message)
         return super(ResPartner, self - ecuadorian_partners).check_vat()
 
+    @api.depends("vat", "country_id", "l10n_latam_identification_type_id")
+    def _compute_l10n_ec_vat_validation(self):
+        it_ruc = self.env.ref("l10n_ec.ec_ruc", False)
+        it_dni = self.env.ref("l10n_ec.ec_dni", False)
+        ruc = stdnum.util.get_cc_module("ec", "ruc")
+        ci = stdnum.util.get_cc_module("ec", "ci")
+        for partner in self:
+            partner.l10n_ec_vat_validation = False
+            if partner and partner.l10n_latam_identification_type_id in (it_ruc, it_dni) and partner.vat:
+                final_consumer = verify_final_consumer(partner.vat)
+                if not final_consumer:
+                    if partner.l10n_latam_identification_type_id.id == it_dni.id and not ci.is_valid(partner.vat):
+                        partner.l10n_ec_vat_validation = _("The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+                                                           "(could be an old VAT number)") % partner.vat
+                    if partner.l10n_latam_identification_type_id.id == it_ruc.id and not ruc.is_valid(partner.vat):
+                        partner.l10n_ec_vat_validation = _("The VAT %s seems to be invalid as the tenth digit doesn't comply with the validation algorithm "
+                                                           "(SRI has stated that this validation is not required anymore for some VAT numbers)") % partner.vat
+
     def _l10n_ec_get_identification_type(self):
         """Maps Odoo identification types to Ecuadorian ones.
         Useful for document type domains, electronic documents, ats, others.
diff --git a/addons/l10n_ec/views/res_partner_view.xml b/addons/l10n_ec/views/res_partner_view.xml
new file mode 100644
index 000000000000..65d7faeccd33
--- /dev/null
+++ b/addons/l10n_ec/views/res_partner_view.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+    <record id="view_partner_form" model="ir.ui.view">
+        <field name="name">res.partner.form</field>
+        <field name="model">res.partner</field>
+        <field name="inherit_id" ref="base.view_partner_form"/>
+        <field name="arch" type="xml">
+            <div attrs="{'invisible': [('same_vat_partner_id', '=', False)]}" position="after">
+                <div class="alert alert-warning" role="alert" attrs="{'invisible': [('l10n_ec_vat_validation', '=', False)]}">
+                    <field name="l10n_ec_vat_validation"/>
+                </div>
+            </div>
+        </field>
+    </record>
+</odoo>
\ No newline at end of file
-- 
GitLab