blob: 7c6603af7b6b74e8247b1928d9beedf49cfaef52 [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
18 # dict to be passed to sql function
19 query_args = {
20 'limit_start': cint(args.get('limit_start')),
21 'limit_page_length': cint(args.get('limit_page_length'))
22 }
23
24 # base query
25 query = """\
26 select name, item_name, page_name, website_image,
27 description, web_short_description
28 from `tabItem`
29 where is_sales_item = 'Yes'
30 and docstatus = 0
31 and show_in_website = 1"""
32
33 # search term condition
34 if args.get('search'):
35 query += """
36 and (
37 web_short_description like %(search)s or
38 web_long_description like %(search)s or
39 description like %(search)s or
40 item_name like %(search)s or
41 name like %(search)s
42 )"""
43 query_args['search'] = "%" + cstr(args.get('search')) + "%"
44
45 # product group condition
46 if args.get('product_group') and args.get('product_group') != 'All Products':
47 query += """
48 and item_group = %(product_group)s"""
49 query_args['product_group'] = args.get('product_group')
50
51 # order by
52 query += """
53 order by item_name asc, name asc"""
54
55 if args.get('limit_page_length'):
56 query += """
57 limit %(limit_start)s, %(limit_page_length)s"""
58
59 return webnotes.conn.sql(query, query_args, as_dict=1)
60
61@webnotes.whitelist(allow_guest=True)
62def get_product_category_list():
63 import webnotes
64
65 result = webnotes.conn.sql("""\
66 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
72 order by items desc""", as_dict=1)
73
74 # add All Products link
75 total_count = sum((r.get('items') or 0 for r in result))
76 result = [{'items': total_count, 'item_group': 'All Products'}] + (result or [])
77
78 return result
79
80@webnotes.whitelist(allow_guest=True)
81def get_similar_product_list(args=None):
82 import webnotes
83
84 if not args: args = webnotes.form_dict
85
86 result = webnotes.conn.sql("""\
87 select name, item_name, page_name, website_image,
88 description, web_short_description
89 from `tabItem`
90 where is_sales_item = 'Yes'
91 and docstatus = 0
92 and show_in_website = 1
93 and name != %(product_name)s
94 and item_group = %(product_group)s
95 order by item_name""", args, as_dict=1)
96
97 return result