blob: 1e8257a7a7264a28f1a1ef8e6d6a736a3f7f1309 [file] [log] [blame]
Rushabh Mehtaa494b882012-12-07 12:44:45 +05301# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
2# License: GNU General Public License (v3). For more information see license.txt
3
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 Mehta5f1d57e2012-12-27 14:05:02 +05307from webnotes.utils import cstr
8from website.utils import build_html, url_for_website, delete_page_cache
Anand Doshie47def82012-07-09 15:56:12 +05309
Rushabh Mehtafc19f252012-12-20 17:11:51 +053010
Anand Doshie47def82012-07-09 15:56:12 +053011@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaee472b02012-12-18 11:47:13 +053012def get_product_info(item_code):
13 """get product price / stock info"""
14 price_list = webnotes.conn.get_value("Item", item_code, "website_price_list")
15 warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
16 if warehouse:
17 in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
18 item_code=%s and warehouse=%s""", (item_code, warehouse))
19 if in_stock:
20 in_stock = in_stock[0][0] > 0 and 1 or 0
21 else:
22 in_stock = -1
23 return {
24 "price": price_list and webnotes.conn.sql("""select ref_rate, ref_currency from
25 `tabItem Price` where parent=%s and price_list_name=%s""",
26 (item_code, price_list), as_dict=1) or [],
27 "stock": in_stock
28 }
29
30@webnotes.whitelist(allow_guest=True)
Rushabh Mehta7837d802012-12-25 15:09:14 +053031def get_product_list(search=None, product_group=None, start=0, limit=10):
Anand Doshie47def82012-07-09 15:56:12 +053032 # base query
Rushabh Mehta7837d802012-12-25 15:09:14 +053033 query = """select name, item_name, page_name, website_image, item_group,
Rushabh Mehtafc19f252012-12-20 17:11:51 +053034 web_long_description as website_description
Rushabh Mehta7837d802012-12-25 15:09:14 +053035 from `tabItem` where docstatus = 0 and show_in_website = 1 """
Anand Doshie47def82012-07-09 15:56:12 +053036
37 # search term condition
Rushabh Mehtafc19f252012-12-20 17:11:51 +053038 if search:
Rushabh Mehta7837d802012-12-25 15:09:14 +053039 query += """and (web_long_description like %(search)s or
40 item_name like %(search)s or name like %(search)s)"""
Rushabh Mehtafc19f252012-12-20 17:11:51 +053041 search = "%" + cstr(search) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053042
Anand Doshie47def82012-07-09 15:56:12 +053043 # order by
Rushabh Mehta7837d802012-12-25 15:09:14 +053044 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053045
Rushabh Mehta7837d802012-12-25 15:09:14 +053046 data = webnotes.conn.sql(query, {
Rushabh Mehtafc19f252012-12-20 17:11:51 +053047 "search": search,
48 "product_group": product_group
Rushabh Mehta7837d802012-12-25 15:09:14 +053049 }, as_dict=1)
50
51 return [get_item_for_list_in_html(r) for r in data]
52
53
54def get_product_list_for_group(product_group=None, start=0, limit=10):
55 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])
56
57 # base query
58 query = """select name, item_name, page_name, website_image, item_group,
59 web_long_description as website_description
60 from `tabItem` where docstatus = 0 and show_in_website = 1
61 and (item_group in (%s)
62 or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups)
63
64 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
65
66 data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1)
67
68 return [get_item_for_list_in_html(r) for r in data]
69
70def get_child_groups(item_group_name):
71 item_group = webnotes.doc("Item Group", item_group_name)
72 return webnotes.conn.sql("""select name
73 from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s""" \
74 % item_group.fields)
75
76def get_group_item_count(item_group):
77 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
78 return webnotes.conn.sql("""select count(*) from `tabItem`
79 where docstatus = 0 and show_in_website = 1
80 and (item_group in (%s)
81 or name in (select parent from `tabWebsite Item Group`
82 where item_group in (%s))) """ % (child_groups, child_groups))[0][0]
83
84def get_item_for_list_in_html(r):
Rushabh Mehta7837d802012-12-25 15:09:14 +053085 scrub_item_for_list(r)
86 r.template = "html/product_in_list.html"
87 return build_html(r)
88
89def scrub_item_for_list(r):
90 if not r.website_description:
91 r.website_description = "No description given"
92 if len(r.website_description.split(" ")) > 24:
93 r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..."
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +053094 r.website_image = url_for_website(r.website_image)
Rushabh Mehta7837d802012-12-25 15:09:14 +053095
96def get_parent_item_groups(item_group_name):
97 item_group = webnotes.doc("Item Group", item_group_name)
98 return webnotes.conn.sql("""select name, page_name from `tabItem Group`
99 where lft <= %s and rgt >= %s
100 and ifnull(show_in_website,0)=1
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530101 order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True)
102
103def invalidate_cache_for(item_group):
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530104 for i in get_parent_item_groups(item_group):
105 delete_page_cache(i.page_name)