Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 1 | # Copyright (c) 2012 Web Notes Technologies Pvt Ltd. |
| 2 | # License: GNU General Public License (v3). For more information see license.txt |
| 3 | |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 4 | from __future__ import unicode_literals |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 5 | import webnotes |
| 6 | |
| 7 | @webnotes.whitelist(allow_guest=True) |
| 8 | def 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 Doshi | 0fd99e7 | 2012-11-30 16:38:04 +0530 | [diff] [blame] | 18 | from webnotes.utils import cstr |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 19 | |
| 20 | if not args: args = webnotes.form_dict |
| 21 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 22 | # 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 41 | args['search'] = "%" + cstr(args.get('search')) + "%" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 42 | |
| 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 47 | |
| 48 | # order by |
| 49 | query += """ |
| 50 | order by item_name asc, name asc""" |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 51 | |
| 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 56 | |
| 57 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 58 | def get_product_category_list(args=None): |
| 59 | """ |
| 60 | args = { |
| 61 | 'limit_start': 0, |
| 62 | 'limit_page_length': 5, |
| 63 | } |
| 64 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 65 | import webnotes |
| 66 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 67 | if not args: args = webnotes.form_dict |
| 68 | |
| 69 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 70 | 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 76 | 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 83 | |
| 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) |
| 91 | def get_similar_product_list(args=None): |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 92 | """ |
| 93 | args = { |
| 94 | 'limit_start': 0, |
| 95 | 'limit_page_length': 5, |
| 96 | 'product_name': '', |
| 97 | 'product_group': '', |
| 98 | } |
| 99 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 100 | import webnotes |
| 101 | |
| 102 | if not args: args = webnotes.form_dict |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 103 | |
| 104 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 105 | 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 Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 113 | 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 Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 119 | |
| 120 | return result |