blob: 1c0d6d9995c16b58054fe1fb83060a1a9c27750a [file] [log] [blame]
Anand Doshi486f9df2012-07-19 13:40:31 +05301from __future__ import unicode_literals
Anand Doshie47def82012-07-09 15:56:12 +05302import webnotes
3
4@webnotes.whitelist(allow_guest=True)
5def get_product_list(args=None):
6 """
7 args = {
8 'limit_start': 0,
9 'limit_page_length': 20,
10 'search': '',
11 'product_group': '',
12 }
13 """
14 import webnotes
15 from webnotes.utils import cstr, cint
16
17 if not args: args = webnotes.form_dict
18
Anand Doshie47def82012-07-09 15:56:12 +053019 # base query
20 query = """\
21 select name, item_name, page_name, website_image,
22 description, web_short_description
23 from `tabItem`
24 where is_sales_item = 'Yes'
25 and docstatus = 0
26 and show_in_website = 1"""
27
28 # search term condition
29 if args.get('search'):
30 query += """
31 and (
32 web_short_description like %(search)s or
33 web_long_description like %(search)s or
34 description like %(search)s or
35 item_name like %(search)s or
36 name like %(search)s
37 )"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053038 args['search'] = "%" + cstr(args.get('search')) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053039
40 # product group condition
41 if args.get('product_group') and args.get('product_group') != 'All Products':
42 query += """
43 and item_group = %(product_group)s"""
Anand Doshie47def82012-07-09 15:56:12 +053044
45 # order by
46 query += """
47 order by item_name asc, name asc"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053048
49 from webnotes.widgets.query_builder import add_limit_to_query
50 query, args = add_limit_to_query(query, args)
51
52 return webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053053
54@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053055def get_product_category_list(args=None):
56 """
57 args = {
58 'limit_start': 0,
59 'limit_page_length': 5,
60 }
61 """
Anand Doshie47def82012-07-09 15:56:12 +053062 import webnotes
63
Anand Doshi8c7e76b2012-07-11 18:40:57 +053064 if not args: args = webnotes.form_dict
65
66 query = """\
Anand Doshie47def82012-07-09 15:56:12 +053067 select count(name) as items, item_group
68 from `tabItem`
69 where is_sales_item = 'Yes'
70 and docstatus = 0
71 and show_in_website = 1
72 group by item_group
Anand Doshi8c7e76b2012-07-11 18:40:57 +053073 order by items desc"""
74
75 from webnotes.widgets.query_builder import add_limit_to_query
76 query, args = add_limit_to_query(query, args)
77
78
79 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053080
81 # add All Products link
82 total_count = sum((r.get('items') or 0 for r in result))
83 result = [{'items': total_count, 'item_group': 'All Products'}] + (result or [])
84
85 return result
86
87@webnotes.whitelist(allow_guest=True)
88def get_similar_product_list(args=None):
Anand Doshi8c7e76b2012-07-11 18:40:57 +053089 """
90 args = {
91 'limit_start': 0,
92 'limit_page_length': 5,
93 'product_name': '',
94 'product_group': '',
95 }
96 """
Anand Doshie47def82012-07-09 15:56:12 +053097 import webnotes
98
99 if not args: args = webnotes.form_dict
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530100
101 query = """\
Anand Doshie47def82012-07-09 15:56:12 +0530102 select name, item_name, page_name, website_image,
103 description, web_short_description
104 from `tabItem`
105 where is_sales_item = 'Yes'
106 and docstatus = 0
107 and show_in_website = 1
108 and name != %(product_name)s
109 and item_group = %(product_group)s
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530110 order by item_name"""
111
112 from webnotes.widgets.query_builder import add_limit_to_query
113 query, args = add_limit_to_query(query, args)
114
115 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +0530116
117 return result