From da336947c0961c8c3a0f9f33a16e9fa90edbae45 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Mon, 19 Apr 2021 09:38:30 +0000 Subject: [PATCH] [FIX] sale_timesheet: fix unnecessarily recompute price_unit in mapping Before this commit, the currency_id field has the same compute method than the price_unit field in the employee mapping. When the industry_fsm_sale module is installed, the price_unit is recomputed because of the currency_id and then the user cannot manually edit the price_unit in fsm project form view. This commit adds a compute for the currency_id field to not unnecessarily recompute the price unit in the employee mapping. Related PR: odoo/odoo#66181 closes odoo/odoo#69458 Related: odoo/enterprise#17770 Signed-off-by: LTU-Odoo --- .../models/project_sale_line_employee_map.py | 11 ++++++---- .../tests/test_project_billing.py | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) 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 bcb55ffd2f6..7e12c500274 100644 --- a/addons/sale_timesheet/models/project_sale_line_employee_map.py +++ b/addons/sale_timesheet/models/project_sale_line_employee_map.py @@ -20,7 +20,7 @@ class ProjectProductEmployeeMap(models.Model): company_id = fields.Many2one('res.company', string='Company', related='project_id.company_id') partner_id = fields.Many2one(related='project_id.partner_id') price_unit = fields.Float("Unit Price", compute='_compute_price_unit', store=True, readonly=True) - currency_id = fields.Many2one('res.currency', string="Currency", compute='_compute_price_unit', store=True, readonly=False) + currency_id = fields.Many2one('res.currency', string="Currency", compute='_compute_currency_id', store=True, readonly=False) _sql_constraints = [ ('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.'), @@ -35,15 +35,18 @@ class ProjectProductEmployeeMap(models.Model): 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') + @api.depends('sale_line_id.price_unit') def _compute_price_unit(self): for line in self: if line.sale_line_id: line.price_unit = line.sale_line_id.price_unit - line.currency_id = line.sale_line_id.currency_id else: line.price_unit = 0 - line.currency_id = False + + @api.depends('sale_line_id.price_unit') + def _compute_currency_id(self): + for line in self: + line.currency_id = line.sale_line_id.currency_id if line.sale_line_id else False @api.model def create(self, values): diff --git a/addons/sale_timesheet/tests/test_project_billing.py b/addons/sale_timesheet/tests/test_project_billing.py index 6556fb578ca..f9c7002ee7e 100644 --- a/addons/sale_timesheet/tests/test_project_billing.py +++ b/addons/sale_timesheet/tests/test_project_billing.py @@ -3,6 +3,7 @@ from odoo.addons.sale_timesheet.tests.common import TestCommonSaleTimesheet from odoo.fields import Command from odoo.tests import tagged +from odoo.tests.common import Form @tagged('post_install', '-at_install') class TestProjectBilling(TestCommonSaleTimesheet): @@ -431,3 +432,23 @@ class TestProjectBilling(TestCommonSaleTimesheet): self.assertFalse(self.project_employee_rate_manager.sale_line_id, "The SOL in the mapping should be False because the actual customer in the project has not this SOL.") self.assertFalse(self.project_employee_rate_user.sale_line_id, "The SOL in the mapping should be False because the actual customer in the project has not this SOL.") self.assertEqual(self.project_employee_rate.pricing_type, 'employee_rate', 'Since the mappings have not been removed, the pricing type should remain the same, that is employee rate.') + + def test_project_form_view(self): + """ Test if in the form view, the partner is correctly computed when the user adds a mapping + + Test Case: + ========= + 1) Use the Form class to create a project with a form view + 2) Define a billable project + 3) Create an employee mapping in this project + 4) Check if the partner_id and pricing_type fields have been changed + """ + with Form(self.env['project.project'].with_context({'tracking_disable': True})) as project_form: + project_form.name = 'Test Billable Project' + project_form.allow_billable = True + with project_form.sale_line_employee_ids.new() as mapping_form: + mapping_form.employee_id = self.employee_manager + mapping_form.sale_line_id = self.so.order_line[:1] + self.assertEqual(project_form.partner_id, self.so.partner_id, 'The partner should be the one defined the SO linked to the SOL defined in the mapping.') + project = project_form.save() + self.assertEqual(project.pricing_type, 'employee_rate', 'Since there is a mapping in this project, the pricing type should be employee rate.')