Skip to content

Commit

Permalink
[MIG] partner_contact_sale_info_propagation: Migration to 17.0
Browse files Browse the repository at this point in the history
TT52337
  • Loading branch information
juancarlosonate-tecnativa committed Feb 21, 2025
1 parent 0dda1f3 commit 9517021
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 128 deletions.
15 changes: 8 additions & 7 deletions partner_contact_sale_info_propagation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ Partner contact sale info propagation

|badge1| |badge2| |badge3| |badge4| |badge5|

This module propagates Salesperson and Sales Channel from Company to
This module propagates Salesperson and Sales Teams from Company to
Contacts

- Put the *Salesperson* or *Sales Channel* of the parent company when
the contact doesn't have a *Salesperson* or *Sales Channel* and this
parent company is assigned.
- Put the *Salesperson* or *Sales Teams* of the parent company when the
contact doesn't have a *Salesperson* or *Sales Teams* and this parent
company is assigned.
- When the company changes the *Salesperson*, it fills with the same
*Salesperson* all the contacts that don't have any or have the
previous *Salesperson* of the parent company.
- When the company changes the *Sales Channel*, it fills with the same
*Sales Channel* all the contacts that don't have any or have the
previous *Sales Channel* of the parent company.
- When the company changes the *Sales Teams*, it fills with the same
*Sales Teams* all the contacts that don't have any or have the
previous *Sales Teams* of the parent company.

**Table of contents**

Expand Down Expand Up @@ -86,6 +86,7 @@ Contributors
- Ernesto Tejeda
- Pedro M. Baeza
- César A. Sánchez
- Juan Carlos Oñate

Maintainers
-----------
Expand Down
5 changes: 2 additions & 3 deletions partner_contact_sale_info_propagation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Partner contact sale info propagation",
"summary": "Propagate Salesperson and Sales Channel " "from Company to Contacts",
"version": "16.0.1.0.1",
"summary": "Propagate Salesperson and Sales Teams from Company to Contacts",
"version": "17.0.1.0.0",
"category": "Sales Management",
"website": "https://github.com/OCA/sale-workflow",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["sales_team"],
"data": ["views/res_partner_view.xml"],
}
59 changes: 16 additions & 43 deletions partner_contact_sale_info_propagation/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from lxml import etree

from odoo import api, models
from odoo.tools import config

Expand All @@ -18,67 +15,43 @@ def _check_propagation_allowed(self):
)

def write(self, vals):
"""Propagate Salesperson and Sales Channel change in the partner to the
child contacts."""
"""
Propagate Salesperson and Sales Channel change in the partner to the
child contacts, and ensure team_id is inherited from parent_id.
"""
if not self._check_propagation_allowed():
return super().write(vals)
for record in self:
if "user_id" in vals:
childs = record.mapped("child_ids").filtered(
lambda r: not r.user_id or r.user_id == record.user_id
lambda r, user_id=record.user_id: not r.user_id
or r.user_id == user_id
)
if childs:
childs.write({"user_id": vals["user_id"]})
if "team_id" in vals:
childs = record.mapped("child_ids").filtered(
lambda r: not r.team_id or r.team_id == record.team_id
lambda r, team_id=record.team_id: not r.team_id
or r.team_id == team_id
)
if childs:
childs.write({"team_id": vals["team_id"]})
if "parent_id" in vals and "team_id" not in vals:
new_parent = self.browse(vals["parent_id"])
if new_parent.team_id:
vals["team_id"] = new_parent.team_id.id
return super().write(vals)

@api.model_create_multi
def create(self, vals_list):
"""
Ensure that when a contact is created under a company,
it inherits the team_id
"""
if not self._check_propagation_allowed():
return super().create(vals_list)
for vals in vals_list:
if "parent_id" in vals:
if "user_id" not in vals:
vals.update(user_id=self.browse(vals["parent_id"]).user_id.id)
if "team_id" not in vals:
vals.update(team_id=self.browse(vals["parent_id"]).team_id.id)
return super().create(vals_list)

