From 06ea65589bded470d0d8a6c60e401ccda28ca1fb Mon Sep 17 00:00:00 2001 From: Emilio Pascual Date: Tue, 25 Jul 2023 15:23:01 +0200 Subject: [PATCH] [15.0][ADD] link between lead and task - Added field lead_id in task. - Added field task_ids in lead. - Added smart button in lead to show related tasks. --- crm_project_task/__manifest__.py | 1 + crm_project_task/i18n/crm_project_task.pot | 27 ++++++++++++- crm_project_task/models/__init__.py | 2 + crm_project_task/models/crm_lead.py | 24 ++++++++++++ crm_project_task/models/project_task.py | 10 +++++ .../tests/test_crm_project_task.py | 39 +++++++++++++------ crm_project_task/views/crm_lead.xml | 13 ++++++- crm_project_task/views/project_task.xml | 17 ++++++++ crm_project_task/wizards/crm_create_task.py | 2 + 9 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 crm_project_task/models/crm_lead.py create mode 100644 crm_project_task/models/project_task.py create mode 100644 crm_project_task/views/project_task.xml diff --git a/crm_project_task/__manifest__.py b/crm_project_task/__manifest__.py index df5d954ce370..d80ec4ef81b6 100644 --- a/crm_project_task/__manifest__.py +++ b/crm_project_task/__manifest__.py @@ -22,5 +22,6 @@ "views/res_config_settings.xml", "wizards/crm_create_task.xml", "views/crm_lead.xml", + "views/project_task.xml", ], } diff --git a/crm_project_task/i18n/crm_project_task.pot b/crm_project_task/i18n/crm_project_task.pot index 31a4d7fbbc3d..79cc25f22ff9 100644 --- a/crm_project_task/i18n/crm_project_task.pot +++ b/crm_project_task/i18n/crm_project_task.pot @@ -4,8 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 15.0+e\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-07-25 13:19+0000\n" +"PO-Revision-Date: 2023-07-25 13:19+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -13,6 +15,11 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" +#. module: crm_project_task +#: model:ir.model.fields,field_description:crm_project_task.field_crm_lead__task_count +msgid "#Task" +msgstr "" + #. module: crm_project_task #: model_terms:ir.ui.view,arch_db:crm_project_task.res_config_settings_view_form msgid "" @@ -95,9 +102,16 @@ msgstr "" #. module: crm_project_task #: model:ir.model.fields,field_description:crm_project_task.field_crm_create_task__lead_id +#: model:ir.model.fields,field_description:crm_project_task.field_project_task__lead_id msgid "Lead" msgstr "" +#. module: crm_project_task +#: model:ir.model,name:crm_project_task.model_crm_lead +#: model_terms:ir.ui.view,arch_db:crm_project_task.view_task_form2_inherit_crm_project_task +msgid "Lead/Opportunity" +msgstr "" + #. module: crm_project_task #: code:addons/crm_project_task/wizards/crm_create_task.py:0 #, python-format @@ -110,6 +124,12 @@ msgstr "" msgid "Projects" msgstr "" +#. module: crm_project_task +#: model:ir.model,name:crm_project_task.model_project_task +#: model:ir.model.fields,field_description:crm_project_task.field_crm_lead__task_ids +msgid "Task" +msgstr "" + #. module: crm_project_task #: code:addons/crm_project_task/wizards/crm_create_task.py:0 #, python-format @@ -131,6 +151,11 @@ msgid "" "id=%(lead)d>%(name)s." msgstr "" +#. module: crm_project_task +#: model_terms:ir.ui.view,arch_db:crm_project_task.inherit_crm_lead_view_form_crm_project_task +msgid "Task(s)" +msgstr "" + #. module: crm_project_task #: model:ir.model,name:crm_project_task.model_crm_create_task msgid "Wizard to create task" diff --git a/crm_project_task/models/__init__.py b/crm_project_task/models/__init__.py index 938485e46479..345bf94dd2e2 100644 --- a/crm_project_task/models/__init__.py +++ b/crm_project_task/models/__init__.py @@ -1,2 +1,4 @@ from . import res_config_settings from . import res_company +from . import project_task +from . import crm_lead diff --git a/crm_project_task/models/crm_lead.py b/crm_project_task/models/crm_lead.py new file mode 100644 index 000000000000..613d12b6e5ca --- /dev/null +++ b/crm_project_task/models/crm_lead.py @@ -0,0 +1,24 @@ +# Copyright 2023 Moduon Team S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo import api, fields, models + + +class CrmLead(models.Model): + _inherit = "crm.lead" + + task_ids = fields.One2many("project.task", "lead_id") + task_count = fields.Integer("#Task", compute="_compute_task_count") + + @api.depends("task_ids") + def _compute_task_count(self): + for lead in self: + lead.task_count = len(lead.task_ids) + + def action_tasks(self): + self.ensure_one() + ctx = self._context.copy() + action = self.env.ref("project.action_view_task").read()[0] + ctx.update({"default_lead_id": self.id}) + action.update({"context": ctx, "domain": [("lead_id", "=", self.id)]}) + return action diff --git a/crm_project_task/models/project_task.py b/crm_project_task/models/project_task.py new file mode 100644 index 000000000000..e7636ee0c28a --- /dev/null +++ b/crm_project_task/models/project_task.py @@ -0,0 +1,10 @@ +# Copyright 2023 Moduon Team S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo import fields, models + + +class CrmLead(models.Model): + _inherit = "project.task" + + lead_id = fields.Many2one("crm.lead") diff --git a/crm_project_task/tests/test_crm_project_task.py b/crm_project_task/tests/test_crm_project_task.py index 71fb9afa5db7..df7dd06df2a9 100644 --- a/crm_project_task/tests/test_crm_project_task.py +++ b/crm_project_task/tests/test_crm_project_task.py @@ -46,20 +46,23 @@ def test_create_task(self): self.company.crm_default_project_id = self.project task_name = "Task Test" task_description = "Line1
Line2" - prev_tasks = self.project.task_ids - self.env["crm.create.task"].with_user(self.user_salesman).create( - { - "lead_id": self.lead.id, - "task_name": task_name, - "description": task_description, - } - ).create_task() - tasks = self.project.task_ids - self.assertEqual(len(prev_tasks) + 1, len(tasks)) - task = tasks - prev_tasks + action = ( + self.env["crm.create.task"] + .with_user(self.user_salesman) + .create( + { + "lead_id": self.lead.id, + "task_name": task_name, + "description": task_description, + } + ) + .create_task() + ) + task = self.env["project.task"].browse(action["res_id"]) self.assertEqual(task.name, task_name) self.assertEqual(task.project_id, self.company.crm_default_project_id) self.assertEqual(task.partner_id, self.partner) + self.assertEqual(task.lead_id, self.lead) def test_create_task_no_project(self): self.company.crm_default_project_id = False @@ -78,3 +81,17 @@ def test_create_task_no_project(self): ) with self.assertRaises(UserError): wizard.create_task() + + def test_action_tasks(self): + self.company.crm_default_project_id = self.project + self.env["crm.create.task"].with_user(self.user_salesman).create( + { + "lead_id": self.lead.id, + "task_name": "Task Test", + "description": "Line1
Line2", + } + ).create_task() + action = self.lead.action_tasks() + tasks = self.env["project.task"].search(action["domain"]) + tasks_lead = tasks.mapped("lead_id") + self.assertEqual(self.lead, tasks_lead) diff --git a/crm_project_task/views/crm_lead.xml b/crm_project_task/views/crm_lead.xml index 4a5fde728a39..997a77b2f4ee 100644 --- a/crm_project_task/views/crm_lead.xml +++ b/crm_project_task/views/crm_lead.xml @@ -14,9 +14,20 @@ type="action" class="oe_highlight" context="{'default_lead_id': active_id, 'default_task_name': name}" - attrs="{'invisible': [('active','=',False)]}" + attrs="{'invisible': ['|', ('active','=',False), ('task_count', '=', 0)]}" /> + + + diff --git a/crm_project_task/views/project_task.xml b/crm_project_task/views/project_task.xml new file mode 100644 index 000000000000..5820d38f4494 --- /dev/null +++ b/crm_project_task/views/project_task.xml @@ -0,0 +1,17 @@ + + + + + project.task.form.inherit + project.task + + + + + + + + + + diff --git a/crm_project_task/wizards/crm_create_task.py b/crm_project_task/wizards/crm_create_task.py index 35f84bd58424..d58463996c81 100644 --- a/crm_project_task/wizards/crm_create_task.py +++ b/crm_project_task/wizards/crm_create_task.py @@ -56,10 +56,12 @@ def create_task(self): } def _get_data_create(self, project): + """Get dict to create task""" return { "name": self.task_name, "project_id": project.id, "partner_id": self.lead_id.partner_id.id, + "lead_id": self.lead_id.id, "description": self.description, "user_ids": [(6, 0, [])], }