blob: 6bafc2b436ba73e6b0e6fc22df43207ada397f46 [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)
8def get_product_list(args=None):
9 """
10 args = {
11 'limit_start': 0,
12 'limit_page_length': 20,
13 'search': '',
14 'product_group': '',
15 }
16 """
17 import webnotes
Anand Doshi0fd99e72012-11-30 16:38:04 +053018 from webnotes.utils import cstr
Anand Doshie47def82012-07-09 15:56:12 +053019
20 if not args: args = webnotes.form_dict
21
Anand Doshie47def82012-07-09 15:56:12 +053022 # base query
23 query = """\
24 select name, item_name, page_name, website_image,
25 description, web_short_description
26 from `tabItem`
27 where is_sales_item = 'Yes'
28 and docstatus = 0
29 and show_in_website = 1"""
30
31 # search term condition
32 if args.get('search'):
33 query += """
34 and (
35 web_short_description like %(search)s or
36 web_long_description like %(search)s or
37 description like %(search)s or
38 item_name like %(search)s or
39 name like %(search)s
40 )"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053041 args['search'] = "%" + cstr(args.get('search')) + "%"
Anand Doshie47def82012-07-09 15:56:12 +053042
43 # product group condition
44 if args.get('product_group') and args.get('product_group') != 'All Products':
45 query += """
46 and item_group = %(product_group)s"""
Anand Doshie47def82012-07-09 15:56:12 +053047
48 # order by
49 query += """
50 order by item_name asc, name asc"""
Anand Doshi8c7e76b2012-07-11 18:40:57 +053051
52 from webnotes.widgets.query_builder import add_limit_to_query
53 query, args = add_limit_to_query(query, args)
54
55 return webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053056
57@webnotes.whitelist(allow_guest=True)
Anand Doshi8c7e76b2012-07-11 18:40:57 +053058def get_product_category_list(args=None):
59 """
60 args = {
61 'limit_start': 0,
62 'limit_page_length': 5,
63 }
64 """
Anand Doshie47def82012-07-09 15:56:12 +053065 import webnotes
66
Anand Doshi8c7e76b2012-07-11 18:40:57 +053067 if not args: args = webnotes.form_dict
68
69 query = """\
Anand Doshie47def82012-07-09 15:56:12 +053070 select count(name) as items, item_group
71 from `tabItem`
72 where is_sales_item = 'Yes'
73 and docstatus = 0
74 and show_in_website = 1
75 group by item_group
Anand Doshi8c7e76b2012-07-11 18:40:57 +053076 order by items desc"""
77
78 from webnotes.widgets.query_builder import add_limit_to_query
79 query, args = add_limit_to_query(query, args)
80
81
82 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +053083
84 # add All Products link
85 total_count = sum((r.get('items') or 0 for r in result))
86 result = [{'items': total_count, 'item_group': 'All Products'}] + (result or [])
87
88 return result
89
90@webnotes.whitelist(allow_guest=True)
91def get_similar_product_list(args=None):
Anand Doshi8c7e76b2012-07-11 18:40:57 +053092 """
93 args = {
94 'limit_start': 0,
95 'limit_page_length': 5,
96 'product_name': '',
97 'product_group': '',
98 }
99 """
Anand Doshie47def82012-07-09 15:56:12 +0530100 import webnotes
101
102 if not args: args = webnotes.form_dict
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530103
104 query = """\
Anand Doshie47def82012-07-09 15:56:12 +0530105 select name, item_name, page_name, website_image,
106 description, web_short_description
107 from `tabItem`
108 where is_sales_item = 'Yes'
109 and docstatus = 0
110 and show_in_website = 1
111 and name != %(product_name)s
112 and item_group = %(product_group)s
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530113 order by item_name"""
114
115 from webnotes.widgets.query_builder import add_limit_to_query
116 query, args = add_limit_to_query(query, args)
117
118 result = webnotes.conn.sql(query, args, as_dict=1)
Anand Doshie47def82012-07-09 15:56:12 +0530119
120 return result