2017-06-23 19:53:23 +02:00
|
|
|
# Copyright 2015 Tecnativa - Antonio Espinosa
|
|
|
|
# Copyright 2017 Tecnativa - David Vidal
|
2019-01-10 12:14:00 +01:00
|
|
|
# Copyright 2019 FactorLibre - Rodrigo Bonilla
|
2022-06-06 19:03:27 +02:00
|
|
|
# Copyright 2022 Moduon - Eduardo de Miguel
|
2023-08-12 12:45:34 +02:00
|
|
|
# Copyright 2023 Moduon - Emilio Pascual
|
2020-05-09 19:29:26 +02:00
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
2022-06-06 19:03:27 +02:00
|
|
|
from odoo import _, api, fields, models
|
|
|
|
|
|
|
|
from odoo.addons.base_vat.models.res_partner import _ref_vat
|
2015-10-06 11:10:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
2020-05-09 19:29:26 +02:00
|
|
|
_inherit = "res.partner"
|
2015-10-06 11:10:40 +02:00
|
|
|
|
2020-05-09 19:29:26 +02:00
|
|
|
vies_passed = fields.Boolean(string="VIES validation", readonly=True)
|
2015-10-06 11:10:40 +02:00
|
|
|
|
2017-06-23 19:53:23 +02:00
|
|
|
@api.model
|
|
|
|
def simple_vat_check(self, country_code, vat_number):
|
2023-01-26 15:08:20 +01:00
|
|
|
res = super().simple_vat_check(country_code, vat_number)
|
|
|
|
partner = self._context.get("vat_partner")
|
2023-01-31 02:05:31 +01:00
|
|
|
if res is False and partner:
|
2020-05-09 19:29:26 +02:00
|
|
|
partner.update({"vies_passed": False})
|
2023-01-31 02:05:31 +01:00
|
|
|
elif partner:
|
|
|
|
if self.env.context.get("company_id"):
|
|
|
|
company = self.env["res.company"].browse(self.env.context["company_id"])
|
|
|
|
else:
|
|
|
|
company = self.env.company
|
|
|
|
if company.vat_check_vies:
|
|
|
|
self.vies_vat_check(country_code, vat_number)
|
2015-10-06 11:10:40 +02:00
|
|
|
return res
|
|
|
|
|
2017-06-23 19:53:23 +02:00
|
|
|
@api.model
|
|
|
|
def vies_vat_check(self, country_code, vat_number):
|
2023-01-26 15:08:20 +01:00
|
|
|
partner = self._context.get("vat_partner")
|
|
|
|
res = super().vies_vat_check(country_code, vat_number)
|
2023-01-31 02:05:31 +01:00
|
|
|
if res is False and partner:
|
|
|
|
partner.update({"vies_passed": False})
|
2022-01-05 08:08:31 +01:00
|
|
|
elif partner:
|
|
|
|
partner.update({"vies_passed": True})
|
2015-10-06 11:10:40 +02:00
|
|
|
return res
|
|
|
|
|
2020-05-09 19:31:26 +02:00
|
|
|
@api.constrains("vat", "country_id")
|
2017-06-23 19:53:23 +02:00
|
|
|
def check_vat(self):
|
2022-06-06 19:03:27 +02:00
|
|
|
self.update({"vies_passed": False})
|
2023-01-31 02:05:31 +01:00
|
|
|
for partner in self.sorted(lambda p: bool(p.commercial_partner_id)):
|
2017-06-23 19:53:23 +02:00
|
|
|
partner = partner.with_context(vat_partner=partner)
|
2023-01-31 02:05:31 +01:00
|
|
|
if (
|
|
|
|
partner.commercial_partner_id
|
|
|
|
and partner.commercial_partner_id != partner
|
|
|
|
):
|
|
|
|
partner.update(
|
|
|
|
{"vies_passed": partner.commercial_partner_id.vies_passed}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
super(ResPartner, partner).check_vat()
|
2021-07-03 02:16:21 +02:00
|
|
|
return True
|
2022-06-06 19:03:27 +02:00
|
|
|
|
2023-01-31 02:05:31 +01:00
|
|
|
@api.onchange("vat", "country_id")
|
|
|
|
def _onchange_check_vies(self):
|
|
|
|
self.update({"vies_passed": False})
|
|
|
|
return super(
|
|
|
|
ResPartner, self.with_context(vat_partner=self)
|
|
|
|
)._onchange_check_vies()
|
|
|
|
|
2022-06-06 19:03:27 +02:00
|
|
|
@api.model
|
|
|
|
def _build_vat_error_message(self, country_code, wrong_vat, record_label):
|
|
|
|
return "\n" + _(
|
|
|
|
"The VAT number [%(wrong_vat)s] for %(record_label)s does not seem to be valid. "
|
|
|
|
"\nNote: the expected format is %(expected_format)s",
|
|
|
|
wrong_vat=wrong_vat,
|
|
|
|
record_label=record_label,
|
|
|
|
expected_format=_ref_vat.get(
|
|
|
|
country_code, "'CC##' (CC=Country Code, ##=VAT Number)"
|
|
|
|
),
|
|
|
|
)
|