brotherton-erpnext/erpnext/patches/v13_0/update_sla_enhancements.py

110 lines
3.5 KiB
Python
Raw Normal View History

2020-05-29 06:13:05 +00:00
# Copyright (c) 2018, Frappe and Contributors
# License: GNU General Public License v3. See license.txt
import frappe
2020-05-29 06:13:05 +00:00
def execute():
# add holiday list and employee group fields in SLA
# change response and resolution time in priorities child table
2022-03-28 13:22:46 +00:00
if frappe.db.exists("DocType", "Service Level Agreement"):
sla_details = frappe.db.get_all("Service Level Agreement", fields=["name", "service_level"])
priorities = frappe.db.get_all(
"Service Level Priority",
fields=["*"],
filters={"parenttype": ("in", ["Service Level Agreement", "Service Level"])},
)
2020-05-29 06:13:05 +00:00
2022-03-28 13:22:46 +00:00
frappe.reload_doc("support", "doctype", "service_level_agreement")
frappe.reload_doc("support", "doctype", "pause_sla_on_status")
frappe.reload_doc("support", "doctype", "service_level_priority")
frappe.reload_doc("support", "doctype", "service_day")
2020-05-29 06:13:05 +00:00
for entry in sla_details:
2022-03-28 13:22:46 +00:00
values = frappe.db.get_value(
"Service Level", entry.service_level, ["holiday_list", "employee_group"]
)
2020-05-29 06:13:05 +00:00
if values:
holiday_list = values[0]
employee_group = values[1]
2022-03-28 13:22:46 +00:00
frappe.db.set_value(
"Service Level Agreement",
entry.name,
{"holiday_list": holiday_list, "employee_group": employee_group},
)
2020-05-29 06:13:05 +00:00
priority_dict = {}
for priority in priorities:
2022-03-28 13:22:46 +00:00
if priority.parenttype == "Service Level Agreement":
2020-05-29 06:13:05 +00:00
response_time = convert_to_seconds(priority.response_time, priority.response_time_period)
resolution_time = convert_to_seconds(priority.resolution_time, priority.resolution_time_period)
2022-03-28 13:22:46 +00:00
frappe.db.set_value(
"Service Level Priority",
priority.name,
{"response_time": response_time, "resolution_time": resolution_time},
)
if priority.parenttype == "Service Level":
2020-05-29 06:13:05 +00:00
if not priority.parent in priority_dict:
priority_dict[priority.parent] = []
priority_dict[priority.parent].append(priority)
# copy Service Levels to Service Level Agreements
sl = [entry.service_level for entry in sla_details]
2022-03-28 13:22:46 +00:00
if frappe.db.exists("DocType", "Service Level"):
service_levels = frappe.db.get_all(
"Service Level", filters={"service_level": ("not in", sl)}, fields=["*"]
)
2020-06-11 12:11:05 +00:00
for entry in service_levels:
2022-03-28 13:22:46 +00:00
sla = frappe.new_doc("Service Level Agreement")
2020-06-11 12:11:05 +00:00
sla.service_level = entry.service_level
sla.holiday_list = entry.holiday_list
sla.employee_group = entry.employee_group
sla.flags.ignore_validate = True
sla = sla.insert(ignore_mandatory=True)
2020-05-29 06:13:05 +00:00
2022-03-28 13:22:46 +00:00
frappe.db.sql(
"""
2020-06-11 12:11:05 +00:00
UPDATE
`tabService Day`
SET
parent = %(new_parent)s , parentfield = 'support_and_resolution', parenttype = 'Service Level Agreement'
WHERE
parent = %(old_parent)s
2022-03-28 13:22:46 +00:00
""",
{"new_parent": sla.name, "old_parent": entry.name},
as_dict=1,
)
2020-05-29 06:13:05 +00:00
2020-06-11 12:11:05 +00:00
priority_list = priority_dict.get(entry.name)
if priority_list:
2022-03-28 13:22:46 +00:00
sla = frappe.get_doc("Service Level Agreement", sla.name)
2020-06-11 12:11:05 +00:00
for priority in priority_list:
2022-03-28 13:22:46 +00:00
row = sla.append(
"priorities",
{
"priority": priority.priority,
"default_priority": priority.default_priority,
"response_time": convert_to_seconds(priority.response_time, priority.response_time_period),
"resolution_time": convert_to_seconds(
priority.resolution_time, priority.resolution_time_period
),
},
)
2020-06-11 12:11:05 +00:00
row.db_update()
sla.db_update()
2020-05-29 06:13:05 +00:00
2022-03-28 13:22:46 +00:00
frappe.delete_doc_if_exists("DocType", "Service Level")
2020-06-04 07:05:00 +00:00
2020-05-29 06:13:05 +00:00
def convert_to_seconds(value, unit):
seconds = 0
if unit == "Hour":
seconds = value * 3600
if unit == "Day":
seconds = value * 3600 * 24
if unit == "Week":
seconds = value * 3600 * 24 * 7
return seconds