Skip to content

Commit

Permalink
refactor: use frappe.qb instead of sql
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Jan 14, 2022
1 parent fc65a3d commit 0a9ec9f
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions erpnext/stock/doctype/delivery_note/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,35 @@ def make_return_invoice(self):
frappe.throw(_("Could not create Credit Note automatically, please uncheck 'Issue Credit Note' and submit again"))

def update_billed_amount_based_on_so(so_detail, update_modified=True):
from frappe.query_builder.functions import Sum

# Billed against Sales Order directly
billed_against_so = frappe.db.sql("""select sum(si_item.amount)
from `tabSales Invoice Item` si_item, `tabSales Invoice` si
where
si_item.parent = si.name
and si_item.so_detail=%s
and (si_item.dn_detail is null or si_item.dn_detail = '')
and si_item.docstatus=1
and si.update_stock = 0
""", so_detail)
si = frappe.qb.DocType("Sales Invoice").as_("si")
si_item = frappe.qb.DocType("Sales Invoice Item").as_("si_item")
sum_amount = Sum(si_item.amount).as_("amount")

billed_against_so = frappe.qb.from_(si).from_(si_item).select(sum_amount).where(
(si_item.parent == si.name) &
(si_item.so_detail == so_detail) &
((si_item.dn_detail.isnull()) | (si_item.dn_detail == '')) &
(si_item.docstatus == 1) &
(si.update_stock == 0)
).run()
billed_against_so = billed_against_so and billed_against_so[0][0] or 0

# Get all Delivery Note Item rows against the Sales Order Item row
dn_details = frappe.db.sql("""select dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent, dn_item.stock_qty, dn_item.returned_qty
from `tabDelivery Note Item` dn_item, `tabDelivery Note` dn
where
dn.name = dn_item.parent
and dn_item.so_detail=%s
and dn.docstatus=1
and dn.is_return = 0
order by dn.posting_date asc, dn.posting_time asc, dn.name asc""", so_detail, as_dict=1)

dn = frappe.qb.DocType("Delivery Note").as_("dn")
dn_item = frappe.qb.DocType("Delivery Note Item").as_("dn_item")

dn_details = frappe.qb.from_(dn).from_(dn_item).select(dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent, dn_item.stock_qty, dn_item.returned_qty).where(
(dn.name == dn_item.parent) &
(dn_item.so_detail == so_detail) &
(dn.docstatus == 1) &
(dn.is_return == 0)
).orderby(
dn.posting_date, dn.posting_time, dn.name
).run(as_dict=True)

updated_dn = []
for dnd in dn_details:
Expand Down

0 comments on commit 0a9ec9f

Please sign in to comment.