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 | |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | from frappe.utils.bot import BotParser |
| 8 | |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 9 | |
| 10 | class FindItemBot(BotParser): |
| 11 | def get_reply(self): |
| 12 | if self.startswith('where is', 'find item', 'locate'): |
Rushabh Mehta | 3117b4b | 2016-03-29 11:53:07 +0530 | [diff] [blame] | 13 | if not frappe.has_permission('Warehouse'): |
| 14 | raise frappe.PermissionError |
| 15 | |
Rushabh Mehta | fe027b3 | 2016-03-28 13:21:43 +0530 | [diff] [blame] | 16 | 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 Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 28 | 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] | 29 | item[0], warehouse.name)) |
| 30 | found = True |
| 31 | |
| 32 | if not found: |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 33 | 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] | 34 | |
| 35 | return "\n\n".join(out) |
| 36 | |
| 37 | else: |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 38 | return _("Did not find any item called {0}").format(item) |