From 35e3af77051d1dfdf54600b607acfb593aa49e34 Mon Sep 17 00:00:00 2001 From: "Pieter Claeys (clpi)" <clpi@odoo.com> Date: Fri, 31 Mar 2023 08:50:29 +0000 Subject: [PATCH] [FIX] mrp: Update analytic lines when changing account on MO Upon changing/removing/adding the analytic account on a manufacturing order, only the analytic lines due to the raw material moves are updated, but not the ones for workcenter costs. To reproduce: - Create an MO with a workorder on a workcenter which has an operating cost set - Complete time on this workorder to generate the [WC] AALs on this MO - Change the analytic account on the MO (or delete it) Bug: The [WC] AALs never get correctly updated. This fix builds on https://github.com/odoo/odoo/pull/79614 to correct this behaviour and also take into account changes for the workcenter cost AALs when the analytic_account of a manufacturing order is changed. Community PR: https://github.com/odoo/odoo/pull/117308 closes odoo/odoo#121891 Task: 3252742 X-original-commit: cdf286286dc579d7a57b1f2a770980b3b9a48f24 Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com> --- addons/mrp_account/models/mrp_production.py | 3 +++ .../tests/test_analytic_account.py | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/addons/mrp_account/models/mrp_production.py b/addons/mrp_account/models/mrp_production.py index f744a72060c0..c810bf1391dc 100644 --- a/addons/mrp_account/models/mrp_production.py +++ b/addons/mrp_account/models/mrp_production.py @@ -49,11 +49,14 @@ class MrpProduction(models.Model): if vals['analytic_account_id'] and origin_analytic_account[production]: # Link the account analytic lines to the new AA production.move_raw_ids.analytic_account_line_id.write({'account_id': vals['analytic_account_id']}) + production.workorder_ids.mo_analytic_account_line_id.write({'account_id': vals['analytic_account_id']}) elif vals['analytic_account_id'] and not origin_analytic_account[production]: # Create the account analytic lines if no AA is set in the MO production.move_raw_ids._account_analytic_entry_move() + production.workorder_ids._create_or_update_analytic_entry() else: production.move_raw_ids.analytic_account_line_id.unlink() + production.workorder_ids.mo_analytic_account_line_id.unlink() return res def action_view_stock_valuation_layers(self): diff --git a/addons/mrp_account/tests/test_analytic_account.py b/addons/mrp_account/tests/test_analytic_account.py index 71b69c9a854c..c25e3e3de8cd 100644 --- a/addons/mrp_account/tests/test_analytic_account.py +++ b/addons/mrp_account/tests/test_analytic_account.py @@ -207,6 +207,8 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): """ Check if the MO account analytic lines are correctly updated after the change of the MO account analytic. """ + # Required for `workorder_ids` to be visible in the view + self.env.user.groups_id += self.env.ref('mrp.group_mrp_routings') # create a mo mo_form = Form(self.env['mrp.production']) mo_form.product_id = self.product @@ -217,6 +219,14 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): mo.action_confirm() self.assertEqual(mo.state, 'confirmed') self.assertEqual(len(mo.move_raw_ids.analytic_account_line_id), 0) + self.assertEqual(len(mo.workorder_ids.mo_analytic_account_line_id), 0) + + # Change duration to 60 + mo_form = Form(mo) + with mo_form.workorder_ids.edit(0) as line_edit: + line_edit.duration = 60.0 + mo_form.save() + self.assertEqual(mo.workorder_ids.mo_analytic_account_line_id.account_id, self.analytic_account) # Mark as done wizard_dict = mo.button_mark_done() @@ -230,16 +240,21 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): # Change the MO analytic account mo.analytic_account_id = new_analytic_account self.assertEqual(mo.move_raw_ids.analytic_account_line_id.account_id.id, new_analytic_account.id) + self.assertEqual(mo.workorder_ids.mo_analytic_account_line_id.account_id.id, new_analytic_account.id) #Get the MO analytic account lines - mo_analytic_account_lines = mo.move_raw_ids.analytic_account_line_id + mo_analytic_account_raw_lines = mo.move_raw_ids.analytic_account_line_id + mo_analytic_account_wc_lines = mo.workorder_ids.mo_analytic_account_line_id mo.analytic_account_id = False # Check that the MO analytic account lines are deleted self.assertEqual(len(mo.move_raw_ids.analytic_account_line_id), 0) - self.assertFalse(mo_analytic_account_lines.exists()) + self.assertEqual(len(mo.workorder_ids.mo_analytic_account_line_id), 0) + self.assertFalse(mo_analytic_account_raw_lines.exists()) + self.assertFalse(mo_analytic_account_wc_lines.exists()) # Check that the AA lines are recreated correctly if we delete the AA, save the MO, and assign a new one mo.analytic_account_id = self.analytic_account 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 -- GitLab