Skip to content

Commit

Permalink
[FIX] mrp: change qty_producing when updating product_qty
Browse files Browse the repository at this point in the history
Currently, when no workorders are defined for a production, updating its
`product_qty` through the wizard won't update its qty_producing, meaning
that if the production gets validated right afterwards, the total
produced will be the same as prior the update.

This is missleading and unconsistent with how things work when
workorders are involved, since in that case the `qty_producing` of the
production will be updated through the inverse of the workorder's
`qty_producing`.

Part-of: odoo#129009
  • Loading branch information
clesgow committed Oct 6, 2023
1 parent 82bac1c commit 0dc4215
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
26 changes: 16 additions & 10 deletions addons/mrp/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,26 @@ def test_update_quantity_2(self):
mo_form.qty_producing = 2
mo = mo_form.save()

mo._post_inventory()
# Produce & backorder
action = mo.button_mark_done()
backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context']))
backorder.save().action_backorder()
mo_backorder = mo.procurement_group_id.mrp_production_ids[-1]
self.assertEqual(mo_backorder.product_qty, 1)

update_quantity_wizard = self.env['change.production.qty'].create({
'mo_id': mo.id,
'product_qty': 5,
'mo_id': mo_backorder.id,
'product_qty': 3,
})
update_quantity_wizard.change_prod_qty()
mo_form = Form(mo)
mo_form.qty_producing = 5
mo = mo_form.save()
mo.button_mark_done()

self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).mapped('quantity_done')), 20)
self.assertEqual(sum(mo.move_finished_ids.mapped('quantity_done')), 5)
mo_back_form = Form(mo_backorder)
mo_back_form.qty_producing = 3
mo_backorder = mo_back_form.save()
mo_backorder.button_mark_done()

productions = mo | mo_backorder
self.assertEqual(sum(productions.move_raw_ids.filtered(lambda m: m.product_id == p1).mapped('quantity_done')), 20)
self.assertEqual(sum(productions.move_finished_ids.mapped('quantity_done')), 5)

def test_update_quantity_3(self):
bom = self.env['mrp.bom'].create({
Expand Down
12 changes: 7 additions & 5 deletions addons/mrp/wizard/change_production_qty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools import float_is_zero, float_round
from odoo.tools import float_is_zero


class ChangeProductionQty(models.TransientModel):
Expand Down Expand Up @@ -71,10 +71,8 @@ def change_prod_qty(self):
))
old_production_qty = production.product_qty
new_production_qty = wizard.product_qty
done_moves = production.move_finished_ids.filtered(lambda x: x.state == 'done' and x.product_id == production.product_id)
qty_produced = production.product_id.uom_id._compute_quantity(sum(done_moves.mapped('product_qty')), production.product_uom_id)

factor = (new_production_qty - qty_produced) / (old_production_qty - qty_produced)
factor = new_production_qty / old_production_qty
update_info = production._update_raw_moves(factor)
documents = {}
for move, old_qty, new_qty in update_info:
Expand All @@ -87,8 +85,12 @@ def change_prod_qty(self):
else:
documents[key] = [value]
production._log_manufacture_exception(documents)
self._update_finished_moves(production, new_production_qty - qty_produced, old_production_qty - qty_produced)
self._update_finished_moves(production, new_production_qty, old_production_qty)
production.write({'product_qty': new_production_qty})
if not float_is_zero(production.qty_producing, precision_rounding=production.product_uom_id.rounding) and\
not production.workorder_ids:
production.qty_producing = new_production_qty
production._set_qty_producing()

for wo in production.workorder_ids:
operation = wo.operation_id
Expand Down

0 comments on commit 0dc4215

Please sign in to comment.