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) |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 8 | def get_product_info(item_code): |
| 9 | """get product price / stock info""" |
| 10 | price_list = webnotes.conn.get_value("Item", item_code, "website_price_list") |
| 11 | warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse") |
| 12 | if warehouse: |
| 13 | in_stock = webnotes.conn.sql("""select actual_qty from tabBin where |
| 14 | item_code=%s and warehouse=%s""", (item_code, warehouse)) |
| 15 | if in_stock: |
| 16 | in_stock = in_stock[0][0] > 0 and 1 or 0 |
| 17 | else: |
| 18 | in_stock = -1 |
| 19 | return { |
| 20 | "price": price_list and webnotes.conn.sql("""select ref_rate, ref_currency from |
| 21 | `tabItem Price` where parent=%s and price_list_name=%s""", |
| 22 | (item_code, price_list), as_dict=1) or [], |
| 23 | "stock": in_stock |
| 24 | } |
| 25 | |
| 26 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 27 | def get_product_list(args=None): |
| 28 | """ |
| 29 | args = { |
| 30 | 'limit_start': 0, |
| 31 | 'limit_page_length': 20, |
| 32 | 'search': '', |
| 33 | 'product_group': '', |
| 34 | } |
| 35 | """ |
| 36 | import webnotes |
Anand Doshi | 0fd99e7 | 2012-11-30 16:38:04 +0530 | [diff] [blame] | 37 | from webnotes.utils import cstr |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 38 | |
| 39 | if not args: args = webnotes.form_dict |
| 40 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 41 | # base query |
| 42 | query = """\ |
| 43 | select name, item_name, page_name, website_image, |
| 44 | description, web_short_description |
| 45 | from `tabItem` |
| 46 | where is_sales_item = 'Yes' |
| 47 | and docstatus = 0 |
| 48 | and show_in_website = 1""" |
| 49 | |
| 50 | # search term condition |
| 51 | if args.get('search'): |
| 52 | query += """ |
| 53 | and ( |
| 54 | web_short_description like %(search)s or |
| 55 | web_long_description like %(search)s or |
| 56 | description like %(search)s or |
| 57 | item_name like %(search)s or |
| 58 | name like %(search)s |
| 59 | )""" |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 60 | args['search'] = "%" + cstr(args.get('search')) + "%" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 61 | |
| 62 | # product group condition |
| 63 | if args.get('product_group') and args.get('product_group') != 'All Products': |
| 64 | query += """ |
| 65 | and item_group = %(product_group)s""" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 66 | |
| 67 | # order by |
| 68 | query += """ |
| 69 | order by item_name asc, name asc""" |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 70 | |
| 71 | from webnotes.widgets.query_builder import add_limit_to_query |
| 72 | query, args = add_limit_to_query(query, args) |
| 73 | |
| 74 | return webnotes.conn.sql(query, args, as_dict=1) |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 75 | |
| 76 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 77 | def get_product_category_list(args=None): |
| 78 | """ |
| 79 | args = { |
| 80 | 'limit_start': 0, |
| 81 | 'limit_page_length': 5, |
| 82 | } |
| 83 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 84 | import webnotes |
| 85 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 86 | if not args: args = webnotes.form_dict |
| 87 | |
| 88 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 89 | select count(name) as items, item_group |
| 90 | from `tabItem` |
| 91 | where is_sales_item = 'Yes' |
| 92 | and docstatus = 0 |
| 93 | and show_in_website = 1 |
| 94 | group by item_group |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 95 | order by items desc""" |
| 96 | |
| 97 | from webnotes.widgets.query_builder import add_limit_to_query |
| 98 | query, args = add_limit_to_query(query, args) |
| 99 | |
| 100 | |
| 101 | result = webnotes.conn.sql(query, args, as_dict=1) |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 102 | |
| 103 | # add All Products link |
| 104 | total_count = sum((r.get('items') or 0 for r in result)) |
| 105 | result = [{'items': total_count, 'item_group': 'All Products'}] + (result or []) |
| 106 | |
| 107 | return result |
| 108 | |
| 109 | @webnotes.whitelist(allow_guest=True) |
| 110 | def get_similar_product_list(args=None): |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 111 | """ |
| 112 | args = { |
| 113 | 'limit_start': 0, |
| 114 | 'limit_page_length': 5, |
| 115 | 'product_name': '', |
| 116 | 'product_group': '', |
| 117 | } |
| 118 | """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 119 | import webnotes |
| 120 | |
| 121 | if not args: args = webnotes.form_dict |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 122 | |
| 123 | query = """\ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 124 | select name, item_name, page_name, website_image, |
| 125 | description, web_short_description |
| 126 | from `tabItem` |
| 127 | where is_sales_item = 'Yes' |
| 128 | and docstatus = 0 |
| 129 | and show_in_website = 1 |
| 130 | and name != %(product_name)s |
| 131 | and item_group = %(product_group)s |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 132 | order by item_name""" |
| 133 | |
| 134 | from webnotes.widgets.query_builder import add_limit_to_query |
| 135 | query, args = add_limit_to_query(query, args) |
| 136 | |
| 137 | result = webnotes.conn.sql(query, args, as_dict=1) |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 138 | |
| 139 | return result |