Skip to content

Commit

Permalink
fix: avoid increasing rank for "" in "some_description"
Browse files Browse the repository at this point in the history
(cherry picked from commit 3a0915f)
  • Loading branch information
barredterra authored and mergify[bot] committed Dec 12, 2024
1 parent f928059 commit c158f06
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pypika.queries import Table
from pypika.terms import Case, Field
from frappe.query_builder.functions import CustomFunction
from frappe.query_builder.functions import CustomFunction, Cast

Instr = CustomFunction("INSTR", ["a", "b"])
RegExpReplace = CustomFunction("REGEXP_REPLACE", ["a", "b", "c"])
Expand All @@ -27,6 +27,10 @@ def amount_rank_condition(amount: Field, bank_amount: float) -> Case:

def ref_equality_condition(reference_no: Field, bank_reference_no: str) -> Case:
"""Get the rank query for reference number matching."""
if not bank_reference_no or bank_reference_no == "NOTPROVIDED":
# If bank reference number is not provided, then it is not a match
return Cast(0, "int")

return frappe.qb.terms.Case().when(reference_no == bank_reference_no, 1).else_(0)


Expand All @@ -43,21 +47,31 @@ def get_description_match_condition(
A query condition that will be 1 if the description contains the document number
and 0 otherwise.
"""
if not description:
return Cast(0, "int")

column_name = column_name or "name"
column = table[column_name]
# Perform replace if the column is the name, else the column value is ambiguous
# Eg. column_name = "custom_ref_no" and its value = "tuf5673i" should be untouched
search_term = (
RegExpReplace(column, r"^[^0-9]*", "") if column_name == "name" else column
)
return (
frappe.qb.terms.Case()
.when(
Instr(description or "", search_term) > 0,
1,
if column_name == "name":
return (
frappe.qb.terms.Case()
.when(
Instr(description, RegExpReplace(column, r"^[^0-9]*", "")) > 0,
1,
)
.else_(0)
)
else:
return (
frappe.qb.terms.Case()
.when(
column.notnull() & (column != "") & (Instr(description, column) > 0),
1,
)
.else_(0)
)
.else_(0)
)


def get_reference_field_map() -> dict:
Expand Down

0 comments on commit c158f06

Please sign in to comment.