Skip to content

Commit

Permalink
fix: PCV posting issues (#37029)
Browse files Browse the repository at this point in the history
* fix: PCV posting issues

* fix: process closing entries separately in a background job

* test: Update tests

* chore: fix broken ci
  • Loading branch information
deepeshgarg007 authored Sep 27, 2023
1 parent a223935 commit 8c5fcb8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
7 changes: 3 additions & 4 deletions erpnext/accounts/doctype/payment_request/payment_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@
"fieldname": "email_to",
"fieldtype": "Data",
"in_global_search": 1,
"label": "To",
"options": "Email"
"label": "To"
},
{
"depends_on": "eval: doc.payment_channel != \"Phone\"",
Expand Down Expand Up @@ -340,8 +339,8 @@
},
{
"fieldname": "payment_url",
"hidden": 1,
"fieldtype": "Data",
"hidden": 1,
"length": 500,
"options": "URL",
"read_only": 1
Expand Down Expand Up @@ -396,7 +395,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2023-09-16 14:15:02.510890",
"modified": "2023-09-27 09:51:42.277638",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Payment Request",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"transaction_date",
"posting_date",
"fiscal_year",
"year_start_date",
"amended_from",
"company",
"column_break1",
Expand Down Expand Up @@ -100,16 +101,22 @@
"fieldtype": "Text",
"label": "Error Message",
"read_only": 1
},
{
"fieldname": "year_start_date",
"fieldtype": "Date",
"label": "Year Start Date"
}
],
"icon": "fa fa-file-text",
"idx": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-07-20 14:51:04.714154",
"modified": "2023-09-11 20:19:11.810533",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Period Closing Voucher",
"naming_rule": "Expression (old style)",
"owner": "Administrator",
"permissions": [
{
Expand Down Expand Up @@ -144,5 +151,6 @@
"search_fields": "posting_date, fiscal_year",
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "closing_account_head"
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,23 @@ def validate_posting_date(self):

self.check_if_previous_year_closed()

pce = frappe.db.sql(
"""select name from `tabPeriod Closing Voucher`
where posting_date > %s and fiscal_year = %s and docstatus = 1 and company = %s""",
(self.posting_date, self.fiscal_year, self.company),
pcv = frappe.qb.DocType("Period Closing Voucher")
existing_entry = (
frappe.qb.from_(pcv)
.select(pcv.name)
.where(
(pcv.posting_date >= self.posting_date)
& (pcv.fiscal_year == self.fiscal_year)
& (pcv.docstatus == 1)
& (pcv.company == self.company)
)
.run()
)
if pce and pce[0][0]:

if existing_entry and existing_entry[0][0]:
frappe.throw(
_("Another Period Closing Entry {0} has been made after {1}").format(
pce[0][0], self.posting_date
existing_entry[0][0], self.posting_date
)
)

Expand All @@ -130,18 +138,27 @@ def make_gl_entries(self, get_opening_entries=False):
frappe.enqueue(
process_gl_entries,
gl_entries=gl_entries,
voucher_name=self.name,
timeout=3000,
)

frappe.enqueue(
process_closing_entries,
gl_entries=gl_entries,
closing_entries=closing_entries,
voucher_name=self.name,
company=self.company,
closing_date=self.posting_date,
queue="long",
timeout=3000,
)

frappe.msgprint(
_("The GL Entries will be processed in the background, it can take a few minutes."),
alert=True,
)
else:
process_gl_entries(gl_entries, closing_entries, self.name, self.company, self.posting_date)
process_gl_entries(gl_entries, self.name)
process_closing_entries(gl_entries, closing_entries, self.name, self.company, self.posting_date)

def get_grouped_gl_entries(self, get_opening_entries=False):
closing_entries = []
Expand Down Expand Up @@ -322,24 +339,32 @@ def get_balances_based_on_dimensions(
return query.run(as_dict=1)


def process_gl_entries(gl_entries, closing_entries, voucher_name, company, closing_date):
from erpnext.accounts.doctype.account_closing_balance.account_closing_balance import (
make_closing_entries,
)
def process_gl_entries(gl_entries, voucher_name):
from erpnext.accounts.general_ledger import make_gl_entries

try:
if gl_entries:
make_gl_entries(gl_entries, merge_entries=False)

make_closing_entries(gl_entries + closing_entries, voucher_name, company, closing_date)
frappe.db.set_value("Period Closing Voucher", voucher_name, "gle_processing_status", "Completed")
except Exception as e:
frappe.db.rollback()
frappe.log_error(e)
frappe.db.set_value("Period Closing Voucher", voucher_name, "gle_processing_status", "Failed")


def process_closing_entries(gl_entries, closing_entries, voucher_name, company, closing_date):
from erpnext.accounts.doctype.account_closing_balance.account_closing_balance import (
make_closing_entries,
)

try:
if gl_entries + closing_entries:
make_closing_entries(gl_entries + closing_entries, voucher_name, company, closing_date)
except Exception as e:
frappe.db.rollback()
frappe.log_error(e)


def make_reverse_gl_entries(voucher_type, voucher_no):
from erpnext.accounts.general_ledger import make_reverse_gl_entries

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from erpnext.accounts.doctype.finance_book.test_finance_book import create_finance_book
from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
from erpnext.accounts.utils import get_fiscal_year, now
from erpnext.accounts.utils import get_fiscal_year


class TestPeriodClosingVoucher(unittest.TestCase):
Expand Down

0 comments on commit 8c5fcb8

Please sign in to comment.