Updated forms and search
This commit is contained in:
@@ -11,13 +11,56 @@ from sqlalchemy import or_, and_
|
||||
def search_customers():
|
||||
"""
|
||||
Searches for customers based on a keyword.
|
||||
The new logic searches across multiple fields (name, address, town, phone, etc.)
|
||||
for each word provided in the search query.
|
||||
Supports special prefixes: @ for last name, ! for address, # for phone, $ for account.
|
||||
Otherwise searches across multiple fields for each word provided in the search query.
|
||||
"""
|
||||
keyword = request.args.get('q')
|
||||
if not keyword or len(keyword) < 2:
|
||||
return jsonify([])
|
||||
|
||||
# Check for special search prefixes
|
||||
search_type = None
|
||||
if keyword[0] in '!@#$':
|
||||
search_type = keyword[0]
|
||||
keyword = keyword[1:] # remove the prefix for the search term
|
||||
|
||||
if search_type:
|
||||
search_pattern = f"%{keyword}%"
|
||||
if search_type == '@':
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_last_name.ilike(search_pattern))
|
||||
.order_by(Customer_Customer.customer_last_name.asc())
|
||||
.limit(20)
|
||||
.all())
|
||||
elif search_type == '!':
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_address.ilike(search_pattern))
|
||||
.order_by(Customer_Customer.customer_last_name.asc())
|
||||
.limit(20)
|
||||
.all())
|
||||
elif search_type == '#':
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_phone_number.ilike(search_pattern))
|
||||
.order_by(Customer_Customer.customer_last_name.asc())
|
||||
.limit(20)
|
||||
.all())
|
||||
elif search_type == '$':
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.account_number.ilike(search_pattern))
|
||||
.order_by(Customer_Customer.account_number.asc())
|
||||
.limit(20)
|
||||
.all())
|
||||
else:
|
||||
# fallback, though shouldn't happen
|
||||
return jsonify([])
|
||||
|
||||
customer_schema = Customer_Customer_schema(many=True)
|
||||
return jsonify(customer_schema.dump(customer_list))
|
||||
|
||||
# --- NEW SEARCH LOGIC ---
|
||||
|
||||
# 1. Split the incoming search query into individual words.
|
||||
@@ -65,4 +108,4 @@ def search_delivery():
|
||||
delivery_schema = Delivery_Delivery_schema(many=False)
|
||||
return jsonify(delivery_schema.dump(delivery_ticket))
|
||||
else:
|
||||
return jsonify({})
|
||||
return jsonify({})
|
||||
|
||||
Reference in New Issue
Block a user