brotherton-erpnext/erpnext/shopping_cart/product_query.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
4.8 KiB
Python
Raw Normal View History

2020-12-24 12:28:18 +00:00
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
2020-12-24 12:24:07 +00:00
# License: GNU General Public License v3. See license.txt
import frappe
2021-01-20 12:14:08 +00:00
from erpnext.shopping_cart.product_info import get_product_info_for_website
2020-12-24 12:24:07 +00:00
class ProductQuery:
"""Query engine for product listing
2020-12-29 11:47:03 +00:00
2020-12-24 12:24:07 +00:00
Attributes:
cart_settings (Document): Settings for Cart
fields (list): Fields to fetch in query
2020-12-24 12:28:18 +00:00
filters (TYPE): Description
or_filters (list): Description
2020-12-24 12:24:07 +00:00
page_length (Int): Length of page for the query
settings (Document): Products Settings DocType
2020-12-24 12:28:18 +00:00
filters (list)
or_filters (list)
2020-12-24 12:24:07 +00:00
"""
def __init__(self):
self.settings = frappe.get_doc("Products Settings")
self.cart_settings = frappe.get_doc("Shopping Cart Settings")
self.page_length = self.settings.products_per_page or 20
self.fields = ['name', 'item_name', 'item_code', 'website_image', 'variant_of', 'has_variants',
'item_group', 'image', 'web_long_description', 'description', 'route', 'weightage']
self.filters = []
self.or_filters = [['show_in_website', '=', 1]]
if not self.settings.get('hide_variants'):
self.or_filters.append(['show_variant_in_website', '=', 1])
2020-12-24 12:24:07 +00:00
def query(self, attributes=None, fields=None, search_term=None, start=0, item_group=None):
2020-12-24 12:28:18 +00:00
"""Summary
2020-12-29 11:47:03 +00:00
2020-12-24 12:28:18 +00:00
Args:
attributes (dict, optional): Item Attribute filters
fields (dict, optional): Field level filters
search_term (str, optional): Search term to lookup
start (int, optional): Page start
2020-12-29 11:47:03 +00:00
2020-12-24 12:28:18 +00:00
Returns:
list: List of results with set fields
"""
2020-12-24 12:24:07 +00:00
if fields: self.build_fields_filters(fields)
if search_term: self.build_search_filters(search_term)
2020-12-29 11:47:03 +00:00
2020-12-24 12:24:07 +00:00
result = []
website_item_groups = []
# if from item group page consider website item group table
if item_group:
website_item_groups = frappe.db.get_all(
"Item",
fields=self.fields + ["`tabWebsite Item Group`.parent as wig_parent"],
filters=[["Website Item Group", "item_group", "=", item_group]]
)
2020-12-24 12:24:07 +00:00
if attributes:
all_items = []
for attribute, values in attributes.items():
if not isinstance(values, list):
values = [values]
items = frappe.get_all(
"Item",
fields=self.fields,
filters=[
*self.filters,
2020-12-24 12:32:07 +00:00
["Item Variant Attribute", "attribute", "=", attribute],
2020-12-24 12:24:07 +00:00
["Item Variant Attribute", "attribute_value", "in", values],
],
or_filters=self.or_filters,
2021-01-20 12:14:08 +00:00
start=start,
limit=self.page_length
2020-12-24 12:24:07 +00:00
)
items_dict = {item.name: item for item in items}
2020-12-29 11:47:18 +00:00
all_items.append(set(items_dict.keys()))
2020-12-24 12:24:07 +00:00
result = [items_dict.get(item) for item in list(set.intersection(*all_items))]
else:
result = frappe.get_all(
"Item",
fields=self.fields,
filters=self.filters,
or_filters=self.or_filters,
start=start,
limit=self.page_length
)
2021-01-20 12:14:08 +00:00
# Combine results having context of website item groups into item results
if item_group and website_item_groups:
items_list = {row.name for row in result}
for row in website_item_groups:
if row.wig_parent not in items_list:
result.append(row)
result = sorted(result, key=lambda x: x.get("weightage"), reverse=True)
2021-01-20 12:14:08 +00:00
for item in result:
product_info = get_product_info_for_website(item.item_code, skip_quotation_creation=True).get('product_info')
2021-02-05 09:22:31 +00:00
if product_info:
item.formatted_price = (product_info.get('price') or {}).get('formatted_price')
2020-12-24 12:24:07 +00:00
return result
def build_fields_filters(self, filters):
2020-12-24 12:28:18 +00:00
"""Build filters for field values
2020-12-29 11:47:03 +00:00
2020-12-24 12:28:18 +00:00
Args:
filters (dict): Filters
"""
2020-12-24 12:24:07 +00:00
for field, values in filters.items():
if not values:
continue
2020-12-29 11:47:03 +00:00
# handle multiselect fields in filter addition
meta = frappe.get_meta('Item', cached=True)
df = meta.get_field(field)
if df.fieldtype == 'Table MultiSelect':
child_doctype = df.options
child_meta = frappe.get_meta(child_doctype, cached=True)
fields = child_meta.get("fields")
if fields:
self.filters.append([child_doctype, fields[0].fieldname, 'IN', values])
elif isinstance(values, list):
2020-12-24 12:28:18 +00:00
# If value is a list use `IN` query
2020-12-24 12:24:07 +00:00
self.filters.append([field, 'IN', values])
else:
2020-12-24 12:28:18 +00:00
# `=` will be faster than `IN` for most cases
2020-12-24 12:24:07 +00:00
self.filters.append([field, '=', values])
def build_search_filters(self, search_term):
2020-12-24 12:28:18 +00:00
"""Query search term in specified fields
2020-12-29 11:47:03 +00:00
2020-12-24 12:28:18 +00:00
Args:
search_term (str): Search candidate
"""
2020-12-24 12:24:07 +00:00
# Default fields to search from
default_fields = {'name', 'item_name', 'description', 'item_group'}
# Get meta search fields
meta = frappe.get_meta("Item")
meta_fields = set(meta.get_search_fields())
# Join the meta fields and default fields set
search_fields = default_fields.union(meta_fields)
try:
if frappe.db.count('Item', cache=True) > 50000:
search_fields.remove('description')
except KeyError:
pass
# Build or filters for query
search = '%{}%'.format(search_term)
self.or_filters += [[field, 'like', search] for field in search_fields]