Revert "refactor: bank transaction (#38182)"
This reverts commit a4b5fc8100.
This commit is contained in:
@@ -423,9 +423,7 @@ def reconcile_vouchers(bank_transaction_name, vouchers):
|
|||||||
vouchers = json.loads(vouchers)
|
vouchers = json.loads(vouchers)
|
||||||
transaction = frappe.get_doc("Bank Transaction", bank_transaction_name)
|
transaction = frappe.get_doc("Bank Transaction", bank_transaction_name)
|
||||||
transaction.add_payment_entries(vouchers)
|
transaction.add_payment_entries(vouchers)
|
||||||
transaction.save()
|
return frappe.get_doc("Bank Transaction", bank_transaction_name)
|
||||||
|
|
||||||
return transaction
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
"status",
|
"status",
|
||||||
"bank_account",
|
"bank_account",
|
||||||
"company",
|
"company",
|
||||||
"amended_from",
|
|
||||||
"section_break_4",
|
"section_break_4",
|
||||||
"deposit",
|
"deposit",
|
||||||
"withdrawal",
|
"withdrawal",
|
||||||
@@ -26,10 +25,10 @@
|
|||||||
"transaction_id",
|
"transaction_id",
|
||||||
"transaction_type",
|
"transaction_type",
|
||||||
"section_break_14",
|
"section_break_14",
|
||||||
"column_break_oufv",
|
|
||||||
"payment_entries",
|
"payment_entries",
|
||||||
"section_break_18",
|
"section_break_18",
|
||||||
"allocated_amount",
|
"allocated_amount",
|
||||||
|
"amended_from",
|
||||||
"column_break_17",
|
"column_break_17",
|
||||||
"unallocated_amount",
|
"unallocated_amount",
|
||||||
"party_section",
|
"party_section",
|
||||||
@@ -139,12 +138,10 @@
|
|||||||
"fieldtype": "Section Break"
|
"fieldtype": "Section Break"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"allow_on_submit": 1,
|
|
||||||
"fieldname": "allocated_amount",
|
"fieldname": "allocated_amount",
|
||||||
"fieldtype": "Currency",
|
"fieldtype": "Currency",
|
||||||
"label": "Allocated Amount",
|
"label": "Allocated Amount",
|
||||||
"options": "currency",
|
"options": "currency"
|
||||||
"read_only": 1
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname": "amended_from",
|
"fieldname": "amended_from",
|
||||||
@@ -160,12 +157,10 @@
|
|||||||
"fieldtype": "Column Break"
|
"fieldtype": "Column Break"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"allow_on_submit": 1,
|
|
||||||
"fieldname": "unallocated_amount",
|
"fieldname": "unallocated_amount",
|
||||||
"fieldtype": "Currency",
|
"fieldtype": "Currency",
|
||||||
"label": "Unallocated Amount",
|
"label": "Unallocated Amount",
|
||||||
"options": "currency",
|
"options": "currency"
|
||||||
"read_only": 1
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname": "party_section",
|
"fieldname": "party_section",
|
||||||
@@ -230,15 +225,11 @@
|
|||||||
"fieldname": "bank_party_account_number",
|
"fieldname": "bank_party_account_number",
|
||||||
"fieldtype": "Data",
|
"fieldtype": "Data",
|
||||||
"label": "Party Account No. (Bank Statement)"
|
"label": "Party Account No. (Bank Statement)"
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "column_break_oufv",
|
|
||||||
"fieldtype": "Column Break"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"is_submittable": 1,
|
"is_submittable": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2023-11-18 18:32:47.203694",
|
"modified": "2023-06-06 13:58:12.821411",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Accounts",
|
"module": "Accounts",
|
||||||
"name": "Bank Transaction",
|
"name": "Bank Transaction",
|
||||||
|
|||||||
@@ -2,73 +2,78 @@
|
|||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
|
||||||
from frappe.utils import flt
|
from frappe.utils import flt
|
||||||
|
|
||||||
from erpnext.controllers.status_updater import StatusUpdater
|
from erpnext.controllers.status_updater import StatusUpdater
|
||||||
|
|
||||||
|
|
||||||
class BankTransaction(StatusUpdater):
|
class BankTransaction(StatusUpdater):
|
||||||
def before_validate(self):
|
def after_insert(self):
|
||||||
self.update_allocated_amount()
|
self.unallocated_amount = abs(flt(self.withdrawal) - flt(self.deposit))
|
||||||
|
|
||||||
def validate(self):
|
def on_submit(self):
|
||||||
self.validate_duplicate_references()
|
self.clear_linked_payment_entries()
|
||||||
|
|
||||||
def validate_duplicate_references(self):
|
|
||||||
"""Make sure the same voucher is not allocated twice within the same Bank Transaction"""
|
|
||||||
if not self.payment_entries:
|
|
||||||
return
|
|
||||||
|
|
||||||
pe = []
|
|
||||||
for row in self.payment_entries:
|
|
||||||
reference = (row.payment_document, row.payment_entry)
|
|
||||||
if reference in pe:
|
|
||||||
frappe.throw(
|
|
||||||
_("{0} {1} is allocated twice in this Bank Transaction").format(
|
|
||||||
row.payment_document, row.payment_entry
|
|
||||||
)
|
|
||||||
)
|
|
||||||
pe.append(reference)
|
|
||||||
|
|
||||||
def update_allocated_amount(self):
|
|
||||||
self.allocated_amount = (
|
|
||||||
sum(p.allocated_amount for p in self.payment_entries) if self.payment_entries else 0.0
|
|
||||||
)
|
|
||||||
self.unallocated_amount = abs(flt(self.withdrawal) - flt(self.deposit)) - self.allocated_amount
|
|
||||||
|
|
||||||
def before_submit(self):
|
|
||||||
self.allocate_payment_entries()
|
|
||||||
self.set_status()
|
self.set_status()
|
||||||
|
|
||||||
if frappe.db.get_single_value("Accounts Settings", "enable_party_matching"):
|
if frappe.db.get_single_value("Accounts Settings", "enable_party_matching"):
|
||||||
self.auto_set_party()
|
self.auto_set_party()
|
||||||
|
|
||||||
def before_update_after_submit(self):
|
_saving_flag = False
|
||||||
self.validate_duplicate_references()
|
|
||||||
self.allocate_payment_entries()
|
# nosemgrep: frappe-semgrep-rules.rules.frappe-modifying-but-not-comitting
|
||||||
self.update_allocated_amount()
|
def on_update_after_submit(self):
|
||||||
|
"Run on save(). Avoid recursion caused by multiple saves"
|
||||||
|
if not self._saving_flag:
|
||||||
|
self._saving_flag = True
|
||||||
|
self.clear_linked_payment_entries()
|
||||||
|
self.update_allocations()
|
||||||
|
self._saving_flag = False
|
||||||
|
|
||||||
def on_cancel(self):
|
def on_cancel(self):
|
||||||
for payment_entry in self.payment_entries:
|
self.clear_linked_payment_entries(for_cancel=True)
|
||||||
self.clear_linked_payment_entry(payment_entry, for_cancel=True)
|
self.set_status(update=True)
|
||||||
|
|
||||||
|
def update_allocations(self):
|
||||||
|
"The doctype does not allow modifications after submission, so write to the db direct"
|
||||||
|
if self.payment_entries:
|
||||||
|
allocated_amount = sum(p.allocated_amount for p in self.payment_entries)
|
||||||
|
else:
|
||||||
|
allocated_amount = 0.0
|
||||||
|
|
||||||
|
amount = abs(flt(self.withdrawal) - flt(self.deposit))
|
||||||
|
self.db_set("allocated_amount", flt(allocated_amount))
|
||||||
|
self.db_set("unallocated_amount", amount - flt(allocated_amount))
|
||||||
|
self.reload()
|
||||||
self.set_status(update=True)
|
self.set_status(update=True)
|
||||||
|
|
||||||
def add_payment_entries(self, vouchers):
|
def add_payment_entries(self, vouchers):
|
||||||
"Add the vouchers with zero allocation. Save() will perform the allocations and clearance"
|
"Add the vouchers with zero allocation. Save() will perform the allocations and clearance"
|
||||||
if 0.0 >= self.unallocated_amount:
|
if 0.0 >= self.unallocated_amount:
|
||||||
frappe.throw(_("Bank Transaction {0} is already fully reconciled").format(self.name))
|
frappe.throw(frappe._("Bank Transaction {0} is already fully reconciled").format(self.name))
|
||||||
|
|
||||||
|
added = False
|
||||||
for voucher in vouchers:
|
for voucher in vouchers:
|
||||||
self.append(
|
# Can't add same voucher twice
|
||||||
"payment_entries",
|
found = False
|
||||||
{
|
for pe in self.payment_entries:
|
||||||
|
if (
|
||||||
|
pe.payment_document == voucher["payment_doctype"]
|
||||||
|
and pe.payment_entry == voucher["payment_name"]
|
||||||
|
):
|
||||||
|
found = True
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
pe = {
|
||||||
"payment_document": voucher["payment_doctype"],
|
"payment_document": voucher["payment_doctype"],
|
||||||
"payment_entry": voucher["payment_name"],
|
"payment_entry": voucher["payment_name"],
|
||||||
"allocated_amount": 0.0, # Temporary
|
"allocated_amount": 0.0, # Temporary
|
||||||
},
|
}
|
||||||
)
|
child = self.append("payment_entries", pe)
|
||||||
|
added = True
|
||||||
|
|
||||||
|
# runs on_update_after_submit
|
||||||
|
if added:
|
||||||
|
self.save()
|
||||||
|
|
||||||
def allocate_payment_entries(self):
|
def allocate_payment_entries(self):
|
||||||
"""Refactored from bank reconciliation tool.
|
"""Refactored from bank reconciliation tool.
|
||||||
@@ -85,7 +90,6 @@ class BankTransaction(StatusUpdater):
|
|||||||
- clear means: set the latest transaction date as clearance date
|
- clear means: set the latest transaction date as clearance date
|
||||||
"""
|
"""
|
||||||
remaining_amount = self.unallocated_amount
|
remaining_amount = self.unallocated_amount
|
||||||
to_remove = []
|
|
||||||
for payment_entry in self.payment_entries:
|
for payment_entry in self.payment_entries:
|
||||||
if payment_entry.allocated_amount == 0.0:
|
if payment_entry.allocated_amount == 0.0:
|
||||||
unallocated_amount, should_clear, latest_transaction = get_clearance_details(
|
unallocated_amount, should_clear, latest_transaction = get_clearance_details(
|
||||||
@@ -95,39 +99,49 @@ class BankTransaction(StatusUpdater):
|
|||||||
if 0.0 == unallocated_amount:
|
if 0.0 == unallocated_amount:
|
||||||
if should_clear:
|
if should_clear:
|
||||||
latest_transaction.clear_linked_payment_entry(payment_entry)
|
latest_transaction.clear_linked_payment_entry(payment_entry)
|
||||||
to_remove.append(payment_entry)
|
self.db_delete_payment_entry(payment_entry)
|
||||||
|
|
||||||
elif remaining_amount <= 0.0:
|
elif remaining_amount <= 0.0:
|
||||||
to_remove.append(payment_entry)
|
self.db_delete_payment_entry(payment_entry)
|
||||||
|
|
||||||
elif 0.0 < unallocated_amount <= remaining_amount:
|
elif 0.0 < unallocated_amount and unallocated_amount <= remaining_amount:
|
||||||
payment_entry.allocated_amount = unallocated_amount
|
payment_entry.db_set("allocated_amount", unallocated_amount)
|
||||||
remaining_amount -= unallocated_amount
|
remaining_amount -= unallocated_amount
|
||||||
if should_clear:
|
if should_clear:
|
||||||
latest_transaction.clear_linked_payment_entry(payment_entry)
|
latest_transaction.clear_linked_payment_entry(payment_entry)
|
||||||
|
|
||||||
elif 0.0 < unallocated_amount:
|
elif 0.0 < unallocated_amount and unallocated_amount > remaining_amount:
|
||||||
payment_entry.allocated_amount = remaining_amount
|
payment_entry.db_set("allocated_amount", remaining_amount)
|
||||||
remaining_amount = 0.0
|
remaining_amount = 0.0
|
||||||
|
|
||||||
elif 0.0 > unallocated_amount:
|
elif 0.0 > unallocated_amount:
|
||||||
frappe.throw(_("Voucher {0} is over-allocated by {1}").format(unallocated_amount))
|
self.db_delete_payment_entry(payment_entry)
|
||||||
|
frappe.throw(frappe._("Voucher {0} is over-allocated by {1}").format(unallocated_amount))
|
||||||
|
|
||||||
for payment_entry in to_remove:
|
self.reload()
|
||||||
self.remove(to_remove)
|
|
||||||
|
def db_delete_payment_entry(self, payment_entry):
|
||||||
|
frappe.db.delete("Bank Transaction Payments", {"name": payment_entry.name})
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def remove_payment_entries(self):
|
def remove_payment_entries(self):
|
||||||
for payment_entry in self.payment_entries:
|
for payment_entry in self.payment_entries:
|
||||||
self.remove_payment_entry(payment_entry)
|
self.remove_payment_entry(payment_entry)
|
||||||
|
# runs on_update_after_submit
|
||||||
self.save() # runs before_update_after_submit
|
self.save()
|
||||||
|
|
||||||
def remove_payment_entry(self, payment_entry):
|
def remove_payment_entry(self, payment_entry):
|
||||||
"Clear payment entry and clearance"
|
"Clear payment entry and clearance"
|
||||||
self.clear_linked_payment_entry(payment_entry, for_cancel=True)
|
self.clear_linked_payment_entry(payment_entry, for_cancel=True)
|
||||||
self.remove(payment_entry)
|
self.remove(payment_entry)
|
||||||
|
|
||||||
|
def clear_linked_payment_entries(self, for_cancel=False):
|
||||||
|
if for_cancel:
|
||||||
|
for payment_entry in self.payment_entries:
|
||||||
|
self.clear_linked_payment_entry(payment_entry, for_cancel)
|
||||||
|
else:
|
||||||
|
self.allocate_payment_entries()
|
||||||
|
|
||||||
def clear_linked_payment_entry(self, payment_entry, for_cancel=False):
|
def clear_linked_payment_entry(self, payment_entry, for_cancel=False):
|
||||||
clearance_date = None if for_cancel else self.date
|
clearance_date = None if for_cancel else self.date
|
||||||
set_voucher_clearance(
|
set_voucher_clearance(
|
||||||
@@ -148,10 +162,11 @@ class BankTransaction(StatusUpdater):
|
|||||||
deposit=self.deposit,
|
deposit=self.deposit,
|
||||||
).match()
|
).match()
|
||||||
|
|
||||||
if not result:
|
if result:
|
||||||
return
|
party_type, party = result
|
||||||
|
frappe.db.set_value(
|
||||||
self.party_type, self.party = result
|
"Bank Transaction", self.name, field={"party_type": party_type, "party": party}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@@ -183,7 +198,9 @@ def get_clearance_details(transaction, payment_entry):
|
|||||||
if gle["gl_account"] == gl_bank_account:
|
if gle["gl_account"] == gl_bank_account:
|
||||||
if gle["amount"] <= 0.0:
|
if gle["amount"] <= 0.0:
|
||||||
frappe.throw(
|
frappe.throw(
|
||||||
_("Voucher {0} value is broken: {1}").format(payment_entry.payment_entry, gle["amount"])
|
frappe._("Voucher {0} value is broken: {1}").format(
|
||||||
|
payment_entry.payment_entry, gle["amount"]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
unmatched_gles -= 1
|
unmatched_gles -= 1
|
||||||
@@ -204,7 +221,7 @@ def get_clearance_details(transaction, payment_entry):
|
|||||||
|
|
||||||
def get_related_bank_gl_entries(doctype, docname):
|
def get_related_bank_gl_entries(doctype, docname):
|
||||||
# nosemgrep: frappe-semgrep-rules.rules.frappe-using-db-sql
|
# nosemgrep: frappe-semgrep-rules.rules.frappe-using-db-sql
|
||||||
return frappe.db.sql(
|
result = frappe.db.sql(
|
||||||
"""
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
ABS(gle.credit_in_account_currency - gle.debit_in_account_currency) AS amount,
|
ABS(gle.credit_in_account_currency - gle.debit_in_account_currency) AS amount,
|
||||||
@@ -222,6 +239,7 @@ def get_related_bank_gl_entries(doctype, docname):
|
|||||||
dict(doctype=doctype, docname=docname),
|
dict(doctype=doctype, docname=docname),
|
||||||
as_dict=True,
|
as_dict=True,
|
||||||
)
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_total_allocated_amount(doctype, docname):
|
def get_total_allocated_amount(doctype, docname):
|
||||||
@@ -354,7 +372,6 @@ def set_voucher_clearance(doctype, docname, clearance_date, self):
|
|||||||
if clearance_date:
|
if clearance_date:
|
||||||
vouchers = [{"payment_doctype": "Bank Transaction", "payment_name": self.name}]
|
vouchers = [{"payment_doctype": "Bank Transaction", "payment_name": self.name}]
|
||||||
bt.add_payment_entries(vouchers)
|
bt.add_payment_entries(vouchers)
|
||||||
bt.save()
|
|
||||||
else:
|
else:
|
||||||
for pe in bt.payment_entries:
|
for pe in bt.payment_entries:
|
||||||
if pe.payment_document == self.doctype and pe.payment_entry == self.name:
|
if pe.payment_document == self.doctype and pe.payment_entry == self.name:
|
||||||
|
|||||||
Reference in New Issue
Block a user