[FIX] mrp, purchase{stock,mrp}: use route set on product in orderpoint

Steps to reproduce the bug:
- Install purchase, then mrp (in this order)
- Create a storable product “P1”
    - route: buy
    - Add a supplier
    - Add a BoM
- Create a delivery for the product “P1”
- A need is created
- Go to inventory > operation > replenishment

Problem:
An orderpoint is created with the preferred route: Manufacture instead
of buy

As the "Purchase" module was installed first, the override of
the `_set_default_route_id` function will be triggered first, the 'buy'
route will be set in the created order point because the product has a
supplier. Then, the function in the MRP module will be triggered and
since the product has a BoM, the 'buy' route will be replaced by
"Manufacture".

opw-3228971

closes odoo/odoo#119172

X-original-commit: 227d038efbadf25e3a7be7cceedd9159b71162a3
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
This commit is contained in:
Touati Djamel (otd)
2023-04-20 14:10:27 +02:00
parent ae412254f3
commit 3c3c856c2b
3 changed files with 54 additions and 4 deletions
+2 -2
View File
@@ -125,9 +125,9 @@ class StockWarehouseOrderpoint(models.Model):
def _set_default_route_id(self):
route_id = self.env['stock.rule'].search([
('action', '=', 'manufacture')
]).route_id
], limit=1).route_id
orderpoint_wh_bom = self.filtered(lambda o: o.product_id.bom_ids)
if route_id and orderpoint_wh_bom:
if route_id and orderpoint_wh_bom and (not self.product_id.route_ids or route_id in self.product_id.route_ids):
orderpoint_wh_bom.route_id = route_id[0].id
return super()._set_default_route_id()
@@ -564,3 +564,53 @@ class TestPurchaseMrpFlow(TransactionCase):
po.button_confirm()
move_in = po.picking_ids.move_ids
self.assertEqual(move_in.move_dest_ids.ids, move_check.ids)
def test_procurement_with_preferred_route_2(self):
"""
Check that the route set in the product is taken into account
when the product have a supplier and bom.
"""
manu_route = self.warehouse.manufacture_pull_id.route_id
buy_route = self.warehouse.buy_pull_id.route_id
vendor = self.env['res.partner'].create({'name': 'super vendor'})
product = self.env['product.product'].create({
'name': 'super product',
'type': 'product',
'seller_ids': [(0, 0, {'partner_id': vendor.id})],
'route_ids': buy_route,
})
self.env['mrp.bom'].create({
'product_tmpl_id': product.product_tmpl_id.id,
'product_qty': 1.0,
'product_uom_id': product.uom_id.id,
})
# create a need of the product with a picking
warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
picking = self.env['stock.picking'].create({
'location_id': warehouse.lot_stock_id.id,
'location_dest_id': self.env.ref('stock.stock_location_customers').id,
'picking_type_id': warehouse.out_type_id.id,
'move_ids': [(0, 0, {
'name': product.name,
'product_id': product.id,
'product_uom': product.uom_id.id,
'product_uom_qty': 1,
'location_id': warehouse.lot_stock_id.id,
'location_dest_id': self.env.ref('stock.stock_location_customers').id,
})]
})
picking.action_assign()
self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
orderpoint_product = self.env['stock.warehouse.orderpoint'].search(
[('product_id', '=', product.id)])
self.assertEqual(orderpoint_product.route_id, buy_route, "The route buy should be set on the orderpoint")
# Delete the orderpoint to generate a new one with the manufacture route
orderpoint_product.unlink()
# switch the product route to manufacture
product.write({'route_ids': [(3, buy_route.id), (4, manu_route.id)]})
self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
orderpoint_product = self.env['stock.warehouse.orderpoint'].search(
[('product_id', '=', product.id)])
self.assertEqual(orderpoint_product.route_id, manu_route, "The route manufacture should be set on the orderpoint")
+2 -2
View File
@@ -188,9 +188,9 @@ class Orderpoint(models.Model):
def _set_default_route_id(self):
route_id = self.env['stock.rule'].search([
('action', '=', 'buy')
]).route_id
], limit=1).route_id
orderpoint_wh_supplier = self.filtered(lambda o: o.product_id.seller_ids)
if route_id and orderpoint_wh_supplier:
if route_id and orderpoint_wh_supplier and (not self.product_id.route_ids or route_id in self.product_id.route_ids):
orderpoint_wh_supplier.route_id = route_id[0].id
return super()._set_default_route_id()