blob: fec462607b2eaa80dd9eca5a01eaa1ee480bbb48 [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):
28 """
29 args = {
30 'limit_start': 0,
31 'limit_page_length': 20,
32 'search': '',
33 'product_group': '',
34 }
35 """
36 import webnotes
Anand Doshi0fd99e72012-11-30 16:38:04 +053037 from webnotes.utils import cstr
Anand Doshie47def82012-07-09 15:56:12 +053038
39 if not args: args = webnotes.form_dict
40
Anand Doshie47def82012-07-09 15:56:12 +053041 # base query
42 query = """\
43 select name, item_name, page_name, website_image,
44 description, web_short_description
45 from `tabItem`
46 where is_sales_item = 'Yes'
47 and docstatus = 0
48 and show_in_website = 1"""
49
50 # search term condition
51 if args.get('search'):
52 query += """
53 and (
54 web_short_description like %(search)s or
55 web_long_description like %(search)s or
56 description like %(search)s or
57 item_name like %(search)s or
58 name like %(search)s
59 )"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053060 args['search'] = "%" + cstr(args.get('search')) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053061
62 # product group condition
63 if args.get('product_group') and args.get('product_group') != 'All Products':
64 query += """
65 and item_group = %(product_group)s"""
Anand Doshie47def82012-07-09 15:56:12 +053066
67 # order by
68 query += """
69 order by item_name asc, name asc"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053070
71 from webnotes.widgets.query_builder import add_limit_to_query
72 query, args = add_limit_to_query(query, args)
73
74 return webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053075
76@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053077def get_product_category_list(args=None):
78 """
79 args = {
80 'limit_start': 0,
81 'limit_page_length': 5,
82 }
83 """
Anand Doshie47def82012-07-09 15:56:12 +053084 import webnotes
85
Anand Doshi8c7e76b2012-07-11 18:40:57 +053086 if not args: args = webnotes.form_dict
87
88 query = """\
Anand Doshie47def82012-07-09 15:56:12 +053089 select count(name) as items, item_group
90 from `tabItem`
91 where is_sales_item = 'Yes'
92 and docstatus = 0
93 and show_in_website = 1
94 group by item_group
Anand Doshi8c7e76b2012-07-11 18:40:57 +053095 order by items desc"""
96
97 from webnotes.widgets.query_builder import add_limit_to_query
98 query, args = add_limit_to_query(query, args)
99
100
101 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +0530102
103 # add All Products link
104 total_count = sum((r.get('items') or 0 for r in result))
105 result = [{'items': total_count, 'item_group': 'All Products'}] + (result or [])
106
107 return result
108
109@webnotes.whitelist(allow_guest=True)
110def get_similar_product_list(args=None):
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530111 """
112 args = {
113 'limit_start': 0,
114 'limit_page_length': 5,
115 'product_name': '',
116 'product_group': '',
117 }
118 """
Anand Doshie47def82012-07-09 15:56:12 +0530119 import webnotes
120
121 if not args: args = webnotes.form_dict
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530122
123 query = """\
Anand Doshie47def82012-07-09 15:56:12 +0530124 select name, item_name, page_name, website_image,
125 description, web_short_description
126 from `tabItem`
127 where is_sales_item = 'Yes'
128 and docstatus = 0
129 and show_in_website = 1
130 and name != %(product_name)s
131 and item_group = %(product_group)s
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530132 order by item_name"""
133
134 from webnotes.widgets.query_builder import add_limit_to_query
135 query, args = add_limit_to_query(query, args)
136
137 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +0530138
139 return result