Skip to content
Snippets Groups Projects
Commit 90f3da01 authored by Adrien Widart's avatar Adrien Widart
Browse files

[FIX] mrp_account: generate analytic lines only once


When the analytic account feature is enabled, if a user marks a MO as
done and if it displays a wizard (consumption warning, backorder...),
the analytic lines will be generated twice.

To reproduce the issue:
(Need mrp_workorder, account_accountant)
1. In Settings, enable "Analytic Accounting"
2. Create a work center WC:
    - Cost per hour: 100
    - Analytic Account: a new analytic account AA
3. Create two products P_finished and P_compo
    - P_finished is storable
    - P_compo is consumable
4. Create a BoM:
    - Product: P_finished
    - Components: 1 x P_compo
    - Operations: add a new one:
        - Work Center: WC
5. Create and confirm a MO with 1 x P_finished
6. Start the WO for few seconds then mark it as done
7. Set the consumed qty of P_compo to 2
8. Mark the MO as done
    - there is a consumption warning, confirm it
9. Open the account analytic lines related to AA

Error: there are two same lines for the MO, there should be only one

Step 8, when the user marks the MO as done, we call `button_mark_done`.
Because there is a consumption issue, this method returns an action (the
wizard with the consumption warning). However, in
`/mrp_account.button_mark_done`, we still generate the costs (this is
where the analytic lines will be generated). That is not correct since
the MO is not yet done. As a result, when confirming the consumption
warning, it leads again to `button_mark_done`, so we will generate the
cost a second time.

Note: this issue will happen each time the call of `button_mark_done`
does not mark the MO as done (for instance, in case of a backorder)

OPW-2972407

closes odoo/odoo#101528

Signed-off-by: default avatarTiffany Chang <tic@odoo.com>
parent 5a41c9f4
No related branches found
No related tags found
No related merge requests found
......@@ -78,6 +78,8 @@ class MrpProduction(models.Model):
def button_mark_done(self):
res = super(MrpProduction, self).button_mark_done()
for order in self:
if order.state != 'done':
continue
order._costs_generate()
return res
......
......@@ -190,6 +190,63 @@ class TestMrpAccount(TestMrpCommon):
# 1 table head at 20 + 4 table leg at 15 + 4 bolt at 10 + 10 screw at 10 + 1*20 (extra cost)
self.assertEqual(move_value, 141, 'Thing should have the correct price')
def test_generate_analytic_account(self):
"""
Suppose a workcenter with a cost and an analytic account. A MO is
processed and one of the components has been consumed more than
expected. The test ensures that the analytic account line will be
generated only once.
"""
analytic_account = self.env['account.analytic.account'].create({'name': 'Super Analytic Account'})
cost_per_hour = 100
workcenter = self.env['mrp.workcenter'].create({
'name': 'SuperWorkcenter',
'costs_hour': cost_per_hour,
'costs_hour_account_id': analytic_account.id,
})
bom = self.env['mrp.bom'].create({
'product_tmpl_id': self.product_2.product_tmpl_id.id,
'product_qty': 1,
'bom_line_ids': [(0, 0, {
'product_id': self.product_1.id,
'product_qty': 1,
})],
'operation_ids': [(0, 0, {
'name': 'Super Operation',
'workcenter_id': workcenter.id,
})],
})
mo = self.env['mrp.production'].create({
'name': 'Super MO',
'product_id': self.product_2.id,
'product_uom_id': self.product_2.uom_id.id,
'product_qty': 1,
'bom_id': bom.id,
})
mo._onchange_move_raw()
mo._onchange_move_finished()
mo._onchange_workorder_ids()
mo.action_confirm()
duration = 30
mo.qty_producing = 1
mo.workorder_ids.duration = duration
mo.move_raw_ids.move_line_ids.qty_done = 2
action = mo.button_mark_done()
self.assertNotEqual(mo.state, 'done')
self.assertFalse(analytic_account.line_ids)
warning = Form(self.env['mrp.consumption.warning'].with_context(**action['context']))
warning = warning.save()
warning.action_confirm()
self.assertEqual(mo.state, 'done')
self.assertEqual(analytic_account.line_ids.amount, - duration / 60 * cost_per_hour)
@tagged("post_install", "-at_install")
class TestMrpAccountMove(TestAccountMove):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment