From 35d84b0c27c752b925ca41f9e98703671d8cefba Mon Sep 17 00:00:00 2001 From: Walid Date: Tue, 6 Jun 2023 09:48:27 +0000 Subject: [PATCH] [FIX] mrp_account: remove related analytic line when WO is deleted Steps to reproduce: * Create an analytic account * Create MO with Analytic Account * Create WO on MO * Create Time Tracking on WO * Delete the WO Bug: The analytic line related to the work duration remains on the Account Fix: Remove the lines when the WorkOrder is deleted opw-3174736 closes odoo/odoo#124014 X-original-commit: 59ebf21d126d9d5c93e97e6b404b23270050aa6f Signed-off-by: William Henrotin (whe) Signed-off-by: Walid Hanniche (waha) --- addons/mrp_account/models/mrp_workorder.py | 4 ++++ addons/mrp_account/tests/test_analytic_account.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/addons/mrp_account/models/mrp_workorder.py b/addons/mrp_account/models/mrp_workorder.py index be3792ac0aa..89c7e8a2fab 100644 --- a/addons/mrp_account/models/mrp_workorder.py +++ b/addons/mrp_account/models/mrp_workorder.py @@ -76,3 +76,7 @@ class MrpWorkorder(models.Model): wo.mo_analytic_account_line_id = analytic_line for wo, analytic_line in zip(wo_to_link_wc_analytic_line, wc_analytic_lines): wo.wc_analytic_account_line_id = analytic_line + + def unlink(self): + (self.mo_analytic_account_line_id | self.wc_analytic_account_line_id).unlink() + return super().unlink() diff --git a/addons/mrp_account/tests/test_analytic_account.py b/addons/mrp_account/tests/test_analytic_account.py index c2e2f3039e5..ee25eb56ac1 100644 --- a/addons/mrp_account/tests/test_analytic_account.py +++ b/addons/mrp_account/tests/test_analytic_account.py @@ -261,8 +261,8 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): self.assertEqual(len(mo.move_raw_ids.analytic_account_line_id), 1) self.assertEqual(len(mo.workorder_ids.mo_analytic_account_line_id), 1) - def test_add_wo_analytic_no_company(self): - """Test the addition of work orders to a MO linked to + def test_add_remove_wo_analytic_no_company(self): + """Test the addition and removal of work orders to a MO linked to an analytic account that has no company associated """ # Create an analytic account and remove the company @@ -280,11 +280,16 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): }) mo_no_c_form = Form(mo_no_company) - self.env['mrp.workorder'].create({ + wo = self.env['mrp.workorder'].create({ 'name': 'Work_order', 'workcenter_id': self.workcenter.id, 'product_uom_id': self.bom.product_uom_id.id, 'production_id': mo_no_c_form.id, + 'duration': 60, }) mo_no_c_form.save() self.assertTrue(mo_no_company.workorder_ids) + self.assertEqual(wo.production_id.analytic_account_id, analytic_account_no_company) + self.assertEqual(len(analytic_account_no_company.line_ids), 1) + mo_no_company.workorder_ids.unlink() + self.assertEqual(len(analytic_account_no_company.line_ids), 0)