refactor: auto_reconcile_vouchers
- Use `reconcile_vouchers` instead of re-implementing - More clear result message. Consider partially reconciled transactions
This commit is contained in:
parent
0d26f67be5
commit
a1ae4262c3
@ -283,68 +283,68 @@ def auto_reconcile_vouchers(
|
|||||||
to_reference_date=None,
|
to_reference_date=None,
|
||||||
):
|
):
|
||||||
frappe.flags.auto_reconcile_vouchers = True
|
frappe.flags.auto_reconcile_vouchers = True
|
||||||
document_types = ["payment_entry", "journal_entry"]
|
reconciled, partially_reconciled = set(), set()
|
||||||
|
|
||||||
bank_transactions = get_bank_transactions(bank_account)
|
bank_transactions = get_bank_transactions(bank_account)
|
||||||
matched_transaction = []
|
|
||||||
for transaction in bank_transactions:
|
for transaction in bank_transactions:
|
||||||
linked_payments = get_linked_payments(
|
linked_payments = get_linked_payments(
|
||||||
transaction.name,
|
transaction.name,
|
||||||
document_types,
|
["payment_entry", "journal_entry"],
|
||||||
from_date,
|
from_date,
|
||||||
to_date,
|
to_date,
|
||||||
filter_by_reference_date,
|
filter_by_reference_date,
|
||||||
from_reference_date,
|
from_reference_date,
|
||||||
to_reference_date,
|
to_reference_date,
|
||||||
)
|
)
|
||||||
vouchers = []
|
|
||||||
for r in linked_payments:
|
if not linked_payments:
|
||||||
vouchers.append(
|
continue
|
||||||
{
|
|
||||||
"payment_doctype": r[1],
|
vouchers = list(
|
||||||
"payment_name": r[2],
|
map(
|
||||||
"amount": r[4],
|
lambda entry: {
|
||||||
}
|
"payment_doctype": entry[1],
|
||||||
)
|
"payment_name": entry[2],
|
||||||
transaction = frappe.get_doc("Bank Transaction", transaction.name)
|
"amount": entry[4],
|
||||||
account = frappe.db.get_value("Bank Account", transaction.bank_account, "account")
|
|
||||||
matched_trans = 0
|
|
||||||
for voucher in vouchers:
|
|
||||||
gl_entry = frappe.db.get_value(
|
|
||||||
"GL Entry",
|
|
||||||
dict(
|
|
||||||
account=account, voucher_type=voucher["payment_doctype"], voucher_no=voucher["payment_name"]
|
|
||||||
),
|
|
||||||
["credit", "debit"],
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
gl_amount, transaction_amount = (
|
|
||||||
(gl_entry.credit, transaction.deposit)
|
|
||||||
if gl_entry.credit > 0
|
|
||||||
else (gl_entry.debit, transaction.withdrawal)
|
|
||||||
)
|
|
||||||
allocated_amount = gl_amount if gl_amount >= transaction_amount else transaction_amount
|
|
||||||
transaction.append(
|
|
||||||
"payment_entries",
|
|
||||||
{
|
|
||||||
"payment_document": voucher["payment_doctype"],
|
|
||||||
"payment_entry": voucher["payment_name"],
|
|
||||||
"allocated_amount": allocated_amount,
|
|
||||||
},
|
},
|
||||||
|
linked_payments,
|
||||||
)
|
)
|
||||||
matched_transaction.append(str(transaction.name))
|
)
|
||||||
transaction.save()
|
|
||||||
transaction.update_allocations()
|
updated_transaction = reconcile_vouchers(transaction.name, json.dumps(vouchers))
|
||||||
matched_transaction_len = len(set(matched_transaction))
|
|
||||||
if matched_transaction_len == 0:
|
if updated_transaction.status == "Reconciled":
|
||||||
frappe.msgprint(_("No matching references found for auto reconciliation"))
|
reconciled.add(updated_transaction.name)
|
||||||
elif matched_transaction_len == 1:
|
elif flt(transaction.unallocated_amount) != flt(updated_transaction.unallocated_amount):
|
||||||
frappe.msgprint(_("{0} transaction is reconcilied").format(matched_transaction_len))
|
# Partially reconciled (status = Unreconciled & unallocated amount changed)
|
||||||
else:
|
partially_reconciled.add(updated_transaction.name)
|
||||||
frappe.msgprint(_("{0} transactions are reconcilied").format(matched_transaction_len))
|
|
||||||
|
alert_message, indicator = get_auto_reconcile_message(partially_reconciled, reconciled)
|
||||||
|
frappe.msgprint(title=_("Auto Reconciliation"), msg=alert_message, indicator=indicator)
|
||||||
|
|
||||||
frappe.flags.auto_reconcile_vouchers = False
|
frappe.flags.auto_reconcile_vouchers = False
|
||||||
|
return reconciled, partially_reconciled
|
||||||
|
|
||||||
return frappe.get_doc("Bank Transaction", transaction.name)
|
|
||||||
|
def get_auto_reconcile_message(partially_reconciled, reconciled):
|
||||||
|
"""Returns alert message and indicator for auto reconciliation depending on result state."""
|
||||||
|
alert_message, indicator = "", "blue"
|
||||||
|
if not partially_reconciled and not reconciled:
|
||||||
|
alert_message = _("No matches occurred via auto reconciliation")
|
||||||
|
return alert_message, indicator
|
||||||
|
|
||||||
|
indicator = "green"
|
||||||
|
if reconciled:
|
||||||
|
alert_message += _("{0} Transaction(s) Reconciled").format(len(reconciled))
|
||||||
|
alert_message += "<br>"
|
||||||
|
|
||||||
|
if partially_reconciled:
|
||||||
|
alert_message += _("{0} {1} Partially Reconciled").format(
|
||||||
|
len(partially_reconciled),
|
||||||
|
_("Transactions") if len(partially_reconciled) > 1 else _("Transaction"),
|
||||||
|
)
|
||||||
|
|
||||||
|
return alert_message, indicator
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user