Skip to content

Commit

Permalink
[FIX] stock_location_orderpoint_source_relocate
Browse files Browse the repository at this point in the history
Fix quantity to replenish in case of source relocation
  • Loading branch information
jbaudoux committed Sep 18, 2023
1 parent 246b1cc commit d9b0d0e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import stock_location_orderpoint
from . import stock_move
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2023 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, models
from odoo.tools import float_round


class StockLocationOrderpoint(models.Model):
_inherit = "stock.location.orderpoint"

@api.model
def _compute_quantities_dict(self, locations, products):
qties = super()._compute_quantities_dict(locations, products)
# With the source relocation, we could have stock on the location that
# is reserved by moves with a source location on the parent location.
# Those moves are not considered by the standard virtual available
# stock.
Move = self.env["stock.move"].with_context(active_test=False)
for location, location_dict in qties.items():
products = products.with_context(location=location.id)
_, _, domain_move_out_loc = products._get_domain_locations()
domain_move_out_loc_todo = [
(
"state",
"in",
("waiting", "confirmed", "assigned", "partially_available"),
)
] + domain_move_out_loc
for product, qty in location_dict.items():
moves = Move.search(
domain_move_out_loc_todo + [("product_id", "=", product.id)],
order="id",
)
rounding = product.uom_id.rounding
unreserved_availability = float_round(
qty["outgoing_qty"] - sum(m.reserved_availability for m in moves),
precision_rounding=rounding,
)
qty["virtual_available"] = float_round(
qty["free_qty"] + qty["incoming_qty"] - unreserved_availability,
precision_rounding=rounding,
)

return qties
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def test_auto_replenishment_without_relocation(self):
job_func = self.env["stock.location.orderpoint"].run_auto_replenishment

with trap_jobs() as trap:
move = self._create_outgoing_move(10, self.internal_location)
move.picking_type_id = self.wh.out_type_id.id
move._assign_picking()
move._action_assign()
move = self._create_outgoing_move(
10,
self.internal_location,
defaults={"picking_type_id": self.wh.out_type_id.id},
)
self.assertEqual(move.location_id, self.internal_location)
trap.assert_jobs_count(1, only=job_func)
trap.assert_enqueued_job(
Expand All @@ -75,10 +76,11 @@ def test_auto_replenishment_with_relocation(self):
self.wh.lot_stock_id, self.internal_location, self.wh.out_type_id
)
with trap_jobs() as trap:
move = self._create_outgoing_move(10, self.wh.lot_stock_id)
move.picking_type_id = self.wh.out_type_id.id
move._assign_picking()
move._action_assign()
move = self._create_outgoing_move(
10,
self.wh.lot_stock_id,
defaults={"picking_type_id": self.wh.out_type_id.id},
)
self.assertEqual(move.location_id, self.internal_location)
trap.assert_jobs_count(1, only=job_func)
trap.assert_enqueued_job(
Expand All @@ -94,3 +96,32 @@ def test_auto_replenishment_with_relocation(self):
trap.perform_enqueued_jobs()
replenish_move = self._get_replenishment_move(self.orderpoint)
self._check_replenishment_move(replenish_move, 10, self.orderpoint)

def test_auto_replenishment_with_partial_relocation(self):
job_func = self.env["stock.location.orderpoint"].run_auto_replenishment

self._create_relocate_rule(
self.wh.lot_stock_id, self.internal_location, self.wh.out_type_id
)
self._create_quants(self.product, self.internal_location, 1)
with trap_jobs() as trap:
move = self._create_outgoing_move(
10,
self.wh.lot_stock_id,
defaults={"picking_type_id": self.wh.out_type_id.id},
)
self.assertEqual(move.location_id, self.wh.lot_stock_id)
trap.assert_jobs_count(1, only=job_func)
trap.assert_enqueued_job(
job_func,
args=(move.product_id, self.internal_location, "location_id"),
kwargs={},
properties=dict(
identity_key=identity_exact,
),
)

self.product.invalidate_cache()
trap.perform_enqueued_jobs()
replenish_move = self._get_replenishment_move(self.orderpoint)
self._check_replenishment_move(replenish_move, 9, self.orderpoint)

0 comments on commit d9b0d0e

Please sign in to comment.