Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 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 | 7c2312a | 2013-09-10 12:51:52 +0530 | [diff] [blame] | 7 | from webnotes.utils import cstr, cint, fmt_money, get_base_path |
| 8 | from webnotes.webutils import delete_page_cache |
Rushabh Mehta | c59c4e0 | 2013-09-09 12:17:45 +0530 | [diff] [blame] | 9 | from selling.utils.cart import _get_cart_quotation |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 10 | |
| 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""" |
Anand Doshi | 3d21463 | 2013-09-06 15:53:04 +0530 | [diff] [blame] | 14 | if not cint(webnotes.conn.get_default("shopping_cart_enabled")): |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 15 | return {} |
| 16 | |
| 17 | cart_quotation = _get_cart_quotation() |
| 18 | |
Anand Doshi | 365c241 | 2013-12-02 16:08:26 +0530 | [diff] [blame] | 19 | price_list = webnotes.local.request.cookies.get("selling_price_list") |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 21 | warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse") |
| 22 | if warehouse: |
| 23 | in_stock = webnotes.conn.sql("""select actual_qty from tabBin where |
| 24 | item_code=%s and warehouse=%s""", (item_code, warehouse)) |
| 25 | if in_stock: |
| 26 | in_stock = in_stock[0][0] > 0 and 1 or 0 |
| 27 | else: |
| 28 | in_stock = -1 |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 29 | |
Akhilesh Darjee | e82eee5 | 2014-01-20 16:09:45 +0530 | [diff] [blame] | 30 | price = price_list and webnotes.conn.sql("""select ip.ref_rate, ip.currency from |
| 31 | `tabItem Price` ip, `tabPrice List` pl where ip.price_list=pl.name and |
| 32 | ip.item_code=%s and ip.price_list=%s and pl.enabled=1""", |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 33 | (item_code, price_list), as_dict=1) or [] |
| 34 | |
| 35 | price = price and price[0] or None |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 36 | qty = 0 |
| 37 | |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 38 | if price: |
Anand Doshi | e122121 | 2013-09-18 17:28:06 +0530 | [diff] [blame] | 39 | price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["currency"]) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 40 | |
Anand Doshi | e122121 | 2013-09-18 17:28:06 +0530 | [diff] [blame] | 41 | price["currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \ |
| 42 | and (webnotes.conn.get_value("Currency", price.currency, "symbol") or price.currency) \ |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 43 | or "" |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 44 | |
| 45 | if webnotes.session.user != "Guest": |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 46 | item = cart_quotation.doclist.get({"item_code": item_code}) |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 47 | if item: |
| 48 | qty = item[0].qty |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 49 | |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 50 | return { |
Anand Doshi | 220ff30 | 2013-05-14 15:33:34 +0530 | [diff] [blame] | 51 | "price": price, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 52 | "stock": in_stock, |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 53 | "uom": webnotes.conn.get_value("Item", item_code, "stock_uom"), |
| 54 | "qty": qty |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @webnotes.whitelist(allow_guest=True) |
Anand Doshi | d8c3aa2 | 2013-02-11 19:33:33 +0530 | [diff] [blame] | 58 | def get_product_list(search=None, start=0, limit=10): |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 59 | # base query |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 60 | query = """select name, item_name, page_name, website_image, item_group, |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 61 | web_long_description as website_description |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 62 | from `tabItem` where docstatus = 0 and show_in_website = 1 """ |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 63 | |
| 64 | # search term condition |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 65 | if search: |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 66 | query += """and (web_long_description like %(search)s or |
| 67 | item_name like %(search)s or name like %(search)s)""" |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 68 | search = "%" + cstr(search) + "%" |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 69 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 70 | # order by |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 71 | query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit) |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 72 | |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 73 | data = webnotes.conn.sql(query, { |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 74 | "search": search, |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 75 | }, as_dict=1) |
| 76 | |
| 77 | return [get_item_for_list_in_html(r) for r in data] |
| 78 | |
| 79 | |
| 80 | def get_product_list_for_group(product_group=None, start=0, limit=10): |
| 81 | child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)]) |
| 82 | |
| 83 | # base query |
| 84 | query = """select name, item_name, page_name, website_image, item_group, |
| 85 | web_long_description as website_description |
| 86 | from `tabItem` where docstatus = 0 and show_in_website = 1 |
| 87 | and (item_group in (%s) |
| 88 | or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups) |
| 89 | |
| 90 | query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit) |
| 91 | |
| 92 | data = webnotes.conn.sql(query, {"product_group": product_group}, as_dict=1) |
| 93 | |
| 94 | return [get_item_for_list_in_html(r) for r in data] |
| 95 | |
| 96 | def get_child_groups(item_group_name): |
| 97 | item_group = webnotes.doc("Item Group", item_group_name) |
| 98 | return webnotes.conn.sql("""select name |
Anand Doshi | 9e830ec | 2013-02-08 20:08:46 +0530 | [diff] [blame] | 99 | from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s |
| 100 | and show_in_website = 1""", item_group.fields) |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 101 | |
| 102 | def get_group_item_count(item_group): |
| 103 | child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)]) |
| 104 | return webnotes.conn.sql("""select count(*) from `tabItem` |
| 105 | where docstatus = 0 and show_in_website = 1 |
| 106 | and (item_group in (%s) |
| 107 | or name in (select parent from `tabWebsite Item Group` |
| 108 | where item_group in (%s))) """ % (child_groups, child_groups))[0][0] |
| 109 | |
Rushabh Mehta | 7c2312a | 2013-09-10 12:51:52 +0530 | [diff] [blame] | 110 | def get_item_for_list_in_html(context): |
| 111 | from jinja2 import Environment, FileSystemLoader |
| 112 | scrub_item_for_list(context) |
| 113 | jenv = Environment(loader = FileSystemLoader(get_base_path())) |
| 114 | template = jenv.get_template("app/stock/doctype/item/templates/includes/product_in_grid.html") |
| 115 | return template.render(context) |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 116 | |
| 117 | def scrub_item_for_list(r): |
| 118 | if not r.website_description: |
| 119 | r.website_description = "No description given" |
Anand Doshi | e1ad443 | 2013-02-11 20:28:56 +0530 | [diff] [blame] | 120 | if len(r.website_description.split(" ")) > 24: |
| 121 | r.website_description = " ".join(r.website_description.split(" ")[:24]) + "..." |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 122 | |
| 123 | def get_parent_item_groups(item_group_name): |
| 124 | item_group = webnotes.doc("Item Group", item_group_name) |
| 125 | return webnotes.conn.sql("""select name, page_name from `tabItem Group` |
| 126 | where lft <= %s and rgt >= %s |
| 127 | and ifnull(show_in_website,0)=1 |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 128 | order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True) |
| 129 | |
| 130 | def invalidate_cache_for(item_group): |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 131 | for i in get_parent_item_groups(item_group): |
Nabin Hait | 833d7a6 | 2012-12-30 19:56:57 +0530 | [diff] [blame] | 132 | if i.page_name: |
| 133 | delete_page_cache(i.page_name) |