diff --git a/addons/mrp/models/mrp_unbuild.py b/addons/mrp/models/mrp_unbuild.py index 629e9fbffbc..7e54e280bf8 100644 --- a/addons/mrp/models/mrp_unbuild.py +++ b/addons/mrp/models/mrp_unbuild.py @@ -257,7 +257,7 @@ class MrpUnbuild(models.Model): 'name': self.name, 'date': self.create_date, 'product_id': move.product_id.id, - 'product_uom_qty': move.product_uom_qty * factor, + 'product_uom_qty': move.quantity * factor, 'product_uom': move.product_uom.id, 'procure_method': 'make_to_stock', 'location_dest_id': location_dest_id.id, diff --git a/addons/mrp/tests/test_unbuild.py b/addons/mrp/tests/test_unbuild.py index 882c96a025f..26ac369164b 100644 --- a/addons/mrp/tests/test_unbuild.py +++ b/addons/mrp/tests/test_unbuild.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import Command from odoo.tests import Form from odoo.addons.mrp.tests.common import TestMrpCommon from odoo.exceptions import UserError @@ -942,3 +943,36 @@ class TestUnbuild(TestMrpCommon): {'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}, ]) + + def test_unbuild_less_quantity_consumed(self): + """ + Tests that you don't unbuild more than you consumed during production. + BoM uses component x20, but only 15 are consumed during the production order. + Unbuilding the MO should only put 15 components back in stock. + """ + bom = self.env['mrp.bom'].create({ + 'product_id': self.product_2.id, + 'product_tmpl_id': self.product_2.product_tmpl_id.id, + 'consumption': 'flexible', + 'product_qty': 1.0, + 'type': 'normal', + 'bom_line_ids': [ + Command.create({'product_id': self.product_3.id, 'product_qty': 20}), + ] + }) + + with Form(self.env['mrp.production']) as mo_form: + mo_form.product_id = self.product_2 + mo_form.bom_id = bom + mo_form.product_qty = 1 + mo = mo_form.save() + mo.action_confirm() + + mo.qty_producing = 1.0 + mo.move_raw_ids.write({'quantity': 15, 'picked': True}) + mo.button_mark_done() + + unbuild_action = mo.button_unbuild() + unbuild_wizard = Form(self.env[unbuild_action['res_model']].with_context(**unbuild_action['context'])).save() + unbuild_wizard.action_validate() + self.assertEqual(mo.unbuild_ids.produce_line_ids.filtered(lambda m: m.product_id == self.product_3).product_uom_qty, 15)