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