From dd94587ef807f6d5e9797a8ddf1db9ff4d9a14ff Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sat, 7 Nov 2020 17:08:30 +0530 Subject: [PATCH] fix: Loan write off precision issue --- .../loan_interest_accrual.json | 3 ++- .../doctype/loan_write_off/loan_write_off.js | 18 ++++++++++++++++++ .../doctype/loan_write_off/loan_write_off.py | 6 ++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.json b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.json index d6bf08ac51..f157f0df8f 100644 --- a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.json +++ b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.json @@ -142,6 +142,7 @@ "read_only": 1 }, { + "depends_on": "eval:doc.is_term_loan", "fieldname": "paid_principal_amount", "fieldtype": "Currency", "label": "Paid Principal Amount", @@ -177,7 +178,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2020-11-06 13:22:40.197916", + "modified": "2020-11-07 05:49:25.448875", "modified_by": "Administrator", "module": "Loan Management", "name": "Loan Interest Accrual", diff --git a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.js b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.js index cc5cd0d3a0..4e3319c208 100644 --- a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.js +++ b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.js @@ -4,6 +4,12 @@ {% include 'erpnext/loan_management/loan_common.js' %}; frappe.ui.form.on('Loan Write Off', { + loan: function(frm) { + frm.trigger('show_pending_principal_amount'); + }, + onload: function(frm) { + frm.trigger('show_pending_principal_amount'); + }, refresh: function(frm) { frm.set_query('write_off_account', function(){ return { @@ -14,5 +20,17 @@ frappe.ui.form.on('Loan Write Off', { } } }); + }, + show_pending_principal_amount: function(frm) { + if (frm.doc.loan && frm.doc.docstatus === 0) { + frappe.db.get_value('Loan', frm.doc.loan, ['total_payment', 'total_interest_payable', + 'total_principal_paid', 'written_off_amount'], function(values) { + frm.set_df_property('write_off_amount', 'description', + "Pending principal amount is " + cstr(flt(values.total_payment - values.total_interest_payable + - values.total_principal_paid - values.written_off_amount, 2))); + frm.refresh_field('write_off_amount'); + }); + + } } }); diff --git a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py index 823e6a904f..6e402edcd2 100644 --- a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py +++ b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe, erpnext from frappe import _ -from frappe.utils import getdate, flt +from frappe.utils import getdate, flt, cint from erpnext.controllers.accounts_controller import AccountsController from erpnext.accounts.general_ledger import make_gl_entries @@ -19,10 +19,12 @@ class LoanWriteOff(AccountsController): self.cost_center = erpnext.get_default_cost_center(self.company) def validate_write_off_amount(self): + precision = cint(frappe.db.get_default("currency_precision")) or 2 total_payment, principal_paid, interest_payable, written_off_amount = frappe.get_value("Loan", self.loan, ['total_payment', 'total_principal_paid','total_interest_payable', 'written_off_amount']) - pending_principal_amount = flt(total_payment) - flt(interest_payable) - flt(principal_paid) - flt(written_off_amount) + pending_principal_amount = flt(flt(total_payment) - flt(interest_payable) - flt(principal_paid) - flt(written_off_amount), + precision) if self.write_off_amount > pending_principal_amount: frappe.throw(_("Write off amount cannot be greater than pending principal amount"))