Skip to content

Commit

Permalink
feat: unreconcile on cancellation of bank transaction (#27109) (#27137)
Browse files Browse the repository at this point in the history
  • Loading branch information
frappe-pr-bot authored Aug 25, 2021
1 parent 4eb7c2a commit 7ac4916
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
35 changes: 24 additions & 11 deletions erpnext/accounts/doctype/bank_transaction/bank_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def on_update_after_submit(self):
self.update_allocations()
self.clear_linked_payment_entries()
self.set_status(update=True)

def on_cancel(self):
self.clear_linked_payment_entries(for_cancel=True)
self.set_status(update=True)

def update_allocations(self):
if self.payment_entries:
Expand All @@ -41,21 +45,30 @@ def update_allocations(self):
frappe.db.set_value(self.doctype, self.name, "status", "Reconciled")

self.reload()

def clear_linked_payment_entries(self):
def clear_linked_payment_entries(self, for_cancel=False):
for payment_entry in self.payment_entries:
if payment_entry.payment_document in ["Payment Entry", "Journal Entry", "Purchase Invoice", "Expense Claim"]:
self.clear_simple_entry(payment_entry)
self.clear_simple_entry(payment_entry, for_cancel=for_cancel)

elif payment_entry.payment_document == "Sales Invoice":
self.clear_sales_invoice(payment_entry)

def clear_simple_entry(self, payment_entry):
frappe.db.set_value(payment_entry.payment_document, payment_entry.payment_entry, "clearance_date", self.date)

def clear_sales_invoice(self, payment_entry):
frappe.db.set_value("Sales Invoice Payment", dict(parenttype=payment_entry.payment_document,
parent=payment_entry.payment_entry), "clearance_date", self.date)
self.clear_sales_invoice(payment_entry, for_cancel=for_cancel)

def clear_simple_entry(self, payment_entry, for_cancel=False):
clearance_date = self.date if not for_cancel else None
frappe.db.set_value(
payment_entry.payment_document, payment_entry.payment_entry,
"clearance_date", clearance_date)

def clear_sales_invoice(self, payment_entry, for_cancel=False):
clearance_date = self.date if not for_cancel else None
frappe.db.set_value(
"Sales Invoice Payment",
dict(
parenttype=payment_entry.payment_document,
parent=payment_entry.payment_entry
),
"clearance_date", clearance_date)

def get_total_allocated_amount(payment_entry):
return frappe.db.sql("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
frappe.listview_settings['Bank Transaction'] = {
add_fields: ["unallocated_amount"],
get_indicator: function(doc) {
if(flt(doc.unallocated_amount)>0) {
return [__("Unreconciled"), "orange", "unallocated_amount,>,0"];
if(doc.docstatus == 2) {
return [__("Cancelled"), "red", "docstatus,=,2"];
} else if(flt(doc.unallocated_amount)<=0) {
return [__("Reconciled"), "green", "unallocated_amount,=,0"];
} else if(flt(doc.unallocated_amount)>0) {
return [__("Unreconciled"), "orange", "unallocated_amount,>,0"];
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def setUpClass(cls):
def tearDownClass(cls):
for bt in frappe.get_all("Bank Transaction"):
doc = frappe.get_doc("Bank Transaction", bt.name)
doc.cancel()
if doc.docstatus == 1:
doc.cancel()
doc.delete()

# Delete directly in DB to avoid validation errors for countries not allowing deletion
Expand Down Expand Up @@ -57,6 +58,12 @@ def test_reconcile(self):
clearance_date = frappe.db.get_value("Payment Entry", payment.name, "clearance_date")
self.assertTrue(clearance_date is not None)

bank_transaction.reload()
bank_transaction.cancel()

clearance_date = frappe.db.get_value("Payment Entry", payment.name, "clearance_date")
self.assertFalse(clearance_date)

# Check if ERPNext can correctly filter a linked payments based on the debit/credit amount
def test_debit_credit_output(self):
bank_transaction = frappe.get_doc("Bank Transaction", dict(description="Auszahlung Karte MC/000002916 AUTOMAT 698769 K002 27.10. 14:07"))
Expand Down
3 changes: 2 additions & 1 deletion erpnext/controllers/status_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def validate_status(status, options):
],
"Bank Transaction": [
["Unreconciled", "eval:self.docstatus == 1 and self.unallocated_amount>0"],
["Reconciled", "eval:self.docstatus == 1 and self.unallocated_amount<=0"]
["Reconciled", "eval:self.docstatus == 1 and self.unallocated_amount<=0"],
["Cancelled", "eval:self.docstatus == 2"]
],
"POS Opening Entry": [
["Draft", None],
Expand Down

0 comments on commit 7ac4916

Please sign in to comment.