From 2bd10fa7578a44f0d534020efef059fb054e853d Mon Sep 17 00:00:00 2001 From: Florent de Labarre Date: Mon, 13 Nov 2017 14:29:09 +0100 Subject: [PATCH] [FIX] mrp: unbuild use the MO instead of BOM When the user unbuild a product with a MO specified the system will generate the inverse moves from the BOM. However the BOM could have change. This commit instead use the MO specified in order to generate the inverse moves. --- addons/mrp/models/mrp_unbuild.py | 31 ++++++++++++++++++++++---- addons/mrp/views/mrp_unbuild_views.xml | 2 +- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/addons/mrp/models/mrp_unbuild.py b/addons/mrp/models/mrp_unbuild.py index 6c87ed8614e..0ff3db0a337 100644 --- a/addons/mrp/models/mrp_unbuild.py +++ b/addons/mrp/models/mrp_unbuild.py @@ -88,6 +88,10 @@ class MrpUnbuild(models.Model): if self.product_id.tracking != 'none' and not self.lot_id.id: raise UserError(_('Should have a lot for the finished product')) + if self.mo_id: + if self.mo_id.state != 'done': + raise UserError(_('You cannot unbuild a undone manufacturing order.')) + consume_move = self._generate_consume_moves()[0] produce_moves = self._generate_produce_moves() @@ -156,12 +160,31 @@ class MrpUnbuild(models.Model): def _generate_produce_moves(self): moves = self.env['stock.move'] for unbuild in self: - factor = unbuild.product_uom_id._compute_quantity(unbuild.product_qty, unbuild.bom_id.product_uom_id) / unbuild.bom_id.product_qty - boms, lines = unbuild.bom_id.explode(unbuild.product_id, factor, picking_type=unbuild.bom_id.picking_type_id) - for line, line_data in lines: - moves += unbuild._generate_move_from_bom_line(line, line_data['qty']) + 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) + for raw_move in raw_moves: + moves += unbuild._generate_move_from_raw_moves(raw_move, factor) + else: + factor = unbuild.product_uom_id._compute_quantity(unbuild.product_qty, unbuild.bom_id.product_uom_id) / unbuild.bom_id.product_qty + boms, lines = unbuild.bom_id.explode(unbuild.product_id, factor, picking_type=unbuild.bom_id.picking_type_id) + for line, line_data in lines: + moves += unbuild._generate_move_from_bom_line(line, line_data['qty']) return moves + def _generate_move_from_raw_moves(self, raw_move, factor): + return self.env['stock.move'].create({ + 'name': self.name, + 'date': self.create_date, + 'product_id': raw_move.product_id.id, + 'product_uom_qty': raw_move.product_uom_qty * factor, + 'product_uom': raw_move.product_uom.id, + 'procure_method': 'make_to_stock', + 'location_dest_id': self.location_dest_id.id, + 'location_id': raw_move.location_dest_id.id, + 'unbuild_id': self.id, + }) + def _generate_move_from_bom_line(self, bom_line, quantity): return self.env['stock.move'].create({ 'name': self.name, diff --git a/addons/mrp/views/mrp_unbuild_views.xml b/addons/mrp/views/mrp_unbuild_views.xml index 6692e735f36..d71d49138c5 100644 --- a/addons/mrp/views/mrp_unbuild_views.xml +++ b/addons/mrp/views/mrp_unbuild_views.xml @@ -122,7 +122,7 @@ - +