mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-09 13:26:39 +00:00
fix: add GROUP BY for dn_detail and convert SQL query to QB
(cherry picked from commit fd9167f2af)
# Conflicts:
# erpnext/stock/doctype/delivery_note/delivery_note.py
This commit is contained in:
@@ -10,6 +10,8 @@ from frappe.contacts.doctype.address.address import get_company_address
|
|||||||
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.model.utils import get_fetch_values
|
from frappe.model.utils import get_fetch_values
|
||||||
|
from frappe.query_builder import DocType
|
||||||
|
from frappe.query_builder.functions import Abs, Sum
|
||||||
from frappe.utils import cint, flt
|
from frappe.utils import cint, flt
|
||||||
|
|
||||||
from erpnext.accounts.party import get_due_date
|
from erpnext.accounts.party import get_due_date
|
||||||
@@ -790,23 +792,27 @@ def get_list_context(context=None):
|
|||||||
|
|
||||||
def get_invoiced_qty_map(delivery_note):
|
def get_invoiced_qty_map(delivery_note):
|
||||||
"""returns a map: {dn_detail: invoiced_qty}"""
|
"""returns a map: {dn_detail: invoiced_qty}"""
|
||||||
invoiced_qty_map = {}
|
sii = DocType("Sales Invoice Item")
|
||||||
|
|
||||||
for dn_detail, qty in frappe.db.sql(
|
invoiced_qty_map = frappe._dict(
|
||||||
"""select dn_detail, qty from `tabSales Invoice Item`
|
(
|
||||||
where delivery_note=%s and docstatus=1""",
|
frappe.qb.from_(sii)
|
||||||
delivery_note,
|
.select(sii.dn_detail, Sum(sii.qty).as_("qty"))
|
||||||
):
|
.where((sii.delivery_note == delivery_note) & (sii.docstatus == 1))
|
||||||
if not invoiced_qty_map.get(dn_detail):
|
.groupby(sii.dn_detail)
|
||||||
invoiced_qty_map[dn_detail] = 0
|
).run()
|
||||||
invoiced_qty_map[dn_detail] += qty
|
)
|
||||||
|
|
||||||
return invoiced_qty_map
|
return invoiced_qty_map
|
||||||
|
|
||||||
|
|
||||||
def get_returned_qty_map(delivery_note):
|
def get_returned_qty_map(delivery_note):
|
||||||
"""returns a map: {so_detail: returned_qty}"""
|
"""returns a map: {so_detail: returned_qty}"""
|
||||||
|
dn = DocType("Delivery Note")
|
||||||
|
dni = DocType("Delivery Note Item")
|
||||||
|
|
||||||
returned_qty_map = frappe._dict(
|
returned_qty_map = frappe._dict(
|
||||||
|
<<<<<<< HEAD
|
||||||
frappe.db.sql(
|
frappe.db.sql(
|
||||||
"""select dn_item.dn_detail, sum(abs(dn_item.qty)) as qty
|
"""select dn_item.dn_detail, sum(abs(dn_item.qty)) as qty
|
||||||
from `tabDelivery Note Item` dn_item, `tabDelivery Note` dn
|
from `tabDelivery Note Item` dn_item, `tabDelivery Note` dn
|
||||||
@@ -819,6 +825,21 @@ def get_returned_qty_map(delivery_note):
|
|||||||
""",
|
""",
|
||||||
delivery_note,
|
delivery_note,
|
||||||
)
|
)
|
||||||
|
=======
|
||||||
|
(
|
||||||
|
frappe.qb.from_(dni)
|
||||||
|
.join(dn)
|
||||||
|
.on(dn.name == dni.parent)
|
||||||
|
.select(dni.dn_detail, Sum(Abs(dni.qty)).as_("qty"))
|
||||||
|
.where(
|
||||||
|
(dn.docstatus == 1)
|
||||||
|
& (dn.is_return == 1)
|
||||||
|
& (dn.return_against == delivery_note)
|
||||||
|
& (dni.qty <= 0)
|
||||||
|
)
|
||||||
|
.groupby(dni.dn_detail)
|
||||||
|
).run()
|
||||||
|
>>>>>>> fd9167f2af (fix: add GROUP BY for dn_detail and convert SQL query to QB)
|
||||||
)
|
)
|
||||||
|
|
||||||
return returned_qty_map
|
return returned_qty_map
|
||||||
|
|||||||
Reference in New Issue
Block a user