From cf7c9bd9b7d42ac89a1d6bcdf009ae3d376d6511 Mon Sep 17 00:00:00 2001 From: jbm-odoo Date: Tue, 30 Jul 2019 14:04:46 +0000 Subject: [PATCH] [FIX] mrp: Avoid creating new line when changing qty to produce When we change quantity to produce in mrp, all component lines are removed and new line with the updated amount to product are created. The problem appears when we change the quantity to produce and directly click on a component line, because we open the line that will be deleted. So if we change the quantity for this component manually, this suppressed line will be added with the new value. And the computed line will be also be added. So the line for one component will be present twice (one with computed value and one with manual entry value). To avoid this problem, when we change the quantity to produce, we update information of each line (instead of deleting and creating new one). So if we click on a component line just after changing the quantity, we open the updated line (and not de deleted one). --- addons/mrp/models/mrp_production.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index b3e2795871a..5788dfcf8bf 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -397,6 +397,7 @@ class MrpProduction(models.Model): def _onchange_bom_id(self): self.product_qty = self.bom_id.product_qty self.product_uom_id = self.bom_id.product_uom_id.id + self.move_raw_ids = [(2, move.id) for move in self.move_raw_ids.filtered(lambda m: m.bom_line_id)] @api.onchange('date_planned_start') def _onchange_date_planned_start(self): @@ -409,11 +410,21 @@ class MrpProduction(models.Model): @api.onchange('bom_id', 'product_id', 'product_qty', 'product_uom_id') def _onchange_move_raw(self): - self.move_raw_ids = [(2, 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: + # keep manual entries + list_move_raw = [(4, move.id) for move in self.move_raw_ids.filtered(lambda m: not m.bom_line_id)] moves_raw_values = self._get_moves_raw_values() + move_raw_dict = {move.bom_line_id.id: move for move in self.move_raw_ids.filtered(lambda m: m.bom_line_id)} for move_raw_values in moves_raw_values: - self.move_raw_ids += self.env['stock.move'].new(move_raw_values) + if move_raw_values['bom_line_id'] in move_raw_dict: + # update existing entries + list_move_raw += [(1, move_raw_dict[move_raw_values['bom_line_id']].id, move_raw_values)] + else: + # add new entries + list_move_raw += [(0, 0, move_raw_values)] + self.move_raw_ids = list_move_raw + else: + self.move_raw_ids = [(2, move.id) for move in self.move_raw_ids.filtered(lambda m: m.bom_line_id)] @api.onchange('location_src_id', 'move_raw_ids', 'routing_id') def _onchange_location(self):