refactor: for better readability

(cherry picked from commit 8ba199016a)
This commit is contained in:
ruthra kumar
2025-10-16 13:13:33 +05:30
committed by Mergify
parent e8f8abd685
commit 633ccef2ff
2 changed files with 100 additions and 96 deletions

View File

@@ -44,7 +44,7 @@ frappe.ui.form.on("Process Period Closing Voucher", {
frm.add_custom_button(execute_btn, () => { frm.add_custom_button(execute_btn, () => {
frm.call({ frm.call({
method: "erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.call_next_date", method: "erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.schedule_next_date",
args: { args: {
docname: frm.doc.name, docname: frm.doc.name,
}, },

View File

@@ -50,6 +50,7 @@ class ProcessPeriodClosingVoucher(Document):
@frappe.whitelist() @frappe.whitelist()
def start_pcv_processing(docname: str): def start_pcv_processing(docname: str):
if frappe.db.get_value("Process Period Closing Voucher", docname, "status") in ["Queued", "Paused"]: if frappe.db.get_value("Process Period Closing Voucher", docname, "status") in ["Queued", "Paused"]:
# TODO: move this inside if block
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Running") frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Running")
if dates_to_process := frappe.db.get_all( if dates_to_process := frappe.db.get_all(
"Process Period Closing Voucher Detail", "Process Period Closing Voucher Detail",
@@ -152,19 +153,19 @@ def get_gle_for_closing_account(pcv, dimension_balance, dimensions):
@frappe.whitelist() @frappe.whitelist()
def call_next_date(docname: str): def schedule_next_date(docname: str):
if next_date_to_process := frappe.db.get_all( if to_process := frappe.db.get_all(
"Process Period Closing Voucher Detail", "Process Period Closing Voucher Detail",
filters={"parent": docname, "status": "Queued"}, filters={"parent": docname, "status": "Queued"},
fields=["processing_date"], fields=["processing_date"],
order_by="processing_date", order_by="processing_date",
limit=1, limit=1,
): ):
next_date_to_process = next_date_to_process[0].processing_date next_date = to_process[0].processing_date
if not is_scheduler_inactive(): if not is_scheduler_inactive():
frappe.db.set_value( frappe.db.set_value(
"Process Period Closing Voucher Detail", "Process Period Closing Voucher Detail",
{"processing_date": next_date_to_process, "parent": docname}, {"processing_date": next_date, "parent": docname},
"status", "status",
"Running", "Running",
) )
@@ -175,66 +176,68 @@ def call_next_date(docname: str):
is_async=True, is_async=True,
enqueue_after_commit=True, enqueue_after_commit=True,
docname=docname, docname=docname,
date=next_date_to_process, date=next_date,
) )
else: else:
process_individual_date(docname, next_date_to_process) process_individual_date(docname, next_date)
else: else:
running = frappe.db.get_all( # summarize, build and post GL
"Process Period Closing Voucher Detail", summarize_and_post_ledger_entries(docname)
filters={"parent": docname, "status": "Running"},
fields=["processing_date"],
order_by="processing_date",
limit=1,
)
# TODO: ensure all dates are processed
if not running:
# Calculate total balances for PCV period
ppcv = frappe.get_doc("Process Period Closing Voucher", docname)
gl_entries = [] def summarize_and_post_ledger_entries(docname):
for x in ppcv.dates_to_process: # TODO: ensure all dates are processed
closing_balances = [frappe._dict(gle) for gle in frappe.json.loads(x.closing_balance)] running = frappe.db.get_all(
gl_entries.extend(closing_balances) "Process Period Closing Voucher Detail",
filters={"parent": docname, "status": "Running"},
fields=["processing_date"],
order_by="processing_date",
limit=1,
)
if not running:
# calculate balances for whole PCV period
ppcv = frappe.get_doc("Process Period Closing Voucher", docname)
# Build dimension wise dictionary from all GLE's gl_entries = []
dimension_wise_acc_balances = build_dimension_wise_balance_dict(gl_entries) for x in ppcv.dates_to_process:
closing_balances = [frappe._dict(gle) for gle in frappe.json.loads(x.closing_balance)]
gl_entries.extend(closing_balances)
# convert dict keys to json compliant json dictionary keys # build dimension wise dictionary from all GLE's
json_dict = {} dimension_wise_acc_balances = build_dimension_wise_balance_dict(gl_entries)
for k, v in dimension_wise_acc_balances.items():
str_key = [str(x) for x in k]
str_key = ",".join(str_key)
json_dict[str_key] = v
frappe.db.set_value( # convert tuple key to str to make it json compliant
"Process Period Closing Voucher", docname, "total", frappe.json.dumps(json_dict) json_dict = {}
for k, v in dimension_wise_acc_balances.items():
str_key = [str(x) for x in k]
str_key = ",".join(str_key)
json_dict[str_key] = v
# save
frappe.db.set_value("Process Period Closing Voucher", docname, "total", frappe.json.dumps(json_dict))
# build gl map
pcv = frappe.get_doc("Period Closing Voucher", ppcv.parent_pcv)
pl_accounts_reverse_gle = []
closing_account_gle = []
for dimensions, account_balances in dimension_wise_acc_balances.items():
for acc, balances in account_balances.items():
balance_in_company_currency = flt(balances.debit) - flt(balances.credit)
if balance_in_company_currency:
pl_accounts_reverse_gle.append(get_gle_for_pl_account(pcv, acc, balances, dimensions))
closing_account_gle.append(
get_gle_for_closing_account(pcv, account_balances["balances"], dimensions)
) )
# Build GL map gl_entries = pl_accounts_reverse_gle + closing_account_gle
pcv = frappe.get_doc("Period Closing Voucher", ppcv.parent_pcv) from erpnext.accounts.general_ledger import make_gl_entries
pl_accounts_reverse_gle = []
closing_account_gle = []
for dimensions, account_balances in dimension_wise_acc_balances.items(): if gl_entries:
for acc, balances in account_balances.items(): make_gl_entries(gl_entries, merge_entries=False)
balance_in_company_currency = flt(balances.debit) - flt(balances.credit)
if balance_in_company_currency:
pl_accounts_reverse_gle.append(get_gle_for_pl_account(pcv, acc, balances, dimensions))
# closing liability account frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
closing_account_gle.append(
get_gle_for_closing_account(pcv, account_balances["balances"], dimensions)
)
gl_entries = pl_accounts_reverse_gle + closing_account_gle
from erpnext.accounts.general_ledger import make_gl_entries
if gl_entries:
make_gl_entries(gl_entries, merge_entries=False)
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
def get_dimensions(): def get_dimensions():
@@ -299,52 +302,53 @@ def build_dimension_wise_balance_dict(gl_entries):
def process_individual_date(docname: str, date: str): def process_individual_date(docname: str, date: str):
if frappe.db.get_value("Process Period Closing Voucher", docname, "status") == "Running": if frappe.db.get_value("Process Period Closing Voucher", docname, "status") != "Running":
pcv_name = frappe.db.get_value("Process Period Closing Voucher", docname, "parent_pcv") return
pcv = frappe.get_doc("Period Closing Voucher", pcv_name)
dimensions = get_dimensions() pcv_name = frappe.db.get_value("Process Period Closing Voucher", docname, "parent_pcv")
company = frappe.db.get_value("Period Closing Voucher", pcv_name, "company")
p_l_accounts = frappe.db.get_all( dimensions = get_dimensions()
"Account", filters={"company": pcv.company, "report_type": "Profit and Loss"}, pluck="name"
)
gle = qb.DocType("GL Entry") p_l_accounts = frappe.db.get_all(
query = qb.from_(gle).select(gle.account) "Account", filters={"company": company, "report_type": "Profit and Loss"}, pluck="name"
for dim in dimensions: )
query = query.select(gle[dim])
query = query.select( # summarize
Sum(gle.debit).as_("debit"), gle = qb.DocType("GL Entry")
Sum(gle.credit).as_("credit"), query = qb.from_(gle).select(gle.account)
Sum(gle.debit_in_account_currency).as_("debit_in_account_currency"), for dim in dimensions:
Sum(gle.credit_in_account_currency).as_("credit_in_account_currency"), query = query.select(gle[dim])
gle.account_currency, query = query.select(
).where( Sum(gle.debit).as_("debit"),
(gle.company.eq(pcv.company)) Sum(gle.credit).as_("credit"),
& (gle.is_cancelled.eq(0)) Sum(gle.debit_in_account_currency).as_("debit_in_account_currency"),
& (gle.posting_date.eq(date)) Sum(gle.credit_in_account_currency).as_("credit_in_account_currency"),
& (gle.account.isin(p_l_accounts)) gle.account_currency,
) ).where(
(gle.company.eq(company))
& (gle.is_cancelled.eq(0))
& (gle.posting_date.eq(date))
& (gle.account.isin(p_l_accounts))
)
query = query.groupby(gle.account)
for dim in dimensions:
query = query.groupby(gle[dim])
res = query.run(as_dict=True)
query = query.groupby(gle.account) # save results
for dim in dimensions: frappe.db.set_value(
query = query.groupby(gle[dim]) "Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname},
"closing_balance",
frappe.json.dumps(res),
)
frappe.db.set_value(
"Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname},
"status",
"Completed",
)
res = query.run(as_dict=True) # chain call
schedule_next_date(docname)
frappe.db.set_value(
"Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname},
"status",
"Completed",
)
frappe.db.set_value(
"Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname},
"closing_balance",
frappe.json.dumps(res),
)
call_next_date(docname)