From 9979cf5fcc20cf6f267b94bd23a63ce6c4d761c6 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 21 May 2021 12:12:42 +0530 Subject: [PATCH 1/2] fix: wrong quantity after transaction for parallel stock transactions When two transactions are inserted parallelly then previous SLE could be incorrect for some of them. Locking SLE table would prevent reading from it till transaction is complete. --- erpnext/stock/stock_ledger.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 9729987d2d3..790318a625c 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -232,7 +232,8 @@ class update_entries_after(object): and is_cancelled = 0 and timestamp(posting_date, time_format(posting_time, %(time_format)s)) < timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) order by timestamp(posting_date, posting_time) desc, creation desc - limit 1""", args, as_dict=1) + limit 1 + for update""", args, as_dict=1) return sle[0] if sle else frappe._dict() From 4dcac4ae8198ec6e594e1bdfe2408fc33f627940 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 21 May 2021 13:12:30 +0530 Subject: [PATCH 2/2] refactor(minor): Use identity instead of equality Ignore false positive. --- erpnext/stock/stock_ledger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 790318a625c..b2825fc26f5 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -624,7 +624,7 @@ class update_entries_after(object): break # If no entry found with outgoing rate, collapse stack - if index == None: + if index is None: # nosemgrep new_stock_value = sum((d[0]*d[1] for d in self.wh_data.stock_queue)) - qty_to_pop*outgoing_rate new_stock_qty = sum((d[0] for d in self.wh_data.stock_queue)) - qty_to_pop self.wh_data.stock_queue = [[new_stock_qty, new_stock_value/new_stock_qty if new_stock_qty > 0 else outgoing_rate]]