blob: 5a8d84a2c5cb7e9aae8a7c49ee48ce0d9374802f [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 (
6 get_sre_reserved_qty_for_item_and_warehouse as get_reserved_stock,
7)
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-r2d8363a2023-09-02 11:02:24 +053064 sre_reserved_stock_details = get_reserved_stock(item_code, warehouse)
Marica5467d7c2021-04-09 11:56:49 +053065 precision = cint(frappe.db.get_single_value("System Settings", "float_precision"))
Anurag Mishraabf974d2021-03-17 18:40:21 +053066
deepeshgarg007942c8942019-02-19 19:51:23 +053067 for item in items:
Ankush Menat494bd9e2022-03-28 18:52:46 +053068 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 Menatd0eac472023-09-18 21:49:52 +053078 "reserved_stock": sre_reserved_stock_details,
Ankush Menat494bd9e2022-03-28 18:52:46 +053079 }
80 )
deepeshgarg007942c8942019-02-19 19:51:23 +053081 return items