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