From b7e4e5b2aabbabfc89561387122eccab0ab6a291 Mon Sep 17 00:00:00 2001 From: Dang Van Loc Date: Tue, 6 Feb 2024 09:22:36 +0700 Subject: [PATCH] [FIX] mrp: incorrect quantity_done Step: * Create Bom A: product A - qty 1, bom line: product = Component X, qty = 20 * Create MO with Bom A and 1 produced_qty = 1: + on components: To Consume = 20, Consumed = 15 + Unbuild MO, check Unbuild, check Product Moves of Unbuild, check line product Component X has Quantity = 20, in fact quantity must = 15 closes odoo/odoo#156952 X-original-commit: 18c45260f50663e042da3bbec13d02ef269e26b2 Signed-off-by: Quentin Wolfs (quwo) --- addons/mrp/models/mrp_unbuild.py | 2 +- addons/mrp/tests/test_unbuild.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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)