From cdf286286dc579d7a57b1f2a770980b3b9a48f24 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#117308

Task: 3252742
Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
---
 addons/mrp_account/models/mrp_production.py      |  3 +++
 .../mrp_account/tests/test_analytic_account.py   | 16 ++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/addons/mrp_account/models/mrp_production.py b/addons/mrp_account/models/mrp_production.py
index 31fb122e9b9f..0b0fd3901011 100644
--- a/addons/mrp_account/models/mrp_production.py
+++ b/addons/mrp_account/models/mrp_production.py
@@ -46,11 +46,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 dbf80907be9e..1f3acf71a1de 100644
--- a/addons/mrp_account/tests/test_analytic_account.py
+++ b/addons/mrp_account/tests/test_analytic_account.py
@@ -202,6 +202,13 @@ class TestAnalyticAccount(TransactionCase):
         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
+        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()
@@ -214,16 +221,21 @@ class TestAnalyticAccount(TransactionCase):
         # 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