Skip to content

Commit

Permalink
[FIX] stock_reserve: Set the correct forecasted quantity on products
Browse files Browse the repository at this point in the history
TT43413
  • Loading branch information
victoralmau committed Oct 6, 2023
1 parent 2986b3a commit 005b430
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
14 changes: 14 additions & 0 deletions stock_reserve/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def _compute_reservation_count(self):
reservations = self.env["stock.reservation"].search(domain)
product.reservation_count = sum(reservations.mapped("product_qty"))

def _get_domain_locations_new(
self, location_ids, company_id=False, compute_child=True
):
"""Add location reservation for avoiding reservations to be substracted on
the forecasted quantity."""
location_ids = location_ids.union(
set(self.env.ref("stock_reserve.stock_location_reservation").ids)
)
return super()._get_domain_locations_new(
location_ids=location_ids,
company_id=company_id,
compute_child=compute_child,
)

def action_view_reservations(self):
self.ensure_one()
ref = "stock_reserve.action_stock_reservation_tree"
Expand Down
44 changes: 31 additions & 13 deletions stock_reserve/tests/test_stock_reserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ def setUp(self):
product_form.name = "Test Product"
product_form.type = "product"
self.product = product_form.save()
self._create_stock_quant(10)

def _create_stock_quant(self, qty):
self.env["stock.quant"].create(
{
"product_id": self.product.id,
"location_id": self.warehouse.lot_stock_id.id,
"quantity": 10.0,
"quantity": qty,
}
)

Expand All @@ -30,24 +33,45 @@ def _create_stock_reservation(self, qty):
form_reservation.location_id = self.warehouse.lot_stock_id
return form_reservation.save()

def test_reservation_with_previous_picking(self):
self.assertEqual(self.product.virtual_available, 10)
picking_form = Form(
self.env["stock.picking"].with_context(
default_picking_type_id=self.env.ref("stock.picking_type_out").id
)
)
with picking_form.move_ids_without_package.new() as line_form:
line_form.product_id = self.product
picking = picking_form.save()
picking.move_ids_without_package.product_uom_qty = 10
picking.action_confirm()
self.assertEqual(self.product.virtual_available, 0)
reservation_1 = self._create_stock_reservation(10)
reservation_1.reserve()
self.assertEqual(self.product.virtual_available, 0)
reservation_1.release_reserve()
self.assertEqual(self.product.virtual_available, 0)
picking.action_cancel()
self.assertEqual(self.product.virtual_available, 10)

def test_reservation_and_reservation_release(self):
reservation_1 = self._create_stock_reservation(6)
reservation_1.reserve()
self.assertFalse(reservation_1.picking_id)
self.assertEqual(self.product.virtual_available, 4)
self.assertEqual(self.product.virtual_available, 10)
reservation_2 = self._create_stock_reservation(1)
reservation_2.reserve()
self.assertFalse(reservation_2.picking_id)
self.assertEqual(self.product.virtual_available, 3)
self.assertEqual(self.product.virtual_available, 10)
reservation_1.release_reserve()
self.assertEqual(self.product.virtual_available, 9)
self.assertEqual(self.product.virtual_available, 10)

def test_cron_release(self):
reservation_1 = self._create_stock_reservation(6)
reservation_1.date_validity = fields.Date.from_string("2021-01-01")
reservation_1.reserve()
self.assertFalse(reservation_1.picking_id)
self.assertEqual(self.product.virtual_available, 4)
self.assertEqual(self.product.virtual_available, 10)
cron = self.env.ref("stock_reserve.ir_cron_release_stock_reservation")
cron.method_direct_trigger()
self.assertEqual(self.product.virtual_available, 10)
Expand All @@ -57,14 +81,8 @@ def test_cron_reserve(self):
reservation_1.reserve()
self.assertFalse(reservation_1.picking_id)
self.assertEqual(reservation_1.state, "partially_available")
self.env["stock.quant"].create(
{
"product_id": self.product.id,
"location_id": self.warehouse.lot_stock_id.id,
"quantity": 10.0,
}
)
self._create_stock_quant(10)
cron = self.env.ref("stock_reserve.ir_cron_reserve_waiting_confirmed")
cron.method_direct_trigger()
self.assertEqual(reservation_1.state, "assigned")
self.assertEqual(self.product.virtual_available, 9)
self.assertEqual(self.product.virtual_available, 20)

0 comments on commit 005b430

Please sign in to comment.