Skip to content

Commit

Permalink
[IMP] transfer_client_portfolio: tests + pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisho99 committed Sep 17, 2024
1 parent cd849d9 commit 355ca75
Show file tree
Hide file tree
Showing 19 changed files with 718 additions and 63 deletions.
Empty file modified transfer_client_portfolio/README.rst
100755 → 100644
Empty file.
Empty file modified transfer_client_portfolio/__init__.py
100755 → 100644
Empty file.
Empty file modified transfer_client_portfolio/__manifest__.py
100755 → 100644
Empty file.
1 change: 0 additions & 1 deletion transfer_client_portfolio/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
- Ángel García de la Chica Herrera <angel.garcia@sygel.es>
- Alberto Martínez <alberto.martinez@sygel.es>
- Roger Sans <roger.sans@sygel.es>

2 changes: 1 addition & 1 deletion transfer_client_portfolio/readme/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Open the CRM application and then
Select the desired opportunities in list mode and click on "Action" button and select "Transfer selected opportunities".
In the wizard select if you want to update the salesperson on the contacts too and the new Salesperson.
Select the contacts/opportunities you want to transfer and click on transfer.
One they have been transferred you can view the Record and see the previous salesperson in the 'Transfer Clients Portfolios'.
One they have been transferred you can view the Record and see the previous salesperson in the 'Transfer Clients Portfolios'.
4 changes: 4 additions & 0 deletions transfer_client_portfolio/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Roger Sans <roger.sans@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import test_transfer_client_portfolio
146 changes: 146 additions & 0 deletions transfer_client_portfolio/tests/test_transfer_client_portfolio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Copyright 2024 Roger Sans <roger.sans@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.addons.crm.tests.common import TestCrmCommon


