Add generic search methods for products handler

This commit is contained in:
Lance Edgar 2023-09-26 15:03:18 -05:00
parent cf4902b648
commit 41f31d3885
2 changed files with 95 additions and 1 deletions

View file

@ -36,6 +36,12 @@ class POSBatchHandler(BatchHandler):
""" """
batch_model_class = POSBatch batch_model_class = POSBatch
def get_terminal_id(self):
"""
Returns the ID string for current POS terminal.
"""
return self.config.get('rattail', 'pos.terminal_id')
# TODO: should also filter this by terminal # TODO: should also filter this by terminal
def get_current_batch(self, user, create=True, return_created=False, **kwargs): def get_current_batch(self, user, create=True, return_created=False, **kwargs):
""" """
@ -56,6 +62,8 @@ class POSBatchHandler(BatchHandler):
.one() .one()
except orm.exc.NoResultFound: except orm.exc.NoResultFound:
if not create: if not create:
if return_created:
return None, False
return return
batch = self.make_batch(session, created_by=user) batch = self.make_batch(session, created_by=user)
session.add(batch) session.add(batch)
@ -102,8 +110,13 @@ class POSBatchHandler(BatchHandler):
row to the batch and return the row. row to the batch and return the row.
""" """
session = self.app.get_session(batch) session = self.app.get_session(batch)
model = self.model
product = self.app.get_products_handler().locate_product_for_entry(session, entry) if isinstance(entry, model.Product):
product = entry
entry = product.uuid
else:
product = self.app.get_products_handler().locate_product_for_entry(session, entry)
if product: if product:
# product located, so add item row # product located, so add item row

View file

@ -562,6 +562,86 @@ class ProductsHandler(GenericHandler, MergeMixin):
products = products.filter(model.Product.deleted == False) products = products.filter(model.Product.deleted == False)
return products.filter(model.ProductCode.code == entry).first() return products.filter(model.ProductCode.code == entry).first()
def search_products(self, session, entry, **kwargs):
"""
Perform a product search across multiple fields, and return
results as JSON data rows.
"""
model = self.model
final_results = []
# first we'll attempt "lookup" logic..
lookup_fields = kwargs.get('lookup_fields', [
'_product_key_',
])
if lookup_fields:
product = self.locate_product_for_entry(
session, entry, lookup_fields=lookup_fields)
if product:
final_results.append(product)
# then we'll attempt "search" logic..
search_fields = kwargs.get('search_fields', [
'brand',
'description',
'size',
])
searches = {
'brand': self.search_products_for_brand,
'description': self.search_products_for_description,
'size': self.search_products_for_size,
}
for field in search_fields:
if field in searches:
search = searches[field]
if search:
products = search(session, entry, **kwargs)
final_results.extend(products)
else:
log.warning("unknown search field: %s", field)
return [self.normalize_product(c)
for c in final_results]
def search_products_for_brand(self, session, entry, **kwargs):
model = self.model
entry = entry.lower()
products = session.query(model.Product)\
.join(model.Brand)\
.filter(model.Brand.name.ilike(f'%{entry}%'))\
.all()
results = products
return results
def search_products_for_description(self, session, entry, **kwargs):
model = self.model
entry = entry.lower()
products = session.query(model.Product)\
.filter(model.Product.description.ilike(f'%{entry}%'))\
.all()
results = products
return results
def search_products_for_size(self, session, entry, **kwargs):
model = self.model
entry = entry.lower()
products = session.query(model.Product)\
.filter(model.Product.size.ilike(f'%{entry}%'))\
.all()
results = products
return results
def is_active_for_store(self, product, store, **kwargs): def is_active_for_store(self, product, store, **kwargs):
""" """
Return boolean indicating whether the given product is Return boolean indicating whether the given product is
@ -589,6 +669,7 @@ class ProductsHandler(GenericHandler, MergeMixin):
'brand_name', 'brand_name',
'full_description', 'full_description',
'department_name', 'department_name',
'unit_price_display',
] ]
if 'url' in fields: if 'url' in fields: