Skip to content

Commit

Permalink
[FIX] mrp: unbuild the qty produced
Browse files Browse the repository at this point in the history
Steps to reproduce the bug:
- Create a storable product “P1” with BoM:
    - Component: C1, Qty: 1

- Create a Mo to produce 5 units of P1
- Confirm and mark as done the MO
- Unlock the MO
- Change the qty produced to 20
- Try to unbuild the MO

Problem:
Bug 1: The wizard triggered is set with the product_qty(5) instead
of the qty_produced (20)

Bug 2: Set the product_qty to 5 and validate the unbuild, in result
the stock moves in the unbuild order are 20 units instead of 5 units

opw-3630001

closes odoo#149494

X-original-commit: e4bac3e
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
  • Loading branch information
DjamelTouati committed Jan 17, 2024
1 parent beaf1af commit 33c7235
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions addons/mrp/models/mrp_unbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _onchange_mo_id(self):
if self.has_tracking == 'serial':
self.product_qty = 1
else:
self.product_qty = self.mo_id.product_qty
self.product_qty = self.mo_id.qty_produced


@api.onchange('product_id')
Expand Down Expand Up @@ -158,7 +158,7 @@ def action_unbuild(self):
self.env['stock.move.line'].create({
'move_id': finished_move.id,
'lot_id': self.lot_id.id,
'quantity': finished_move.product_uom_qty,
'quantity': self.product_qty,
'product_id': finished_move.product_id.id,
'product_uom_id': finished_move.product_uom.id,
'location_id': finished_move.location_id.id,
Expand Down Expand Up @@ -234,7 +234,7 @@ def _generate_produce_moves(self):
for unbuild in self:
if unbuild.mo_id:
raw_moves = unbuild.mo_id.move_raw_ids.filtered(lambda move: move.state == 'done')
factor = unbuild.product_qty / unbuild.mo_id.product_uom_id._compute_quantity(unbuild.mo_id.product_qty, unbuild.product_uom_id)
factor = unbuild.product_qty / unbuild.mo_id.product_uom_id._compute_quantity(unbuild.mo_id.qty_produced, unbuild.product_uom_id)
for raw_move in raw_moves:
moves += unbuild._generate_move_from_existing_move(raw_move, factor, raw_move.location_dest_id, self.location_dest_id)
else:
Expand Down
46 changes: 46 additions & 0 deletions addons/mrp/tests/test_unbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,49 @@ def test_unbuild_mo_with_tracked_product_and_component(self):
self.assertEqual(mo_2.unbuild_ids.produce_line_ids[0].lot_ids, finished_product_sn)
self.assertEqual(mo_2.unbuild_ids.produce_line_ids[1].product_id, component)
self.assertEqual(mo_2.unbuild_ids.produce_line_ids[1].lot_ids, component_sn)

def test_unbuild_different_qty(self):
"""
Test that the quantity to unbuild is the qty produced in the MO
BoM:
- 4x final product
components:
- 2 x (storable)
- 4 x (consumable)
- Create a MO with 4 final products to produce.
- Confirm and validate, then unlock the mo and update the qty produced to 10
- open the wizard to unbuild > the quantity proposed should be 10
- unbuild 4 units
- the move lines should be created with the correct quantity
"""
mo_form = Form(self.env['mrp.production'])
mo_form.bom_id = self.bom_1
mo = mo_form.save()

mo.action_confirm()
mo.move_finished_ids._do_unreserve()
mo_form = Form(mo)
mo_form.qty_producing = 4
mo = mo_form.save()
mo.button_mark_done()
self.assertEqual(mo.state, 'done', "Production order should be in done state.")
# unlock and update the qty produced
mo.action_toggle_is_locked()
with Form(mo) as mo_form:
mo_form.qty_producing = 10
self.assertEqual(mo.qty_producing, 10)
#unbuild order
unbuild_form = Form(self.env['mrp.unbuild'])
unbuild_form.mo_id = mo
# check that the quantity to unbuild is the qty produced in the MO
self.assertEqual(unbuild_form.product_qty, 10)
unbuild_form.product_qty = 3
unbuild_order = unbuild_form.save()
unbuild_order.action_unbuild()
self.assertRecordValues(unbuild_order.produce_line_ids.move_line_ids, [
# pylint: disable=bad-whitespace
{'product_id': self.bom_1.product_id.id, 'quantity': 3},
{'product_id': self.bom_1.bom_line_ids[0].product_id.id, 'quantity': 0.6},
{'product_id': self.bom_1.bom_line_ids[1].product_id.id, 'quantity': 1.2},
])

0 comments on commit 33c7235

Please sign in to comment.