blob: fb4d4e4aedc45f0def4822793b3e7ff3655fb1ec [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
Anand Doshiabc10032013-06-14 17:44:03 +05307from webnotes.utils import cstr, cint, fmt_money
Rushabh Mehta9db1a682013-04-02 10:41:37 +05308from webnotes.webutils import build_html, delete_page_cache
Anand Doshie47def82012-07-09 15:56:12 +05309
10@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaee472b02012-12-18 11:47:13 +053011def get_product_info(item_code):
12 """get product price / stock info"""
Anand Doshiabc10032013-06-14 17:44:03 +053013 price_list = webnotes.conn.get_value("Price List", {"use_for_website": 1})
Rushabh Mehtaee472b02012-12-18 11:47:13 +053014 warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
15 if warehouse:
16 in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
17 item_code=%s and warehouse=%s""", (item_code, warehouse))
18 if in_stock:
19 in_stock = in_stock[0][0] > 0 and 1 or 0
20 else:
21 in_stock = -1
Anand Doshi220ff302013-05-14 15:33:34 +053022
23 price = price_list and webnotes.conn.sql("""select ref_rate, ref_currency from
24 `tabItem Price` where parent=%s and price_list_name=%s""",
25 (item_code, price_list), as_dict=1) or []
26
27 price = price and price[0] or None
28
29 if price:
Anand Doshiabc10032013-06-14 17:44:03 +053030 price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["ref_currency"])
31
Anand Doshi220ff302013-05-14 15:33:34 +053032 price["ref_currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
33 and (webnotes.conn.get_value("Currency", price.ref_currency, "symbol") or price.ref_currency) \
34 or ""
35
Rushabh Mehtaee472b02012-12-18 11:47:13 +053036 return {
Anand Doshi220ff302013-05-14 15:33:34 +053037 "price": price,
Anand Doshiabc10032013-06-14 17:44:03 +053038 "stock": in_stock,
39 "uom": webnotes.conn.get_value("Item", item_code, "stock_uom")
Rushabh Mehtaee472b02012-12-18 11:47:13 +053040 }
41
42@webnotes.whitelist(allow_guest=True)
Anand Doshid8c3aa22013-02-11 19:33:33 +053043def get_product_list(search=None, start=0, limit=10):
Anand Doshie47def82012-07-09 15:56:12 +053044 # base query
Rushabh Mehta7837d802012-12-25 15:09:14 +053045 query = """select name, item_name, page_name, website_image, item_group,
Rushabh Mehtafc19f252012-12-20 17:11:51 +053046 web_long_description as website_description
Rushabh Mehta7837d802012-12-25 15:09:14 +053047 from `tabItem` where docstatus = 0 and show_in_website = 1 """
Anand Doshie47def82012-07-09 15:56:12 +053048
49 # search term condition
Rushabh Mehtafc19f252012-12-20 17:11:51 +053050 if search:
Rushabh Mehta7837d802012-12-25 15:09:14 +053051 query += """and (web_long_description like %(search)s or
52 item_name like %(search)s or name like %(search)s)"""
Rushabh Mehtafc19f252012-12-20 17:11:51 +053053 search = "%" + cstr(search) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053054
Anand Doshie47def82012-07-09 15:56:12 +053055 # order by
Rushabh Mehta7837d802012-12-25 15:09:14 +053056 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053057
Rushabh Mehta7837d802012-12-25 15:09:14 +053058 data = webnotes.conn.sql(query, {
Rushabh Mehtafc19f252012-12-20 17:11:51 +053059 "search": search,
Rushabh Mehta7837d802012-12-25 15:09:14 +053060 }, as_dict=1)
61
62 return [get_item_for_list_in_html(r) for r in data]
63
64
65def get_product_list_for_group(product_group=None, start=0, limit=10):
66 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])
67
68 # base query
69 query = """select name, item_name, page_name, website_image, item_group,
70 web_long_description as website_description
71 from `tabItem` where docstatus = 0 and show_in_website = 1
72 and (item_group in (%s)
73 or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups)
74
75 query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)
76
77 data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1)
78
79 return [get_item_for_list_in_html(r) for r in data]
80
81def get_child_groups(item_group_name):
82 item_group = webnotes.doc("Item Group", item_group_name)
83 return webnotes.conn.sql("""select name
Anand Doshi9e830ec2013-02-08 20:08:46 +053084 from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s
85 and show_in_website = 1""", item_group.fields)
Rushabh Mehta7837d802012-12-25 15:09:14 +053086
87def get_group_item_count(item_group):
88 child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
89 return webnotes.conn.sql("""select count(*) from `tabItem`
90 where docstatus = 0 and show_in_website = 1
91 and (item_group in (%s)
92 or name in (select parent from `tabWebsite Item Group`
93 where item_group in (%s))) """ % (child_groups, child_groups))[0][0]
94
95def get_item_for_list_in_html(r):
Rushabh Mehta7837d802012-12-25 15:09:14 +053096 scrub_item_for_list(r)
Anand Doshi060d9242013-06-12 17:40:36 +053097 r.template = "app/website/templates/html/product_in_grid.html"
Rushabh Mehta7837d802012-12-25 15:09:14 +053098 return build_html(r)
99
100def scrub_item_for_list(r):
101 if not r.website_description:
102 r.website_description = "No description given"
Anand Doshie1ad4432013-02-11 20:28:56 +0530103 if len(r.website_description.split(" ")) > 24:
104 r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..."
Rushabh Mehta7837d802012-12-25 15:09:14 +0530105
106def get_parent_item_groups(item_group_name):
107 item_group = webnotes.doc("Item Group", item_group_name)
108 return webnotes.conn.sql("""select name, page_name from `tabItem Group`
109 where lft <= %s and rgt >= %s
110 and ifnull(show_in_website,0)=1
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530111 order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True)
112
113def invalidate_cache_for(item_group):
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530114 for i in get_parent_item_groups(item_group):
Nabin Hait833d7a62012-12-30 19:56:57 +0530115 if i.page_name:
116 delete_page_cache(i.page_name)