diff --git a/addons/mrp/models/__init__.py b/addons/mrp/models/__init__.py index db69451b2fe..237e0273151 100644 --- a/addons/mrp/models/__init__.py +++ b/addons/mrp/models/__init__.py @@ -14,6 +14,7 @@ from . import mrp_workorder from . import product from . import res_company from . import stock_move +from . import stock_orderpoint from . import stock_picking from . import stock_production_lot from . import stock_rule diff --git a/addons/mrp/models/stock_orderpoint.py b/addons/mrp/models/stock_orderpoint.py index 262ed5f83c1..5d1da66f2fb 100644 --- a/addons/mrp/models/stock_orderpoint.py +++ b/addons/mrp/models/stock_orderpoint.py @@ -24,3 +24,12 @@ class StockWarehouseOrderpoint(models.Model): } } return super()._get_replenishment_order_notification() + + def _set_default_route_id(self): + route_id = self.env['stock.rule'].search([ + ('action', '=', 'manufacture') + ]).route_id + orderpoint_wh_bom = self.filtered(lambda o: o.product_id.bom_ids) + if route_id and orderpoint_wh_bom: + orderpoint_wh_bom.route_id = route_id[0].id + return super()._set_default_route_id() diff --git a/addons/purchase_stock/models/stock.py b/addons/purchase_stock/models/stock.py index 2af59c74ae7..48a152f4fa3 100644 --- a/addons/purchase_stock/models/stock.py +++ b/addons/purchase_stock/models/stock.py @@ -206,6 +206,15 @@ class Orderpoint(models.Model): res[orderpoint.id] += product_uom_qty return res + def _set_default_route_id(self): + route_id = self.env['stock.rule'].search([ + ('action', '=', 'buy') + ]).route_id + orderpoint_wh_supplier = self.filtered(lambda o: o.product_id.seller_ids) + if route_id and orderpoint_wh_supplier: + orderpoint_wh_supplier.route_id = route_id[0].id + return super()._set_default_route_id() + class ProductionLot(models.Model): _inherit = 'stock.production.lot' diff --git a/addons/stock/models/stock_orderpoint.py b/addons/stock/models/stock_orderpoint.py index 9a927077be9..64be17bffd6 100644 --- a/addons/stock/models/stock_orderpoint.py +++ b/addons/stock/models/stock_orderpoint.py @@ -236,6 +236,22 @@ class StockWarehouseOrderpoint(models.Model): qty_to_order += orderpoint.qty_multiple - remainder orderpoint.qty_to_order = qty_to_order + def _set_default_route_id(self): + """ Write the `route_id` field on `self`. This method is intendend to be called on the + orderpoints generated when openning the replenish report. + """ + self = self.filtered(lambda o: not o.route_id) + rules_groups = self.env['stock.rule'].read_group([ + ('route_id.product_selectable', '!=', False), + ('location_id', 'in', self.location_id.ids), + ('action', 'in', ['pull_push', 'pull']) + ], ['location_id', 'route_id'], ['location_id', 'route_id'], lazy=False) + for g in rules_groups: + if not g.get('route_id'): + continue + orderpoints = self.filtered(lambda o: o.location_id == g['location_id']) + orderpoints.route_id = g['route_id'] + def _get_product_context(self): """Used to call `virtual_available` when running an orderpoint.""" self.ensure_one() @@ -319,7 +335,8 @@ class StockWarehouseOrderpoint(models.Model): orderpoints = self.env['stock.warehouse.orderpoint'].with_user(SUPERUSER_ID).create(orderpoint_values_list) for orderpoint in orderpoints: - orderpoint.route_id = orderpoint._get_default_route_id() + orderpoint.route_id = orderpoint.product_id.route_ids[:1] + orderpoints.filtered(lambda o: not o.route_id)._set_default_route_id() return action @api.model