blob: 23e1dd4fa5f7e462dc924d8834fd852734662673 [file] [log] [blame]
Rushabh Mehtafe027b32016-03-28 13:21:43 +05301# Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and Contributors
2# See license.txt
3
4from __future__ import unicode_literals
5
6from frappe.utils.bot import BotParser
7
8import frappe
9from frappe import _
10
11class FindItemBot(BotParser):
12 def get_reply(self):
13 if self.startswith('where is', 'find item', 'locate'):
Rushabh Mehta3117b4b2016-03-29 11:53:07 +053014 if not frappe.has_permission('Warehouse'):
15 raise frappe.PermissionError
16
Rushabh Mehtafe027b32016-03-28 13:21:43 +053017 item = '%{0}%'.format(self.strip_words(self.query, 'where is', 'find item', 'locate'))
18 items = frappe.db.sql('''select name from `tabItem` where item_code like %(txt)s
19 or item_name like %(txt)s or description like %(txt)s''', dict(txt=item))
20
21 if items:
22 out = []
23 warehouses = frappe.get_all("Warehouse")
24 for item in items:
25 found = False
26 for warehouse in warehouses:
27 qty = frappe.db.get_value("Bin", {'item_code': item[0], 'warehouse': warehouse.name}, 'actual_qty')
28 if qty:
29 out.append(_('{0} units of [{1}](#Form/Item/{1}) found in [{2}](#Form/Warehouse/{2})').format(qty,
30 item[0], warehouse.name))
31 found = True
32
33 if not found:
34 out.append(_('[{0}](#Form/Item/{0}) is out of stock').format(item[0]))
35
36 return "\n\n".join(out)
37
38 else:
39 return _("Did not find any item called {0}".format(item))