Skip to content
Open
Changes from all 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 on lines +32 to +37
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The comment for REF_MATCH_WEIGHT says "Reference number match in description" but REF_RANK_WEIGHT also says "Reference number match". These should be more clearly differentiated. REF_RANK_WEIGHT should say "Reference field equality match" and REF_MATCH_WEIGHT should say "Reference number found in transaction description".

Suggested change
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
REF_RANK_WEIGHT = 3 # Reference field equality 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 found in transaction description

Copilot uses AI. Check for mistakes.


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
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The rank increment for reference number matches found in transaction description was removed but should have been updated to use the weight. For voucher types that don't compute name_match in their SQL queries (Bank Transaction, Loan Disbursement, Loan Repayment, Payment Entry, Journal Entry), this post-processing step needs to apply the weighted rank increment. The line should be: voucher["rank"] += REF_MATCH_WEIGHT

Suggested change
voucher["name_in_desc_match"] = 1
voucher["rank"] += REF_MATCH_WEIGHT

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
)

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
)

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