fix: Group GLs by account for TB generation

(cherry picked from commit f894c6d275)
(cherry picked from commit 416d9bce2c)
This commit is contained in:
Deepesh Garg
2025-04-13 18:53:11 +05:30
committed by Mergify
parent 065c9fa85f
commit 70fa366216
2 changed files with 17 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ import re
import frappe import frappe
from frappe import _ from frappe import _
from frappe.query_builder.functions import Sum
from frappe.utils import add_days, add_months, cint, cstr, flt, formatdate, get_first_day, getdate from frappe.utils import add_days, add_months, cint, cstr, flt, formatdate, get_first_day, getdate
from pypika.terms import ExistsCriterion from pypika.terms import ExistsCriterion
@@ -428,6 +429,7 @@ def set_gl_entries_by_account(
root_type=None, root_type=None,
ignore_closing_entries=False, ignore_closing_entries=False,
ignore_opening_entries=False, ignore_opening_entries=False,
group_by_account=False,
): ):
"""Returns a dict like { "account": [gl entries], ... }""" """Returns a dict like { "account": [gl entries], ... }"""
gl_entries = [] gl_entries = []
@@ -459,6 +461,7 @@ def set_gl_entries_by_account(
root_type, root_type,
ignore_closing_entries, ignore_closing_entries,
last_period_closing_voucher[0].name, last_period_closing_voucher[0].name,
group_by_account=group_by_account,
) )
from_date = add_days(last_period_closing_voucher[0].period_end_date, 1) from_date = add_days(last_period_closing_voucher[0].period_end_date, 1)
ignore_opening_entries = True ignore_opening_entries = True
@@ -473,6 +476,7 @@ def set_gl_entries_by_account(
root_type, root_type,
ignore_closing_entries, ignore_closing_entries,
ignore_opening_entries=ignore_opening_entries, ignore_opening_entries=ignore_opening_entries,
group_by_account=group_by_account,
) )
if filters and filters.get("presentation_currency"): if filters and filters.get("presentation_currency"):
@@ -495,21 +499,29 @@ def get_accounting_entries(
ignore_closing_entries=None, ignore_closing_entries=None,
period_closing_voucher=None, period_closing_voucher=None,
ignore_opening_entries=False, ignore_opening_entries=False,
group_by_account=False,
): ):
gl_entry = frappe.qb.DocType(doctype) gl_entry = frappe.qb.DocType(doctype)
query = ( query = (
frappe.qb.from_(gl_entry) frappe.qb.from_(gl_entry)
.select( .select(
gl_entry.account, gl_entry.account,
gl_entry.debit, gl_entry.debit if not group_by_account else Sum(gl_entry.debit).as_("debit"),
gl_entry.credit, gl_entry.credit if not group_by_account else Sum(gl_entry.credit).as_("credit"),
gl_entry.debit_in_account_currency, gl_entry.debit_in_account_currency
gl_entry.credit_in_account_currency, if not group_by_account
else Sum(gl_entry.debit_in_account_currency).as_("debit_in_account_currency"),
gl_entry.credit_in_account_currency
if not group_by_account
else Sum(gl_entry.credit_in_account_currency).as_("credit_in_account_currency"),
gl_entry.account_currency, gl_entry.account_currency,
) )
.where(gl_entry.company == filters.company) .where(gl_entry.company == filters.company)
) )
if group_by_account:
query = query.groupby(gl_entry.account)
ignore_is_opening = frappe.db.get_single_value( ignore_is_opening = frappe.db.get_single_value(
"Accounts Settings", "ignore_is_opening_check_for_reporting" "Accounts Settings", "ignore_is_opening_check_for_reporting"
) )

View File

@@ -116,6 +116,7 @@ def get_data(filters):
root_rgt=None, root_rgt=None,
ignore_closing_entries=not flt(filters.with_period_closing_entry_for_current_period), ignore_closing_entries=not flt(filters.with_period_closing_entry_for_current_period),
ignore_opening_entries=True, ignore_opening_entries=True,
group_by_account=True,
) )
calculate_values( calculate_values(