mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-30 23:18:02 +00:00
refactor(postgres): port Invoice Discounting doctype queries to the query builder
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -319,56 +319,48 @@ class InvoiceDiscounting(AccountsController):
|
|||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_invoices(filters: str):
|
def get_invoices(filters: str):
|
||||||
filters = frappe._dict(json.loads(filters))
|
filters = frappe._dict(json.loads(filters))
|
||||||
cond = []
|
si = frappe.qb.DocType("Sales Invoice")
|
||||||
if filters.customer:
|
di = frappe.qb.DocType("Discounted Invoice")
|
||||||
cond.append("customer=%(customer)s")
|
|
||||||
if filters.from_date:
|
|
||||||
cond.append("posting_date >= %(from_date)s")
|
|
||||||
if filters.to_date:
|
|
||||||
cond.append("posting_date <= %(to_date)s")
|
|
||||||
if filters.min_amount:
|
|
||||||
cond.append("base_grand_total >= %(min_amount)s")
|
|
||||||
if filters.max_amount:
|
|
||||||
cond.append("base_grand_total <= %(max_amount)s")
|
|
||||||
|
|
||||||
where_condition = ""
|
discounted = frappe.qb.from_(di).select(di.sales_invoice).where(di.docstatus == 1)
|
||||||
if cond:
|
|
||||||
where_condition += " and " + " and ".join(cond)
|
|
||||||
|
|
||||||
return frappe.db.sql(
|
query = (
|
||||||
"""
|
frappe.qb.from_(si)
|
||||||
select
|
.select(
|
||||||
name as sales_invoice,
|
si.name.as_("sales_invoice"),
|
||||||
customer,
|
si.customer,
|
||||||
posting_date,
|
si.posting_date,
|
||||||
outstanding_amount,
|
si.outstanding_amount,
|
||||||
debit_to
|
si.debit_to,
|
||||||
from `tabSales Invoice` si
|
)
|
||||||
where
|
.where((si.docstatus == 1) & (si.outstanding_amount > 0) & si.name.notin(discounted))
|
||||||
docstatus = 1
|
|
||||||
and outstanding_amount > 0
|
|
||||||
%s
|
|
||||||
and not exists(select di.name from `tabDiscounted Invoice` di
|
|
||||||
where di.docstatus=1 and di.sales_invoice=si.name)
|
|
||||||
"""
|
|
||||||
% where_condition,
|
|
||||||
filters,
|
|
||||||
as_dict=1,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if filters.customer:
|
||||||
|
query = query.where(si.customer == filters.customer)
|
||||||
|
if filters.from_date:
|
||||||
|
query = query.where(si.posting_date >= filters.from_date)
|
||||||
|
if filters.to_date:
|
||||||
|
query = query.where(si.posting_date <= filters.to_date)
|
||||||
|
if filters.min_amount:
|
||||||
|
query = query.where(si.base_grand_total >= filters.min_amount)
|
||||||
|
if filters.max_amount:
|
||||||
|
query = query.where(si.base_grand_total <= filters.max_amount)
|
||||||
|
|
||||||
|
return query.run(as_dict=1)
|
||||||
|
|
||||||
|
|
||||||
def get_party_account_based_on_invoice_discounting(sales_invoice):
|
def get_party_account_based_on_invoice_discounting(sales_invoice):
|
||||||
party_account = None
|
party_account = None
|
||||||
invoice_discounting = frappe.db.sql(
|
par = frappe.qb.DocType("Invoice Discounting")
|
||||||
"""
|
ch = frappe.qb.DocType("Discounted Invoice")
|
||||||
select par.accounts_receivable_discounted, par.accounts_receivable_unpaid, par.status
|
invoice_discounting = (
|
||||||
from `tabInvoice Discounting` par, `tabDiscounted Invoice` ch
|
frappe.qb.from_(par)
|
||||||
where par.name=ch.parent
|
.inner_join(ch)
|
||||||
and par.docstatus=1
|
.on(par.name == ch.parent)
|
||||||
and ch.sales_invoice = %s
|
.select(par.accounts_receivable_discounted, par.accounts_receivable_unpaid, par.status)
|
||||||
""",
|
.where((par.docstatus == 1) & (ch.sales_invoice == sales_invoice))
|
||||||
(sales_invoice),
|
.run(as_dict=1)
|
||||||
as_dict=1,
|
|
||||||
)
|
)
|
||||||
if invoice_discounting:
|
if invoice_discounting:
|
||||||
if invoice_discounting[0].status == "Disbursed":
|
if invoice_discounting[0].status == "Disbursed":
|
||||||
|
|||||||
Reference in New Issue
Block a user