mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-02 13:08:27 +00:00
fix: fetch child account data for selected parent (#45904)
* fix: fetch child account data for selected parent
* fix: change reference name
---------
Co-authored-by: venkat102 <venkatesharunachalam659@gmail.com>
(cherry picked from commit 73e82b7afa)
This commit is contained in:
@@ -68,16 +68,12 @@ frappe.query_reports["Trial Balance for Party"] = {
|
|||||||
{
|
{
|
||||||
fieldname: "account",
|
fieldname: "account",
|
||||||
label: __("Account"),
|
label: __("Account"),
|
||||||
fieldtype: "Link",
|
fieldtype: "MultiSelectList",
|
||||||
options: "Account",
|
options: "Account",
|
||||||
get_query: function () {
|
get_data: function (txt) {
|
||||||
var company = frappe.query_report.get_filter_value("company");
|
return frappe.db.get_link_options("Account", txt, {
|
||||||
return {
|
company: frappe.query_report.get_filter_value("company"),
|
||||||
doctype: "Account",
|
});
|
||||||
filters: {
|
|
||||||
company: company,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import cint, flt
|
from frappe.utils import cint, flt
|
||||||
|
|
||||||
|
from erpnext.accounts.report.general_ledger.general_ledger import get_accounts_with_children
|
||||||
from erpnext.accounts.report.trial_balance.trial_balance import validate_filters
|
from erpnext.accounts.report.trial_balance.trial_balance import validate_filters
|
||||||
|
|
||||||
|
|
||||||
@@ -35,9 +37,14 @@ def get_data(filters, show_party_name):
|
|||||||
filters=party_filters,
|
filters=party_filters,
|
||||||
order_by="name",
|
order_by="name",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
account_filter = []
|
||||||
|
if filters.get("account"):
|
||||||
|
account_filter = get_accounts_with_children(filters.get("account"))
|
||||||
|
|
||||||
company_currency = frappe.get_cached_value("Company", filters.company, "default_currency")
|
company_currency = frappe.get_cached_value("Company", filters.company, "default_currency")
|
||||||
opening_balances = get_opening_balances(filters)
|
opening_balances = get_opening_balances(filters, account_filter)
|
||||||
balances_within_period = get_balances_within_period(filters)
|
balances_within_period = get_balances_within_period(filters, account_filter)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
# total_debit, total_credit = 0, 0
|
# total_debit, total_credit = 0, 0
|
||||||
@@ -89,30 +96,34 @@ def get_data(filters, show_party_name):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_opening_balances(filters):
|
def get_opening_balances(filters, account_filter=None):
|
||||||
account_filter = ""
|
GL_Entry = frappe.qb.DocType("GL Entry")
|
||||||
if filters.get("account"):
|
|
||||||
account_filter = "and account = %s" % (frappe.db.escape(filters.get("account")))
|
|
||||||
|
|
||||||
gle = frappe.db.sql(
|
query = (
|
||||||
f"""
|
frappe.qb.from_(GL_Entry)
|
||||||
select party, sum(debit) as opening_debit, sum(credit) as opening_credit
|
.select(
|
||||||
from `tabGL Entry`
|
GL_Entry.party,
|
||||||
where company=%(company)s
|
Sum(GL_Entry.debit).as_("opening_debit"),
|
||||||
and is_cancelled=0
|
Sum(GL_Entry.credit).as_("opening_credit"),
|
||||||
and ifnull(party_type, '') = %(party_type)s and ifnull(party, '') != ''
|
)
|
||||||
and (posting_date < %(from_date)s or (ifnull(is_opening, 'No') = 'Yes' and posting_date <= %(to_date)s))
|
.where(
|
||||||
{account_filter}
|
(GL_Entry.company == filters.company)
|
||||||
group by party""",
|
& (GL_Entry.is_cancelled == 0)
|
||||||
{
|
& (GL_Entry.party_type == filters.party_type)
|
||||||
"company": filters.company,
|
& (GL_Entry.party != "")
|
||||||
"from_date": filters.from_date,
|
& (
|
||||||
"to_date": filters.to_date,
|
(GL_Entry.posting_date < filters.from_date)
|
||||||
"party_type": filters.party_type,
|
| ((GL_Entry.is_opening == "Yes") & (GL_Entry.posting_date <= filters.to_date))
|
||||||
},
|
)
|
||||||
as_dict=True,
|
)
|
||||||
|
.groupby(GL_Entry.party)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if account_filter:
|
||||||
|
query = query.where(GL_Entry.account.isin(account_filter))
|
||||||
|
|
||||||
|
gle = query.run(as_dict=True)
|
||||||
|
|
||||||
opening = frappe._dict()
|
opening = frappe._dict()
|
||||||
for d in gle:
|
for d in gle:
|
||||||
opening_debit, opening_credit = toggle_debit_credit(d.opening_debit, d.opening_credit)
|
opening_debit, opening_credit = toggle_debit_credit(d.opening_debit, d.opening_credit)
|
||||||
@@ -121,31 +132,33 @@ def get_opening_balances(filters):
|
|||||||
return opening
|
return opening
|
||||||
|
|
||||||
|
|
||||||
def get_balances_within_period(filters):
|
def get_balances_within_period(filters, account_filter=None):
|
||||||
account_filter = ""
|
GL_Entry = frappe.qb.DocType("GL Entry")
|
||||||
if filters.get("account"):
|
|
||||||
account_filter = "and account = %s" % (frappe.db.escape(filters.get("account")))
|
|
||||||
|
|
||||||
gle = frappe.db.sql(
|
query = (
|
||||||
f"""
|
frappe.qb.from_(GL_Entry)
|
||||||
select party, sum(debit) as debit, sum(credit) as credit
|
.select(
|
||||||
from `tabGL Entry`
|
GL_Entry.party,
|
||||||
where company=%(company)s
|
Sum(GL_Entry.debit).as_("debit"),
|
||||||
and is_cancelled = 0
|
Sum(GL_Entry.credit).as_("credit"),
|
||||||
and ifnull(party_type, '') = %(party_type)s and ifnull(party, '') != ''
|
)
|
||||||
and posting_date >= %(from_date)s and posting_date <= %(to_date)s
|
.where(
|
||||||
and ifnull(is_opening, 'No') = 'No'
|
(GL_Entry.company == filters.company)
|
||||||
{account_filter}
|
& (GL_Entry.is_cancelled == 0)
|
||||||
group by party""",
|
& (GL_Entry.party_type == filters.party_type)
|
||||||
{
|
& (GL_Entry.party != "")
|
||||||
"company": filters.company,
|
& (GL_Entry.posting_date >= filters.from_date)
|
||||||
"from_date": filters.from_date,
|
& (GL_Entry.posting_date <= filters.to_date)
|
||||||
"to_date": filters.to_date,
|
& (GL_Entry.is_opening == "No")
|
||||||
"party_type": filters.party_type,
|
)
|
||||||
},
|
.groupby(GL_Entry.party)
|
||||||
as_dict=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if account_filter:
|
||||||
|
query = query.where(GL_Entry.account.isin(account_filter))
|
||||||
|
|
||||||
|
gle = query.run(as_dict=True)
|
||||||
|
|
||||||
balances_within_period = frappe._dict()
|
balances_within_period = frappe._dict()
|
||||||
for d in gle:
|
for d in gle:
|
||||||
balances_within_period.setdefault(d.party, [d.debit, d.credit])
|
balances_within_period.setdefault(d.party, [d.debit, d.credit])
|
||||||
|
|||||||
Reference in New Issue
Block a user