From 139a193f1ded455d249d762b0a055177ffec6d5c Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 29 May 2023 09:24:20 +0530 Subject: [PATCH] fix: incorrect `POS Reserved Qty` in `Stock Projected Qty` Report (#35437) * fix: incorrect `POS Reserved Qty` in `Stock Projected Qty` Report (cherry picked from commit 027de4160097b90f93b90c0a2d24b82e1553df54) # Conflicts: # erpnext/accounts/doctype/pos_invoice/pos_invoice.py * chore: `conflicts` --------- Co-authored-by: Sagar Sharma --- .../doctype/pos_invoice/pos_invoice.py | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py index 64071597eab..08656d1c0be 100644 --- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py +++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py @@ -4,6 +4,7 @@ import frappe from frappe import _ +from frappe.query_builder.functions import IfNull, Sum from frappe.utils import cint, flt, get_link_to_form, getdate, nowdate from six import iteritems @@ -675,18 +676,22 @@ def get_bin_qty(item_code, warehouse): def get_pos_reserved_qty(item_code, warehouse): - reserved_qty = frappe.db.sql( - """select sum(p_item.qty) as qty - from `tabPOS Invoice` p, `tabPOS Invoice Item` p_item - where p.name = p_item.parent - and ifnull(p.consolidated_invoice, '') = '' - and p_item.docstatus = 1 - and p_item.item_code = %s - and p_item.warehouse = %s - """, - (item_code, warehouse), - as_dict=1, - ) + p_inv = frappe.qb.DocType("POS Invoice") + p_item = frappe.qb.DocType("POS Invoice Item") + + reserved_qty = ( + frappe.qb.from_(p_inv) + .from_(p_item) + .select(Sum(p_item.qty).as_("qty")) + .where( + (p_inv.name == p_item.parent) + & (IfNull(p_inv.consolidated_invoice, "") == "") + & (p_inv.is_return == 0) + & (p_item.docstatus == 1) + & (p_item.item_code == item_code) + & (p_item.warehouse == warehouse) + ) + ).run(as_dict=True) return reserved_qty[0].qty or 0 if reserved_qty else 0