From 8c5fcb825716a63a074b1d83b830a06412b4c035 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 27 Sep 2023 10:22:09 +0530 Subject: [PATCH] fix: PCV posting issues (#37029) * fix: PCV posting issues * fix: process closing entries separately in a background job * test: Update tests * chore: fix broken ci --- .../payment_request/payment_request.json | 7 ++- .../period_closing_voucher.json | 10 +++- .../period_closing_voucher.py | 53 ++++++++++++++----- .../test_period_closing_voucher.py | 2 +- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/erpnext/accounts/doctype/payment_request/payment_request.json b/erpnext/accounts/doctype/payment_request/payment_request.json index 5ffd7180f626..66b5c4b983eb 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.json +++ b/erpnext/accounts/doctype/payment_request/payment_request.json @@ -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\"", @@ -340,8 +339,8 @@ }, { "fieldname": "payment_url", - "hidden": 1, "fieldtype": "Data", + "hidden": 1, "length": 500, "options": "URL", "read_only": 1 @@ -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", diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json index 54a76b341963..624b5f82f64c 100644 --- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json +++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json @@ -8,6 +8,7 @@ "transaction_date", "posting_date", "fiscal_year", + "year_start_date", "amended_from", "company", "column_break1", @@ -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": [ { @@ -144,5 +151,6 @@ "search_fields": "posting_date, fiscal_year", "sort_field": "modified", "sort_order": "DESC", + "states": [], "title_field": "closing_account_head" } \ No newline at end of file diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py index d984d86af254..674db6c2e430 100644 --- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py +++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py @@ -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 ) ) @@ -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 = [] @@ -322,17 +339,12 @@ 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() @@ -340,6 +352,19 @@ def process_gl_entries(gl_entries, closing_entries, voucher_name, company, closi 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 diff --git a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py index 5d08e8d1c295..1bd565e1b361 100644 --- a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py +++ b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py @@ -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):