From 4cb2e626a9ea30ea05e2844a29ca8eec6f2bc567 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Tue, 19 Sep 2017 15:48:31 +0200 Subject: [PATCH] [IMP] mrp: Allow optional bom lines with 0 qty When processing moves with 0 initial demand, it will set the initial demand = qty_done. Also, when updating the quantity of an MO, don't take into account those with 0 quantity. --- addons/mrp/models/mrp_bom.py | 3 ++- addons/mrp/models/mrp_production.py | 4 +++- addons/mrp/models/mrp_workorder.py | 2 +- addons/mrp/wizard/mrp_product_produce.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/addons/mrp/models/mrp_bom.py b/addons/mrp/models/mrp_bom.py index 06e3479c190..f41e4a5a111 100644 --- a/addons/mrp/models/mrp_bom.py +++ b/addons/mrp/models/mrp_bom.py @@ -214,7 +214,8 @@ class MrpBomLine(models.Model): has_attachments = fields.Boolean('Has Attachments', compute='_compute_has_attachments') _sql_constraints = [ - ('bom_qty_zero', 'CHECK (product_qty>0)', 'All product quantities must be greater than 0.\n' + ('bom_qty_zero', 'CHECK (product_qty>=0)', 'All product quantities must be greater or equal to 0.\n' + 'Lines with 0 quantities can be used as optional lines. \n' 'You should install the mrp_byproduct module if you want to manage extra products on BoMs !'), ] diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 5098601f358..9d2076596a0 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -399,7 +399,7 @@ class MrpProduction(models.Model): if move: if quantity > 0: move[0].write({'product_uom_qty': quantity}) - else: + elif quantity < 0: # Do not remove 0 lines if move[0].quantity_done > 0: raise UserError(_('Lines need to be deleted, but can not as you still have some quantities to consume in them. ')) move[0]._action_cancel() @@ -510,6 +510,8 @@ class MrpProduction(models.Model): for order in self: moves_not_to_do = order.move_raw_ids.filtered(lambda x: x.state == 'done') moves_to_do = order.move_raw_ids.filtered(lambda x: x.state not in ('done', 'cancel')) + for move in moves_to_do.filtered(lambda m: m.product_qty == 0.0 and m.quantity_done > 0): + move.product_uom_qty = move.quantity_done moves_to_do._action_done() moves_to_do = order.move_raw_ids.filtered(lambda x: x.state == 'done') - moves_not_to_do order._cal_price(moves_to_do) diff --git a/addons/mrp/models/mrp_workorder.py b/addons/mrp/models/mrp_workorder.py index f0854ee3523..18329432edd 100644 --- a/addons/mrp/models/mrp_workorder.py +++ b/addons/mrp/models/mrp_workorder.py @@ -245,7 +245,7 @@ class MrpWorkorder(models.Model): self.ensure_one() MoveLine = self.env['stock.move.line'] tracked_moves = self.move_raw_ids.filtered( - lambda move: move.state not in ('done', 'cancel') and move.product_id.tracking != 'none' and move.product_id != self.production_id.product_id) + lambda move: move.state not in ('done', 'cancel') and move.product_id.tracking != 'none' and move.product_id != self.production_id.product_id and move.bom_line_id) for move in tracked_moves: qty = move.unit_factor * self.qty_producing if move.product_id.tracking == 'serial': diff --git a/addons/mrp/wizard/mrp_product_produce.py b/addons/mrp/wizard/mrp_product_produce.py index 617e5f012f9..338dbabea42 100644 --- a/addons/mrp/wizard/mrp_product_produce.py +++ b/addons/mrp/wizard/mrp_product_produce.py @@ -37,7 +37,7 @@ class MrpProductProduce(models.TransientModel): res['product_qty'] = todo_quantity if 'produce_line_ids' in fields: lines = [] - for move in production.move_raw_ids.filtered(lambda x: (x.product_id.tracking != 'none') and x.state not in ('done', 'cancel')): + for move in production.move_raw_ids.filtered(lambda x: (x.product_id.tracking != 'none') and x.state not in ('done', 'cancel') and x.bom_line_id): qty_to_consume = todo_quantity / move.bom_line_id.bom_id.product_qty * move.bom_line_id.product_qty for move_line in move.move_line_ids: if float_compare(qty_to_consume, 0.0, precision_rounding=move.product_uom.rounding) < 0\