2016-04-21 13:30:34 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import frappe
|
2019-01-24 10:15:46 +00:00
|
|
|
from frappe.model.db_query import DatabaseQuery
|
2021-04-09 06:25:44 +00:00
|
|
|
from frappe.utils import flt, cint
|
2016-04-21 13:30:34 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2016-11-08 07:10:04 +00:00
|
|
|
def get_data(item_code=None, warehouse=None, item_group=None,
|
|
|
|
start=0, sort_by='actual_qty', sort_order='desc'):
|
|
|
|
'''Return data to render the item dashboard'''
|
2019-01-24 10:15:46 +00:00
|
|
|
filters = []
|
2016-04-21 13:30:34 +00:00
|
|
|
if item_code:
|
2019-01-24 10:15:46 +00:00
|
|
|
filters.append(['item_code', '=', item_code])
|
2016-04-21 13:30:34 +00:00
|
|
|
if warehouse:
|
2019-01-24 10:15:46 +00:00
|
|
|
filters.append(['warehouse', '=', warehouse])
|
2016-11-08 07:10:04 +00:00
|
|
|
if item_group:
|
2019-02-01 06:29:18 +00:00
|
|
|
lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"])
|
|
|
|
items = frappe.db.sql_list("""
|
|
|
|
select i.name from `tabItem` i
|
|
|
|
where exists(select name from `tabItem Group`
|
|
|
|
where name=i.item_group and lft >=%s and rgt<=%s)
|
|
|
|
""", (lft, rgt))
|
|
|
|
filters.append(['item_code', 'in', items])
|
2019-01-24 10:15:46 +00:00
|
|
|
try:
|
|
|
|
# check if user has any restrictions based on user permissions on warehouse
|
|
|
|
if DatabaseQuery('Warehouse', user=frappe.session.user).build_match_conditions():
|
|
|
|
filters.append(['warehouse', 'in', [w.name for w in frappe.get_list('Warehouse')]])
|
|
|
|
except frappe.PermissionError:
|
2019-01-24 10:56:48 +00:00
|
|
|
# user does not have access on warehouse
|
2019-01-24 10:15:46 +00:00
|
|
|
return []
|
2016-11-08 07:10:04 +00:00
|
|
|
|
2019-02-19 14:21:23 +00:00
|
|
|
items = frappe.db.get_all('Bin', fields=['item_code', 'warehouse', 'projected_qty',
|
2019-01-24 10:48:43 +00:00
|
|
|
'reserved_qty', 'reserved_qty_for_production', 'reserved_qty_for_sub_contract', 'actual_qty', 'valuation_rate'],
|
2019-01-24 10:15:46 +00:00
|
|
|
or_filters={
|
|
|
|
'projected_qty': ['!=', 0],
|
|
|
|
'reserved_qty': ['!=', 0],
|
|
|
|
'reserved_qty_for_production': ['!=', 0],
|
|
|
|
'reserved_qty_for_sub_contract': ['!=', 0],
|
|
|
|
'actual_qty': ['!=', 0],
|
|
|
|
},
|
|
|
|
filters=filters,
|
|
|
|
order_by=sort_by + ' ' + sort_order,
|
|
|
|
limit_start=start,
|
2019-01-24 10:56:48 +00:00
|
|
|
limit_page_length='21')
|
2019-02-19 14:21:23 +00:00
|
|
|
|
2021-04-09 06:26:49 +00:00
|
|
|
precision = cint(frappe.db.get_single_value("System Settings", "float_precision"))
|
2021-03-17 13:10:21 +00:00
|
|
|
|
2019-02-19 14:21:23 +00:00
|
|
|
for item in items:
|
|
|
|
item.update({
|
2021-03-17 13:10:21 +00:00
|
|
|
'item_name': frappe.get_cached_value(
|
|
|
|
"Item", item.item_code, 'item_name'),
|
|
|
|
'disable_quick_entry': frappe.get_cached_value(
|
|
|
|
"Item", item.item_code, 'has_batch_no')
|
|
|
|
or frappe.get_cached_value(
|
|
|
|
"Item", item.item_code, 'has_serial_no'),
|
|
|
|
'projected_qty': flt(item.projected_qty, precision),
|
|
|
|
'reserved_qty': flt(item.reserved_qty, precision),
|
|
|
|
'reserved_qty_for_production': flt(item.reserved_qty_for_production, precision),
|
|
|
|
'reserved_qty_for_sub_contract': flt(item.reserved_qty_for_sub_contract, precision),
|
|
|
|
'actual_qty': flt(item.actual_qty, precision),
|
2019-02-19 14:21:23 +00:00
|
|
|
})
|
|
|
|
return items
|