# Copyright (c) 2020, Xhiveframework and Contributors
# License: GNU General Public License v3. See license.txt


import xhiveframework
from xhiveframework.model.utils.rename_field import rename_field


def execute():
	xhiveframework.reload_doc("support", "doctype", "sla_fulfilled_on_status")
	xhiveframework.reload_doc("support", "doctype", "service_level_agreement")
	if xhiveframework.db.has_column("Service Level Agreement", "enable"):
		rename_field("Service Level Agreement", "enable", "enabled")

	for sla in xhiveframework.get_all("Service Level Agreement"):
		agreement = xhiveframework.get_doc("Service Level Agreement", sla.name)
		agreement.db_set("document_type", "Issue")
		agreement.reload()
		agreement.apply_sla_for_resolution = 1
		agreement.append("sla_fulfilled_on", {"status": "Resolved"})
		agreement.append("sla_fulfilled_on", {"status": "Closed"})
		agreement.save()
