fix: incorrect invoice qty

(cherry picked from commit 96cdb7d54f)
This commit is contained in:
Rohit Waghchaure
2025-12-12 12:21:10 +05:30
committed by Mergify
parent 860486bb34
commit ebbecdba23

View File

@@ -8,7 +8,7 @@ import frappe
from frappe import _, throw from frappe import _, throw
from frappe.desk.notifications import clear_doctype_notifications from frappe.desk.notifications import clear_doctype_notifications
from frappe.model.mapper import get_mapped_doc from frappe.model.mapper import get_mapped_doc
from frappe.query_builder.functions import CombineDatetime from frappe.query_builder.functions import Abs, CombineDatetime, Sum
from frappe.utils import cint, flt, get_datetime, getdate, nowdate from frappe.utils import cint, flt, get_datetime, getdate, nowdate
from pypika import functions as fn from pypika import functions as fn
@@ -1372,21 +1372,26 @@ def get_invoiced_qty_map(purchase_receipt):
def get_returned_qty_map(purchase_receipt): def get_returned_qty_map(purchase_receipt):
"""returns a map: {so_detail: returned_qty}""" """returns a map: {pr_detail: returned_qty}"""
returned_qty_map = frappe._dict(
frappe.db.sql(
"""select pr_item.purchase_receipt_item, abs(pr_item.qty) as qty
from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr
where pr.name = pr_item.parent
and pr.docstatus = 1
and pr.is_return = 1
and pr.return_against = %s
""",
purchase_receipt,
)
)
return returned_qty_map pr = frappe.qb.DocType("Purchase Receipt")
pr_item = frappe.qb.DocType("Purchase Receipt Item")
query = (
frappe.qb.from_(pr)
.inner_join(pr_item)
.on(pr.name == pr_item.parent)
.select(pr_item.purchase_receipt_item, Sum(Abs(pr_item.qty)).as_("qty"))
.where(
(pr.docstatus == 1)
& (pr.is_return == 1)
& (pr.return_against == purchase_receipt)
& (pr_item.purchase_receipt_item.isnotnull())
)
.groupby(pr_item.purchase_receipt_item)
).run(as_list=1)
return frappe._dict(query) if query else frappe._dict()
@frappe.whitelist() @frappe.whitelist()