Skip to content

Commit 8b551cd

Browse files
committed
[FIX] {purchase_,}stock: recompute orderpoints on SM's date change
Updating the scheduled date of a delivery doesn't update the quantity to order of an orderpoint To reproduce the issue: (Need purchase) 1. Create a storable product P with a seller 2. Create and confirm a planned delivery D with 1 x P 3. Open the replenishment page - There should be a line for P (Forecast: -1, To Order: 1) 4. Edit D and postpone the scheduled date 5. Go back to replenishment page Error: The line is still present, its forecast qty is correct (0) but the quantity to order is still 1 (instead of 0) `qty_to_order` is a stored field, so when loading the replenishment page, its compute method is not called. Moreover, even though `qty_to_order` depends on `qty_forecast` and the compute method of `qty_forecast` is called, it still won't trigger the compute: when setting the value of `qty_forecast` from its compute method, it will lead to: https://github.com/odoo/odoo/blob/b54f78de307543efcea934206806f361eaac811a/odoo/fields.py#L1106-L1109 So, as shown and explained, we bypass the `write` of `BaseModel` and skip the business logic and the recomputations The compute of `qty_to_order` should actually be triggered earlier: when we edit the scheduled date (step 4). That's the reason why this commit adds a dependency to the compute of `qty_forecast`: it makes more sense and becomes an implicit dependency of `qty_to_order` -> update the scheduled date will trigger the compute of `qty_to_order` OPW-2868167 closes odoo#94284 X-original-commit: 1262120 Signed-off-by: William Henrotin (whe) <[email protected]> Signed-off-by: Adrien Widart <[email protected]>
1 parent 4543a0a commit 8b551cd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

addons/purchase_stock/tests/test_reordering_rule.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,35 @@ def test_multi_locations_and_reordering_rule(self):
656656
{'product_id': self.product_01.id, 'qty_done': 1.0, 'state': 'done', 'location_dest_id': stock_location.id},
657657
{'product_id': self.product_01.id, 'qty_done': 2.0, 'state': 'done', 'location_dest_id': sub_location.id},
658658
])
659+
660+
def test_change_of_scheduled_date(self):
661+
"""
662+
A user creates a delivery, an orderpoint is created. Its forecast
663+
quantity becomes -1 and the quantity to order is 1. Then the user
664+
postpones the scheduled date of the delivery. The quantities of the
665+
orderpoint should be reset to zero.
666+
"""
667+
delivery_form = Form(self.env['stock.picking'])
668+
delivery_form.partner_id = self.partner
669+
delivery_form.picking_type_id = self.env.ref('stock.picking_type_out')
670+
with delivery_form.move_ids_without_package.new() as move:
671+
move.product_id = self.product_01
672+
move.product_uom_qty = 1
673+
delivery = delivery_form.save()
674+
delivery.action_confirm()
675+
676+
self.env['report.stock.quantity'].flush()
677+
self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
678+
679+
orderpoint = self.env['stock.warehouse.orderpoint'].search([('product_id', '=', self.product_01.id)])
680+
self.assertRecordValues(orderpoint, [
681+
{'qty_forecast': -1, 'qty_to_order': 1},
682+
])
683+
684+
delivery.scheduled_date += td(days=7)
685+
orderpoint.invalidate_cache(fnames=['qty_forecast', 'qty_to_order'], ids=orderpoint.ids)
686+
orderpoint.product_id.invalidate_cache(fnames=['virtual_available'], ids=orderpoint.product_id.ids)
687+
688+
self.assertRecordValues(orderpoint, [
689+
{'qty_forecast': 0, 'qty_to_order': 0},
690+
])

addons/stock/models/stock_orderpoint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ def action_replenish_auto(self):
231231
self.trigger = 'auto'
232232
return self.action_replenish()
233233

234-
@api.depends('product_id', 'location_id', 'product_id.stock_move_ids', 'product_id.stock_move_ids.state', 'product_id.stock_move_ids.product_uom_qty')
234+
@api.depends('product_id', 'location_id', 'product_id.stock_move_ids', 'product_id.stock_move_ids.state',
235+
'product_id.stock_move_ids.date', 'product_id.stock_move_ids.product_uom_qty')
235236
def _compute_qty(self):
236237
orderpoints_contexts = defaultdict(lambda: self.env['stock.warehouse.orderpoint'])
237238
for orderpoint in self:

0 commit comments

Comments
 (0)