Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency Precision Issue in Journal Entry giving: Total Debit must be equal to Total Credit. The difference is -0.0000001 #44265

Open
dawoodjee opened this issue Nov 21, 2024 · 0 comments
Labels

Comments

@dawoodjee
Copy link
Contributor

Information about bug

This issue has been tracked for 3 years now

Expected behaviour:

Journal Entry should be able to ignore the debit credit difference if its under the standard or set currency precision.

Actual Behaviour

Getting Exception Message: Total Debit must be equal to Total Credit. The difference is -0.000000XXXX

Workaround

The only workaround is reducing Currency Precision to 1 decimal place in System Settings

Suggested Solution

I believe we can resolve the issue by ignoring small discrepancies less than 0.01 by rounding-up one of the amounts so that there's no difference

We can change

	def set_total_debit_credit(self):
		self.total_debit, self.total_credit, self.difference = 0, 0, 0
		for d in self.get("accounts"):
			if d.debit and d.credit:
				frappe.throw(_("You cannot credit and debit same account at the same time"))

			self.total_debit = flt(self.total_debit) + flt(d.debit, d.precision("debit"))
			self.total_credit = flt(self.total_credit) + flt(d.credit, d.precision("credit"))

		self.difference = flt(self.total_debit, self.precision("total_debit")) - flt(
			self.total_credit, self.precision("total_credit")
		)

To

	def set_total_debit_credit(self):
		self.total_debit, self.total_credit, self.difference = 0, 0, 0

		# Calculate total debit and credit
		for d in self.get("accounts"):
			if d.debit and d.credit:
				frappe.throw(_("You cannot credit and debit the same account at the same time"))

			self.total_debit += flt(d.debit, d.precision("debit"))
			self.total_credit += flt(d.credit, d.precision("credit"))

		# Compute the difference with rounding
		self.difference = flt(
			flt(self.total_debit, self.precision("total_debit")) - 
			flt(self.total_credit, self.precision("total_credit")),
			self.precision("difference")
		)

		# Adjust for small discrepancies
		if abs(self.difference) <= 0.01:
			if self.total_debit > self.total_credit:
				self.total_credit = flt(self.total_debit, self.precision("total_credit"))
			else:
				self.total_debit = flt(self.total_credit, self.precision("total_debit"))

			self.difference = 0  # Ensure no discrepancy remains

Module

accounts

Version

ERPNext: v15.39.3 (version-15)
Frappe Framework: v15.45.1 (version-15)

Installation method

None

Relevant log output / Stack trace / Full Error Message.

File "apps/frappe/frappe/model/document.py", line 291, in insert
11:12:05 web.1         |     self.run_before_save_methods()
11:12:05 web.1         |   File "apps/frappe/frappe/model/document.py", line 1091, in run_before_save_methods
11:12:05 web.1         |     self.run_method("validate")
11:12:05 web.1         |   File "apps/frappe/frappe/model/document.py", line 962, in run_method
11:12:05 web.1         |     out = Document.hook(fn)(self, *args, **kwargs)
11:12:05 web.1         |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11:12:05 web.1         |   File "apps/frappe/frappe/model/document.py", line 1322, in composer
11:12:05 web.1         |     return composed(self, method, *args, **kwargs)
11:12:05 web.1         |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11:12:05 web.1         |   File "apps/frappe/frappe/model/document.py", line 1304, in runner
11:12:05 web.1         |     add_to_return_value(self, fn(self, *args, **kwargs))
11:12:05 web.1         |                               ^^^^^^^^^^^^^^^^^^^^^^^^^
11:12:05 web.1         |   File "apps/frappe/frappe/model/document.py", line 959, in fn
11:12:05 web.1         |     return method_object(*args, **kwargs)
11:12:05 web.1         |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11:12:05 web.1         |   File "apps/erpnext/erpnext/accounts/doctype/journal_entry/journal_entry.py", line 132, in validate
11:12:05 web.1         |     self.validate_total_debit_and_credit()
11:12:05 web.1         |   File "apps/erpnext/erpnext/accounts/doctype/journal_entry/journal_entry.py", line 868, in validate_total_debit_and_credit
11:12:05 web.1         |     frappe.throw(
11:12:05 web.1         |   File "apps/frappe/frappe/__init__.py", line 655, in throw
11:12:05 web.1         |     msgprint(
11:12:05 web.1         |   File "apps/frappe/frappe/__init__.py", line 620, in msgprint
11:12:05 web.1         |     _raise_exception()
11:12:05 web.1         |   File "apps/frappe/frappe/__init__.py", line 571, in _raise_exception
11:12:05 web.1         |     raise exc
11:12:05 web.1         | frappe.exceptions.ValidationError: Total Debit must be equal to Total Credit. The difference is -0.0000001
@dawoodjee dawoodjee added the bug label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant