blob: 398049d4bdb0a927dbaf71679a0d82f4ac75782c [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
Rushabh Mehtafc19f252012-12-20 17:11:51 +05307
Anand Doshie47def82012-07-09 15:56:12 +05308@webnotes.whitelist(allow_guest=True)
Rushabh Mehtaee472b02012-12-18 11:47:13 +05309def get_product_info(item_code):
10 """get product price / stock info"""
11 price_list = webnotes.conn.get_value("Item", item_code, "website_price_list")
12 warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
13 if warehouse:
14 in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
15 item_code=%s and warehouse=%s""", (item_code, warehouse))
16 if in_stock:
17 in_stock = in_stock[0][0] > 0 and 1 or 0
18 else:
19 in_stock = -1
20 return {
21 "price": price_list and webnotes.conn.sql("""select ref_rate, ref_currency from
22 `tabItem Price` where parent=%s and price_list_name=%s""",
23 (item_code, price_list), as_dict=1) or [],
24 "stock": in_stock
25 }
26
27@webnotes.whitelist(allow_guest=True)
Rushabh Mehtafc19f252012-12-20 17:11:51 +053028def get_product_list(search=None, product_group=None, start=0):
Anand Doshie47def82012-07-09 15:56:12 +053029 import webnotes
Anand Doshi0fd99e72012-11-30 16:38:04 +053030 from webnotes.utils import cstr
Rushabh Mehtafc19f252012-12-20 17:11:51 +053031
Anand Doshie47def82012-07-09 15:56:12 +053032 # base query
33 query = """\
Rushabh Mehta647e0d12012-12-18 14:47:54 +053034 select name, item_name, page_name, website_image, item_group,
Rushabh Mehtafc19f252012-12-20 17:11:51 +053035 web_long_description as website_description
Anand Doshie47def82012-07-09 15:56:12 +053036 from `tabItem`
Rushabh Mehta647e0d12012-12-18 14:47:54 +053037 where docstatus = 0
Rushabh Mehtae109fa42012-12-19 10:14:59 +053038 and show_in_website = 1 """
Anand Doshie47def82012-07-09 15:56:12 +053039
40 # search term condition
Rushabh Mehtafc19f252012-12-20 17:11:51 +053041 if search:
Anand Doshie47def82012-07-09 15:56:12 +053042 query += """
43 and (
Anand Doshie47def82012-07-09 15:56:12 +053044 web_long_description like %(search)s or
Anand Doshie47def82012-07-09 15:56:12 +053045 item_name like %(search)s or
46 name like %(search)s
47 )"""
Rushabh Mehtafc19f252012-12-20 17:11:51 +053048 search = "%" + cstr(search) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053049
50 # product group condition
Rushabh Mehtafc19f252012-12-20 17:11:51 +053051 if product_group:
Anand Doshie47def82012-07-09 15:56:12 +053052 query += """
Rushabh Mehtae109fa42012-12-19 10:14:59 +053053 and item_group = %(product_group)s """
Anand Doshie47def82012-07-09 15:56:12 +053054
55 # order by
Rushabh Mehtafc19f252012-12-20 17:11:51 +053056 query += """order by item_name asc, name asc limit %s, 10""" % start
Anand Doshi8c7e76b2012-07-11 18:40:57 +053057
Rushabh Mehtafc19f252012-12-20 17:11:51 +053058 return webnotes.conn.sql(query, {
59 "search": search,
60 "product_group": product_group
61 }, as_dict=1)