From fc692354b60aaa5e4331051056c758d21bc5b201 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Tue, 17 Mar 2020 13:07:14 +0000 Subject: [PATCH] [IMP] mrp, stock: default route on orderpoints Set a default route on orderpoint created by the replenishment report. First it select the first route on the product if it exists (it's uselss since it's a preferred route on the report but it's used for clarity). If the product has no route then it tries to guess it with vendor pricelist or BoM on the product. Task: 2161378 --- addons/mrp/models/__init__.py | 1 + addons/mrp/models/stock_orderpoint.py | 9 +++++++++ addons/purchase_stock/models/stock.py | 9 +++++++++ addons/stock/models/stock_orderpoint.py | 19 ++++++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) 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