From 4f2a64479dfcaf6a4bc164315abb4865723f75fe Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 9 Nov 2020 17:00:09 +0530 Subject: [PATCH 01/14] fix: Patch for old loans --- erpnext/patches/v13_0/update_old_loans.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 77239429c5..eaeda093f5 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -70,7 +70,7 @@ def execute(): payments = frappe.db.sql(''' SELECT j.name, a.debit, a.debit_in_account_currency, j.posting_date FROM `tabJournal Entry` j, `tabJournal Entry Account` a WHERE a.parent = j.name and a.reference_type='Loan' and a.reference_name = %s - and account = %s + and a.account = %s and j.docstatus = 1 ''', (loan.name, loan.loan_account), as_dict=1) for payment in payments: From 928dc432aba2cde284072b958c2ba3d801e4aeee Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 9 Nov 2020 17:17:12 +0530 Subject: [PATCH 02/14] fix: Reload journal entry account doc --- erpnext/patches/v13_0/update_old_loans.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index eaeda093f5..dd15f10e09 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -18,6 +18,7 @@ def execute(): frappe.reload_doc('loan_management', 'doctype', 'loan_repayment_detail') frappe.reload_doc('loan_management', 'doctype', 'loan_interest_accrual') frappe.reload_doc('accounts', 'doctype', 'gl_entry') + frappe.reload_doc('accounts', 'doctype', 'journal_entry_account') updated_loan_types = [] From 1c969d64a2f928f78470c4dd42899d2393c6291f Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 10 Nov 2020 20:25:35 +0530 Subject: [PATCH 03/14] fix: Handle cases where same loan type is used for multiple companies --- erpnext/patches/v13_0/update_old_loans.py | 35 +++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index dd15f10e09..2925f0a5bc 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -23,7 +23,8 @@ def execute(): updated_loan_types = [] loans = frappe.get_all('Loan', fields=['name', 'loan_type', 'company', 'status', 'mode_of_payment', - 'applicant_type', 'applicant', 'loan_account', 'payment_account', 'interest_income_account']) + 'applicant_type', 'applicant', 'loan_account', 'payment_account', 'interest_income_account'], + filters={'docstatus': 1}) for loan in loans: # Update details in Loan Types and Loan @@ -39,7 +40,26 @@ def execute(): penalty_account = create_account(company=loan.company, account_type='Income Account', account_name='Penalty Account', parent_account=group_income_account) - if not loan_type_company: + # Same loan type used for multiple companies + if loan_type_company and loan_type_company != loan.company: + # get loan type for appropriate company + loan_type_name = frappe.get_value('Loan Type', {'company': loan.company, + 'mode_of_payment': loan.mode_of_payment, 'loan_account': loan.loan_account, + 'payment_account': loan.payment_account, 'interest_income_account': loan.interest_income_account, + 'penalty_income_account': loan.penalty_income_account}, 'name') + + if not loan_type_name: + loan_type_name = loan.loan_type + " - " + ''.join([c[0] for c in loan.company.split()]).upper() + create_loan_type(loan, loan_type_name, penalty_account) + + # update loan type in loan + frappe.db.sql("UPDATE `tabLoan` set loan_type = %s where name = %s", (loan_type_name, + loan.name)) + + if loan_type_name not in updated_loan_types: + updated_loan_types.append(loan_type_name) + + elif not loan_type_company: loan_type_doc = frappe.get_doc('Loan Type', loan.loan_type) loan_type_doc.is_term_loan = 1 loan_type_doc.company = loan.company @@ -87,3 +107,14 @@ def execute(): jv.flags.ignore_links = True jv.cancel() +def create_loan_type(loan, loan_type_name, penalty_account): + loan_type_doc = frappe.new_doc('Loan Type') + loan_type_doc.loan_name = loan_type_name + loan_type_doc.is_term_loan = 1 + loan_type_doc.company = loan.company + loan_type_doc.mode_of_payment = loan.mode_of_payment + loan_type_doc.payment_account = loan.payment_account + loan_type_doc.loan_account = loan.loan_account + loan_type_doc.interest_income_account = loan.interest_income_account + loan_type_doc.penalty_income_account = penalty_account + loan_type_doc.submit() From 73bde45bc5f488906f911cef57e6035712b38cb0 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 10 Nov 2020 22:08:02 +0530 Subject: [PATCH 04/14] fix: Pass updated loan type --- erpnext/patches/v13_0/update_old_loans.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 2925f0a5bc..c16c2c81b8 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -29,6 +29,7 @@ def execute(): for loan in loans: # Update details in Loan Types and Loan loan_type_company = frappe.db.get_value('Loan Type', loan.loan_type, 'company') + loan_type = loan.loan_type group_income_account = frappe.get_value('Account', {'company': loan.company, 'is_group': 1, 'root_type': 'Income', 'account_name': _('Indirect Income')}) @@ -56,6 +57,7 @@ def execute(): frappe.db.sql("UPDATE `tabLoan` set loan_type = %s where name = %s", (loan_type_name, loan.name)) + loan_type = loan_type_name if loan_type_name not in updated_loan_types: updated_loan_types.append(loan_type_name) @@ -70,8 +72,9 @@ def execute(): loan_type_doc.penalty_income_account = penalty_account loan_type_doc.submit() updated_loan_types.append(loan.loan_type) + loan_type = loan.loan_type - if loan.loan_type in updated_loan_types: + if loan_type in updated_loan_types: if loan.status == 'Fully Disbursed': status = 'Disbursed' elif loan.status == 'Repaid/Closed': @@ -85,7 +88,7 @@ def execute(): 'status': status }) - process_loan_interest_accrual_for_term_loans(posting_date=nowdate(), loan_type=loan.loan_type, + process_loan_interest_accrual_for_term_loans(posting_date=nowdate(), loan_type=loan_type, loan=loan.name) payments = frappe.db.sql(''' SELECT j.name, a.debit, a.debit_in_account_currency, j.posting_date @@ -96,7 +99,7 @@ def execute(): for payment in payments: repayment_entry = make_repayment_entry(loan.name, loan.loan_applicant_type, loan.applicant, - loan.loan_type, loan.company) + loan_type, loan.company) repayment_entry.amount_paid = payment.debit_in_account_currency repayment_entry.posting_date = payment.posting_date From 13d1dda74b88c8c46d0e3adf618433e687c6cda2 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 11 Nov 2020 11:07:17 +0530 Subject: [PATCH 05/14] fix: Handle loan type naming collisions --- erpnext/patches/v13_0/update_old_loans.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index c16c2c81b8..70c1b7eb39 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -51,7 +51,7 @@ def execute(): if not loan_type_name: loan_type_name = loan.loan_type + " - " + ''.join([c[0] for c in loan.company.split()]).upper() - create_loan_type(loan, loan_type_name, penalty_account) + loan_type_name = create_loan_type(loan, loan_type_name, penalty_account) # update loan type in loan frappe.db.sql("UPDATE `tabLoan` set loan_type = %s where name = %s", (loan_type_name, @@ -111,6 +111,10 @@ def execute(): jv.cancel() def create_loan_type(loan, loan_type_name, penalty_account): + + if frappe.db.get_value('Loan Type', loan_type_name): + loan_type_name = loan_type_name + '-1' + loan_type_doc = frappe.new_doc('Loan Type') loan_type_doc.loan_name = loan_type_name loan_type_doc.is_term_loan = 1 From 0dc052e635d5b9846265807af29f704105c9afc4 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 11 Nov 2020 12:57:16 +0530 Subject: [PATCH 06/14] fix: Return loan type name --- erpnext/patches/v13_0/update_old_loans.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 70c1b7eb39..fcadc6273e 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -125,3 +125,5 @@ def create_loan_type(loan, loan_type_name, penalty_account): loan_type_doc.interest_income_account = loan.interest_income_account loan_type_doc.penalty_income_account = penalty_account loan_type_doc.submit() + + return loan_type_name From a2dc1740df6d4dea70d76d19fabadbd2dc885c2e Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 11 Nov 2020 13:57:10 +0530 Subject: [PATCH 07/14] fix: Use autoname for loan creation --- erpnext/patches/v13_0/update_old_loans.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index fcadc6273e..23e4803029 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -5,6 +5,7 @@ from frappe.utils import nowdate from erpnext.accounts.doctype.account.test_account import create_account from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import process_loan_interest_accrual_for_term_loans from erpnext.loan_management.doctype.loan.loan import make_repayment_entry +from frappe.model.naming import make_autoname def execute(): @@ -50,7 +51,6 @@ def execute(): 'penalty_income_account': loan.penalty_income_account}, 'name') if not loan_type_name: - loan_type_name = loan.loan_type + " - " + ''.join([c[0] for c in loan.company.split()]).upper() loan_type_name = create_loan_type(loan, loan_type_name, penalty_account) # update loan type in loan @@ -111,12 +111,8 @@ def execute(): jv.cancel() def create_loan_type(loan, loan_type_name, penalty_account): - - if frappe.db.get_value('Loan Type', loan_type_name): - loan_type_name = loan_type_name + '-1' - loan_type_doc = frappe.new_doc('Loan Type') - loan_type_doc.loan_name = loan_type_name + loan_type_doc.loan_name = make_autoname("Loan Type-.####") loan_type_doc.is_term_loan = 1 loan_type_doc.company = loan.company loan_type_doc.mode_of_payment = loan.mode_of_payment @@ -126,4 +122,4 @@ def create_loan_type(loan, loan_type_name, penalty_account): loan_type_doc.penalty_income_account = penalty_account loan_type_doc.submit() - return loan_type_name + return loan_type_doc.name From b58dca8d942c18bc3ab0e1afa7ca7e744967c5c8 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 12 Nov 2020 13:37:11 +0530 Subject: [PATCH 08/14] fix: Only update open loans --- erpnext/patches/v13_0/update_old_loans.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 23e4803029..8ed789cf45 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -25,7 +25,7 @@ def execute(): loans = frappe.get_all('Loan', fields=['name', 'loan_type', 'company', 'status', 'mode_of_payment', 'applicant_type', 'applicant', 'loan_account', 'payment_account', 'interest_income_account'], - filters={'docstatus': 1}) + filters={'docstatus': 1, 'status': ('!=', 'Closed')}) for loan in loans: # Update details in Loan Types and Loan From c51b340ddf46486cc98d92af03a9332c3e899517 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 12 Nov 2020 18:43:43 +0530 Subject: [PATCH 09/14] fix: Update closed loans --- erpnext/patches/v13_0/update_old_loans.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 8ed789cf45..3042db331a 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -23,6 +23,14 @@ def execute(): updated_loan_types = [] + # Update old loan status as closed + loans_list = frappe.db.sql("""SELECT distinct parent from `tabRepayment Schedule` + where paid = 0 and docstatus = 1""", as_dict=1) + + loans_to_close = [d.parent for d in loans_list] + + frappe.db.sql("UPDATE `tabLoan` set status = 'Closed' where name not in (%s)" % (', '.join(['%s'] * len(loans_to_close))), tuple(loans_to_close)) + loans = frappe.get_all('Loan', fields=['name', 'loan_type', 'company', 'status', 'mode_of_payment', 'applicant_type', 'applicant', 'loan_account', 'payment_account', 'interest_income_account'], filters={'docstatus': 1, 'status': ('!=', 'Closed')}) @@ -91,7 +99,7 @@ def execute(): process_loan_interest_accrual_for_term_loans(posting_date=nowdate(), loan_type=loan_type, loan=loan.name) - payments = frappe.db.sql(''' SELECT j.name, a.debit, a.debit_in_account_currency, j.posting_date + payments = frappe.db.sql(''' SELECT j.name, a.credit, a.credit_in_account_currency, j.posting_date FROM `tabJournal Entry` j, `tabJournal Entry Account` a WHERE a.parent = j.name and a.reference_type='Loan' and a.reference_name = %s and a.account = %s and j.docstatus = 1 From 78690af440ca67b3dd9de60b585522f172dfc423 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 12 Nov 2020 18:47:34 +0530 Subject: [PATCH 10/14] fix: Update only if loans to close --- erpnext/patches/v13_0/update_old_loans.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index 3042db331a..c7f372e26f 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -29,7 +29,8 @@ def execute(): loans_to_close = [d.parent for d in loans_list] - frappe.db.sql("UPDATE `tabLoan` set status = 'Closed' where name not in (%s)" % (', '.join(['%s'] * len(loans_to_close))), tuple(loans_to_close)) + if loans_to_close: + frappe.db.sql("UPDATE `tabLoan` set status = 'Closed' where name not in (%s)" % (', '.join(['%s'] * len(loans_to_close))), tuple(loans_to_close)) loans = frappe.get_all('Loan', fields=['name', 'loan_type', 'company', 'status', 'mode_of_payment', 'applicant_type', 'applicant', 'loan_account', 'payment_account', 'interest_income_account'], From a862eb25e6c51b31eafe96f0bbfdb5f20d9d3cf2 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 13 Nov 2020 17:57:57 +0530 Subject: [PATCH 11/14] fix: Make repayment entry only if amount exists --- erpnext/patches/v13_0/update_old_loans.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index c7f372e26f..c4d9bdb7af 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -107,17 +107,18 @@ def execute(): ''', (loan.name, loan.loan_account), as_dict=1) for payment in payments: - repayment_entry = make_repayment_entry(loan.name, loan.loan_applicant_type, loan.applicant, - loan_type, loan.company) + if payment.credit_in_account_currency: + repayment_entry = make_repayment_entry(loan.name, loan.loan_applicant_type, loan.applicant, + loan_type, loan.company) - repayment_entry.amount_paid = payment.debit_in_account_currency - repayment_entry.posting_date = payment.posting_date - repayment_entry.save() - repayment_entry.submit() + repayment_entry.amount_paid = payment.credit_in_account_currency + repayment_entry.posting_date = payment.posting_date + repayment_entry.save() + repayment_entry.submit() - jv = frappe.get_doc('Journal Entry', payment.name) - jv.flags.ignore_links = True - jv.cancel() + jv = frappe.get_doc('Journal Entry', payment.name) + jv.flags.ignore_links = True + jv.cancel() def create_loan_type(loan, loan_type_name, penalty_account): loan_type_doc = frappe.new_doc('Loan Type') From 452cbcd6eaa1d63538261ab91296b3cf2116ff79 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 30 Nov 2020 10:55:12 +0530 Subject: [PATCH 12/14] fix: Update payments directly in Loan Interest Accrual --- erpnext/patches/v13_0/update_old_loans.py | 40 ++++++++++++++--------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index c4d9bdb7af..de29d329d1 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -5,6 +5,7 @@ from frappe.utils import nowdate from erpnext.accounts.doctype.account.test_account import create_account from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import process_loan_interest_accrual_for_term_loans from erpnext.loan_management.doctype.loan.loan import make_repayment_entry +from erpnext.loan_management.doctype.loan_repayment.loan_repayment import get_accrued_interest_entries from frappe.model.naming import make_autoname def execute(): @@ -100,25 +101,32 @@ def execute(): process_loan_interest_accrual_for_term_loans(posting_date=nowdate(), loan_type=loan_type, loan=loan.name) - payments = frappe.db.sql(''' SELECT j.name, a.credit, a.credit_in_account_currency, j.posting_date - FROM `tabJournal Entry` j, `tabJournal Entry Account` a - WHERE a.parent = j.name and a.reference_type='Loan' and a.reference_name = %s - and a.account = %s and j.docstatus = 1 - ''', (loan.name, loan.loan_account), as_dict=1) + accrued_entries = get_accrued_interest_entries(loan.name) + total_principal, total_interest = frappe.db.get_value('Repayment Schedule', fields=['sum(principal_amount) as total_principal', + 'sum(interest_amount) as total_interest'], filters={'is_paid': 1, 'parent': loan.name}) - for payment in payments: - if payment.credit_in_account_currency: - repayment_entry = make_repayment_entry(loan.name, loan.loan_applicant_type, loan.applicant, - loan_type, loan.company) + for entry in accrued_entries: + interest_paid = 0 + principal_paid = 0 - repayment_entry.amount_paid = payment.credit_in_account_currency - repayment_entry.posting_date = payment.posting_date - repayment_entry.save() - repayment_entry.submit() + if total_interest > entry.interest_amount: + interest_paid = entry.interest_amount + else: + interest_paid = total_interest - jv = frappe.get_doc('Journal Entry', payment.name) - jv.flags.ignore_links = True - jv.cancel() + if total_principal > entry.payable_principal_amount: + principal_paid = entry.payable_principal_amount + else: + principal_paid = total_principal + + frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` + SET paid_principal_amount = `paid_principal_amount` + %s, + paid_interest_amount = `paid_interest_amount` + %s + WHERE name = %s""", + (principal_paid, interest_paid, entry.name)) + + total_principal -= principal_paid + total_interest -= interest_paid def create_loan_type(loan, loan_type_name, penalty_account): loan_type_doc = frappe.new_doc('Loan Type') From 2d1cfae5967b1969f6fa7351ca9239dac85cb0ab Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sat, 5 Dec 2020 22:44:45 +0530 Subject: [PATCH 13/14] fix: fieldname --- erpnext/patches/v13_0/update_old_loans.py | 44 ++++++++++++----------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index de29d329d1..caec53b3fd 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -101,32 +101,34 @@ def execute(): process_loan_interest_accrual_for_term_loans(posting_date=nowdate(), loan_type=loan_type, loan=loan.name) - accrued_entries = get_accrued_interest_entries(loan.name) - total_principal, total_interest = frappe.db.get_value('Repayment Schedule', fields=['sum(principal_amount) as total_principal', - 'sum(interest_amount) as total_interest'], filters={'is_paid': 1, 'parent': loan.name}) - for entry in accrued_entries: - interest_paid = 0 - principal_paid = 0 + if frappe.db.has_column('Repayment Schedule', 'paid'): + total_principal, total_interest = frappe.db.get_value('Repayment Schedule', {'paid': 1, 'parent': loan.name}, + ['sum(principal_amount) as total_principal', 'sum(interest_amount) as total_interest']) - if total_interest > entry.interest_amount: - interest_paid = entry.interest_amount - else: - interest_paid = total_interest + accrued_entries = get_accrued_interest_entries(loan.name) + for entry in accrued_entries: + interest_paid = 0 + principal_paid = 0 - if total_principal > entry.payable_principal_amount: - principal_paid = entry.payable_principal_amount - else: - principal_paid = total_principal + if total_interest > entry.interest_amount: + interest_paid = entry.interest_amount + else: + interest_paid = total_interest - frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` - SET paid_principal_amount = `paid_principal_amount` + %s, - paid_interest_amount = `paid_interest_amount` + %s - WHERE name = %s""", - (principal_paid, interest_paid, entry.name)) + if total_principal > entry.payable_principal_amount: + principal_paid = entry.payable_principal_amount + else: + principal_paid = total_principal - total_principal -= principal_paid - total_interest -= interest_paid + frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` + SET paid_principal_amount = `paid_principal_amount` + %s, + paid_interest_amount = `paid_interest_amount` + %s + WHERE name = %s""", + (principal_paid, interest_paid, entry.name)) + + total_principal -= principal_paid + total_interest -= interest_paid def create_loan_type(loan, loan_type_name, penalty_account): loan_type_doc = frappe.new_doc('Loan Type') From ced3b13492b3ce1c95255d4edba54839ab3dbc78 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sat, 12 Dec 2020 19:31:05 +0530 Subject: [PATCH 14/14] fix: Check for paid field --- erpnext/patches/v13_0/update_old_loans.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/erpnext/patches/v13_0/update_old_loans.py b/erpnext/patches/v13_0/update_old_loans.py index caec53b3fd..561e967d6d 100644 --- a/erpnext/patches/v13_0/update_old_loans.py +++ b/erpnext/patches/v13_0/update_old_loans.py @@ -23,12 +23,14 @@ def execute(): frappe.reload_doc('accounts', 'doctype', 'journal_entry_account') updated_loan_types = [] + loans_to_close = [] # Update old loan status as closed - loans_list = frappe.db.sql("""SELECT distinct parent from `tabRepayment Schedule` - where paid = 0 and docstatus = 1""", as_dict=1) + if frappe.db.has_column('Repayment Schedule', 'paid'): + loans_list = frappe.db.sql("""SELECT distinct parent from `tabRepayment Schedule` + where paid = 0 and docstatus = 1""", as_dict=1) - loans_to_close = [d.parent for d in loans_list] + loans_to_close = [d.parent for d in loans_list] if loans_to_close: frappe.db.sql("UPDATE `tabLoan` set status = 'Closed' where name not in (%s)" % (', '.join(['%s'] * len(loans_to_close))), tuple(loans_to_close))