Skip to content

Commit

Permalink
Merge PR #511 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by rafaelbn
  • Loading branch information
OCA-git-bot committed Aug 2, 2023
2 parents be8d5fa + 5155a6f commit 4ea1adf
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
63 changes: 60 additions & 3 deletions crm_lead_vat/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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."""
Expand Down
2 changes: 2 additions & 0 deletions crm_lead_vat/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Stefan Ungureanu

* Emilio Pascual (`Moduon <https://www.moduon.team/>`__)
2 changes: 2 additions & 0 deletions crm_lead_vat/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 9 additions & 0 deletions crm_lead_vat/tests/test_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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)
32 changes: 28 additions & 4 deletions crm_lead_vat/views/crm_lead_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_lead_view_form" />
<field name="arch" type="xml">
<xpath expr="//group/group[1]/field[@name='partner_id']" position="after">
<field name="vat" />
<xpath
expr="//group/group[@name='lead_partner']/field[@name='partner_id']"
position="after"
>
<label for="vat" class="oe_inline" />
<div class="o_row o_row_readonly">
<field name="partner_vat_update" invisible="1" />
<field name="vat" />
<span
class="fa fa-exclamation-triangle text-warning oe_edit_only"
title="By saving this change, the customer vat number will also be updated."
attrs="{'invisible': [('partner_vat_update', '=', False)]}"
/>
</div>
</xpath>
<xpath expr="//group/group[2]/field[@name='partner_id']" position="after">
<field name="vat" />
<xpath
expr="//group/group[@name='opportunity_partner']/field[@name='partner_id']"
position="after"
>
<label for="vat" class="oe_inline" />
<div class="o_row o_row_readonly">
<field name="partner_vat_update" invisible="1" />
<field name="vat" />
<span
class="fa fa-exclamation-triangle text-warning oe_edit_only"
title="By saving this change, the customer vat number will also be updated."
attrs="{'invisible': [('partner_vat_update', '=', False)]}"
/>
</div>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 4ea1adf

Please sign in to comment.