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 |
Rushabh Mehta | 5f1d57e | 2012-12-27 14:05:02 +0530 | [diff] [blame] | 5 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 6 | import webnotes |
Rushabh Mehta | 5f1d57e | 2012-12-27 14:05:02 +0530 | [diff] [blame] | 7 | from webnotes.utils import cstr |
Rushabh Mehta | 9db1a68 | 2013-04-02 10:41:37 +0530 | [diff] [blame] | 8 | from webnotes.webutils import build_html, delete_page_cache |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 10 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 11 | @webnotes.whitelist(allow_guest=True) |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 12 | def get_product_info(item_code): |
| 13 | """get product price / stock info""" |
| 14 | price_list = webnotes.conn.get_value("Item", item_code, "website_price_list") |
| 15 | warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse") |
| 16 | if warehouse: |
| 17 | in_stock = webnotes.conn.sql("""select actual_qty from tabBin where |
| 18 | item_code=%s and warehouse=%s""", (item_code, warehouse)) |
| 19 | if in_stock: |
| 20 | in_stock = in_stock[0][0] > 0 and 1 or 0 |
| 21 | else: |
| 22 | in_stock = -1 |
| 23 | return { |
| 24 | "price": price_list and webnotes.conn.sql("""select ref_rate, ref_currency from |
| 25 | `tabItem Price` where parent=%s and price_list_name=%s""", |
| 26 | (item_code, price_list), as_dict=1) or [], |
| 27 | "stock": in_stock |
| 28 | } |
| 29 | |
| 30 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | d8c3aa2 | 2013-02-11 19:33:33 +0530 | [diff] [blame] | 31 | def get_product_list(search=None, start=0, limit=10): |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 32 | # base query |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 33 | query = """select name, item_name, page_name, website_image, item_group, |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 34 | web_long_description as website_description |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 35 | from `tabItem` where docstatus = 0 and show_in_website = 1 """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 36 | |
| 37 | # search term condition |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 38 | if search: |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 39 | query += """and (web_long_description like %(search)s or |
| 40 | item_name like %(search)s or name like %(search)s)""" |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 41 | search = "%" + cstr(search) + "%" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 42 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 43 | # order by |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 44 | query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 45 | |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 46 | data = webnotes.conn.sql(query, { |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 47 | "search": search, |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 48 | }, as_dict=1) |
| 49 | |
| 50 | return [get_item_for_list_in_html(r) for r in data] |
| 51 | |
| 52 | |
| 53 | def get_product_list_for_group(product_group=None, start=0, limit=10): |
| 54 | child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)]) |
| 55 | |
| 56 | # base query |
| 57 | query = """select name, item_name, page_name, website_image, item_group, |
| 58 | web_long_description as website_description |
| 59 | from `tabItem` where docstatus = 0 and show_in_website = 1 |
| 60 | and (item_group in (%s) |
| 61 | or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups) |
| 62 | |
| 63 | query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit) |
| 64 | |
| 65 | data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1) |
| 66 | |
| 67 | return [get_item_for_list_in_html(r) for r in data] |
| 68 | |
| 69 | def get_child_groups(item_group_name): |
| 70 | item_group = webnotes.doc("Item Group", item_group_name) |
| 71 | return webnotes.conn.sql("""select name |
Anand Doshi | 9e830ec | 2013-02-08 20:08:46 +0530 | [diff] [blame] | 72 | from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s |
| 73 | and show_in_website = 1""", item_group.fields) |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 74 | |
| 75 | def get_group_item_count(item_group): |
| 76 | child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)]) |
| 77 | return webnotes.conn.sql("""select count(*) from `tabItem` |
| 78 | where docstatus = 0 and show_in_website = 1 |
| 79 | and (item_group in (%s) |
| 80 | or name in (select parent from `tabWebsite Item Group` |
| 81 | where item_group in (%s))) """ % (child_groups, child_groups))[0][0] |
| 82 | |
| 83 | def get_item_for_list_in_html(r): |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 84 | scrub_item_for_list(r) |
Rushabh Mehta | 3023a8f | 2013-04-03 16:34:23 +0530 | [diff] [blame] | 85 | r.template = "app/website/templates/html/product_in_list.html" |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 86 | return build_html(r) |
| 87 | |
| 88 | def scrub_item_for_list(r): |
| 89 | if not r.website_description: |
| 90 | r.website_description = "No description given" |
Anand Doshi | e1ad443 | 2013-02-11 20:28:56 +0530 | [diff] [blame] | 91 | if len(r.website_description.split(" ")) > 24: |
| 92 | r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..." |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 93 | |
| 94 | def get_parent_item_groups(item_group_name): |
| 95 | item_group = webnotes.doc("Item Group", item_group_name) |
| 96 | return webnotes.conn.sql("""select name, page_name from `tabItem Group` |
| 97 | where lft <= %s and rgt >= %s |
| 98 | and ifnull(show_in_website,0)=1 |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 99 | order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True) |
| 100 | |
| 101 | def invalidate_cache_for(item_group): |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 102 | for i in get_parent_item_groups(item_group): |
Nabin Hait | 833d7a6 | 2012-12-30 19:56:57 +0530 | [diff] [blame] | 103 | if i.page_name: |
| 104 | delete_page_cache(i.page_name) |