[FIX] mrp_account: Update the AAL when changing the MO analytic account

Analytic Accounts can be defined in the MO and can be changed in any MO stage. So the analytic account line will also have to be updated to be linked to the new AA

opw-2674347

closes odoo/odoo#79955

X-original-commit: 65a18382c1712968148898998e3778b77f199dea
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
This commit is contained in:
Touati Djamel (otd)
2021-11-18 08:37:45 +00:00
parent 65a2e46572
commit dc8948d79c
2 changed files with 49 additions and 2 deletions
+12 -2
View File
@@ -34,13 +34,23 @@ class MrpProduction(models.Model):
self.analytic_account_id = self.bom_id.analytic_account_id
def write(self, vals):
origin_analytic_account = {production: production.analytic_account_id for production in self}
res = super().write(vals)
if vals.get('name'):
for production in self:
for production in self:
if vals.get('name'):
production.move_raw_ids.analytic_account_line_id.ref = production.display_name
for workorder in production.workorder_ids:
workorder.mo_analytic_account_line_id.ref = production.display_name
workorder.mo_analytic_account_line_id.name = _("[WC] %s", workorder.display_name)
if 'analytic_account_id' in vals and production.state != 'draft':
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']})
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()
else:
production.move_raw_ids.analytic_account_line_id.unlink()
return res
def action_view_stock_valuation_layers(self):
@@ -187,3 +187,40 @@ class TestAnalyticAccount(TransactionCase):
self.assertEqual(mo.workorder_ids.mo_analytic_account_line_id.account_id, self.analytic_account)
self.assertEqual(mo.workorder_ids.wc_analytic_account_line_id.amount, -20.0)
self.assertEqual(mo.workorder_ids.wc_analytic_account_line_id.account_id, wc_analytic_account)
def test_changing_mo_analytic_account(self):
""" Check if the MO account analytic lines are correctly updated
after the change of the MO account analytic.
"""
# create a mo
mo_form = Form(self.env['mrp.production'])
mo_form.product_id = self.product
mo_form.bom_id = self.bom
mo_form.product_qty = 1
mo_form.analytic_account_id = self.analytic_account
mo = mo_form.save()
mo.action_confirm()
self.assertEqual(mo.state, 'confirmed')
self.assertEqual(len(mo.move_raw_ids.analytic_account_line_id), 0)
# Mark as done
wizard_dict = mo.button_mark_done()
Form(self.env[(wizard_dict.get('res_model'))].with_context(wizard_dict['context'])).save().process()
self.assertEqual(mo.state, 'done')
self.assertEqual(len(mo.move_raw_ids.analytic_account_line_id), 1)
# Create a new analytic account
new_analytic_account = self.env['account.analytic.account'].create({'name': 'test_analytic_account_2'})
# 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)
#Get the MO analytic account lines
mo_analytic_account_lines = mo.move_raw_ids.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())
# 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)