Permalink
Browse files

[IMP] mrp: raw moves generation in onchange

  • Loading branch information...
amoyaux committed Nov 8, 2018
1 parent fc120dc commit 89159e60ffe7883263e8c9239bd7d49b0d136071
@@ -253,7 +253,7 @@ def _compute_workorder_done_count(self):
production.workorder_done_count = count_data.get(production.id, 0)
@api.multi
@api.depends('move_raw_ids.state', 'move_finished_ids.state', 'workorder_ids', 'qty_produced', 'move_raw_ids.quantity_done')
@api.depends('move_raw_ids.state', 'move_finished_ids.state', 'workorder_ids', 'qty_produced', 'move_raw_ids.quantity_done', 'product_qty')
def _compute_state(self):
""" Compute the production state. It use the same process than stock
picking. It exists 3 extra steps for production:
@@ -368,8 +368,17 @@ def onchange_product_id(self):
@api.onchange('bom_id')
def _onchange_bom_id(self):
self.product_qty = self.bom_id.product_qty
self.product_uom_id = self.bom_id.product_uom_id.id
if self.bom_id:
self.product_qty = self.bom_id.product_qty
self.product_uom_id = self.bom_id.product_uom_id.id
@api.onchange('bom_id', 'product_id', 'product_qty')
def _onchange_move_raw(self):
self.move_raw_ids = [(3, move.id) for move in self.move_raw_ids.filtered(lambda m: m.bom_line_id)]
if self.bom_id and self.product_qty > 0:
moves_raw_values = self._get_moves_raw_values()
for move_raw_values in moves_raw_values:
self.move_raw_ids += self.env['stock.move'].new(move_raw_values)
@api.onchange('location_src_id', 'move_raw_ids')
def _onchange_location_source(self):
@@ -394,11 +403,6 @@ def write(self, vals):
})
if 'move_raw_ids' in vals and self.state != 'draft':
self.move_raw_ids.filtered(lambda m: m.state == 'draft')._action_confirm()
# TODO: maybe use update wizard instead
if any(field in vals for field in ('bom_id', 'product_qty')) and self.state == 'draft':
self.move_raw_ids.filtered(lambda m: m.bom_line_id).unlink()
self.move_finished_ids.unlink()
self._generate_moves()
return res
@api.model
@@ -412,9 +416,7 @@ def create(self, values):
values['name'] = self.env['ir.sequence'].next_by_code('mrp.production') or _('New')
if not values.get('procurement_group_id'):
values['procurement_group_id'] = self.env["procurement.group"].create({'name': values['name']}).id
production = super(MrpProduction, self).create(values)
production._generate_moves()
return production
return super(MrpProduction, self).create(values)
@api.multi
def unlink(self):
@@ -427,15 +429,6 @@ def action_toggle_is_locked(self):
self.is_locked = not self.is_locked
return True
@api.multi
def _generate_moves(self):
for production in self:
production._generate_finished_moves()
factor = production.product_uom_id._compute_quantity(production.product_qty, production.bom_id.product_uom_id) / production.bom_id.product_qty
boms, lines = production.bom_id.explode(production.product_id, factor, picking_type=production.bom_id.picking_type_id)
production._generate_raw_moves(lines)
return True
def _generate_finished_moves(self):
move = self.env['stock.move'].create({
'name': self.name,
@@ -457,14 +450,16 @@ def _generate_finished_moves(self):
})
return move
def _generate_raw_moves(self, exploded_lines):
def _get_moves_raw_values(self):
self.ensure_one()
moves = self.env['stock.move']
for bom_line, line_data in exploded_lines:
moves += self._generate_raw_move(bom_line, line_data)
moves = []
factor = self.product_uom_id._compute_quantity(self.product_qty, self.bom_id.product_uom_id) / self.bom_id.product_qty
boms, lines = self.bom_id.explode(self.product_id, factor, picking_type=self.bom_id.picking_type_id)
for bom_line, line_data in lines:
moves.append(self._get_move_raw_values(bom_line, line_data))
return moves
def _generate_raw_move(self, bom_line, line_data):
def _get_move_raw_values(self, bom_line, line_data):
quantity = line_data['qty']
# alt_op needed for the case when you explode phantom bom and all the lines will be consumed in the operation given by the parent bom line
alt_op = line_data['parent_line'] and line_data['parent_line'].operation_id.id or False
@@ -491,11 +486,12 @@ def _generate_raw_move(self, bom_line, line_data):
'price_unit': bom_line.product_id.standard_price,
'procure_method': 'make_to_stock',
'origin': self.name,
'state': 'draft',
'warehouse_id': source_location.get_warehouse().id,
'group_id': self.procurement_group_id.id,
'propagate': self.propagate,
}
return self.env['stock.move'].create(data)
return data
def _get_raw_location(self):
if self.routing_id:
@@ -542,7 +538,8 @@ def _update_raw_move(self, bom_line, line_data):
move[0].unlink()
return move[0], old_qty, quantity
else:
move = self._generate_raw_move(bom_line, line_data)
move_values = self._get_move_raw_values(bom_line, line_data)
move = self.env['stock.move'].create(move_values)
return move, 0, quantity
def _get_ready_to_produce_state(self):
@@ -579,9 +576,9 @@ def action_confirm(self):
'group_id': production.procurement_group_id.id,
'unit_factor': move_raw.product_uom_qty / quantity
})
production._generate_finished_moves()
production._adjust_procure_method()
production.move_raw_ids._action_confirm()
production.move_finished_ids._action_confirm()
(production.move_raw_ids | production.move_finished_ids)._action_confirm()
return True
@api.multi
@@ -40,6 +40,7 @@ def _run_manufacture(self, product_id, product_qty, product_uom, location_id, na
# create the MO as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example)
production = ProductionSudo.create(self._prepare_mo_vals(product_id, product_qty, product_uom, location_id, name, origin, values, bom))
production.move_raw_ids = self.env['stock.move'].create(production._get_moves_raw_values())
production.action_confirm()
origin_production = values.get('move_dest_ids') and values['move_dest_ids'][0].raw_material_production_id or False
orderpoint = values.get('orderpoint_id')
@@ -122,6 +122,7 @@
<field name="has_tracking" invisible="1"/>
<field name="needs_lots" readonly="1" groups="stock.group_production_lot"/>
<field name="is_done" invisible="1"/>
<field name="bom_line_id" invisible="1"/>
<field name="sequence" invisible="1"/>
<field name="location_id" invisible="1"/>
<field name="warehouse_id" invisible="1"/>
@@ -38,11 +38,11 @@ def _create_byproduct_move(self, sub_product):
move._action_confirm()
@api.multi
def _generate_moves(self):
def _generate_finished_moves(self):
""" Generates moves and work orders
@return: Newly generated picking Id.
"""
res = super(MrpProduction, self)._generate_moves()
res = super(MrpProduction, self)._generate_finished_moves()
for production in self.filtered(lambda production: production.bom_id):
for sub_product in production.bom_id.sub_products:
production._create_byproduct_move(sub_product)

0 comments on commit 89159e6

Please sign in to comment.