brotherton-erpnext/erpnext/patches/v14_0/update_closing_balances.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
1.9 KiB
Python
Raw Normal View History

# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
2023-03-13 15:21:35 +00:00
from erpnext.accounts.doctype.account_closing_balance.account_closing_balance import (
make_closing_entries,
)
from erpnext.accounts.utils import get_fiscal_year
def execute():
2023-05-05 12:24:34 +00:00
frappe.db.truncate("Account Closing Balance")
2023-05-17 16:35:46 +00:00
i = 0
company_wise_order = {}
for pcv in frappe.db.get_all(
"Period Closing Voucher",
fields=["company", "posting_date", "name"],
filters={"docstatus": 1},
order_by="posting_date",
):
company_wise_order.setdefault(pcv.company, [])
if pcv.posting_date not in company_wise_order[pcv.company]:
pcv_doc = frappe.get_doc("Period Closing Voucher", pcv.name)
pcv_doc.year_start_date = get_fiscal_year(
pcv.posting_date, pcv.fiscal_year, company=pcv.company
)[1]
2023-05-17 16:35:46 +00:00
# get gl entries against pcv
gl_entries = frappe.db.get_all(
"GL Entry", filters={"voucher_no": pcv.name, "is_cancelled": 0}, fields=["*"]
)
for entry in gl_entries:
entry["is_period_closing_voucher_entry"] = 1
entry["closing_date"] = pcv_doc.posting_date
entry["period_closing_voucher"] = pcv_doc.name
2023-05-17 16:35:46 +00:00
# get all gl entries for the year
2023-05-05 12:24:34 +00:00
closing_entries = frappe.db.get_all(
"GL Entry",
filters={
"is_cancelled": 0,
2023-05-10 11:11:47 +00:00
"voucher_no": ["!=", pcv.name],
2023-05-17 16:35:46 +00:00
"posting_date": ["between", [pcv_doc.year_start_date, pcv.posting_date]],
"is_opening": "No",
2023-05-05 12:24:34 +00:00
},
fields=["*"],
)
2023-05-17 16:35:46 +00:00
if i == 0:
# add opening entries only for the first pcv
closing_entries += frappe.db.get_all(
"GL Entry",
filters={"is_cancelled": 0, "is_opening": "Yes"},
fields=["*"],
)
2023-05-05 12:24:34 +00:00
for entry in closing_entries:
entry["closing_date"] = pcv_doc.posting_date
entry["period_closing_voucher"] = pcv_doc.name
make_closing_entries(gl_entries + closing_entries, voucher_name=pcv.name)
company_wise_order[pcv.company].append(pcv.posting_date)
2023-05-17 16:35:46 +00:00
i += 1