2013-08-05 09:29:54 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
2012-12-07 07:14:45 +00:00
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-12-27 08:35:02 +00:00
|
|
|
|
2012-07-09 10:26:12 +00:00
|
|
|
import webnotes
|
2013-06-14 12:14:03 +00:00
|
|
|
from webnotes.utils import cstr, cint, fmt_money
|
2013-04-02 05:11:37 +00:00
|
|
|
from webnotes.webutils import build_html, delete_page_cache
|
2013-07-08 13:20:33 +00:00
|
|
|
from website.helpers.cart import _get_cart_quotation
|
2012-07-09 10:26:12 +00:00
|
|
|
|
2012-12-18 06:17:13 +00:00
|
|
|
@webnotes.whitelist(allow_guest=True)
|
|
|
|
def get_product_info(item_code):
|
|
|
|
"""get product price / stock info"""
|
2013-07-08 13:20:33 +00:00
|
|
|
if not webnotes.conn.get_default("shopping_cart_enabled"):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
cart_quotation = _get_cart_quotation()
|
|
|
|
|
|
|
|
price_list = webnotes.cookies.get("price_list_name").value
|
|
|
|
|
2012-12-18 06:17:13 +00:00
|
|
|
warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
|
|
|
|
if warehouse:
|
|
|
|
in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
|
|
|
|
item_code=%s and warehouse=%s""", (item_code, warehouse))
|
|
|
|
if in_stock:
|
|
|
|
in_stock = in_stock[0][0] > 0 and 1 or 0
|
|
|
|
else:
|
|
|
|
in_stock = -1
|
2013-05-14 10:03:34 +00:00
|
|
|
|
|
|
|
price = price_list and webnotes.conn.sql("""select ref_rate, ref_currency from
|
|
|
|
`tabItem Price` where parent=%s and price_list_name=%s""",
|
|
|
|
(item_code, price_list), as_dict=1) or []
|
|
|
|
|
|
|
|
price = price and price[0] or None
|
2013-06-19 09:27:14 +00:00
|
|
|
qty = 0
|
|
|
|
|
2013-05-14 10:03:34 +00:00
|
|
|
if price:
|
2013-06-14 12:14:03 +00:00
|
|
|
price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["ref_currency"])
|
|
|
|
|
2013-05-14 10:03:34 +00:00
|
|
|
price["ref_currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
|
|
|
|
and (webnotes.conn.get_value("Currency", price.ref_currency, "symbol") or price.ref_currency) \
|
|
|
|
or ""
|
2013-06-19 09:27:14 +00:00
|
|
|
|
|
|
|
if webnotes.session.user != "Guest":
|
2013-07-08 13:20:33 +00:00
|
|
|
item = cart_quotation.doclist.get({"item_code": item_code})
|
2013-06-19 09:27:14 +00:00
|
|
|
if item:
|
|
|
|
qty = item[0].qty
|
2013-05-14 10:03:34 +00:00
|
|
|
|
2012-12-18 06:17:13 +00:00
|
|
|
return {
|
2013-05-14 10:03:34 +00:00
|
|
|
"price": price,
|
2013-06-14 12:14:03 +00:00
|
|
|
"stock": in_stock,
|
2013-06-19 09:27:14 +00:00
|
|
|
"uom": webnotes.conn.get_value("Item", item_code, "stock_uom"),
|
|
|
|
"qty": qty
|
2012-12-18 06:17:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-09 10:26:12 +00:00
|
|
|
@webnotes.whitelist(allow_guest=True)
|
2013-02-11 14:03:33 +00:00
|
|
|
def get_product_list(search=None, start=0, limit=10):
|
2012-07-09 10:26:12 +00:00
|
|
|
# base query
|
2012-12-25 09:39:14 +00:00
|
|
|
query = """select name, item_name, page_name, website_image, item_group,
|
2012-12-20 11:41:51 +00:00
|
|
|
web_long_description as website_description
|
2012-12-25 09:39:14 +00:00
|
|
|
from `tabItem` where docstatus = 0 and show_in_website = 1 """
|
2012-07-09 10:26:12 +00:00
|
|
|
|
|
|
|
# search term condition
|
2012-12-20 11:41:51 +00:00
|
|
|
if search:
|
2012-12-25 09:39:14 +00:00
|
|
|
query += """and (web_long_description like %(search)s or
|
|
|
|
item_name like %(search)s or name like %(search)s)"""
|
2012-12-20 11:41:51 +00:00
|
|
|
search = "%" + cstr(search) + "%"
|
2012-07-09 10:26:12 +00:00
|
|
|
|
|
|
|
# order by
|
2012-12-25 09:39:14 +00:00
|
|
|
query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
|
2012-07-11 13:10:57 +00:00
|
|
|
|
2012-12-25 09:39:14 +00:00
|
|
|
data = webnotes.conn.sql(query, {
|
2012-12-20 11:41:51 +00:00
|
|
|
"search": search,
|
2012-12-25 09:39:14 +00:00
|
|
|
}, as_dict=1)
|
|
|
|
|
|
|
|
return [get_item_for_list_in_html(r) for r in data]
|
|
|
|
|
|
|
|
|
|
|
|
def get_product_list_for_group(product_group=None, start=0, limit=10):
|
|
|
|
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])
|
|
|
|
|
|
|
|
# base query
|
|
|
|
query = """select name, item_name, page_name, website_image, item_group,
|
|
|
|
web_long_description as website_description
|
|
|
|
from `tabItem` where docstatus = 0 and show_in_website = 1
|
|
|
|
and (item_group in (%s)
|
|
|
|
or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups)
|
|
|
|
|
|
|
|
query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
|
|
|
|
|
|
|
|
data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1)
|
|
|
|
|
|
|
|
return [get_item_for_list_in_html(r) for r in data]
|
|
|
|
|
|
|
|
def get_child_groups(item_group_name):
|
|
|
|
item_group = webnotes.doc("Item Group", item_group_name)
|
|
|
|
return webnotes.conn.sql("""select name
|
2013-02-08 14:38:46 +00:00
|
|
|
from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s
|
|
|
|
and show_in_website = 1""", item_group.fields)
|
2012-12-25 09:39:14 +00:00
|
|
|
|
|
|
|
def get_group_item_count(item_group):
|
|
|
|
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
|
|
|
|
return webnotes.conn.sql("""select count(*) from `tabItem`
|
|
|
|
where docstatus = 0 and show_in_website = 1
|
|
|
|
and (item_group in (%s)
|
|
|
|
or name in (select parent from `tabWebsite Item Group`
|
|
|
|
where item_group in (%s))) """ % (child_groups, child_groups))[0][0]
|
|
|
|
|
|
|
|
def get_item_for_list_in_html(r):
|
|
|
|
scrub_item_for_list(r)
|
2013-06-12 12:10:36 +00:00
|
|
|
r.template = "app/website/templates/html/product_in_grid.html"
|
2012-12-25 09:39:14 +00:00
|
|
|
return build_html(r)
|
|
|
|
|
|
|
|
def scrub_item_for_list(r):
|
|
|
|
if not r.website_description:
|
|
|
|
r.website_description = "No description given"
|
2013-02-11 14:58:56 +00:00
|
|
|
if len(r.website_description.split(" ")) > 24:
|
|
|
|
r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..."
|
2012-12-25 09:39:14 +00:00
|
|
|
|
|
|
|
def get_parent_item_groups(item_group_name):
|
|
|
|
item_group = webnotes.doc("Item Group", item_group_name)
|
|
|
|
return webnotes.conn.sql("""select name, page_name from `tabItem Group`
|
|
|
|
where lft <= %s and rgt >= %s
|
|
|
|
and ifnull(show_in_website,0)=1
|
2012-12-25 12:48:17 +00:00
|
|
|
order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True)
|
|
|
|
|
|
|
|
def invalidate_cache_for(item_group):
|
|
|
|
for i in get_parent_item_groups(item_group):
|
2012-12-30 14:26:57 +00:00
|
|
|
if i.page_name:
|
|
|
|
delete_page_cache(i.page_name)
|