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,11 +176,17 @@ 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:
# summarize, build and post GL
summarize_and_post_ledger_entries(docname)
def summarize_and_post_ledger_entries(docname):
# TODO: ensure all dates are processed
running = frappe.db.get_all( running = frappe.db.get_all(
"Process Period Closing Voucher Detail", "Process Period Closing Voucher Detail",
filters={"parent": docname, "status": "Running"}, filters={"parent": docname, "status": "Running"},
@@ -187,10 +194,8 @@ def call_next_date(docname: str):
order_by="processing_date", order_by="processing_date",
limit=1, limit=1,
) )
# TODO: ensure all dates are processed
if not running: if not running:
# Calculate total balances for PCV period # calculate balances for whole PCV period
ppcv = frappe.get_doc("Process Period Closing Voucher", docname) ppcv = frappe.get_doc("Process Period Closing Voucher", docname)
gl_entries = [] gl_entries = []
@@ -198,21 +203,20 @@ def call_next_date(docname: str):
closing_balances = [frappe._dict(gle) for gle in frappe.json.loads(x.closing_balance)] closing_balances = [frappe._dict(gle) for gle in frappe.json.loads(x.closing_balance)]
gl_entries.extend(closing_balances) gl_entries.extend(closing_balances)
# Build dimension wise dictionary from all GLE's # build dimension wise dictionary from all GLE's
dimension_wise_acc_balances = build_dimension_wise_balance_dict(gl_entries) dimension_wise_acc_balances = build_dimension_wise_balance_dict(gl_entries)
# convert dict keys to json compliant json dictionary keys # convert tuple key to str to make it json compliant
json_dict = {} json_dict = {}
for k, v in dimension_wise_acc_balances.items(): for k, v in dimension_wise_acc_balances.items():
str_key = [str(x) for x in k] str_key = [str(x) for x in k]
str_key = ",".join(str_key) str_key = ",".join(str_key)
json_dict[str_key] = v json_dict[str_key] = v
frappe.db.set_value( # save
"Process Period Closing Voucher", docname, "total", frappe.json.dumps(json_dict) frappe.db.set_value("Process Period Closing Voucher", docname, "total", frappe.json.dumps(json_dict))
)
# Build GL map # build gl map
pcv = frappe.get_doc("Period Closing Voucher", ppcv.parent_pcv) pcv = frappe.get_doc("Period Closing Voucher", ppcv.parent_pcv)
pl_accounts_reverse_gle = [] pl_accounts_reverse_gle = []
closing_account_gle = [] closing_account_gle = []
@@ -223,7 +227,6 @@ def call_next_date(docname: str):
if balance_in_company_currency: if balance_in_company_currency:
pl_accounts_reverse_gle.append(get_gle_for_pl_account(pcv, acc, balances, dimensions)) pl_accounts_reverse_gle.append(get_gle_for_pl_account(pcv, acc, balances, dimensions))
# closing liability account
closing_account_gle.append( closing_account_gle.append(
get_gle_for_closing_account(pcv, account_balances["balances"], dimensions) get_gle_for_closing_account(pcv, account_balances["balances"], dimensions)
) )
@@ -299,21 +302,23 @@ 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":
return
pcv_name = frappe.db.get_value("Process Period Closing Voucher", docname, "parent_pcv") pcv_name = frappe.db.get_value("Process Period Closing Voucher", docname, "parent_pcv")
pcv = frappe.get_doc("Period Closing Voucher", pcv_name) company = frappe.db.get_value("Period Closing Voucher", pcv_name, "company")
dimensions = get_dimensions() dimensions = get_dimensions()
p_l_accounts = frappe.db.get_all( p_l_accounts = frappe.db.get_all(
"Account", filters={"company": pcv.company, "report_type": "Profit and Loss"}, pluck="name" "Account", filters={"company": company, "report_type": "Profit and Loss"}, pluck="name"
) )
# summarize
gle = qb.DocType("GL Entry") gle = qb.DocType("GL Entry")
query = qb.from_(gle).select(gle.account) query = qb.from_(gle).select(gle.account)
for dim in dimensions: for dim in dimensions:
query = query.select(gle[dim]) query = query.select(gle[dim])
query = query.select( query = query.select(
Sum(gle.debit).as_("debit"), Sum(gle.debit).as_("debit"),
Sum(gle.credit).as_("credit"), Sum(gle.credit).as_("credit"),
@@ -321,18 +326,23 @@ def process_individual_date(docname: str, date: str):
Sum(gle.credit_in_account_currency).as_("credit_in_account_currency"), Sum(gle.credit_in_account_currency).as_("credit_in_account_currency"),
gle.account_currency, gle.account_currency,
).where( ).where(
(gle.company.eq(pcv.company)) (gle.company.eq(company))
& (gle.is_cancelled.eq(0)) & (gle.is_cancelled.eq(0))
& (gle.posting_date.eq(date)) & (gle.posting_date.eq(date))
& (gle.account.isin(p_l_accounts)) & (gle.account.isin(p_l_accounts))
) )
query = query.groupby(gle.account) query = query.groupby(gle.account)
for dim in dimensions: for dim in dimensions:
query = query.groupby(gle[dim]) query = query.groupby(gle[dim])
res = query.run(as_dict=True) res = query.run(as_dict=True)
# save results
frappe.db.set_value(
"Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname},
"closing_balance",
frappe.json.dumps(res),
)
frappe.db.set_value( frappe.db.set_value(
"Process Period Closing Voucher Detail", "Process Period Closing Voucher Detail",
{"processing_date": date, "parent": docname}, {"processing_date": date, "parent": docname},
@@ -340,11 +350,5 @@ def process_individual_date(docname: str, date: str):
"Completed", "Completed",
) )
frappe.db.set_value( # chain call
"Process Period Closing Voucher Detail", schedule_next_date(docname)
{"processing_date": date, "parent": docname},
"closing_balance",
frappe.json.dumps(res),
)
call_next_date(docname)