[Website] Price in items list and sales uom management (#13335)
* Price in items list and sales uom management
* Price in product search page
* Display website stock in sales uom
* Add set_product_info_for_website to avoid duplicate code
* Add price uom in item list and page
* Update product.py
diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py
index 0ff7c2b..d977133 100644
--- a/erpnext/setup/doctype/item_group/item_group.py
+++ b/erpnext/setup/doctype/item_group/item_group.py
@@ -9,9 +9,9 @@
from frappe.website.website_generator import WebsiteGenerator
from frappe.website.render import clear_cache
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
+from erpnext.shopping_cart.product_info import set_product_info_for_website
from erpnext.utilities.product import get_qty_in_stock
-
class ItemGroup(NestedSet, WebsiteGenerator):
nsm_parent_field = 'parent_item_group'
website = frappe._dict(
@@ -105,9 +105,11 @@
query += """order by I.weightage desc, in_stock desc, I.modified desc limit %s, %s""" % (start, limit)
data = frappe.db.sql(query, {"product_group": product_group,"search": search, "today": nowdate()}, as_dict=1)
-
data = adjust_qty_for_expired_items(data)
+ for item in data:
+ set_product_info_for_website(item)
+
return [get_item_for_list_in_html(r) for r in data]
diff --git a/erpnext/shopping_cart/product_info.py b/erpnext/shopping_cart/product_info.py
index 6f9c8ac..33cac17 100644
--- a/erpnext/shopping_cart/product_info.py
+++ b/erpnext/shopping_cart/product_info.py
@@ -33,7 +33,8 @@
"in_stock": stock_status.in_stock if stock_status.is_stock_item else 1,
"qty": 0,
"uom": frappe.db.get_value("Item", item_code, "stock_uom"),
- "show_stock_qty": show_quantity_in_website() if stock_status.is_stock_item else 0
+ "show_stock_qty": show_quantity_in_website() if stock_status.is_stock_item else 0,
+ "sales_uom": frappe.db.get_value("Item", item_code, "sales_uom")
}
if product_info["price"]:
@@ -42,4 +43,18 @@
if item:
product_info["qty"] = item[0].qty
- return product_info
\ No newline at end of file
+ return product_info
+
+def set_product_info_for_website(item):
+ """set product price uom for website"""
+ product_info = get_product_info_for_website(item.item_code)
+
+ if product_info:
+ item["stock_uom"] = product_info.get("uom")
+ item["sales_uom"] = product_info.get("sales_uom")
+ if product_info.get("price"):
+ item["price_stock_uom"] = product_info.get("price").get("formatted_price")
+ item["price_sales_uom"] = product_info.get("price").get("formatted_price_sales_uom")
+ else:
+ item["price_stock_uom"] = ""
+ item["price_sales_uom"] = ""
\ No newline at end of file
diff --git a/erpnext/templates/includes/product_page.js b/erpnext/templates/includes/product_page.js
index 722283e..e96263f 100644
--- a/erpnext/templates/includes/product_page.js
+++ b/erpnext/templates/includes/product_page.js
@@ -15,7 +15,8 @@
$(".item-cart").toggleClass("hide", (!!!r.message.price || !!!r.message.in_stock));
if(r.message && r.message.price) {
$(".item-price")
- .html(r.message.price.formatted_price + " / " + r.message.uom);
+ .html(r.message.price.formatted_price_sales_uom + "<div style='font-size: small'>\
+ (" + r.message.price.formatted_price + " / " + r.message.uom + ")</div>");
if(r.message.in_stock==0) {
$(".item-stock").html("<div style='color: red'> <i class='fa fa-close'></i> {{ _("Not in stock") }}</div>");
diff --git a/erpnext/templates/includes/products_as_grid.html b/erpnext/templates/includes/products_as_grid.html
index 4a9692a..34c2ccd 100644
--- a/erpnext/templates/includes/products_as_grid.html
+++ b/erpnext/templates/includes/products_as_grid.html
@@ -5,6 +5,13 @@
<div class="product-image-img">
{{ product_image_square(thumbnail or website_image) }}
<div class="product-text" itemprop="name">{{ item_name }}</div>
+ {% if price_sales_uom %}
+ <div>{{ price_sales_uom }}</div>
+ <div style='font-size: small; margin-bottom: 10px;'>({{ price_stock_uom }} / {{ stock_uom }})</div>
+ {% else %}
+ <div> </div>
+ <div style='font-size: small; margin-bottom: 10px;'> </div>
+ {% endif %}
{% if in_stock or not is_stock_item %}
<div style='color: green'> <i class='fa fa-check'></i> {{ _("In stock") }}</div>
{% else %}
diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py
index f2fd600..ff1dd7d 100644
--- a/erpnext/templates/pages/product_search.py
+++ b/erpnext/templates/pages/product_search.py
@@ -5,6 +5,7 @@
import frappe
from frappe.utils import cstr, nowdate, cint
from erpnext.setup.doctype.item_group.item_group import get_item_for_list_in_html
+from erpnext.shopping_cart.product_info import set_product_info_for_website
no_cache = 1
no_sitemap = 1
@@ -42,5 +43,8 @@
"today": nowdate()
}, as_dict=1)
+ for item in data:
+ set_product_info_for_website(item)
+
return [get_item_for_list_in_html(r) for r in data]
diff --git a/erpnext/utilities/product.py b/erpnext/utilities/product.py
index b0cfbae..8596776 100644
--- a/erpnext/utilities/product.py
+++ b/erpnext/utilities/product.py
@@ -19,14 +19,16 @@
warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field)
if warehouse:
- stock_qty = frappe.db.sql("""select GREATEST(actual_qty - reserved_qty, 0) from tabBin where
- item_code=%s and warehouse=%s""", (item_code, warehouse))
+ stock_qty = frappe.db.sql("""
+ select GREATEST(S.actual_qty - S.reserved_qty - S.reserved_qty_for_production - S.reserved_qty_for_sub_contract, 0) / IFNULL(C.conversion_factor, 1)
+ from tabBin S
+ inner join `tabItem` I on S.item_code = I.Item_code
+ left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code
+ where S.item_code=%s and S.warehouse=%s""", (item_code, warehouse))
- if stock_qty:
- stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
-
- if stock_qty:
- in_stock = stock_qty[0][0] > 0 and 1 or 0
+ if stock_qty:
+ stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
+ in_stock = stock_qty[0][0] > 0 and 1 or 0
return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item})
@@ -103,6 +105,14 @@
and (frappe.db.get_value("Currency", price_obj.currency, "symbol") or price_obj.currency) \
or ""
+ uom_conversion_factor = frappe.db.sql("""select C.conversion_factor
+ from `tabUOM Conversion Detail` C
+ inner join `tabItem` I on C.uom = I.sales_uom
+ where C.parent = %s""", item_code)
+
+ uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
+ price_obj["formatted_price_sales_uom"] = fmt_money(price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"])
+
if not price_obj["price_list_rate"]:
price_obj["price_list_rate"] = 0