@api.onchange("parent_id")
def onchange_parent_id(self):
"""Change Salesperson or Sales Channel if the parent company changes
and there's no Salesperson or Sales Channel defined yet"""
res = super().onchange_parent_id()
if self.parent_id and self.parent_id != self:
parent = self.parent_id
if not self.user_id:
res.setdefault("value", {}).update(user_id=parent.user_id)
if not self.team_id:
res.setdefault("value", {}).update(team_id=parent.team_id)
return res

@api.model
def get_view(self, view_id=None, view_type="form", **options):
res = super().get_view(
view_id=view_id,
view_type=view_type,
**options,
)
if view_type == "form":
partner_xml = etree.XML(res["arch"])
partner_fields = partner_xml.xpath("//field[@name='child_ids']")
if partner_fields:
partner_field = partner_fields[0]
context = partner_field.attrib.get("context", "{}").replace(
"{",
"{'default_user_id': user_id, 'default_team_id': team_id,",
1,
)
partner_field.attrib["context"] = context
res["arch"] = etree.tostring(partner_xml)
return res
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
- Ernesto Tejeda
- Pedro M. Baeza
- César A. Sánchez
- Juan Carlos Oñate
12 changes: 6 additions & 6 deletions partner_contact_sale_info_propagation/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
This module propagates Salesperson and Sales Channel from Company to
This module propagates Salesperson and Sales Teams from Company to
Contacts

- Put the *Salesperson* or *Sales Channel* of the parent company when
the contact doesn't have a *Salesperson* or *Sales Channel* and this
- Put the *Salesperson* or *Sales Teams* of the parent company when
the contact doesn't have a *Salesperson* or *Sales Teams* and this
parent company is assigned.
- When the company changes the *Salesperson*, it fills with the same
*Salesperson* all the contacts that don't have any or have the
previous *Salesperson* of the parent company.
- When the company changes the *Sales Channel*, it fills with the same
*Sales Channel* all the contacts that don't have any or have the
previous *Sales Channel* of the parent company.
- When the company changes the *Sales Teams*, it fills with the same
*Sales Teams* all the contacts that don't have any or have the
previous *Sales Teams* of the parent company.
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,18 @@ <h1 class="title">Partner contact sale info propagation</h1>
!! source digest: sha256:f75211609c822c9b2fb4353623c3408602d1e287879be5f7f4d0a5eb5eb5a274
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-workflow/tree/17.0/partner_contact_sale_info_propagation"><img alt="OCA/sale-workflow" src="https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-workflow-17-0/sale-workflow-17-0-partner_contact_sale_info_propagation"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module propagates Salesperson and Sales Channel from Company to
<p>This module propagates Salesperson and Sales Teams from Company to
Contacts</p>
<ul class="simple">
<li>Put the <em>Salesperson</em> or <em>Sales Channel</em> of the parent company when
the contact doesn’t have a <em>Salesperson</em> or <em>Sales Channel</em> and this
parent company is assigned.</li>
<li>Put the <em>Salesperson</em> or <em>Sales Teams</em> of the parent company when the
contact doesn’t have a <em>Salesperson</em> or <em>Sales Teams</em> and this parent
company is assigned.</li>
<li>When the company changes the <em>Salesperson</em>, it fills with the same
<em>Salesperson</em> all the contacts that don’t have any or have the
previous <em>Salesperson</em> of the parent company.</li>
<li>When the company changes the <em>Sales Channel</em>, it fills with the same
<em>Sales Channel</em> all the contacts that don’t have any or have the
previous <em>Sales Channel</em> of the parent company.</li>
<li>When the company changes the <em>Sales Teams</em>, it fills with the same
<em>Sales Teams</em> all the contacts that don’t have any or have the
previous <em>Sales Teams</em> of the parent company.</li>
</ul>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
Expand Down Expand Up @@ -433,6 +433,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>Ernesto Tejeda</li>
<li>Pedro M. Baeza</li>
<li>César A. Sánchez</li>
<li>Juan Carlos Oñate</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
# Copyright 2019 Tecnativa - Ernesto Tejeda
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase

