From 7e1b66fe19da179ecfd6c27d2890200a3de2c377 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Thu, 7 Jan 2021 09:09:41 +0100 Subject: [PATCH] [IMP] sale_timesheet: changes SOL in mapping if customer changes Before this commit, when the user changes the customer of the project, the SOL of each mapping remains for the old customer of the project. To avoid the user forget to change the SOL of each SOL. This commit add a compute method on sale_line_id field in project.sale.line.employee.map to automatically remove the SOLs when the SO in project is removed because of a customer change in the project. Then, it also adds a require in this field to display a validation error as long as the SOL of each mapping is not set. task-2424382 --- .../sale_timesheet/models/project_sale_line_employee_map.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 76a702d8b9f..d1b5e2322fa 100644 --- a/addons/sale_timesheet/models/project_sale_line_employee_map.py +++ b/addons/sale_timesheet/models/project_sale_line_employee_map.py @@ -10,7 +10,7 @@ class ProjectProductEmployeeMap(models.Model): project_id = fields.Many2one('project.project', "Project", required=True) employee_id = fields.Many2one('hr.employee', "Employee", required=True) - sale_line_id = fields.Many2one('sale.order.line', "Sale Order Item", domain=[('is_service', '=', True)]) + sale_line_id = fields.Many2one('sale.order.line', "Sale Order Item", compute="_compute_sale_line_id", store=True, readonly=False, required=True, domain=[('is_service', '=', True)]) company_id = fields.Many2one('res.company', string='Company', related='project_id.company_id') timesheet_product_id = fields.Many2one( 'product.product', string='Service', @@ -26,6 +26,10 @@ 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.sale_order_id') + def _compute_sale_line_id(self): + self.filtered(lambda map_entry: not map_entry.project_id.sale_order_id and map_entry.sale_line_id).update({'sale_line_id': None}) + @api.depends('sale_line_id', 'sale_line_id.price_unit', 'timesheet_product_id') def _compute_price_unit(self): for line in self: