예제 #1
0
파일: utils.py 프로젝트: mshehata-adv/erpv
def get_attributes_and_values(item_code):
    '''Build a list of attributes and their possible values.
	This will ignore the values upon selection of which there cannot exist one item.
	'''
    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)

    item_attribute_values = frappe.db.get_all(
        'Item Attribute Value', ['parent', 'attribute_value', 'idx'],
        order_by='parent asc, idx asc')
    item_attribute_values += get_numeric_values()
    ordered_attribute_value_map = frappe._dict()
    for iv in item_attribute_values:
        ordered_attribute_value_map.setdefault(iv.parent,
                                               []).append(iv.attribute_value)

    # build attribute values in idx order
    for attr in attributes:
        valid_attribute_values = valid_options.get(attr.attribute, [])
        ordered_values = ordered_attribute_value_map.get(attr.attribute, [])
        attr['values'] = [
            v for v in ordered_values if v in valid_attribute_values
        ]

    return attributes
예제 #2
0
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():
        items.append(set(attribute_value_item_map[(attribute, value)]))

    return set.intersection(*items)
예제 #3
0
def invalidate_item_variants_cache_for_website(doc):
	from erpnext.portal.product_configurator.item_variants_cache import ItemVariantsCacheManager

	item_code = None
	if doc.has_variants and doc.show_in_website:
		item_code = doc.name
	elif doc.variant_of and frappe.db.get_value('Item', doc.variant_of, 'show_in_website'):
		item_code = doc.variant_of

	if item_code:
		item_cache = ItemVariantsCacheManager(item_code)
		item_cache.clear_cache()
예제 #4
0
def get_item_attributes(item_code):
    attributes = frappe.db.get_all(
        "Item Variant Attribute",
        fields=["attribute"],
        filters={
            "parenttype": "Item",
            "parent": item_code
        },
        order_by="idx asc",
    )

    optional_attributes = ItemVariantsCacheManager(
        item_code).get_optional_attributes()

    for a in attributes:
        values = frappe.db.get_all(
            "Item Attribute Value",
            fields=["attribute_value", "abbr"],
            filters={
                "parenttype": "Item Attribute",
                "parent": a.attribute
            },
            order_by="idx asc",
        )
        a.values = values
        if a.attribute in optional_attributes:
            a.optional = True

    return attributes
예제 #5
0
def get_attributes_and_values(item_code):
    '''Build a list of attributes and their possible values.
	This will ignore the values upon selection of which there cannot exist one item.
	'''
    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)

    for attr in attributes:
        attr['values'] = valid_options.get(attr.attribute, [])

    return attributes
예제 #6
0
파일: utils.py 프로젝트: mshehata-adv/erpv
def get_item_attributes(item_code):
    attributes = frappe.db.get_all('Item Variant Attribute',
                                   fields=['attribute'],
                                   filters={
                                       'parenttype': 'Item',
                                       'parent': item_code
                                   },
                                   order_by='idx asc')

    optional_attributes = ItemVariantsCacheManager(
        item_code).get_optional_attributes()

    for a in attributes:
        if a.attribute in optional_attributes:
            a.optional = True

    return attributes
예제 #7
0
파일: utils.py 프로젝트: mshehata-adv/erpv
def get_next_attribute_and_values(item_code, selected_attributes):
    '''Find the count of Items that match the selected attributes.
	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.
	'''
    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

    valid_options_for_attributes = frappe._dict({})

    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
        if item_code in filtered_items and attribute not in selected_attributes and attribute in attribute_list:
            valid_options_for_attributes[attribute].add(attribute_value)

    optional_attributes = item_cache.get_optional_attributes()
    exact_match = []
    shopping_cart_settings = get_shopping_cart_settings()
    allow_items_not_in_stock = cint(
        shopping_cart_settings.allow_items_not_in_stock)
    # 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
    from erpnext.shopping_cart.product_info import get_product_info_for_website
    if exact_match:
        data = get_product_info_for_website(exact_match[0])
        product_info = data.product_info
        if not data.cart_settings.show_price:
            product_info = None
    else:
        product_info = None

    return {
        '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,
        'allow_items_not_in_stock': allow_items_not_in_stock,
        'product_info': product_info
    }