blob: aff15d95f3fef7e2b525446b26c2c1a878782a3e [file] [log] [blame]
Anand Doshie47def82012-07-09 15:56:12 +05301import webnotes
2
3@webnotes.whitelist(allow_guest=True)
4def get_product_list(args=None):
5 """
6 args = {
7 'limit_start': 0,
8 'limit_page_length': 20,
9 'search': '',
10 'product_group': '',
11 }
12 """
13 import webnotes
14 from webnotes.utils import cstr, cint
15
16 if not args: args = webnotes.form_dict
17
Anand Doshie47def82012-07-09 15:56:12 +053018 # base query
19 query = """\
20 select name, item_name, page_name, website_image,
21 description, web_short_description
22 from `tabItem`
23 where is_sales_item = 'Yes'
24 and docstatus = 0
25 and show_in_website = 1"""
26
27 # search term condition
28 if args.get('search'):
29 query += """
30 and (
31 web_short_description like %(search)s or
32 web_long_description like %(search)s or
33 description like %(search)s or
34 item_name like %(search)s or
35 name like %(search)s
36 )"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053037 args['search'] = "%" + cstr(args.get('search')) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053038
39 # product group condition
40 if args.get('product_group') and args.get('product_group') != 'All Products':
41 query += """
42 and item_group = %(product_group)s"""
Anand Doshie47def82012-07-09 15:56:12 +053043
44 # order by
45 query += """
46 order by item_name asc, name asc"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053047
48 from webnotes.widgets.query_builder import add_limit_to_query
49 query, args = add_limit_to_query(query, args)
50
51 return webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053052
53@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053054def get_product_category_list(args=None):
55 """
56 args = {
57 'limit_start': 0,
58 'limit_page_length': 5,
59 }
60 """
Anand Doshie47def82012-07-09 15:56:12 +053061 import webnotes
62
Anand Doshi8c7e76b2012-07-11 18:40:57 +053063 if not args: args = webnotes.form_dict
64
65 query = """\
Anand Doshie47def82012-07-09 15:56:12 +053066 select count(name) as items, item_group
67 from `tabItem`
68 where is_sales_item = 'Yes'
69 and docstatus = 0
70 and show_in_website = 1
71 group by item_group
Anand Doshi8c7e76b2012-07-11 18:40:57 +053072 order by items desc"""
73
74 from webnotes.widgets.query_builder import add_limit_to_query
75 query, args = add_limit_to_query(query, args)
76
77
78 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053079
80 # add All Products link
81 total_count = sum((r.get('items') or 0 for r in result))
82 result = [{'items': total_count, 'item_group': 'All Products'}] + (result or [])
83
84 return result
85
86@webnotes.whitelist(allow_guest=True)
87def get_similar_product_list(args=None):
Anand Doshi8c7e76b2012-07-11 18:40:57 +053088 """
89 args = {
90 'limit_start': 0,
91 'limit_page_length': 5,
92 'product_name': '',
93 'product_group': '',
94 }
95 """
Anand Doshie47def82012-07-09 15:56:12 +053096 import webnotes
97
98 if not args: args = webnotes.form_dict
Anand Doshi8c7e76b2012-07-11 18:40:57 +053099
100 query = """\
Anand Doshie47def82012-07-09 15:56:12 +0530101 select name, item_name, page_name, website_image,
102 description, web_short_description
103 from `tabItem`
104 where is_sales_item = 'Yes'
105 and docstatus = 0
106 and show_in_website = 1
107 and name != %(product_name)s
108 and item_group = %(product_group)s
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530109 order by item_name"""
110
111 from webnotes.widgets.query_builder import add_limit_to_query
112 query, args = add_limit_to_query(query, args)
113
114 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +0530115
116 return result