From 47971aa4be3b7762d99ba5cf47ae3fc65ebce24d Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Wed, 17 Mar 2021 11:34:01 +0100 Subject: [PATCH] [IMP] sale_timesheet: compute partner_id in project when SOL is set Before this commit, the user can choose any SOL for any customer in the sale_line_id field of the project and also in the employee mappings. The problem is when the user create a task for a customer A, the default SOL of the project can be no selected if this SOL is not for the customer A. Also, some employee mappings can be unused for this task if the SOL of the employee mapping is not for the customer A. Moreover, if the customer selects a customer in the project, the SOL initially choose in this project can be removed if the customer is not the same. For these reasons, the commit adds a compute for the partner_id field in the project.project model. This compute is trigger when the user select a default sales order item (SOL) for the project or select an SOL in the first employee mappings for this project. With this changes, the SOLs choose will be for only one partner (customer) in a project. task-2442683 closes #66181 --- addons/sale_timesheet/models/project.py | 10 ++++++++++ .../models/project_sale_line_employee_map.py | 9 +++++++-- addons/sale_timesheet/views/project_task_views.xml | 1 - 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/addons/sale_timesheet/models/project.py b/addons/sale_timesheet/models/project.py index b19c7a5e1ae..e01376ea872 100644 --- a/addons/sale_timesheet/models/project.py +++ b/addons/sale_timesheet/models/project.py @@ -49,6 +49,7 @@ class Project(models.Model): compute="_compute_timesheet_product_id", store=True, readonly=False, default=_default_timesheet_product_id) warning_employee_rate = fields.Boolean(compute='_compute_warning_employee_rate') + partner_id = fields.Many2one(compute='_compute_partner_id', store=True, readonly=False) @api.depends('sale_line_id', 'sale_line_employee_ids', 'allow_billable') def _compute_pricing_type(self): @@ -137,6 +138,15 @@ class Project(models.Model): for project in self.filtered(lambda p: not p.project_overview): project.project_overview = project.allow_billable or project.allow_timesheets + @api.depends('sale_line_employee_ids.sale_line_id', 'sale_line_id') + def _compute_partner_id(self): + for project in self: + if project.partner_id: + continue + if project.allow_billable and project.allow_timesheets and project.pricing_type != 'task_rate': + sol = project.sale_line_id or project.sale_line_employee_ids.sale_line_id[:1] + project.partner_id = sol.order_partner_id + @api.constrains('sale_line_id') def _check_sale_line_type(self): for project in self.filtered(lambda project: project.sale_line_id): diff --git a/addons/sale_timesheet/models/project_sale_line_employee_map.py b/addons/sale_timesheet/models/project_sale_line_employee_map.py index 4df0f5a5ccf..bcb55ffd2f6 100644 --- a/addons/sale_timesheet/models/project_sale_line_employee_map.py +++ b/addons/sale_timesheet/models/project_sale_line_employee_map.py @@ -26,9 +26,14 @@ class ProjectProductEmployeeMap(models.Model): ('uniqueness_employee', 'UNIQUE(project_id,employee_id)', 'An employee cannot be selected more than once in the mapping. Please remove duplicate(s) and try again.'), ] - @api.depends('project_id.partner_id') + @api.depends('partner_id') def _compute_sale_line_id(self): - self.filtered(lambda map_entry: map_entry.sale_line_id.order_partner_id != map_entry.project_id.partner_id).update({'sale_line_id': False}) + self.filtered( + lambda map_entry: + map_entry.sale_line_id + and map_entry.partner_id + and map_entry.sale_line_id.order_partner_id.commercial_partner_id != map_entry.partner_id.commercial_partner_id + ).update({'sale_line_id': False}) @api.depends('sale_line_id', 'sale_line_id.price_unit') def _compute_price_unit(self): diff --git a/addons/sale_timesheet/views/project_task_views.xml b/addons/sale_timesheet/views/project_task_views.xml index 57ec55aafd4..4d0a8cabd28 100644 --- a/addons/sale_timesheet/views/project_task_views.xml +++ b/addons/sale_timesheet/views/project_task_views.xml @@ -31,7 +31,6 @@ -