blob: 485b0b3383f95e03dffcafaa03218034dfd50e40 [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:
Rushabh Mehta542bc012020-11-18 15:00:34 +053029 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 +053030 item[0], warehouse.name))
31 found = True
32
33 if not found:
Rushabh Mehta542bc012020-11-18 15:00:34 +053034 out.append(_('[{0}](/app/Form/Item/{0}) is out of stock').format(item[0]))
Rushabh Mehtafe027b32016-03-28 13:21:43 +053035
36 return "\n\n".join(out)
37
38 else:
Ankush Menat4551d7d2021-08-19 13:41:10 +053039 return _("Did not find any item called {0}").format(item)