mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-03 12:19:12 +00:00
refactor(customer): Replace SQL with query builder in get_customer_name (#55210)
This commit is contained in:
@@ -15,7 +15,8 @@ from frappe.model.document import Document
|
|||||||
from frappe.model.mapper import get_mapped_doc
|
from frappe.model.mapper import get_mapped_doc
|
||||||
from frappe.model.naming import set_name_by_naming_series, set_name_from_naming_options
|
from frappe.model.naming import set_name_by_naming_series, set_name_from_naming_options
|
||||||
from frappe.model.utils.rename_doc import update_linked_doctypes
|
from frappe.model.utils.rename_doc import update_linked_doctypes
|
||||||
from frappe.query_builder import Field, functions
|
from frappe.query_builder import CustomFunction, Field, functions
|
||||||
|
from frappe.query_builder.functions import Cast, Coalesce, Max, Substring
|
||||||
from frappe.utils import cint, cstr, flt, get_formatted_email, today
|
from frappe.utils import cint, cstr, flt, get_formatted_email, today
|
||||||
from frappe.utils.user import get_users_with_role
|
from frappe.utils.user import get_users_with_role
|
||||||
|
|
||||||
@@ -120,36 +121,25 @@ class Customer(TransactionBase):
|
|||||||
self.customer_name = self.customer_name.strip()
|
self.customer_name = self.customer_name.strip()
|
||||||
if frappe.db.get_value("Customer", self.customer_name) and not frappe.flags.in_import:
|
if frappe.db.get_value("Customer", self.customer_name) and not frappe.flags.in_import:
|
||||||
name_prefix = f"{self.customer_name} - %"
|
name_prefix = f"{self.customer_name} - %"
|
||||||
|
Customer = frappe.qb.DocType("Customer")
|
||||||
|
|
||||||
if frappe.db.db_type == "postgres":
|
if frappe.db.db_type == "postgres":
|
||||||
# Postgres: extract trailing digits (e.g. "Customer - 3") and cast to int.
|
# Postgres: extract trailing digits (e.g. "Customer - 3") and cast to int.
|
||||||
# NOTE: PostgreSQL is strict about types; MySQL's UNSIGNED cast does not exist.
|
# NOTE: PostgreSQL is strict about types; MySQL's UNSIGNED cast does not exist.
|
||||||
count = frappe.db.sql(
|
extracted_part = Substring(Customer.name, r"\d+$")
|
||||||
"""
|
casted_part = Cast(extracted_part, "INTEGER")
|
||||||
SELECT COALESCE(
|
|
||||||
MAX(CAST(SUBSTRING(name FROM '\\d+$') AS INTEGER)),
|
|
||||||
0
|
|
||||||
)
|
|
||||||
FROM tabCustomer
|
|
||||||
WHERE name LIKE %(name_prefix)s
|
|
||||||
""",
|
|
||||||
{"name_prefix": name_prefix},
|
|
||||||
as_list=1,
|
|
||||||
)[0][0]
|
|
||||||
else:
|
else:
|
||||||
# MariaDB/MySQL: keep existing behavior.
|
# MariaDB/MySQL: keep existing behavior.
|
||||||
count = frappe.db.sql(
|
SubstringIndex = CustomFunction("SUBSTRING_INDEX", ["str", "delim", "count"])
|
||||||
"""
|
extracted_part = SubstringIndex(Customer.name, " ", -1)
|
||||||
SELECT COALESCE(
|
casted_part = Cast(extracted_part, "UNSIGNED")
|
||||||
MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)),
|
|
||||||
0
|
query = (
|
||||||
)
|
frappe.qb.from_(Customer)
|
||||||
FROM tabCustomer
|
.select(Coalesce(Max(casted_part), 0))
|
||||||
WHERE name LIKE %(name_prefix)s
|
.where(Customer.name.like(name_prefix))
|
||||||
""",
|
)
|
||||||
{"name_prefix": name_prefix},
|
count = query.run()[0][0]
|
||||||
as_list=1,
|
|
||||||
)[0][0]
|
|
||||||
count = cint(count) + 1
|
count = cint(count) + 1
|
||||||
|
|
||||||
new_customer_name = f"{self.customer_name} - {cstr(count)}"
|
new_customer_name = f"{self.customer_name} - {cstr(count)}"
|
||||||
|
|||||||
Reference in New Issue
Block a user