From 02fb3899665c1a0586cd09e9e7b96883fda6ce3f Mon Sep 17 00:00:00 2001 From: kanda999 Date: Wed, 15 May 2024 17:45:05 +0800 Subject: [PATCH] [FIX] stock_account: Correct order in candidates_receipt search for FIFO valuation When decreasing `qty_done` in an outgoing stock move, the search query for `candidates_receipt` was ordering by `date` in ascending order, resulting in the selection of the oldest record instead of the most recent one. This commit changes the order to `date desc, id desc` to ensure the most recent record is selected, as intended by the comments in the write method. --- addons/stock_account/models/stock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/stock_account/models/stock.py b/addons/stock_account/models/stock.py index 5e82cedc9c368..9c7e047ae1ee1 100644 --- a/addons/stock_account/models/stock.py +++ b/addons/stock_account/models/stock.py @@ -115,7 +115,7 @@ def write(self, vals): # no need to adapt `remaining_qty` and `remaining_value` as `_run_fifo` took care of it move_vals['value'] = move_id.value - correction_value elif move_id._is_out() and qty_difference < 0: - candidates_receipt = self.env['stock.move'].search(move_id._get_in_domain(), order='date, id desc', limit=1) + candidates_receipt = self.env['stock.move'].search(move_id._get_in_domain(), order='date desc, id desc', limit=1) if candidates_receipt: candidates_receipt.write({ 'remaining_qty': candidates_receipt.remaining_qty + -qty_difference,