refactor: get outstanding journal entry using query builder

This commit is contained in:
Devin Slauenwhite 2023-11-29 20:59:23 +00:00
parent adfcdb3b65
commit ff27cccff4

View File

@ -9,6 +9,8 @@ import frappe
from frappe import ValidationError, _, qb, scrub, throw from frappe import ValidationError, _, qb, scrub, throw
from frappe.utils import cint, comma_or, flt, getdate, nowdate from frappe.utils import cint, comma_or, flt, getdate, nowdate
from frappe.utils.data import comma_and, fmt_money from frappe.utils.data import comma_and, fmt_money
from pypika import Case
from pypika.functions import Coalesce, Sum
import erpnext import erpnext
from erpnext.accounts.doctype.bank_account.bank_account import ( from erpnext.accounts.doctype.bank_account.bank_account import (
@ -1986,19 +1988,24 @@ def get_company_defaults(company):
def get_outstanding_on_journal_entry(name): def get_outstanding_on_journal_entry(name):
res = frappe.db.sql( gl = frappe.qb.DocType("GL Entry")
"SELECT " res = (
'CASE WHEN party_type IN ("Customer") ' frappe.qb.from_(gl)
"THEN ifnull(sum(debit_in_account_currency - credit_in_account_currency), 0) " .select(
"ELSE ifnull(sum(credit_in_account_currency - debit_in_account_currency), 0) " Case()
"END as outstanding_amount " .when(
"FROM `tabGL Entry` WHERE (voucher_no=%s OR against_voucher=%s) " gl.party_type == "Customer",
"AND party_type IS NOT NULL " Coalesce(Sum(gl.debit_in_account_currency - gl.credit_in_account_currency), 0),
'AND party_type != ""' )
"AND is_cancelled = 0", .else_(Coalesce(Sum(gl.credit_in_account_currency - gl.debit_in_account_currency), 0))
(name, name), .as_("outstanding_amount")
as_dict=1, )
) .where(
(Coalesce(gl.party_type, "") != "")
& (gl.is_cancelled == 0)
& ((gl.voucher_no == name) | (gl.against_voucher == name))
)
).run(as_dict=True)
outstanding_amount = res[0].get("outstanding_amount", 0) if res else 0 outstanding_amount = res[0].get("outstanding_amount", 0) if res else 0