mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-19 13:09:17 +00:00
refactor: store closing balance as JSON
(cherry picked from commit 1a31825409)
This commit is contained in:
@@ -4,10 +4,14 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
|
from frappe import qb
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import add_days, get_datetime
|
from frappe.utils import add_days, get_datetime
|
||||||
from frappe.utils.scheduler import is_scheduler_inactive
|
from frappe.utils.scheduler import is_scheduler_inactive
|
||||||
|
|
||||||
|
BACKGROUND = True
|
||||||
|
|
||||||
|
|
||||||
class ProcessPeriodClosingVoucher(Document):
|
class ProcessPeriodClosingVoucher(Document):
|
||||||
# begin: auto-generated types
|
# begin: auto-generated types
|
||||||
@@ -55,43 +59,94 @@ def start_pcv_processing(docname: str):
|
|||||||
):
|
):
|
||||||
if not is_scheduler_inactive():
|
if not is_scheduler_inactive():
|
||||||
for x in dates_to_process:
|
for x in dates_to_process:
|
||||||
frappe.enqueue(
|
if BACKGROUND:
|
||||||
method="erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.process_individual_date",
|
frappe.enqueue(
|
||||||
queue="long",
|
method="erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.process_individual_date",
|
||||||
is_async=True,
|
queue="long",
|
||||||
enqueue_after_commit=True,
|
is_async=True,
|
||||||
docname=docname,
|
enqueue_after_commit=True,
|
||||||
date=x.processing_date,
|
docname=docname,
|
||||||
)
|
date=x.processing_date,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
process_individual_date(docname, x.processing_date)
|
||||||
else:
|
else:
|
||||||
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
|
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def pause_pcv_processing(docname: str):
|
def pause_pcv_processing(docname: str):
|
||||||
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Paused")
|
queued_dates = frappe.db.get_all(
|
||||||
|
"Process Period Closing Voucher Detail",
|
||||||
|
filters={"parent": docname, "status": "Queued"},
|
||||||
|
fields=["name"],
|
||||||
|
)
|
||||||
|
ppcv = qb.DocType("Process Period Closing Voucher")
|
||||||
|
qb.update(ppcv).set(ppcv.status, "Paused").where(ppcv.name.isin(queued_dates)).run()
|
||||||
|
|
||||||
|
|
||||||
def process_individual_date(docname: str, date: str):
|
def call_next_date(docname: str):
|
||||||
if frappe.db.get_value("Process Period Closing Voucher", docname, "status") == "Running":
|
if next_date_to_process := frappe.db.get_all(
|
||||||
frappe.db.set_value(
|
"Process Period Closing Voucher Detail",
|
||||||
"Process Period Closing Voucher Detail", {"processing_date": date}, "status", "Completed"
|
filters={"parent": docname, "status": "Queued"},
|
||||||
)
|
fields=["processing_date"],
|
||||||
if next_date_to_process := frappe.db.get_all(
|
order_by="processing_date",
|
||||||
"Process Period Closing Voucher Detail",
|
limit=1,
|
||||||
filters={"parent": docname, "status": "Queued"},
|
):
|
||||||
fields=["processing_date"],
|
next_date_to_process = next_date_to_process[0].processing_date
|
||||||
order_by="processing_date",
|
if not is_scheduler_inactive():
|
||||||
limit=1,
|
frappe.db.set_value(
|
||||||
):
|
"Process Period Closing Voucher Detail",
|
||||||
if not is_scheduler_inactive():
|
{"processing_date": next_date_to_process, "parent": docname},
|
||||||
|
"status",
|
||||||
|
"Running",
|
||||||
|
)
|
||||||
|
if BACKGROUND:
|
||||||
frappe.enqueue(
|
frappe.enqueue(
|
||||||
method="erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.process_individual_date",
|
method="erpnext.accounts.doctype.process_period_closing_voucher.process_period_closing_voucher.process_individual_date",
|
||||||
queue="long",
|
queue="long",
|
||||||
is_async=True,
|
is_async=True,
|
||||||
enqueue_after_commit=True,
|
enqueue_after_commit=True,
|
||||||
docname=docname,
|
docname=docname,
|
||||||
date=next_date_to_process[0].processing_date,
|
date=next_date_to_process,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
process_individual_date(docname, next_date_to_process)
|
||||||
|
else:
|
||||||
|
running = frappe.db.get_all(
|
||||||
|
"Process Period Closing Voucher Detail",
|
||||||
|
filters={"parent": docname, "status": "Running"},
|
||||||
|
fields=["processing_date"],
|
||||||
|
order_by="processing_date",
|
||||||
|
limit=1,
|
||||||
|
)
|
||||||
|
if not running:
|
||||||
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
|
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Completed")
|
||||||
|
|
||||||
|
|
||||||
|
def process_individual_date(docname: str, date: str):
|
||||||
|
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")
|
||||||
|
pcv = frappe.get_doc("Period Closing Voucher", pcv_name)
|
||||||
|
gle = qb.DocType("GL Entry")
|
||||||
|
res = (
|
||||||
|
qb.from_(gle)
|
||||||
|
.select(gle.account, Sum(gle.debit).as_("debit"), Sum(gle.credit).as_("credit"))
|
||||||
|
.where((gle.company.eq(pcv.company)) & (gle.is_cancelled.eq(False)) & (gle.posting_date.eq(date)))
|
||||||
|
.groupby(gle.account)
|
||||||
|
.run(as_dict=True)
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user