Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 1 | # Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | from frappe.utils.bot import BotParser |
| 7 | |
| 8 | import frappe |
| 9 | from frappe import _ |
| 10 | |
| 11 | class FindItemBot(BotParser): |
| 12 | def get_reply(self): |
| 13 | if self.startswith('where is', 'find item', 'locate'): |
Rushabh Mehta | 3117b4b | 2016-03-29 11:53:07 +0530 | [diff] [blame] | 14 | if not frappe.has_permission('Warehouse'): |
| 15 | raise frappe.PermissionError |
| 16 | |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 17 | 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 Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 29 | out.append(_('{0} units of [{1}](/app/Form/Item/{1}) found in [{2}](/app/Form/Warehouse/{2})').format(qty, |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 30 | item[0], warehouse.name)) |
| 31 | found = True |
| 32 | |
| 33 | if not found: |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 34 | out.append(_('[{0}](/app/Form/Item/{0}) is out of stock').format(item[0])) |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 35 | |
| 36 | return "\n\n".join(out) |
| 37 | |
| 38 | else: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 39 | return _("Did not find any item called {0}").format(item) |