Rushabh Mehta | 621283c | 2016-04-21 19:00:34 +0530 | [diff] [blame] | 1 | import frappe |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 2 | from frappe.model.db_query import DatabaseQuery |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | from frappe.utils import cint, flt |
| 4 | |
Rushabh Mehta | 621283c | 2016-04-21 19:00:34 +0530 | [diff] [blame] | 5 | |
| 6 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 7 | def get_data( |
| 8 | item_code=None, warehouse=None, item_group=None, start=0, sort_by="actual_qty", sort_order="desc" |
| 9 | ): |
| 10 | """Return data to render the item dashboard""" |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 11 | filters = [] |
Rushabh Mehta | 621283c | 2016-04-21 19:00:34 +0530 | [diff] [blame] | 12 | if item_code: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | filters.append(["item_code", "=", item_code]) |
Rushabh Mehta | 621283c | 2016-04-21 19:00:34 +0530 | [diff] [blame] | 14 | if warehouse: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 15 | filters.append(["warehouse", "=", warehouse]) |
Rushabh Mehta | 057db06 | 2016-11-08 12:40:04 +0530 | [diff] [blame] | 16 | if item_group: |
Nabin Hait | f3f4ce9 | 2019-02-01 11:59:18 +0530 | [diff] [blame] | 17 | lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"]) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 18 | items = frappe.db.sql_list( |
| 19 | """ |
Nabin Hait | f3f4ce9 | 2019-02-01 11:59:18 +0530 | [diff] [blame] | 20 | select i.name from `tabItem` i |
| 21 | where exists(select name from `tabItem Group` |
| 22 | where name=i.item_group and lft >=%s and rgt<=%s) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | """, |
| 24 | (lft, rgt), |
| 25 | ) |
| 26 | filters.append(["item_code", "in", items]) |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 27 | try: |
| 28 | # check if user has any restrictions based on user permissions on warehouse |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 29 | if DatabaseQuery("Warehouse", user=frappe.session.user).build_match_conditions(): |
| 30 | filters.append(["warehouse", "in", [w.name for w in frappe.get_list("Warehouse")]]) |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 31 | except frappe.PermissionError: |
Suraj Shetty | 7a45887 | 2019-01-24 16:26:48 +0530 | [diff] [blame] | 32 | # user does not have access on warehouse |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 33 | return [] |
Rushabh Mehta | 057db06 | 2016-11-08 12:40:04 +0530 | [diff] [blame] | 34 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | items = frappe.db.get_all( |
| 36 | "Bin", |
| 37 | fields=[ |
| 38 | "item_code", |
| 39 | "warehouse", |
| 40 | "projected_qty", |
| 41 | "reserved_qty", |
| 42 | "reserved_qty_for_production", |
| 43 | "reserved_qty_for_sub_contract", |
| 44 | "actual_qty", |
| 45 | "valuation_rate", |
| 46 | ], |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 47 | or_filters={ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | "projected_qty": ["!=", 0], |
| 49 | "reserved_qty": ["!=", 0], |
| 50 | "reserved_qty_for_production": ["!=", 0], |
| 51 | "reserved_qty_for_sub_contract": ["!=", 0], |
| 52 | "actual_qty": ["!=", 0], |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 53 | }, |
| 54 | filters=filters, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | order_by=sort_by + " " + sort_order, |
Suraj Shetty | c165838 | 2019-01-24 15:45:46 +0530 | [diff] [blame] | 56 | limit_start=start, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | limit_page_length=21, |
| 58 | ) |
deepeshgarg007 | 942c894 | 2019-02-19 19:51:23 +0530 | [diff] [blame] | 59 | |
Marica | 5467d7c | 2021-04-09 11:56:49 +0530 | [diff] [blame] | 60 | precision = cint(frappe.db.get_single_value("System Settings", "float_precision")) |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 61 | |
deepeshgarg007 | 942c894 | 2019-02-19 19:51:23 +0530 | [diff] [blame] | 62 | for item in items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 63 | item.update( |
| 64 | { |
| 65 | "item_name": frappe.get_cached_value("Item", item.item_code, "item_name"), |
| 66 | "disable_quick_entry": frappe.get_cached_value("Item", item.item_code, "has_batch_no") |
| 67 | or frappe.get_cached_value("Item", item.item_code, "has_serial_no"), |
| 68 | "projected_qty": flt(item.projected_qty, precision), |
| 69 | "reserved_qty": flt(item.reserved_qty, precision), |
| 70 | "reserved_qty_for_production": flt(item.reserved_qty_for_production, precision), |
| 71 | "reserved_qty_for_sub_contract": flt(item.reserved_qty_for_sub_contract, precision), |
| 72 | "actual_qty": flt(item.actual_qty, precision), |
| 73 | } |
| 74 | ) |
deepeshgarg007 | 942c894 | 2019-02-19 19:51:23 +0530 | [diff] [blame] | 75 | return items |