refactor: simplify Journal Entry client script

- Replace the JournalEntry controller class and cur_frm.cscript free
  functions with frappe.ui.form.on event blocks plus a namespaced
  erpnext.journal_entry helper object
- Drop deprecated APIs: cur_frm/script_manager, add_fetch, $.each and var
- Move the bank_account -> account fetch to fetch_from on the
  Journal Entry Account "account" field
- Keep totals/difference and company-currency conversion on the client
  (cheap, race-free); call the server only to fetch exchange rates
- get_balance now computes its own difference instead of trusting the
  client-sent value, with a regression test
This commit is contained in:
Nabin Hait
2026-06-19 15:19:55 +05:30
parent bda7a8ced2
commit f24ea74ef8
4 changed files with 545 additions and 646 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -889,7 +889,7 @@ class JournalEntry(AccountsController):
msgprint(_("'Entries' cannot be empty"), raise_exception=True)
return
self.total_debit, self.total_credit = 0, 0
self.set_total_debit_credit()
diff = flt(self.difference, self.precision("difference"))
if diff:
self._apply_difference_to_blank_row(diff, difference_account)

View File

@@ -764,6 +764,29 @@ class TestJournalEntry(ERPNextTestSuite):
self.assertEqual(blank_row.credit_in_account_currency, 100)
self.assertEqual(jv.total_debit, jv.total_credit)
def test_get_balance_recomputes_difference_ignoring_client_value(self):
"""get_balance computes its own difference instead of trusting a stale client-sent value."""
jv = frappe.new_doc("Journal Entry")
jv.company = "_Test Company"
jv.posting_date = nowdate()
jv.append(
"accounts",
{
"account": "_Test Cash - _TC",
"debit_in_account_currency": 100,
"debit": 100,
"exchange_rate": 1,
},
)
jv.append("accounts", {"account": "_Test Bank - _TC", "exchange_rate": 1})
# a stale/incorrect value as the client might send; get_balance must not rely on it
jv.difference = 0
jv.get_balance()
self.assertEqual(jv.accounts[1].credit_in_account_currency, 100)
self.assertEqual(jv.total_debit, jv.total_credit)
self.assertEqual(jv.difference, 0)
def test_get_outstanding_invoices_builds_write_off_rows(self):
"""Characterize: get_outstanding_invoices adds a party row for each outstanding invoice."""
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice

View File

@@ -44,6 +44,7 @@
{
"bold": 1,
"columns": 4,
"fetch_from": "bank_account.account",
"fieldname": "account",
"fieldtype": "Link",
"in_global_search": 1,