blob: 5c2e576dd20ad414f9b0bdd8dcb1910317199535 [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):
Ankush Menat494bd9e2022-03-28 18:52:46 +053012 if self.startswith("where is", "find item", "locate"):
13 if not frappe.has_permission("Warehouse"):
Rushabh Mehta3117b4b2016-03-29 11:53:07 +053014 raise frappe.PermissionError
15
Ankush Menat494bd9e2022-03-28 18:52:46 +053016 item = "%{0}%".format(self.strip_words(self.query, "where is", "find item", "locate"))
17 items = frappe.db.sql(
18 """select name from `tabItem` where item_code like %(txt)s
19 or item_name like %(txt)s or description like %(txt)s""",
20 dict(txt=item),
21 )
Rushabh Mehtafe027b32016-03-28 13:21:43 +053022
23 if items:
24 out = []
25 warehouses = frappe.get_all("Warehouse")
26 for item in items:
27 found = False
28 for warehouse in warehouses:
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 qty = frappe.db.get_value(
30 "Bin", {"item_code": item[0], "warehouse": warehouse.name}, "actual_qty"
31 )
Rushabh Mehtafe027b32016-03-28 13:21:43 +053032 if qty:
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 out.append(
34 _("{0} units of [{1}](/app/Form/Item/{1}) found in [{2}](/app/Form/Warehouse/{2})").format(
35 qty, item[0], warehouse.name
36 )
37 )
Rushabh Mehtafe027b32016-03-28 13:21:43 +053038 found = True
39
40 if not found:
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 out.append(_("[{0}](/app/Form/Item/{0}) is out of stock").format(item[0]))
Rushabh Mehtafe027b32016-03-28 13:21:43 +053042
43 return "\n\n".join(out)
44
45 else:
Ankush Menat4551d7d2021-08-19 13:41:10 +053046 return _("Did not find any item called {0}").format(item)