def get_item_groups(pos_profile): item_groups = [] pos_profile = frappe.get_cached_doc('POS Profile', pos_profile) if pos_profile.get('item_groups'): # Get items based on the item groups defined in the POS profile for data in pos_profile.get('item_groups'): item_groups.extend(["'%s'" % frappe.db.escape(d.name) for d in get_child_nodes('Item Group', data.item_group)]) return list(set(item_groups))
def get_item_groups(pos_profile): item_groups = [] pos_profile = frappe.get_cached_doc('POS Profile', pos_profile) if pos_profile.get('item_groups'): # Get items based on the item groups defined in the POS profile for data in pos_profile.get('item_groups'): item_groups.extend(["%s" % frappe.db.escape(d.name) for d in get_child_nodes('Item Group', data.item_group)]) return list(set(item_groups))
def get_items(page="1", field_filters=None, attribute_filters=None, search=None): other_fieldnames = ["item_group", "thumbnail", "has_variants"] price_list = frappe.db.get_single_value("Shopping Cart Settings", "price_list") products_per_page = frappe.db.get_single_value("Products Settings", "products_per_page") get_item_groups = compose( list, unique, map(lambda x: x.get("name")), concat, map(lambda x: get_child_nodes("Item Group", x) if x and frappe.db.exists("Item Group", x, cache=True) else []), ) get_other_fields = compose( valmap(excepts(StopIteration, first, lambda _: {})), groupby("name"), lambda item_codes: frappe.db.sql( """ SELECT name, {other_fieldnames} FROM `tabItem` WHERE name IN %(item_codes)s """.format(other_fieldnames=", ".join(other_fieldnames)), values={"item_codes": item_codes}, as_dict=1, ), lambda items: [x.get("name") for x in items], ) get_page_count = compose( lambda x: frappe.utils.ceil(x[0][0] / products_per_page), lambda x: frappe.db.sql( """ SELECT COUNT(name) FROM `tabItem` WHERE show_in_website = 1 AND item_group IN %(item_groups)s """, values={"item_groups": x}, ), ) field_dict = (frappe.parse_json(field_filters) if isinstance( field_filters, str) else field_filters) or {} item_groups = (get_item_groups(field_dict.get("item_group")) if field_dict.get("item_group") else None) frappe.form_dict.start = (frappe.utils.cint(page) - 1) * products_per_page items = get_products_for_website( field_filters=merge( field_dict, {"item_group": item_groups} if item_groups else {}), attribute_filters=frappe.parse_json(attribute_filters), search=search, ) other_fields = get_other_fields(items) if items else {} item_prices = _get_item_prices(price_list, items) if items else {} get_rates = _rate_getter(price_list, item_prices) return { "page_count": get_page_count(item_groups) if item_groups else 0, "items": [ merge( x, { "route": transform_route(x), "description": frappe.utils.strip_html_tags(x.get("description") or ""), }, get_rates(x.get("name")), { k: other_fields.get(x.get("name"), {}).get(k) for k in other_fieldnames }, ) for x in items ], }