fix: Cancel repayment accrual interest entry on payment cancellation

This commit is contained in:
Deepesh Garg 2020-10-23 19:02:24 +05:30
parent ca6cb6433c
commit 2b3f8e0c3b
2 changed files with 25 additions and 4 deletions

View File

@ -24,8 +24,10 @@ class LoanRepayment(AccountsController):
self.validate_amount() self.validate_amount()
self.allocate_amounts(amounts) self.allocate_amounts(amounts)
def on_submit(self): def before_submit(self):
self.book_unaccrued_interest() self.book_unaccrued_interest()
def on_submit(self):
self.update_paid_amount() self.update_paid_amount()
self.make_gl_entries() self.make_gl_entries()
@ -99,7 +101,8 @@ class LoanRepayment(AccountsController):
self.append('repayment_details', { self.append('repayment_details', {
'loan_interest_accrual': lia.name, 'loan_interest_accrual': lia.name,
'paid_interest_amount': flt(self.total_interest_paid - self.interest_payable, precision), 'paid_interest_amount': flt(self.total_interest_paid - self.interest_payable, precision),
'paid_principal_amount': 0.0 'paid_principal_amount': 0.0,
'accrual_type': 'Repayment'
}) })
def update_paid_amount(self): def update_paid_amount(self):
@ -123,6 +126,8 @@ class LoanRepayment(AccountsController):
def mark_as_unpaid(self): def mark_as_unpaid(self):
loan = frappe.get_doc("Loan", self.against_loan) loan = frappe.get_doc("Loan", self.against_loan)
no_of_repayments = len(self.repayment_details)
for payment in self.repayment_details: for payment in self.repayment_details:
frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` frappe.db.sql(""" UPDATE `tabLoan Interest Accrual`
SET paid_principal_amount = `paid_principal_amount` - %s, SET paid_principal_amount = `paid_principal_amount` - %s,
@ -130,6 +135,12 @@ class LoanRepayment(AccountsController):
WHERE name = %s""", WHERE name = %s""",
(payment.paid_principal_amount, payment.paid_interest_amount, payment.loan_interest_accrual)) (payment.paid_principal_amount, payment.paid_interest_amount, payment.loan_interest_accrual))
# Cancel repayment interest accrual
# checking idx as a preventive measure, repayment accrual will always be the last entry
if payment.accrual_type == 'Repayment' and payment.idx == no_of_repayments:
lia_doc = frappe.get_doc('Loan Interest Accrual', payment.loan_interest_accrual)
lia_doc.cancel()
frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s
WHERE name = %s """, (loan.total_amount_paid - self.amount_paid, WHERE name = %s """, (loan.total_amount_paid - self.amount_paid,
loan.total_principal_paid - self.principal_amount_paid, self.against_loan)) loan.total_principal_paid - self.principal_amount_paid, self.against_loan))

View File

@ -7,7 +7,8 @@
"field_order": [ "field_order": [
"loan_interest_accrual", "loan_interest_accrual",
"paid_principal_amount", "paid_principal_amount",
"paid_interest_amount" "paid_interest_amount",
"accrual_type"
], ],
"fields": [ "fields": [
{ {
@ -27,11 +28,20 @@
"fieldtype": "Currency", "fieldtype": "Currency",
"label": "Paid Interest Amount", "label": "Paid Interest Amount",
"options": "Company:company:default_currency" "options": "Company:company:default_currency"
},
{
"fetch_from": "loan_interest_accrual.accrual_type",
"fetch_if_empty": 1,
"fieldname": "accrual_type",
"fieldtype": "Select",
"label": "Accrual Type",
"options": "Regular\nRepayment\nDisbursement"
} }
], ],
"index_web_pages_for_search": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2020-04-15 21:50:03.837019", "modified": "2020-10-23 08:09:18.267030",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Loan Management", "module": "Loan Management",
"name": "Loan Repayment Detail", "name": "Loan Repayment Detail",