2020-11-26 05:15:44 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.model.db_query import DatabaseQuery
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.utils import flt, nowdate
|
|
|
|
|
2020-11-26 05:15:44 +00:00
|
|
|
from erpnext.stock.utils import get_stock_balance
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-11-26 05:15:44 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_data(
|
|
|
|
item_code=None,
|
|
|
|
warehouse=None,
|
|
|
|
parent_warehouse=None,
|
|
|
|
company=None,
|
|
|
|
start=0,
|
|
|
|
sort_by="stock_capacity",
|
|
|
|
sort_order="desc",
|
|
|
|
):
|
2020-11-26 05:15:44 +00:00
|
|
|
"""Return data to render the warehouse capacity dashboard."""
|
|
|
|
filters = get_filters(item_code, warehouse, parent_warehouse, company)
|
|
|
|
|
|
|
|
no_permission, filters = get_warehouse_filter_based_on_permissions(filters)
|
|
|
|
if no_permission:
|
|
|
|
return []
|
|
|
|
|
|
|
|
capacity_data = get_warehouse_capacity_data(filters, start)
|
|
|
|
|
|
|
|
asc_desc = -1 if sort_order == "desc" else 1
|
2022-03-28 13:22:46 +00:00
|
|
|
capacity_data = sorted(capacity_data, key=lambda i: (i[sort_by] * asc_desc))
|
2020-11-26 05:15:44 +00:00
|
|
|
|
|
|
|
return capacity_data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
def get_filters(item_code=None, warehouse=None, parent_warehouse=None, company=None):
|
|
|
|
filters = [["disable", "=", 0]]
|
2020-11-26 05:15:44 +00:00
|
|
|
if item_code:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.append(["item_code", "=", item_code])
|
2020-11-26 05:15:44 +00:00
|
|
|
if warehouse:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.append(["warehouse", "=", warehouse])
|
2020-11-26 05:15:44 +00:00
|
|
|
if company:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters.append(["company", "=", company])
|
2020-11-26 05:15:44 +00:00
|
|
|
if parent_warehouse:
|
|
|
|
lft, rgt = frappe.db.get_value("Warehouse", parent_warehouse, ["lft", "rgt"])
|
2022-03-28 13:22:46 +00:00
|
|
|
warehouses = frappe.db.sql_list(
|
|
|
|
"""
|
2020-11-26 05:15:44 +00:00
|
|
|
select name from `tabWarehouse`
|
|
|
|
where lft >=%s and rgt<=%s
|
2022-03-28 13:22:46 +00:00
|
|
|
""",
|
|
|
|
(lft, rgt),
|
|
|
|
)
|
|
|
|
filters.append(["warehouse", "in", warehouses])
|
2020-11-26 05:15:44 +00:00
|
|
|
return filters
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-26 05:15:44 +00:00
|
|
|
def get_warehouse_filter_based_on_permissions(filters):
|
|
|
|
try:
|
|
|
|
# check if user has any restrictions based on user permissions on warehouse
|
2022-03-28 13:22:46 +00:00
|
|
|
if DatabaseQuery("Warehouse", user=frappe.session.user).build_match_conditions():
|
|
|
|
filters.append(["warehouse", "in", [w.name for w in frappe.get_list("Warehouse")]])
|
2020-11-26 05:15:44 +00:00
|
|
|
return False, filters
|
|
|
|
except frappe.PermissionError:
|
|
|
|
# user does not have access on warehouse
|
|
|
|
return True, []
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-26 05:15:44 +00:00
|
|
|
def get_warehouse_capacity_data(filters, start):
|
2022-03-28 13:22:46 +00:00
|
|
|
capacity_data = frappe.db.get_all(
|
|
|
|
"Putaway Rule",
|
|
|
|
fields=["item_code", "warehouse", "stock_capacity", "company"],
|
2020-11-26 05:15:44 +00:00
|
|
|
filters=filters,
|
|
|
|
limit_start=start,
|
2022-03-28 13:22:46 +00:00
|
|
|
limit_page_length="11",
|
2020-11-26 05:15:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for entry in capacity_data:
|
|
|
|
balance_qty = get_stock_balance(entry.item_code, entry.warehouse, nowdate()) or 0
|
2022-03-28 13:22:46 +00:00
|
|
|
entry.update(
|
|
|
|
{
|
|
|
|
"actual_qty": balance_qty,
|
|
|
|
"percent_occupied": flt((flt(balance_qty) / flt(entry.stock_capacity)) * 100, 0),
|
|
|
|
}
|
|
|
|
)
|
2020-11-26 05:15:44 +00:00
|
|
|
|
2021-08-19 08:11:10 +00:00
|
|
|
return capacity_data
|