class TestTransferClientPortfolio(TestCrmCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.adm_user = cls.env.ref("base.user_admin")
cls.demo_user = cls.env.ref("base.user_demo")
cls.company_id = cls.env.company

cls.partner = cls.env["res.partner"].create(
{
"name": "Test",
"email": cls.test_email_data[0],
"is_company": True,
"street": "123 Backer Street",
"city": "london",
"country_id": cls.env.ref("base.us").id,
"zip": "12345",
"user_id": cls.demo_user.id,
}
)

cls.lead = cls.env["crm.lead"].create(
{
"name": "Nibbler Spacecraft Request",
"type": "lead",
"user_id": cls.demo_user.id,
"partner_id": cls.partner.id,
"contact_name": "Amy Wong",
"email_from": "amy.wong@test.example.com",
"country_id": cls.env.ref("base.us").id,
"probability": 20,
}
)

cls.activity_type = cls.env["mail.activity.type"].create(
{
"name": "Call for Demo",
"delay_count": 6,
"summary": "ACT 2 : I want to show you my ERP !",
"res_model": "crm.lead",
}
)
cls.opor_activity = (
cls.env["mail.activity"]
.with_user(cls.demo_user)
.create(
{
"activity_type_id": cls.activity_type.id,
"note": "Content of the activity to log oports",
"res_id": cls.lead.id,
"res_model_id": cls.env.ref("crm.model_crm_lead").id,
"user_id": cls.demo_user.id,
}
)
)
cls.partner_activity = (
cls.env["mail.activity"]
.with_user(cls.demo_user)
.create(
{
"activity_type_id": cls.activity_type.id,
"note": "Content of the activity to log partners",
"res_id": cls.partner.id,
"res_model_id": cls.env.ref("base.model_res_partner").id,
"user_id": cls.demo_user.id,
}
)
)

def _execute_transfer(self, context):
transfer = (
self.env["transfer.portfolio.wizard"]
.with_context(**context)
.create(context)
)
transfer.transfer_portfolio()

def test_transfer_client_portfolio(self):
self._execute_transfer(
{
"current_salesperson": self.demo_user.id,
"new_salesperson": self.adm_user.id,
"review_state": True,
"contact_ids": self.partner.ids,
"activity_ids": [self.opor_activity.id, self.partner_activity.id],
"opportunity_ids": [self.lead.id],
"is_server_action": True,
"server_action_type": "lead",
"update_salesperson_contact": True,
"transfer_activities": True,
}
)

self.assertEqual(self.partner.user_id.id, self.adm_user.id)
self.assertEqual(self.lead.user_id.id, self.adm_user.id)
self.assertEqual(self.opor_activity.user_id.id, self.adm_user.id)
self.assertEqual(self.partner_activity.user_id.id, self.adm_user.id)

self._execute_transfer(
{
"current_salesperson": self.adm_user.id,
"new_salesperson": self.demo_user.id,
"update_salesperson_contact": False,
"contact_ids": self.partner.ids,
"activity_ids": [self.opor_activity.id, self.partner_activity.id],
"opportunity_ids": [self.lead.id],
"transfer_activities": True,
}
)

self.assertEqual(self.partner.user_id.id, self.adm_user.id)
self.assertEqual(self.lead.user_id.id, self.demo_user.id)
self.assertEqual(self.opor_activity.user_id.id, self.demo_user.id)
self.assertEqual(self.partner_activity.user_id.id, self.adm_user.id)

self.partner.write({"user_id": self.demo_user.id})
self.lead.write({"user_id": self.demo_user.id})
self.opor_activity.write({"user_id": self.demo_user.id})
self.partner_activity.write({"user_id": self.demo_user.id})

self.assertEqual(self.partner.user_id.id, self.demo_user.id)
self.assertEqual(self.lead.user_id.id, self.demo_user.id)
self.assertEqual(self.opor_activity.user_id.id, self.demo_user.id)
self.assertEqual(self.partner_activity.user_id.id, self.demo_user.id)

self._execute_transfer(
{
"current_salesperson": self.demo_user.id,
"new_salesperson": self.adm_user.id,
"contact_ids": self.partner.ids,
"activity_ids": [self.opor_activity.id, self.partner_activity.id],
"opportunity_ids": [self.lead.id],
"transfer_activities": False,
}
)

self.assertEqual(self.partner.user_id.id, self.adm_user.id)
self.assertEqual(self.lead.user_id.id, self.adm_user.id)
self.assertEqual(self.opor_activity.user_id.id, self.demo_user.id)
self.assertEqual(self.partner_activity.user_id.id, self.demo_user.id)
61 changes: 44 additions & 17 deletions transfer_client_portfolio/wizard/transfer_portfolio_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class TransferPorfolioWizard(models.TransientModel):
opportunity_ids = fields.Many2many(
comodel_name="crm.lead",
string="Opportunities",
domain="[('user_id', '=', current_salesperson), ('stage_id.allow_transfer_opportunity', '=', True)]",
domain="[('user_id', '=', current_salesperson),"
"('stage_id.allow_transfer_opportunity', '=', True)]",
)
activity_ids = fields.Many2many(
comodel_name="mail.activity",
Expand All @@ -53,28 +54,40 @@ def _default_transfer_activities(self):

@api.model
def default_get(self, fields):
res = super(TransferPorfolioWizard, self).default_get(fields)
res = super().default_get(fields)
if self.env.context.get("is_lead_server_action"):
opportunity_ids = self.env.context.get("active_ids")
contact_ids = self.env["crm.lead"].browse(opportunity_ids).mapped("partner_id").ids
contact_ids = (
self.env["crm.lead"].browse(opportunity_ids).mapped("partner_id").ids
)
activity_ids = (
self.env["crm.lead"].browse(opportunity_ids).activity_ids
| self.env["res.partner"].browse(contact_ids).activity_ids
).ids
contact_ids
res.update(
{
"is_server_action": True,
"server_action_type": "lead",
"review_state": True,
"opportunity_ids": [(6, 0, opportunity_ids)],
"contact_ids": [(6, 0, contact_ids,)],
"activity_ids": [(6, 0, activity_ids)]
"contact_ids": [
(
6,
0,
contact_ids,
)
],
"activity_ids": [(6, 0, activity_ids)],
}
)
elif self.env.context.get("is_partner_server_action"):
partner_ids = self.env.context.get("active_ids")
opportunity_ids = self.env["res.partner"].browse(partner_ids).mapped("opportunity_ids").ids
opportunity_ids = (
self.env["res.partner"]
.browse(partner_ids)
.mapped("opportunity_ids")
.ids
)
activity_ids = (
self.env["crm.lead"].browse(opportunity_ids).activity_ids
| self.env["res.partner"].browse(partner_ids).activity_ids
Expand All @@ -86,7 +99,7 @@ def default_get(self, fields):
"review_state": True,
"contact_ids": [(6, 0, partner_ids)],
"opportunity_ids": [(6, 0, opportunity_ids)],
"activity_ids": [(6, 0, activity_ids)]
"activity_ids": [(6, 0, activity_ids)],
}
)
return res
Expand All @@ -102,7 +115,9 @@ def review_transfer(self):
("stage_id.allow_transfer_opportunity", "=", True),
]
)
self.activity_ids = self.opportunity_ids.activity_ids | self.contact_ids.activity_ids
self.activity_ids = (
self.opportunity_ids.activity_ids | self.contact_ids.activity_ids
)
self.review_state = True
return {
"name": "Transfer Portfolio",
Expand Down Expand Up @@ -160,31 +175,39 @@ def transfer_portfolio(self):
user_id = o.get("user_id")[0] if bool(o.get("user_id")) else False
records_by_salesperson[user_id] = {
"opt": self.opportunity_ids.filtered(
lambda x: x.user_id.id == user_id
lambda x, user_id=user_id: x.user_id.id == user_id
).ids,
"contacts": [],
"activities": []
"activities": [],
}

for p in partners_grouped:
user_id = p.get("user_id")[0] if bool(p.get("user_id")) else False
contacts = self.contact_ids.filtered(
lambda x: x.user_id.id == user_id
lambda x, user_id=user_id: x.user_id.id == user_id
).ids
if user_id in records_by_salesperson:
records_by_salesperson[user_id].update({"contacts": contacts})
else:
records_by_salesperson[user_id] = {"opt": [], "contacts": contacts, "activities": []}
records_by_salesperson[user_id] = {
"opt": [],
"contacts": contacts,
"activities": [],
}

for a in activities_grouped:
user_id = a.get("user_id")[0] if bool(a.get("user_id")) else False
activities = self.activity_ids.filtered(
lambda x: x.user_id.id == user_id
lambda x, user_id=user_id: x.user_id.id == user_id
).ids
if user_id in records_by_salesperson:
records_by_salesperson[user_id].update({"activities": activities})
else:
records_by_salesperson[user_id] = {"opt": [], "contacts": [], "activities": activities}
records_by_salesperson[user_id] = {
"opt": [],
"contacts": [],
"activities": activities,
}

ptr_vals = []
for rbs in records_by_salesperson:
Expand Down Expand Up @@ -240,7 +263,9 @@ def transfer_portfolio(self):
)

def clear_records(self):
self.write({"contact_ids": False, "opportunity_ids": False, "activity_ids": False})
self.write(
{"contact_ids": False, "opportunity_ids": False, "activity_ids": False}
)
return {
"name": "Transfer Portfolio",
"view_mode": "form",
Expand All @@ -253,7 +278,9 @@ def clear_records(self):
"res_id": self.id,
}

def _get_vals_transfer_registry(self, vals_def={}):
def _get_vals_transfer_registry(self, vals_def=None):
if vals_def is None:
vals_def = {}
return {
"user_made_transfer_id": self.env.user.id,
"date_transfer": datetime.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
/>
<field name="is_server_action" invisible="1" />
<field name="update_salesperson_contact" />
<field
name="transfer_activities"
/>
<field name="transfer_activities" />
</group>
<group>
<field
Expand Down
Loading

0 comments on commit 355ca75

Please sign in to comment.