blob: 60d54e67f0eccf601fb0be5c21fae0cd73623181 [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
Anand Doshie47def82012-07-09 15:56:12 +05305import webnotes
6
7@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaee472b02012-12-18 11:47:13 +05308def get_product_info(item_code):
9 """get product price / stock info"""
10 price_list = webnotes.conn.get_value("Item", item_code, "website_price_list")
11 warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
12 if warehouse:
13 in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
14 item_code=%s and warehouse=%s""", (item_code, warehouse))
15 if in_stock:
16 in_stock = in_stock[0][0] > 0 and 1 or 0
17 else:
18 in_stock = -1
19 return {
20 "price": price_list and webnotes.conn.sql("""select ref_rate, ref_currency from
21 `tabItem Price` where parent=%s and price_list_name=%s""",
22 (item_code, price_list), as_dict=1) or [],
23 "stock": in_stock
24 }
25
26@webnotes.whitelist(allow_guest=True)
Anand Doshie47def82012-07-09 15:56:12 +053027def get_product_list(args=None):
Anand Doshie47def82012-07-09 15:56:12 +053028 import webnotes
Anand Doshi0fd99e72012-11-30 16:38:04 +053029 from webnotes.utils import cstr
Anand Doshie47def82012-07-09 15:56:12 +053030
31 if not args: args = webnotes.form_dict
Rushabh Mehtae109fa42012-12-19 10:14:59 +053032 if not args.search: args.search = ""
Anand Doshie47def82012-07-09 15:56:12 +053033
Anand Doshie47def82012-07-09 15:56:12 +053034 # base query
35 query = """\
Rushabh Mehta647e0d12012-12-18 14:47:54 +053036 select name, item_name, page_name, website_image, item_group,
37 if(ifnull(web_short_description,'')='', web_long_description,
38 web_short_description) as web_short_description
Anand Doshie47def82012-07-09 15:56:12 +053039 from `tabItem`
Rushabh Mehta647e0d12012-12-18 14:47:54 +053040 where docstatus = 0
Rushabh Mehtae109fa42012-12-19 10:14:59 +053041 and show_in_website = 1 """
Anand Doshie47def82012-07-09 15:56:12 +053042
43 # search term condition
Rushabh Mehtae109fa42012-12-19 10:14:59 +053044 if args.search:
Anand Doshie47def82012-07-09 15:56:12 +053045 query += """
46 and (
47 web_short_description like %(search)s or
48 web_long_description like %(search)s or
49 description like %(search)s or
50 item_name like %(search)s or
51 name like %(search)s
52 )"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053053 args['search'] = "%" + cstr(args.get('search')) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053054
55 # product group condition
56 if args.get('product_group') and args.get('product_group') != 'All Products':
57 query += """
Rushabh Mehtae109fa42012-12-19 10:14:59 +053058 and item_group = %(product_group)s """
Anand Doshie47def82012-07-09 15:56:12 +053059
60 # order by
Rushabh Mehta647e0d12012-12-18 14:47:54 +053061 query += """order by item_name asc, name asc limit %s, 10""" % args.start
Anand Doshi8c7e76b2012-07-11 18:40:57 +053062
Rushabh Mehta647e0d12012-12-18 14:47:54 +053063 return webnotes.conn.sql(query, args, as_dict=1)