Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 1 | import webnotes |
| 2 | |
| 3 | @webnotes.whitelist(allow_guest=True) |
| 4 | def 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 18 | # 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 37 | args['search'] = "%" + cstr(args.get('search')) + "%" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 38 | |
| 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 43 | |
| 44 | # order by |
| 45 | query += """ |
| 46 | order by item_name asc, name asc""" |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 47 | |
| 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 52 | |
| 53 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 54 | def get_product_category_list(args=None): |
| 55 | """ |
| 56 | args = { |
| 57 | 'limit_start': 0, |
| 58 | 'limit_page_length': 5, |
| 59 | } |
| 60 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 61 | import webnotes |
| 62 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 63 | if not args: args = webnotes.form_dict |
| 64 | |
| 65 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 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 |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 72 | 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 79 | |
| 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) |
| 87 | def get_similar_product_list(args=None): |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 88 | """ |
| 89 | args = { |
| 90 | 'limit_start': 0, |
| 91 | 'limit_page_length': 5, |
| 92 | 'product_name': '', |
| 93 | 'product_group': '', |
| 94 | } |
| 95 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 96 | import webnotes |
| 97 | |
| 98 | if not args: args = webnotes.form_dict |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 99 | |
| 100 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 101 | 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 109 | 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 115 | |
| 116 | return result |