Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/stock/models/stock_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def _get_rule(self, product_id, location_id, values):

def extract_rule(rule_dict, route_ids, warehouse_id, location_dest_id):
rule = self.env['stock.rule']
for route_id in route_ids:
for route_id in sorted(route_ids, key=lambda r: r.sequence):
sub_dict = rule_dict.get((location_dest_id.id, route_id.id))
if not sub_dict:
continue
Expand Down
53 changes: 53 additions & 0 deletions addons/stock/tests/test_proc_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,59 @@ def test_proc_rule(self):
])
self.assertEqual(len(moves.ids), 1, "It should have created a picking from Stock to Output with the original picking as destination")

def test_get_rule_respects_sequence_order(self):
"""Test that _get_rule selects the rule associated with the route of the lowest sequence."""

# Create a warehouse and a product
warehouse = self.env['stock.warehouse'].search([], limit=1)
product = self.env['product.product'].create({'name': 'Test Product', 'type': 'product'})

# Create routes with different sequences to simulate prioritization.
route_low_priority = self.env['stock.route'].create({'name': 'Route 1', 'sequence': 10})
rule_low_priority = self.env['stock.rule'].create({
'name': 'Rule for Route 1',
'route_id': route_low_priority.id,
'action': 'pull',
'location_src_id': warehouse.lot_stock_id.id,
'location_dest_id': warehouse.lot_stock_id.id,
'picking_type_id': warehouse.out_type_id.id,
'sequence': 20,
})

# Create a second route with higher priority (lower sequence).
route_high_priority = self.env['stock.route'].create({'name': 'Route 2', 'sequence': 5})
rule_high_priority = self.env['stock.rule'].create({
'name': 'Rule for Route 2',
'route_id': route_high_priority.id,
'action': 'pull',
'location_src_id': warehouse.lot_stock_id.id,
'location_dest_id': warehouse.lot_stock_id.id,
'picking_type_id': warehouse.out_type_id.id,
'sequence': 20,
})

# Assign both routes to the product. This order is set so that the method
# will be forced to sort the routes by their sequence.
product.write({'route_ids': [(4, route_low_priority.id), (4, route_high_priority.id)]})

# Create a procurement group for testing rule selection.
procurement_group = self.env['procurement.group'].create({'name': 'Test Procurement Group'})

# Call the _get_rule method to simulate rule selection.
rule = procurement_group._get_rule(
product_id=product,
location_id=warehouse.lot_stock_id,
values={
'warehouse_id': warehouse,
'route_ids': product.route_ids,
}
)

# Assert that the selected rule corresponds to the route with the lowest sequence.
self.assertEqual(rule, rule_high_priority,
"The rule associated with the route having the lowest sequence "
"(high_priority) should be selected.")

def test_propagate_deadline_move(self):
deadline = datetime.now()
move_dest = self.env['stock.move'].create({
Expand Down
3 changes: 2 additions & 1 deletion doc/cla/corporate/forgeflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Hector Villarreal hector.villarreal@forgeflow.com https://github.com/HviorForgeF
Bernat Puig bernat.puig@forgeflow.com https://github.com/BernatPForgeFlow
Joan Sisquella joan.sisquella@forgeflow.com https://github.com/JoanSForgeFlow
Laura Cazorla laura.cazorla@forgeflow.com https://github.com/LauraCForgeFlow
Arnau Cruz arnau.cruz@forgeflow.com https://github.com/ArnauCForgeFlow
Arnau Cruz arnau.cruz@forgeflow.com https://github.com/ArnauCForgeFlow
Ricard Calvo ricard.calvo@forgeflow.com https://github.com/RicardCForgeFlow