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-09-10 07:21:52 +00:00
|
|
|
from webnotes.utils import cstr, cint, fmt_money, get_base_path
|
|
|
|
from webnotes.webutils import delete_page_cache
|
2013-09-09 06:47:45 +00:00
|
|
|
from selling.utils.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-09-06 10:23:04 +00:00
|
|
|
if not cint(webnotes.conn.get_default("shopping_cart_enabled")):
|
2013-07-08 13:20:33 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
cart_quotation = _get_cart_quotation()
|
|
|
|
|
2013-08-09 12:41:35 +00:00
|
|
|
price_list = webnotes.cookies.get("selling_price_list").value
|
2013-07-08 13:20:33 +00:00
|
|
|
|
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
|
|
|
|
2013-09-18 11:58:06 +00:00
|
|
|
price = price_list and webnotes.conn.sql("""select ip.ref_rate, pl.currency from
|
2013-09-11 07:35:24 +00:00
|
|
|
`tabItem Price` ip, `tabPrice List` pl where ip.parent = pl.name and
|
|
|
|
ip.item_code=%s and ip.parent=%s""",
|
2013-05-14 10:03:34 +00:00
|
|
|
(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-09-18 11:58:06 +00:00
|
|
|
price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["currency"])
|
2013-06-14 12:14:03 +00:00
|
|
|
|
2013-09-18 11:58:06 +00:00
|
|
|
price["currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
|
|
|
|
and (webnotes.conn.get_value("Currency", price.currency, "symbol") or price.currency) \
|
2013-05-14 10:03:34 +00:00
|
|
|
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]
|
|
|
|
|
2013-09-10 07:21:52 +00:00
|
|
|
def get_item_for_list_in_html(context):
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
scrub_item_for_list(context)
|
|
|
|
jenv = Environment(loader = FileSystemLoader(get_base_path()))
|
|
|
|
template = jenv.get_template("app/stock/doctype/item/templates/includes/product_in_grid.html")
|
|
|
|
return template.render(context)
|
2012-12-25 09:39:14 +00:00
|
|
|
|
|
|
|
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)
|