from lxml import etree

from odoo.tests.common import Form, TransactionCase


class TestPartnerContactSaleInfoPropagation(TransactionCase):
class TestResPartner(TransactionCase):
def setUp(self):
super().setUp()
self.partner_model = self.env["res.partner"].with_context(test_propagation=True)
self.parent_company = self.partner_model.create(
self.salesperson = self.env["res.users"].create(
{
"name": "Test Salesperson",
"login": "[email protected]",
}
)
self.team = self.env["crm.team"].create({"name": "Sales Team A"})
self.company = self.partner_model.create(
{
"name": "Parent company",
"name": "Company A",
"company_type": "company",
"user_id": self.ref("base.user_demo"),
"team_id": self.ref("sales_team.crm_team_1"),
"user_id": self.salesperson.id,
"team_id": self.team.id,
}
)

def check_same_user_id_team_id(self, parent, child):
self.assertEqual(parent.user_id, child.user_id)
self.assertEqual(parent.team_id, child.team_id)

def test_create_partner_child(self):
partner_child = self.partner_model.create(
{"name": "Parent child", "parent_id": self.parent_company.id}
self.child_contact = self.partner_model.create(
{
"name": "Child Contact",
"parent_id": self.company.id,
}
)
self.check_same_user_id_team_id(self.parent_company, partner_child)

def test_write_parent_company(self):
partner_child = self.partner_model.create(
{"name": "Parent child", "parent_id": self.parent_company.id}
)
self.parent_company.write(
def test_propagate_user_id(self):
"""Test that changing user_id propagates to child contacts"""
new_salesperson = self.env["res.users"].create(
{
"user_id": self.ref("base.demo_user0"),
"team_id": self.ref("sales_team.team_sales_department"),
"name": "New Salesperson",
"login": "[email protected]",
}
)
self.check_same_user_id_team_id(self.parent_company, partner_child)
self.company.write({"user_id": new_salesperson.id})
self.assertEqual(self.child_contact.user_id, new_salesperson)

partner_child.write({"user_id": False, "team_id": False})
self.parent_company.write(
def test_propagate_team_id(self):
"""Test that changing team_id propagates to child contacts"""
new_team = self.env["crm.team"].create({"name": "Sales Team B"})
self.company.write({"team_id": new_team.id})
self.assertEqual(self.child_contact.team_id, new_team)

def test_inherit_team_id_on_creation(self):
"""Test that a new contact inherits team_id from parent"""
new_contact = self.partner_model.create(
{
"user_id": self.ref("base.user_demo"),
"team_id": self.ref("sales_team.crm_team_1"),
"name": "New Contact",
"parent_id": self.company.id,
}
)
self.check_same_user_id_team_id(self.parent_company, partner_child)

def test_onchange_parent_id_with_values_false(self):
form_partner = Form(self.env["res.partner"])
with form_partner as form:
form.name = self.parent_company.name
form.parent_id = self.parent_company
form_partner.save()
self.assertEqual(form_partner.user_id, self.parent_company.user_id)
self.assertEqual(form_partner.team_id, self.parent_company.team_id)
self.assertEqual(new_contact.team_id, self.team)

def test_fields_view_get(self):
partner_xml = etree.XML(self.partner_model.get_view()["arch"])
partner_field = partner_xml.xpath("//field[@name='child_ids']")[0]
context = partner_field.attrib.get("context", "{}")
sub_ctx = "'default_user_id': user_id, 'default_team_id': team_id,"
self.assertIn(sub_ctx, context)
def test_change_parent_id_updates_team_id(self):
"""Test that changing parent_id updates team_id if not set"""
new_company = self.partner_model.create(
{
"name": "Company B",
"company_type": "company",
"team_id": self.team.id,
}
)
self.child_contact.write({"parent_id": new_company.id})
self.assertEqual(self.child_contact.team_id, self.team)
16 changes: 0 additions & 16 deletions partner_contact_sale_info_propagation/views/res_partner_view.xml

This file was deleted.

0 comments on commit 9517021

Please sign in to comment.