Skip to content

Commit 18c338a

Browse files
committed
[IMP] stock_storage_type: Rely on ORM in order to get locations per quantity
The current implementation was triggerring queries (read_group). Rely now on ORM to get records as 'quant_ids' field exists on locations
1 parent 838188c commit 18c338a

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

stock_storage_type/models/stock_location.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from odoo import api, fields, models
99
from odoo.fields import Command
10-
from odoo.tools import float_compare, index_exists
10+
from odoo.tools import float_compare, groupby, index_exists
1111

1212
_logger = logging.getLogger(__name__)
1313
OUT_MOVE_LINE_DOMAIN = [
@@ -703,18 +703,18 @@ def _order_allowed_locations(self, valid_locations):
703703
valid_no_mix = valid_locations.filtered("do_not_mix_products")
704704
loc_ordered_by_qty = []
705705
if valid_no_mix:
706-
StockQuant = self.env["stock.quant"]
707-
domain_quant = [("location_id", "in", valid_no_mix.ids)]
708-
loc_ordered_by_qty = [
709-
item["location_id"][0]
710-
for item in StockQuant.read_group(
711-
domain_quant,
712-
["location_id", "quantity"],
713-
["location_id"],
714-
orderby="quantity",
706+
for location, items in groupby(
707+
valid_no_mix.quant_ids.sorted("quantity"),
708+
lambda quant: quant.location_id,
709+
):
710+
loc_ordered_by_qty.extend(
711+
[
712+
location.id
713+
for item in items
714+
if (float_compare(item["quantity"], 0, precision_digits=2) > 0)
715+
]
715716
)
716-
if (float_compare(item["quantity"], 0, precision_digits=2) > 0)
717-
]
717+
718718
valid_location_ids = set(valid_locations.ids) - set(loc_ordered_by_qty)
719719
ordered_valid_location_ids = loc_ordered_by_qty + [
720720
id_ for id_ in self.ids if id_ in valid_location_ids

stock_storage_type/tests/test_storage_type_putaway_strategy.py

+14
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,17 @@ def test_storage_strategy_same_ordered_locations_pallets_reapply(self):
921921
int_picking.move_line_ids[0].mapped("location_dest_id"),
922922
self.pallets_bin_3_location,
923923
)
924+
925+
self.env["stock.quant"].with_context(inventory_mode=True).create(
926+
{
927+
"product_id": self.product.id,
928+
"location_id": self.pallets_bin_3_location.id,
929+
"inventory_quantity": 10.0,
930+
}
931+
)._apply_inventory()
932+
933+
int_picking.move_line_ids._apply_putaway_strategy()
934+
self.assertEqual(
935+
int_picking.move_line_ids[0].mapped("location_dest_id"),
936+
self.pallets_bin_3_location,
937+
)

0 commit comments

Comments
 (0)