blob: df5f16df5caa6c1b0a5fe94581c4ae85ff5cd84d [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Rushabh Mehtaa494b882012-12-07 12:44:45 +05303
Anand Doshi486f9df2012-07-19 13:40:31 +05304from __future__ import unicode_literals
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +05305
Anand Doshie47def82012-07-09 15:56:12 +05306import webnotes
Rushabh Mehta7c2312a2013-09-10 12:51:52 +05307from webnotes.utils import cstr, cint, fmt_money, get_base_path
8from webnotes.webutils import delete_page_cache
Rushabh Mehtac59c4e02013-09-09 12:17:45 +05309from selling.utils.cart import _get_cart_quotation
Anand Doshie47def82012-07-09 15:56:12 +053010
11@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaee472b02012-12-18 11:47:13 +053012def get_product_info(item_code):
13 """get product price / stock info"""
Anand Doshi3d214632013-09-06 15:53:04 +053014 if not cint(webnotes.conn.get_default("shopping_cart_enabled")):
Anand Doshi2862c9e2013-07-08 18:50:33 +053015 return {}
16
17 cart_quotation = _get_cart_quotation()
18
Rushabh Mehta4a404e92013-08-09 18:11:35 +053019 price_list = webnotes.cookies.get("selling_price_list").value
Anand Doshi2862c9e2013-07-08 18:50:33 +053020
Rushabh Mehtaee472b02012-12-18 11:47:13 +053021 warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
22 if warehouse:
23 in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
24 item_code=%s and warehouse=%s""", (item_code, warehouse))
25 if in_stock:
26 in_stock = in_stock[0][0] > 0 and 1 or 0
27 else:
28 in_stock = -1
Anand Doshi220ff302013-05-14 15:33:34 +053029
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +053030 price = price_list and webnotes.conn.sql("""select ref_rate, currency from
31 `tabItem Price` where item_code=%s and price_list=%s""",
Anand Doshi220ff302013-05-14 15:33:34 +053032 (item_code, price_list), as_dict=1) or []
33
34 price = price and price[0] or None
Anand Doshi3dceb842013-06-19 14:57:14 +053035 qty = 0
36
Anand Doshi220ff302013-05-14 15:33:34 +053037 if price:
Anand Doshie1221212013-09-18 17:28:06 +053038 price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["currency"])
Anand Doshiabc10032013-06-14 17:44:03 +053039
Anand Doshie1221212013-09-18 17:28:06 +053040 price["currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
41 and (webnotes.conn.get_value("Currency", price.currency, "symbol") or price.currency) \
Anand Doshi220ff302013-05-14 15:33:34 +053042 or ""
Anand Doshi3dceb842013-06-19 14:57:14 +053043
44 if webnotes.session.user != "Guest":
Anand Doshi2862c9e2013-07-08 18:50:33 +053045 item = cart_quotation.doclist.get({"item_code": item_code})
Anand Doshi3dceb842013-06-19 14:57:14 +053046 if item:
47 qty = item[0].qty
Anand Doshi220ff302013-05-14 15:33:34 +053048
Rushabh Mehtaee472b02012-12-18 11:47:13 +053049 return {
Anand Doshi220ff302013-05-14 15:33:34 +053050 "price": price,
Anand Doshiabc10032013-06-14 17:44:03 +053051 "stock": in_stock,
Anand Doshi3dceb842013-06-19 14:57:14 +053052 "uom": webnotes.conn.get_value("Item", item_code, "stock_uom"),
53 "qty": qty
Rushabh Mehtaee472b02012-12-18 11:47:13 +053054 }
55
56@webnotes.whitelist(allow_guest=True)
Anand Doshid8c3aa22013-02-11 19:33:33 +053057def get_product_list(search=None, start=0, limit=10):
Anand Doshie47def82012-07-09 15:56:12 +053058 # base query
Rushabh Mehta7837d802012-12-25 15:09:14 +053059 query = """select name, item_name, page_name, website_image, item_group,
Rushabh Mehtafc19f252012-12-20 17:11:51 +053060 web_long_description as website_description
Rushabh Mehta7837d802012-12-25 15:09:14 +053061 from `tabItem` where docstatus = 0 and show_in_website = 1 """
Anand Doshie47def82012-07-09 15:56:12 +053062
63 # search term condition
Rushabh Mehtafc19f252012-12-20 17:11:51 +053064 if search:
Rushabh Mehta7837d802012-12-25 15:09:14 +053065 query += """and (web_long_description like %(search)s or
66 item_name like %(search)s or name like %(search)s)"""
Rushabh Mehtafc19f252012-12-20 17:11:51 +053067 search = "%" + cstr(search) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053068
Anand Doshie47def82012-07-09 15:56:12 +053069 # order by
Rushabh Mehta7837d802012-12-25 15:09:14 +053070 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053071
Rushabh Mehta7837d802012-12-25 15:09:14 +053072 data = webnotes.conn.sql(query, {
Rushabh Mehtafc19f252012-12-20 17:11:51 +053073 "search": search,
Rushabh Mehta7837d802012-12-25 15:09:14 +053074 }, as_dict=1)
75
76 return [get_item_for_list_in_html(r) for r in data]
77
78
79def get_product_list_for_group(product_group=None, start=0, limit=10):
80 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])
81
82 # base query
83 query = """select name, item_name, page_name, website_image, item_group,
84 web_long_description as website_description
85 from `tabItem` where docstatus = 0 and show_in_website = 1
86 and (item_group in (%s)
87 or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups)
88
89 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
90
91 data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1)
92
93 return [get_item_for_list_in_html(r) for r in data]
94
95def get_child_groups(item_group_name):
96 item_group = webnotes.doc("Item Group", item_group_name)
97 return webnotes.conn.sql("""select name
Anand Doshi9e830ec2013-02-08 20:08:46 +053098 from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s
99 and show_in_website = 1""", item_group.fields)
Rushabh Mehta7837d802012-12-25 15:09:14 +0530100
101def get_group_item_count(item_group):
102 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
103 return webnotes.conn.sql("""select count(*) from `tabItem`
104 where docstatus = 0 and show_in_website = 1
105 and (item_group in (%s)
106 or name in (select parent from `tabWebsite Item Group`
107 where item_group in (%s))) """ % (child_groups, child_groups))[0][0]
108
Rushabh Mehta7c2312a2013-09-10 12:51:52 +0530109def get_item_for_list_in_html(context):
110 from jinja2 import Environment, FileSystemLoader
111 scrub_item_for_list(context)
112 jenv = Environment(loader = FileSystemLoader(get_base_path()))
113 template = jenv.get_template("app/stock/doctype/item/templates/includes/product_in_grid.html")
114 return template.render(context)
Rushabh Mehta7837d802012-12-25 15:09:14 +0530115
116def scrub_item_for_list(r):
117 if not r.website_description:
118 r.website_description = "No description given"
Anand Doshie1ad4432013-02-11 20:28:56 +0530119 if len(r.website_description.split(" ")) > 24:
120 r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..."
Rushabh Mehta7837d802012-12-25 15:09:14 +0530121
122def get_parent_item_groups(item_group_name):
123 item_group = webnotes.doc("Item Group", item_group_name)
124 return webnotes.conn.sql("""select name, page_name from `tabItem Group`
125 where lft <= %s and rgt >= %s
126 and ifnull(show_in_website,0)=1
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530127 order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True)
128
129def invalidate_cache_for(item_group):
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530130 for i in get_parent_item_groups(item_group):
Nabin Hait833d7a62012-12-30 19:56:57 +0530131 if i.page_name:
132 delete_page_cache(i.page_name)