From 96b17a82cf1ea1287e0a52c52c78166ef817ce7b Mon Sep 17 00:00:00 2001 From: William Henrotin Date: Wed, 29 Jan 2020 16:09:58 +0000 Subject: [PATCH] [IMP] mrp: unit_factor as computed field The next commits will allow changing the initial demand of confirmed moves as well as adding move once the production order is confirmed. In order to not duplicate again the unit factor computation in multiple write and create, make the field a stored computed. Unit_factor is stored and only depends on product_uom_qty because updating only quantity_done (after a partial production for instance) should leave unit_factor unchanged for the futur partial production. Task: 2121714 --- addons/mrp/models/mrp_production.py | 3 --- addons/mrp/models/stock_move.py | 11 ++++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 70250d72e63..18055c8481d 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -575,7 +575,6 @@ class MrpProduction(models.Model): 'product_uom': product_uom, 'operation_id': operation_id, 'byproduct_id': byproduct_id, - 'unit_factor': product_uom_qty / self.product_qty, 'name': self.name, 'date': self.date_planned_start, 'date_expected': self.date_planned_finished, @@ -671,7 +670,6 @@ class MrpProduction(models.Model): move[0].with_context(do_not_unreserve=True).write({'product_uom_qty': quantity}) move[0]._recompute_state() move[0]._action_assign() - move[0].unit_factor = remaining_qty and (quantity - move[0].quantity_done) / remaining_qty or 1.0 return move[0], old_qty, quantity else: if move[0].quantity_done > 0: @@ -759,7 +757,6 @@ class MrpProduction(models.Model): for move_raw in production.move_raw_ids: move_raw.write({ 'group_id': production.procurement_group_id.id, - 'unit_factor': move_raw.product_uom_qty / production.product_qty, 'reference': production.name, # set reference when MO name is different than 'New' }) production._generate_finished_moves() diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py index d09c3980fe3..499135b2d4a 100644 --- a/addons/mrp/models/stock_move.py +++ b/addons/mrp/models/stock_move.py @@ -87,7 +87,7 @@ class StockMove(models.Model): byproduct_id = fields.Many2one( 'mrp.bom.byproduct', 'By-products', check_company=True, help="By-product line that generated the move in a manufacturing order") - unit_factor = fields.Float('Unit Factor', default=1) + unit_factor = fields.Float('Unit Factor', compute='_compute_unit_factor', store=True) is_done = fields.Boolean( 'Done', compute='_compute_is_done', store=True, @@ -135,6 +135,15 @@ class StockMove(models.Model): for move in self: move.is_done = (move.state in ('done', 'cancel')) + @api.depends('product_uom_qty') + def _compute_unit_factor(self): + for move in self: + mo = move.raw_material_production_id or move.production_id + if mo: + move.unit_factor = (move.product_uom_qty - move.quantity_done) / ((mo.product_qty - mo.qty_produced) or 1) + else: + move.unit_factor = 1.0 + @api.model def default_get(self, fields_list): defaults = super(StockMove, self).default_get(fields_list)