From 3c553b0938073ee3fb4a355c884812bf6ae136f3 Mon Sep 17 00:00:00 2001 From: Devin Slauenwhite Date: Mon, 27 Mar 2023 10:24:15 -0400 Subject: [PATCH] feat: don't reserve qty on sales return --- erpnext/stock/stock_balance.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/erpnext/stock/stock_balance.py b/erpnext/stock/stock_balance.py index 439ed7a8e0..e3cbb43d8b 100644 --- a/erpnext/stock/stock_balance.py +++ b/erpnext/stock/stock_balance.py @@ -94,10 +94,13 @@ def get_balance_qty_from_sle(item_code, warehouse): def get_reserved_qty(item_code, warehouse): + dont_reserve_on_return = frappe.get_cached_value( + "Selling Settings", "Selling Settings", "dont_reserve_sales_order_qty_on_sales_return" + ) reserved_qty = frappe.db.sql( - """ + f""" select - sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty) / so_item_qty)) + sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty - if(dont_reserve_qty_on_return, so_item_returned_qty, 0)) / so_item_qty)) from ( (select @@ -112,6 +115,12 @@ def get_reserved_qty(item_code, warehouse): where name = dnpi.parent_detail_docname and delivered_by_supplier = 0 ) as so_item_delivered_qty, + ( + select returned_qty from `tabSales Order Item` + where name = dnpi.parent_detail_docname + and delivered_by_supplier = 0 + ) as so_item_returned_qty, + {dont_reserve_on_return} as dont_reserve_qty_on_return, parent, name from ( @@ -125,7 +134,9 @@ def get_reserved_qty(item_code, warehouse): ) dnpi) union (select stock_qty as dnpi_qty, qty as so_item_qty, - delivered_qty as so_item_delivered_qty, parent, name + delivered_qty as so_item_delivered_qty, + returned_qty as so_item_returned_qty, + {dont_reserve_on_return}, parent, name from `tabSales Order Item` so_item where item_code = %s and warehouse = %s and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0)