blob: e6382688669b3e78b3647e9bfbfc99d0a2ffb325 [file] [log] [blame]
Rushabh Mehta621283c2016-04-21 19:00:34 +05301import frappe
Suraj Shettyc1658382019-01-24 15:45:46 +05302from frappe.model.db_query import DatabaseQuery
Chillar Anand915b3432021-09-02 16:44:59 +05303from frappe.utils import cint, flt
4
s-aga-r2d8363a2023-09-02 11:02:24 +05305from erpnext.stock.doctype.stock_reservation_entry.stock_reservation_entry import (
s-aga-rb6437e32023-09-21 14:03:00 +05306 get_sre_reserved_qty_for_items_and_warehouses as get_reserved_stock_details,
s-aga-r2d8363a2023-09-02 11:02:24 +05307)
8
Rushabh Mehta621283c2016-04-21 19:00:34 +05309
10@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +053011def 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 Shettyc1658382019-01-24 15:45:46 +053015 filters = []
Rushabh Mehta621283c2016-04-21 19:00:34 +053016 if item_code:
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 filters.append(["item_code", "=", item_code])
Rushabh Mehta621283c2016-04-21 19:00:34 +053018 if warehouse:
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 filters.append(["warehouse", "=", warehouse])
Rushabh Mehta057db062016-11-08 12:40:04 +053020 if item_group:
Nabin Haitf3f4ce92019-02-01 11:59:18 +053021 lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"])
Ankush Menat494bd9e2022-03-28 18:52:46 +053022 items = frappe.db.sql_list(
23 """
Nabin Haitf3f4ce92019-02-01 11:59:18 +053024 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 Menat494bd9e2022-03-28 18:52:46 +053027 """,
28 (lft, rgt),
29 )
30 filters.append(["item_code", "in", items])
Suraj Shettyc1658382019-01-24 15:45:46 +053031 try:
32 # check if user has any restrictions based on user permissions on warehouse
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 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 Shettyc1658382019-01-24 15:45:46 +053035 except frappe.PermissionError:
Suraj Shetty7a458872019-01-24 16:26:48 +053036 # user does not have access on warehouse
Suraj Shettyc1658382019-01-24 15:45:46 +053037 return []
Rushabh Mehta057db062016-11-08 12:40:04 +053038
Ankush Menat494bd9e2022-03-28 18:52:46 +053039 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 Shettyc1658382019-01-24 15:45:46 +053051 or_filters={
Ankush Menat494bd9e2022-03-28 18:52:46 +053052 "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 Shettyc1658382019-01-24 15:45:46 +053057 },
58 filters=filters,
Ankush Menat494bd9e2022-03-28 18:52:46 +053059 order_by=sort_by + " " + sort_order,
Suraj Shettyc1658382019-01-24 15:45:46 +053060 limit_start=start,
Ankush Menat494bd9e2022-03-28 18:52:46 +053061 limit_page_length=21,
62 )
deepeshgarg007942c8942019-02-19 19:51:23 +053063
s-aga-rb6437e32023-09-21 14:03:00 +053064 item_code_list = [item_code] if item_code else [i.item_code for i in items]
65 warehouse_list = [warehouse] if warehouse else [i.warehouse for i in items]
66
67 sre_reserved_stock_details = get_reserved_stock_details(item_code_list, warehouse_list)
Marica5467d7c2021-04-09 11:56:49 +053068 precision = cint(frappe.db.get_single_value("System Settings", "float_precision"))
Anurag Mishraabf974d2021-03-17 18:40:21 +053069
deepeshgarg007942c8942019-02-19 19:51:23 +053070 for item in items:
Ankush Menat494bd9e2022-03-28 18:52:46 +053071 item.update(
72 {
73 "item_name": frappe.get_cached_value("Item", item.item_code, "item_name"),
74 "disable_quick_entry": frappe.get_cached_value("Item", item.item_code, "has_batch_no")
75 or frappe.get_cached_value("Item", item.item_code, "has_serial_no"),
76 "projected_qty": flt(item.projected_qty, precision),
77 "reserved_qty": flt(item.reserved_qty, precision),
78 "reserved_qty_for_production": flt(item.reserved_qty_for_production, precision),
79 "reserved_qty_for_sub_contract": flt(item.reserved_qty_for_sub_contract, precision),
80 "actual_qty": flt(item.actual_qty, precision),
s-aga-rb6437e32023-09-21 14:03:00 +053081 "reserved_stock": flt(sre_reserved_stock_details.get((item.item_code, item.warehouse))),
Ankush Menat494bd9e2022-03-28 18:52:46 +053082 }
83 )
s-aga-rb6437e32023-09-21 14:03:00 +053084
deepeshgarg007942c8942019-02-19 19:51:23 +053085 return items