Skip to content

Commit 6892283

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#94300 X-original-commit: 1262120 Signed-off-by: William Henrotin (whe) <[email protected]> Signed-off-by: Adrien Widart <[email protected]>
1 parent 2a3a8a6 commit 6892283

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
@@ -682,3 +682,35 @@ def test_2steps_and_partner_on_orderpoint(self):
682682

683683
po_line = self.env['purchase.order.line'].search([('partner_id', '=', self.partner.id), ('product_id', '=', self.product_01.id)])
684684
self.assertEqual(po_line.product_qty, 5)
685+
686+
def test_change_of_scheduled_date(self):
687+
"""
688+
A user creates a delivery, an orderpoint is created. Its forecast
689+
quantity becomes -1 and the quantity to order is 1. Then the user
690+
postpones the scheduled date of the delivery. The quantities of the
691+
orderpoint should be reset to zero.
692+
"""
693+
delivery_form = Form(self.env['stock.picking'])
694+
delivery_form.partner_id = self.partner
695+
delivery_form.picking_type_id = self.env.ref('stock.picking_type_out')
696+
with delivery_form.move_ids_without_package.new() as move:
697+
move.product_id = self.product_01
698+
move.product_uom_qty = 1
699+
delivery = delivery_form.save()
700+
delivery.action_confirm()
701+
702+
self.env['report.stock.quantity'].flush()
703+
self.env['stock.warehouse.orderpoint']._get_orderpoint_action()
704+
705+
orderpoint = self.env['stock.warehouse.orderpoint'].search([('product_id', '=', self.product_01.id)])
706+
self.assertRecordValues(orderpoint, [
707+
{'qty_forecast': -1, 'qty_to_order': 1},
708+
])
709+
710+
delivery.scheduled_date += td(days=7)
711+
orderpoint.invalidate_cache(fnames=['qty_forecast', 'qty_to_order'], ids=orderpoint.ids)
712+
orderpoint.product_id.invalidate_cache(fnames=['virtual_available'], ids=orderpoint.product_id.ids)
713+
714+
self.assertRecordValues(orderpoint, [
715+
{'qty_forecast': 0, 'qty_to_order': 0},
716+
])

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)