From 027ff77a9e113bc0aa097b3ee1f7a75fccd570c3 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Wed, 10 Apr 2019 15:02:27 +0000 Subject: [PATCH] [FIX] stock, mrp: scheduler assign finished move Usecase to reproduce - Create a BoM with finished product A and Comp C - Create a RR on product A with min qty 10 - Product A is tracked by serial number and have route manufacture - Run the scheduler twice. The finished tabs in the manufacturing order created from the order point contains 10 move lines with 1 qty reserved and 0 quantity done. The first run created the MO. The second run, assign the finished moves and thus created the move lines for finished product A. However those move line are not used by mrp because the workorder and the produce wizard will only create new lines instead of using existing lines with reservation. Also action_assign on MO only trigger _action_assign on raw moves. This commit add a hook on scheduler in order to modify the domain that will be used in order to search moves to assign. Use the hook in order to avoid move with the production_id many2one set. closes odoo/odoo#32597 Signed-off-by: Arnold Moyaux --- addons/mrp/models/stock_rule.py | 10 ++++++++++ addons/stock/models/stock_rule.py | 13 ++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/addons/mrp/models/stock_rule.py b/addons/mrp/models/stock_rule.py index d2bcdbbbafe..fb51b6d7aa0 100644 --- a/addons/mrp/models/stock_rule.py +++ b/addons/mrp/models/stock_rule.py @@ -5,6 +5,7 @@ from dateutil.relativedelta import relativedelta from odoo import api, fields, models, _ from odoo.exceptions import UserError +from odoo.osv import expression class StockRule(models.Model): @@ -88,3 +89,12 @@ class StockRule(models.Model): new_move_vals = super(StockRule, self)._push_prepare_move_copy_values(move_to_copy, new_date) new_move_vals['production_id'] = False return new_move_vals + +class ProcurementGroup(models.Model): + _inherit = 'procurement.group' + + @api.model + def _get_moves_to_assign_domain(self): + domain = super(ProcurementGroup, self)._get_moves_to_assign_domain() + domain = expression.AND([domain, [('production_id', '=', False)]]) + return domain diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index 5dc9132834c..c13e5da3cb7 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -356,15 +356,22 @@ class ProcurementGroup(models.Model): ('picking_id.backorder_id', '=', False), ('product_id', '=', values['product_id'].id)] + @api.model + def _get_moves_to_assign_domain(self): + return expression.AND([ + [('state', 'in', ['confirmed', 'partially_available'])], + [('product_uom_qty', '!=', 0.0)] + ]) + @api.model def _run_scheduler_tasks(self, use_new_cursor=False, company_id=False): # Minimum stock rules self.sudo()._procure_orderpoint_confirm(use_new_cursor=use_new_cursor, company_id=company_id) # Search all confirmed stock_moves and try to assign them - moves_to_assign = self.env['stock.move'].search([ - ('state', 'in', ['confirmed', 'partially_available']), ('product_uom_qty', '!=', 0.0) - ], limit=None, order='priority desc, date_expected asc') + domain = self._get_moves_to_assign_domain() + moves_to_assign = self.env['stock.move'].search(domain, limit=None, + order='priority desc, date_expected asc') for moves_chunk in split_every(100, moves_to_assign.ids): self.env['stock.move'].browse(moves_chunk)._action_assign() if use_new_cursor: