From d2797be1cd8dc4928f844161092e0644968a651d Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 19 Apr 2019 15:33:05 +0530 Subject: [PATCH] fix: Close loan considering latest inv outstanding balance --- .../invoice_discounting.py | 34 +++++++++--------- .../test_invoice_discounting.py | 36 ++++++++++++++++--- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/erpnext/accounts/doctype/invoice_discounting/invoice_discounting.py b/erpnext/accounts/doctype/invoice_discounting/invoice_discounting.py index 64aa4e4af6..ba33c7822e 100644 --- a/erpnext/accounts/doctype/invoice_discounting/invoice_discounting.py +++ b/erpnext/accounts/doctype/invoice_discounting/invoice_discounting.py @@ -145,23 +145,25 @@ class InvoiceDiscounting(AccountsController): if getdate(self.loan_end_date) > getdate(nowdate()): for d in self.invoices: - je.append("accounts", { - "account": self.accounts_receivable_discounted, - "credit_in_account_currency": flt(d.outstanding_amount), - "reference_type": "Invoice Discounting", - "reference_name": self.name, - "party_type": "Customer", - "party": d.customer - }) + outstanding_amount = frappe.db.get_value("Sales Invoice", d.sales_invoice, "outstanding_amount") + if flt(outstanding_amount) > 0: + je.append("accounts", { + "account": self.accounts_receivable_discounted, + "credit_in_account_currency": flt(outstanding_amount), + "reference_type": "Invoice Discounting", + "reference_name": self.name, + "party_type": "Customer", + "party": d.customer + }) - je.append("accounts", { - "account": self.accounts_receivable_unpaid, - "debit_in_account_currency": flt(d.outstanding_amount), - "reference_type": "Invoice Discounting", - "reference_name": self.name, - "party_type": "Customer", - "party": d.customer - }) + je.append("accounts", { + "account": self.accounts_receivable_unpaid, + "debit_in_account_currency": flt(outstanding_amount), + "reference_type": "Invoice Discounting", + "reference_name": self.name, + "party_type": "Customer", + "party": d.customer + }) return je diff --git a/erpnext/accounts/doctype/invoice_discounting/test_invoice_discounting.py b/erpnext/accounts/doctype/invoice_discounting/test_invoice_discounting.py index 4f93e11def..a11412cb83 100644 --- a/erpnext/accounts/doctype/invoice_discounting/test_invoice_discounting.py +++ b/erpnext/accounts/doctype/invoice_discounting/test_invoice_discounting.py @@ -143,6 +143,38 @@ class TestInvoiceDiscounting(unittest.TestCase): self.assertEqual(inv_disc.status, "Settled") + def test_on_close_after_loan_period_after_inv_payment(self): + inv = create_sales_invoice(rate=600) + inv_disc = create_invoice_discounting([inv.name], + accounts_receivable_credit=self.ar_credit, + accounts_receivable_discounted=self.ar_discounted, + accounts_receivable_unpaid=self.ar_unpaid, + short_term_loan=self.short_term_loan, + bank_charges_account=self.bank_charges_account, + bank_account=self.bank_account, + start=nowdate(), + period=60 + ) + + je1 = inv_disc.create_disbursement_entry() + je1.posting_date = nowdate() + je1.submit() + + je_on_payment = frappe.get_doc(get_payment_entry_against_invoice("Sales Invoice", inv.name)) + je_on_payment.posting_date = nowdate() + je_on_payment.cheque_no = "126981" + je_on_payment.cheque_date = nowdate() + je_on_payment.save() + je_on_payment.submit() + + je2 = inv_disc.close_loan() + + self.assertEqual(je2.accounts[0].account, self.short_term_loan) + self.assertEqual(je2.accounts[0].debit_in_account_currency, flt(inv_disc.total_amount)) + + self.assertEqual(je2.accounts[1].account, self.bank_account) + self.assertEqual(je2.accounts[1].credit_in_account_currency, flt(inv_disc.total_amount)) + def test_on_close_before_loan_period(self): inv = create_sales_invoice(rate=700) inv_disc = create_invoice_discounting([inv.name], @@ -168,10 +200,6 @@ class TestInvoiceDiscounting(unittest.TestCase): self.assertEqual(je.accounts[1].account, self.bank_account) self.assertEqual(je.accounts[1].credit_in_account_currency, flt(inv_disc.total_amount)) - inv_disc.reload() - - self.assertEqual(inv_disc.status, "Settled") - def test_make_payment_before_loan_period(self): #it has problem inv = create_sales_invoice(rate=700)