2020-04-18 13:29:32 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
|
|
|
from frappe.utils import flt
|
|
|
|
from frappe import _
|
|
|
|
|
|
|
|
def execute(filters=None):
|
|
|
|
columns, data = [], []
|
|
|
|
data = get_data(filters)
|
|
|
|
columns = get_columns(filters)
|
|
|
|
chart_data = get_chart_data(data, filters)
|
|
|
|
return columns, data, None, chart_data
|
|
|
|
|
|
|
|
def get_data(filters):
|
|
|
|
query_filters = {}
|
|
|
|
|
2020-05-19 07:30:54 +00:00
|
|
|
fields = ["name", "workstation", "operator", "from_time", "to_time", "downtime", "stop_reason", "remarks"]
|
2020-04-18 13:29:32 +00:00
|
|
|
|
|
|
|
query_filters["from_time"] = (">=", filters.get("from_date"))
|
|
|
|
query_filters["to_time"] = ("<=", filters.get("to_date"))
|
|
|
|
|
|
|
|
if filters.get("workstation"):
|
|
|
|
query_filters["workstation"] = filters.get("workstation")
|
|
|
|
|
2020-05-26 17:37:11 +00:00
|
|
|
data = frappe.get_all("Downtime Entry", fields= fields, filters=query_filters) or []
|
|
|
|
for d in data:
|
|
|
|
if d.downtime:
|
|
|
|
d.downtime = d.downtime / 60
|
|
|
|
|
|
|
|
return data
|
2020-04-18 13:29:32 +00:00
|
|
|
|
|
|
|
def get_chart_data(data, columns):
|
|
|
|
labels = sorted(list(set([d.workstation for d in data])))
|
|
|
|
|
|
|
|
workstation_wise_data = {}
|
|
|
|
for d in data:
|
|
|
|
if d.workstation not in workstation_wise_data:
|
|
|
|
workstation_wise_data[d.workstation] = 0
|
|
|
|
|
|
|
|
workstation_wise_data[d.workstation] += flt(d.downtime, 2)
|
|
|
|
|
|
|
|
datasets = []
|
|
|
|
for label in labels:
|
|
|
|
datasets.append(workstation_wise_data.get(label, 0))
|
|
|
|
|
|
|
|
chart = {
|
|
|
|
"data": {
|
|
|
|
"labels": labels,
|
|
|
|
"datasets": [
|
2020-05-26 17:37:11 +00:00
|
|
|
{"name": "Machine Downtime", "values": datasets}
|
2020-04-18 13:29:32 +00:00
|
|
|
]
|
|
|
|
},
|
2020-05-05 21:08:27 +00:00
|
|
|
"type": "bar"
|
2020-04-18 13:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return chart
|
|
|
|
|
|
|
|
def get_columns(filters):
|
|
|
|
return [
|
2020-05-19 07:30:54 +00:00
|
|
|
{
|
|
|
|
"label": _("ID"),
|
|
|
|
"fieldname": "name",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Downtime Entry",
|
|
|
|
"width": 100
|
|
|
|
},
|
2020-04-18 13:29:32 +00:00
|
|
|
{
|
|
|
|
"label": _("Machine"),
|
|
|
|
"fieldname": "workstation",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Workstation",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Operator"),
|
|
|
|
"fieldname": "operator",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Employee",
|
|
|
|
"width": 130
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("From Time"),
|
|
|
|
"fieldname": "from_time",
|
|
|
|
"fieldtype": "Datetime",
|
|
|
|
"width": 160
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("To Time"),
|
|
|
|
"fieldname": "to_time",
|
|
|
|
"fieldtype": "Datetime",
|
|
|
|
"width": 160
|
|
|
|
},
|
|
|
|
{
|
2020-05-26 17:37:11 +00:00
|
|
|
"label": _("Downtime (In Hours)"),
|
2020-04-18 13:29:32 +00:00
|
|
|
"fieldname": "downtime",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 150
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 07:30:54 +00:00
|
|
|
"label": _("Stop Reason"),
|
|
|
|
"fieldname": "stop_reason",
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 220
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Remarks"),
|
|
|
|
"fieldname": "remarks",
|
2020-04-18 13:29:32 +00:00
|
|
|
"fieldtype": "Text",
|
2020-05-19 07:30:54 +00:00
|
|
|
"width": 100
|
2020-04-18 13:29:32 +00:00
|
|
|
}
|
|
|
|
]
|