[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.
This commit is contained in:
Florent de Labarre
2018-01-16 09:30:27 +01:00
committed by Arnold Moyaux
parent 3ede4c72df
commit 2bd10fa757
2 changed files with 28 additions and 5 deletions
+27 -4
View File
@@ -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,
+1 -1
View File
@@ -122,7 +122,7 @@
</div>
</group>
<group>
<field name="mo_id" required="True"/>
<field name="mo_id" domain="[('state','=','done')]" required="True"/>
<field name="location_id" groups="stock.group_stock_multi_locations"/>
<field name="location_dest_id" groups="stock.group_stock_multi_locations"/>
<field name="has_tracking" invisible="1"/>