From fe027b34a91910cfadcd5750774622c68f12f9f1 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 28 Mar 2016 13:21:43 +0530 Subject: [PATCH] [bot] first bot --- erpnext/hooks.py | 4 ++++ erpnext/utilities/bot.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 erpnext/utilities/bot.py diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 6e86d84b17..f44daf37aa 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -143,3 +143,7 @@ default_mail_footer = """
get_translated_dict = { ("doctype", "Global Defaults"): "frappe.geo.country_info.get_translated_dict" } + +bot_parsers = [ + 'erpnext.utilities.bot.FindItemBot', +] \ No newline at end of file diff --git a/erpnext/utilities/bot.py b/erpnext/utilities/bot.py new file mode 100644 index 0000000000..e1f3d00157 --- /dev/null +++ b/erpnext/utilities/bot.py @@ -0,0 +1,36 @@ +# Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and Contributors +# See license.txt + +from __future__ import unicode_literals + +from frappe.utils.bot import BotParser + +import frappe +from frappe import _ + +class FindItemBot(BotParser): + def get_reply(self): + if self.startswith('where is', 'find item', 'locate'): + item = '%{0}%'.format(self.strip_words(self.query, 'where is', 'find item', 'locate')) + items = frappe.db.sql('''select name from `tabItem` where item_code like %(txt)s + or item_name like %(txt)s or description like %(txt)s''', dict(txt=item)) + + if items: + out = [] + warehouses = frappe.get_all("Warehouse") + for item in items: + found = False + for warehouse in warehouses: + qty = frappe.db.get_value("Bin", {'item_code': item[0], 'warehouse': warehouse.name}, 'actual_qty') + if qty: + out.append(_('{0} units of [{1}](#Form/Item/{1}) found in [{2}](#Form/Warehouse/{2})').format(qty, + item[0], warehouse.name)) + found = True + + if not found: + out.append(_('[{0}](#Form/Item/{0}) is out of stock').format(item[0])) + + return "\n\n".join(out) + + else: + return _("Did not find any item called {0}".format(item)) \ No newline at end of file