2019-03-19 06:18:32 +00:00
|
|
|
import frappe
|
2023-03-21 08:45:31 +00:00
|
|
|
from frappe.utils import cint, flt
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2022-01-21 09:03:30 +00:00
|
|
|
from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
|
2022-01-21 09:19:10 +00:00
|
|
|
get_shopping_cart_settings,
|
2022-01-21 09:03:30 +00:00
|
|
|
)
|
|
|
|
from erpnext.e_commerce.shopping_cart.cart import _set_price_list
|
2021-08-16 19:18:36 +00:00
|
|
|
from erpnext.e_commerce.variant_selector.item_variants_cache import ItemVariantsCacheManager
|
2022-01-20 14:04:36 +00:00
|
|
|
from erpnext.utilities.product import get_price
|
2021-02-25 08:26:38 +00:00
|
|
|
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2021-02-25 08:26:38 +00:00
|
|
|
def get_item_codes_by_attributes(attribute_filters, template_item_code=None):
|
|
|
|
items = []
|
|
|
|
|
|
|
|
for attribute, values in attribute_filters.items():
|
|
|
|
attribute_values = values
|
|
|
|
|
|
|
|
if not isinstance(attribute_values, list):
|
|
|
|
attribute_values = [attribute_values]
|
|
|
|
|
2021-04-08 09:55:43 +00:00
|
|
|
if not attribute_values:
|
|
|
|
continue
|
2021-02-25 08:26:38 +00:00
|
|
|
|
|
|
|
wheres = []
|
|
|
|
query_values = []
|
|
|
|
for attribute_value in attribute_values:
|
2022-03-28 13:22:46 +00:00
|
|
|
wheres.append("( attribute = %s and attribute_value = %s )")
|
2021-02-25 08:26:38 +00:00
|
|
|
query_values += [attribute, attribute_value]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
attribute_query = " or ".join(wheres)
|
2021-02-25 08:26:38 +00:00
|
|
|
|
|
|
|
if template_item_code:
|
2022-03-28 13:22:46 +00:00
|
|
|
variant_of_query = "AND t2.variant_of = %s"
|
2021-02-25 08:26:38 +00:00
|
|
|
query_values.append(template_item_code)
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
variant_of_query = ""
|
2021-02-25 08:26:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
query = """
|
2021-02-25 08:26:38 +00:00
|
|
|
SELECT
|
|
|
|
t1.parent
|
|
|
|
FROM
|
|
|
|
`tabItem Variant Attribute` t1
|
|
|
|
WHERE
|
|
|
|
1 = 1
|
|
|
|
AND (
|
|
|
|
{attribute_query}
|
|
|
|
)
|
|
|
|
AND EXISTS (
|
|
|
|
SELECT
|
|
|
|
1
|
|
|
|
FROM
|
|
|
|
`tabItem` t2
|
|
|
|
WHERE
|
|
|
|
t2.name = t1.parent
|
|
|
|
{variant_of_query}
|
|
|
|
)
|
|
|
|
GROUP BY
|
|
|
|
t1.parent
|
|
|
|
ORDER BY
|
|
|
|
NULL
|
2022-03-28 13:22:46 +00:00
|
|
|
""".format(
|
|
|
|
attribute_query=attribute_query, variant_of_query=variant_of_query
|
|
|
|
)
|
2021-02-25 08:26:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
item_codes = set([r[0] for r in frappe.db.sql(query, query_values)]) # nosemgrep
|
2021-02-25 08:26:38 +00:00
|
|
|
items.append(item_codes)
|
|
|
|
|
|
|
|
res = list(set.intersection(*items))
|
|
|
|
|
|
|
|
return res
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_attributes_and_values(item_code):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Build a list of attributes and their possible values.
|
2019-03-19 06:18:32 +00:00
|
|
|
This will ignore the values upon selection of which there cannot exist one item.
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2019-03-19 06:18:32 +00:00
|
|
|
item_cache = ItemVariantsCacheManager(item_code)
|
|
|
|
item_variants_data = item_cache.get_item_variants_data()
|
|
|
|
|
|
|
|
attributes = get_item_attributes(item_code)
|
|
|
|
attribute_list = [a.attribute for a in attributes]
|
|
|
|
|
|
|
|
valid_options = {}
|
|
|
|
for item_code, attribute, attribute_value in item_variants_data:
|
|
|
|
if attribute in attribute_list:
|
|
|
|
valid_options.setdefault(attribute, set()).add(attribute_value)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
item_attribute_values = frappe.db.get_all(
|
|
|
|
"Item Attribute Value", ["parent", "attribute_value", "idx"], order_by="parent asc, idx asc"
|
|
|
|
)
|
2019-04-25 07:33:50 +00:00
|
|
|
ordered_attribute_value_map = frappe._dict()
|
|
|
|
for iv in item_attribute_values:
|
|
|
|
ordered_attribute_value_map.setdefault(iv.parent, []).append(iv.attribute_value)
|
|
|
|
|
2019-04-15 17:49:44 +00:00
|
|
|
# build attribute values in idx order
|
2019-03-19 06:18:32 +00:00
|
|
|
for attr in attributes:
|
2019-04-15 17:49:44 +00:00
|
|
|
valid_attribute_values = valid_options.get(attr.attribute, [])
|
|
|
|
ordered_values = ordered_attribute_value_map.get(attr.attribute, [])
|
2022-03-28 13:22:46 +00:00
|
|
|
attr["values"] = [v for v in ordered_values if v in valid_attribute_values]
|
2019-03-19 06:18:32 +00:00
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_next_attribute_and_values(item_code, selected_attributes):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Find the count of Items that match the selected attributes.
|
2019-03-19 06:18:32 +00:00
|
|
|
Also, find the attribute values that are not applicable for further searching.
|
|
|
|
If less than equal to 10 items are found, return item_codes of those items.
|
|
|
|
If one item is matched exactly, return item_code of that item.
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2019-03-19 06:18:32 +00:00
|
|
|
selected_attributes = frappe.parse_json(selected_attributes)
|
|
|
|
|
|
|
|
item_cache = ItemVariantsCacheManager(item_code)
|
|
|
|
item_variants_data = item_cache.get_item_variants_data()
|
|
|
|
|
|
|
|
attributes = get_item_attributes(item_code)
|
|
|
|
attribute_list = [a.attribute for a in attributes]
|
|
|
|
filtered_items = get_items_with_selected_attributes(item_code, selected_attributes)
|
|
|
|
|
|
|
|
next_attribute = None
|
|
|
|
|
|
|
|
for attribute in attribute_list:
|
|
|
|
if attribute not in selected_attributes:
|
|
|
|
next_attribute = attribute
|
|
|
|
break
|
|
|
|
|
2021-05-24 20:05:22 +00:00
|
|
|
valid_options_for_attributes = frappe._dict()
|
2019-03-19 06:18:32 +00:00
|
|
|
|
|
|
|
for a in attribute_list:
|
|
|
|
valid_options_for_attributes[a] = set()
|
|
|
|
|
|
|
|
selected_attribute = selected_attributes.get(a, None)
|
|
|
|
if selected_attribute:
|
|
|
|
# already selected attribute values are valid options
|
|
|
|
valid_options_for_attributes[a].add(selected_attribute)
|
|
|
|
|
|
|
|
for row in item_variants_data:
|
|
|
|
item_code, attribute, attribute_value = row
|
2022-03-28 13:22:46 +00:00
|
|
|
if (
|
|
|
|
item_code in filtered_items
|
|
|
|
and attribute not in selected_attributes
|
|
|
|
and attribute in attribute_list
|
|
|
|
):
|
2019-03-19 06:18:32 +00:00
|
|
|
valid_options_for_attributes[attribute].add(attribute_value)
|
|
|
|
|
|
|
|
optional_attributes = item_cache.get_optional_attributes()
|
|
|
|
exact_match = []
|
|
|
|
# search for exact match if all selected attributes are required attributes
|
|
|
|
if len(selected_attributes.keys()) >= (len(attribute_list) - len(optional_attributes)):
|
|
|
|
item_attribute_value_map = item_cache.get_item_attribute_value_map()
|
|
|
|
for item_code, attr_dict in item_attribute_value_map.items():
|
|
|
|
if item_code in filtered_items and set(attr_dict.keys()) == set(selected_attributes.keys()):
|
|
|
|
exact_match.append(item_code)
|
|
|
|
|
|
|
|
filtered_items_count = len(filtered_items)
|
|
|
|
|
|
|
|
# get product info if exact match
|
2022-01-20 14:04:36 +00:00
|
|
|
# from erpnext.e_commerce.shopping_cart.product_info import get_product_info_for_website
|
2019-03-19 06:18:32 +00:00
|
|
|
if exact_match:
|
2022-01-20 14:04:36 +00:00
|
|
|
cart_settings = get_shopping_cart_settings()
|
|
|
|
product_info = get_item_variant_price_dict(exact_match[0], cart_settings)
|
|
|
|
|
2020-08-06 11:17:43 +00:00
|
|
|
if product_info:
|
2023-06-20 10:57:23 +00:00
|
|
|
product_info["is_stock_item"] = frappe.get_cached_value("Item", exact_match[0], "is_stock_item")
|
2022-01-20 14:04:36 +00:00
|
|
|
product_info["allow_items_not_in_stock"] = cint(cart_settings.allow_items_not_in_stock)
|
2019-03-19 06:18:32 +00:00
|
|
|
else:
|
|
|
|
product_info = None
|
|
|
|
|
2023-03-21 08:45:31 +00:00
|
|
|
product_id = ""
|
|
|
|
website_warehouse = ""
|
|
|
|
if exact_match or filtered_items:
|
|
|
|
if exact_match and len(exact_match) == 1:
|
|
|
|
product_id = exact_match[0]
|
|
|
|
elif filtered_items_count == 1:
|
|
|
|
product_id = list(filtered_items)[0]
|
|
|
|
|
|
|
|
if product_id:
|
|
|
|
website_warehouse = frappe.get_cached_value(
|
|
|
|
"Website Item", {"item_code": product_id}, "website_warehouse"
|
|
|
|
)
|
|
|
|
|
|
|
|
available_qty = 0.0
|
|
|
|
if website_warehouse:
|
|
|
|
available_qty = flt(
|
|
|
|
frappe.db.get_value(
|
|
|
|
"Bin", {"item_code": product_id, "warehouse": website_warehouse}, "actual_qty"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
return {
|
2022-03-28 13:22:46 +00:00
|
|
|
"next_attribute": next_attribute,
|
|
|
|
"valid_options_for_attributes": valid_options_for_attributes,
|
|
|
|
"filtered_items_count": filtered_items_count,
|
|
|
|
"filtered_items": filtered_items if filtered_items_count < 10 else [],
|
|
|
|
"exact_match": exact_match,
|
|
|
|
"product_info": product_info,
|
2023-03-21 08:45:31 +00:00
|
|
|
"available_qty": available_qty,
|
2019-03-19 06:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_items_with_selected_attributes(item_code, selected_attributes):
|
|
|
|
item_cache = ItemVariantsCacheManager(item_code)
|
|
|
|
attribute_value_item_map = item_cache.get_attribute_value_item_map()
|
|
|
|
|
|
|
|
items = []
|
|
|
|
for attribute, value in selected_attributes.items():
|
2019-05-06 10:29:51 +00:00
|
|
|
filtered_items = attribute_value_item_map.get((attribute, value), [])
|
|
|
|
items.append(set(filtered_items))
|
2019-03-19 06:18:32 +00:00
|
|
|
|
|
|
|
return set.intersection(*items)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
# utilities
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
def get_item_attributes(item_code):
|
2022-03-28 13:22:46 +00:00
|
|
|
attributes = frappe.db.get_all(
|
|
|
|
"Item Variant Attribute",
|
|
|
|
fields=["attribute"],
|
|
|
|
filters={"parenttype": "Item", "parent": item_code},
|
|
|
|
order_by="idx asc",
|
2019-03-19 06:18:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
optional_attributes = ItemVariantsCacheManager(item_code).get_optional_attributes()
|
|
|
|
|
|
|
|
for a in attributes:
|
|
|
|
if a.attribute in optional_attributes:
|
|
|
|
a.optional = True
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-01-20 14:04:36 +00:00
|
|
|
def get_item_variant_price_dict(item_code, cart_settings):
|
|
|
|
if cart_settings.enabled and cart_settings.show_price:
|
|
|
|
is_guest = frappe.session.user == "Guest"
|
|
|
|
# Show Price if logged in.
|
|
|
|
# If not logged in, check if price is hidden for guest.
|
|
|
|
if not is_guest or not cart_settings.hide_price_for_guest:
|
2022-01-21 09:03:30 +00:00
|
|
|
price_list = _set_price_list(cart_settings, None)
|
2022-01-20 14:04:36 +00:00
|
|
|
price = get_price(
|
2022-03-28 13:22:46 +00:00
|
|
|
item_code, price_list, cart_settings.default_customer_group, cart_settings.company
|
2022-01-20 14:04:36 +00:00
|
|
|
)
|
|
|
|
return {"price": price}
|
|
|
|
|
|
|
|
return None
|