Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
from banking.overrides.bank_transaction import CustomBankTransaction

MAX_QUERY_RESULTS = 150
# Weights for ranking parameters
REF_RANK_WEIGHT = 3 # Reference number match
PARTY_RANK_WEIGHT = 2 # Party match
AMOUNT_RANK_WEIGHT = 2 # Amount match
DATE_RANK_WEIGHT = 1 # Date match
NAME_MATCH_WEIGHT = 3 # Name (Paid From) match
REF_MATCH_WEIGHT = 3 # Reference number match in description
Comment thread
PatrickDEissler marked this conversation as resolved.
Outdated


class BankReconciliationToolBeta(Document):
Expand Down Expand Up @@ -557,7 +564,6 @@ def check_matching(
# higher rank if voucher name is in bank transaction
reference_no = voucher["reference_no"]
if reference_no and (reference_no.strip() in transaction.description):
voucher["rank"] += 1
voucher["name_in_desc_match"] = 1
Comment thread
PatrickDEissler marked this conversation as resolved.

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_matching sets name_in_desc_match when the voucher’s reference_no is found in the bank transaction description. This conflicts with the established meaning of name_in_desc_match (it’s used by the Match tab to bold the voucher name link), and it also means the reference-column highlighting (ref_in_desc_match) won’t be set for non-invoice doctypes. Consider either (a) matching against voucher["name"] when setting name_in_desc_match, or (b) setting a dedicated ref_in_desc_match flag (and keeping name_in_desc_match for actual name matches).

Suggested change
voucher["name_in_desc_match"] = 1
voucher["ref_in_desc_match"] = 1

Copilot uses AI. Check for mistakes.

return sorted(matching_vouchers, key=lambda x: x["rank"], reverse=True)
Expand Down Expand Up @@ -720,7 +726,13 @@ def get_bt_matching_query(exact_match: bool, common_filters: frappe._dict, trans
)
party_rank = frappe.qb.terms.Case().when(party_filter, 1).else_(0)

rank_expression = ref_rank + amount_rank + party_rank + unallocated_rank + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (unallocated_rank * 1)
+ 1
)

query = (
frappe.qb.from_(bt)
Expand Down Expand Up @@ -770,7 +782,12 @@ def get_ld_matching_query(exact_match: bool, common_filters: frappe._dict):
reference_rank = ref_equality_condition(loan_disbursement.reference_number, common_filters.reference_no)
party_rank = frappe.qb.terms.Case().when(matching_party, 1).else_(0)

rank_expression = reference_rank + party_rank + date_rank + 1
rank_expression = (
(reference_rank * REF_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(loan_disbursement)
Expand Down Expand Up @@ -819,7 +836,12 @@ def get_lr_matching_query(exact_match: bool, common_filters: frappe._dict):
reference_rank = ref_equality_condition(loan_repayment.reference_number, common_filters.reference_no)
party_rank = frappe.qb.terms.Case().when(matching_party, 1).else_(0)

rank_expression = reference_rank + party_rank + date_rank + 1
rank_expression = (
(reference_rank * REF_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(loan_repayment)
Expand Down Expand Up @@ -890,7 +912,13 @@ def get_pe_matching_query(
date_condition = Coalesce(pe.reference_date, pe.posting_date) == common_filters.date
date_rank = frappe.qb.terms.Case().when(date_condition, 1).else_(0)

rank_expression = ref_rank + amount_rank + party_rank + date_rank + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(pe)
Expand Down Expand Up @@ -986,7 +1014,9 @@ def get_je_matching_query(
)
date_condition = subquery.reference_date == common_filters.date
date_rank = frappe.qb.terms.Case().when(date_condition, 1).else_(0)
rank_expression = ref_rank + amount_rank + date_rank + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT) + (amount_rank * AMOUNT_RANK_WEIGHT) + (date_rank * DATE_RANK_WEIGHT) + 1
)

query = (
frappe.qb.from_(subquery)
Expand Down Expand Up @@ -1045,7 +1075,15 @@ def get_si_matching_query(
else Cast(0, "int")
)

rank_expression = ref_rank + party_rank + amount_rank + date_rank + name_match + ref_match + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ (name_match * NAME_MATCH_WEIGHT)
+ (ref_match * REF_MATCH_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(sip)
Expand Down Expand Up @@ -1123,7 +1161,15 @@ def get_unpaid_si_matching_query(
)
date_rank = frappe.qb.terms.Case().when(date_condition, 1).else_(0)

rank_expression = ref_rank + party_rank + amount_rank + date_rank + name_match + ref_match + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ (name_match * NAME_MATCH_WEIGHT)
+ (ref_match * REF_MATCH_WEIGHT)
+ 1
Comment on lines 1250 to +1257

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new weighted rank_expression for unpaid Sales Invoices no longer includes currency_match (it’s still selected as currency_match, but it does not influence ordering anymore). This changes behavior vs. the previous ranking and may cause foreign-currency vouchers to rank similarly to same-currency ones. If currency is still intended to affect ranking, add it back with an explicit weight (or remove it from the select if it’s intentionally no longer used).

Copilot uses AI. Check for mistakes.
)

query = (
frappe.qb.from_(sales_invoice)
Expand Down Expand Up @@ -1215,7 +1261,15 @@ def get_pi_matching_query(
else Cast(0, "int")
)

rank_expression = ref_rank + party_rank + amount_rank + date_rank + name_match + ref_match + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (party_rank * PARTY_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ (name_match * NAME_MATCH_WEIGHT)
+ (ref_match * REF_MATCH_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(purchase_invoice)
Expand Down Expand Up @@ -1300,7 +1354,15 @@ def get_unpaid_pi_matching_query(
)
date_rank = frappe.qb.terms.Case().when(date_condition, 1).else_(0)

rank_expression = ref_rank + party_match + amount_rank + date_rank + name_match + ref_match + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (party_match * PARTY_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (date_rank * DATE_RANK_WEIGHT)
+ (name_match * NAME_MATCH_WEIGHT)
+ (ref_match * REF_MATCH_WEIGHT)
+ 1
Comment on lines 1467 to +1474

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as unpaid Sales Invoices: currency_match was previously part of the unpaid Purchase Invoice ranking but is now omitted from the weighted rank_expression while still being selected. If currency should still influence the ordering, include it with a weight; otherwise drop the currency_match column to avoid dead fields/misleading output.

Copilot uses AI. Check for mistakes.
)

query = (
frappe.qb.from_(purchase_invoice)
Expand Down Expand Up @@ -1385,7 +1447,14 @@ def get_unpaid_ec_matching_query(
else Cast(0, "int")
)

rank_expression = ref_rank + party_match + amount_rank + name_match + ref_match + 1
rank_expression = (
(ref_rank * REF_RANK_WEIGHT)
+ (party_match * PARTY_RANK_WEIGHT)
+ (amount_rank * AMOUNT_RANK_WEIGHT)
+ (name_match * NAME_MATCH_WEIGHT)
+ (ref_match * REF_MATCH_WEIGHT)
+ 1
)

query = (
frappe.qb.from_(expense_claim)
Expand Down
Loading