blob: 87a350864f65e6798ffa9443ce96cd8e87508347 [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
Rushabh Mehtafe027b32016-03-28 13:21:43 +05304
Rushabh Mehtafe027b32016-03-28 13:21:43 +05305import frappe
6from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05307from frappe.utils.bot import BotParser
8
Rushabh Mehtafe027b32016-03-28 13:21:43 +05309
10class FindItemBot(BotParser):
11 def get_reply(self):
12 if self.startswith('where is', 'find item', 'locate'):
Rushabh Mehta3117b4b2016-03-29 11:53:07 +053013 if not frappe.has_permission('Warehouse'):
14 raise frappe.PermissionError
15
Rushabh Mehtafe027b32016-03-28 13:21:43 +053016 item = '%{0}%'.format(self.strip_words(self.query, 'where is', 'find item', 'locate'))
17 items = frappe.db.sql('''select name from `tabItem` where item_code like %(txt)s
18 or item_name like %(txt)s or description like %(txt)s''', dict(txt=item))
19
20 if items:
21 out = []
22 warehouses = frappe.get_all("Warehouse")
23 for item in items:
24 found = False
25 for warehouse in warehouses:
26 qty = frappe.db.get_value("Bin", {'item_code': item[0], 'warehouse': warehouse.name}, 'actual_qty')
27 if qty:
Rushabh Mehta542bc012020-11-18 15:00:34 +053028 out.append(_('{0} units of [{1}](/app/Form/Item/{1}) found in [{2}](/app/Form/Warehouse/{2})').format(qty,
Rushabh Mehtafe027b32016-03-28 13:21:43 +053029 item[0], warehouse.name))
30 found = True
31
32 if not found:
Rushabh Mehta542bc012020-11-18 15:00:34 +053033 out.append(_('[{0}](/app/Form/Item/{0}) is out of stock').format(item[0]))
Rushabh Mehtafe027b32016-03-28 13:21:43 +053034
35 return "\n\n".join(out)
36
37 else:
Ankush Menat4551d7d2021-08-19 13:41:10 +053038 return _("Did not find any item called {0}").format(item)