From 5155a6f40ea787c27922fc7978f836a1adcfd782 Mon Sep 17 00:00:00 2001 From: Emilio Pascual Date: Fri, 28 Jul 2023 16:41:20 +0200 Subject: [PATCH] [15.0][IMP] Sync vat between partner and lead - Add behavior to sync vat between partner and lead. @moduon MT-3130 @rafaelbn Co-authored-by: Rafael Blasco <8736623+rafaelbn@users.noreply.github.com> --- crm_lead_vat/models/crm_lead.py | 63 +++++++++++++++++++++++++-- crm_lead_vat/readme/CONTRIBUTORS.rst | 2 + crm_lead_vat/readme/USAGE.rst | 2 + crm_lead_vat/tests/test_lead.py | 9 ++++ crm_lead_vat/views/crm_lead_views.xml | 32 ++++++++++++-- 5 files changed, 101 insertions(+), 7 deletions(-) diff --git a/crm_lead_vat/models/crm_lead.py b/crm_lead_vat/models/crm_lead.py index d117a688622c..988a86fb676e 100644 --- a/crm_lead_vat/models/crm_lead.py +++ b/crm_lead_vat/models/crm_lead.py @@ -1,7 +1,7 @@ # Copyright 2015 Antiun IngenierĂ­a, S.L. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import api, fields, models class Lead(models.Model): @@ -11,11 +11,68 @@ class Lead(models.Model): string="TIN", help="Tax Identification Number. The first 2 characters are the " "country code.", + compute="_compute_vat", + inverse="_inverse_vat", + readonly=False, + store=True, ) + partner_vat_update = fields.Boolean( + "Partner VAT will Update", compute="_compute_partner_vat_update" + ) + + @api.depends("partner_id.vat", "partner_id.commercial_partner_id.vat") + def _compute_vat(self): + for lead in self: + if ( + lead.partner_id.commercial_partner_id.vat + and lead._get_partner_vat_update() + ): + lead.vat = lead.partner_id.commercial_partner_id.vat + elif lead.partner_id.vat and lead._get_partner_vat_update(): + lead.vat = lead.partner_id.vat + + def _inverse_vat(self): + for lead in self: + if lead._get_partner_vat_update(): + if lead.partner_id.commercial_partner_id: + lead.partner_id.commercial_partner_id.vat = lead.vat + else: + lead.partner_id.vat = lead.vat + + @api.depends("vat", "partner_id") + def _compute_partner_vat_update(self): + for lead in self: + lead.partner_vat_update = lead._get_partner_vat_update() + + def _get_partner_vat_update(self): + """Calculate if we should write the vat on the related partner. When + the vat of the lead / partner is an empty string, we force it to False + to not propagate a False on an empty string. + + Done in a separate method so it can be used in both ribbon and inverse + and compute of vat update methods. + """ + self.ensure_one() + if ( + self.partner_id.commercial_partner_id + and self.vat != self.partner_id.commercial_partner_id.vat + ): + return self.vat != self.partner_id.commercial_partner_id.vat + elif self.partner_id and self.vat != self.partner_id.vat: + return self.vat != self.partner_id.vat + return False - def _create_customer(self): + def _prepare_customer_values(self, partner_name, is_company=False, parent_id=False): """Add VAT to partner.""" - return super(Lead, self.with_context(default_vat=self.vat))._create_customer() + res = super(Lead, self)._prepare_customer_values( + partner_name, is_company, parent_id + ) + res.update( + { + "vat": self.vat, + } + ) + return res def _prepare_values_from_partner(self, partner): """Recover VAT from partner if available.""" diff --git a/crm_lead_vat/readme/CONTRIBUTORS.rst b/crm_lead_vat/readme/CONTRIBUTORS.rst index 2f3bb28bc816..42a50e801fb4 100644 --- a/crm_lead_vat/readme/CONTRIBUTORS.rst +++ b/crm_lead_vat/readme/CONTRIBUTORS.rst @@ -5,3 +5,5 @@ * `Tecnativa `_: * Stefan Ungureanu + +* Emilio Pascual (`Moduon `__) diff --git a/crm_lead_vat/readme/USAGE.rst b/crm_lead_vat/readme/USAGE.rst index ad131e835196..f2c935f8ad5d 100644 --- a/crm_lead_vat/readme/USAGE.rst +++ b/crm_lead_vat/readme/USAGE.rst @@ -3,3 +3,5 @@ To use this module, you need to: * Go to *CRM > Leads*. * Open a lead. * You will see the new field. +* If you change vat in partner, it change in lead. +* If you change vat in lead, a warning will show informing you that if you save, vat change in partner. diff --git a/crm_lead_vat/tests/test_lead.py b/crm_lead_vat/tests/test_lead.py index 2b0a3d04b5b2..eb8af085f4fc 100644 --- a/crm_lead_vat/tests/test_lead.py +++ b/crm_lead_vat/tests/test_lead.py @@ -12,6 +12,7 @@ def setUp(self): ) self.partner = self.env["res.partner"].create({"name": __file__}) self.test_field = "ES98765432M" + self.test2_field = "11111111H" def test_transfered_values(self): """Field gets transfered when creating partner.""" @@ -27,3 +28,11 @@ def test_onchange_partner_id(self): self.lead.partner_id = self.partner result = self.lead._prepare_values_from_partner(self.lead.partner_id) self.assertEqual(result["vat"], self.test_field) + + def test_onchange_vat(self): + """First change vat in partner, after it change in lead""" + self.lead.partner_id = self.partner + self.partner.vat = self.test_field + self.assertEqual(self.partner.vat, self.lead.vat) + self.lead.vat = self.test2_field + self.assertEqual(self.partner.vat, self.lead.vat) diff --git a/crm_lead_vat/views/crm_lead_views.xml b/crm_lead_vat/views/crm_lead_views.xml index 43b5a9f83a75..119c1ca73952 100644 --- a/crm_lead_vat/views/crm_lead_views.xml +++ b/crm_lead_vat/views/crm_lead_views.xml @@ -4,11 +4,35 @@ crm.lead - - + + - - + +