# -*- coding: utf-8 -*-
from ppms.migration import ChangeDTPPacket, PacketCategory, MaxVersionDependency, VersionType


class FixTaskRights2(ChangeDTPPacket):
    '''Fixes a bug in macro of module "Vorgang" which came in witch another migration paket "FixTaskRights"'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14276

    def get_dependencies(self):
        return (MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),)

    def fix(self, runner):
        errors = 0
        rec = self.get_record(405, ['0099CA'], ['py_macro_text'])

        old_value_earth = """
from ppms import ppms_cu
from ppms import project_rights
from ppms.constants import *

def show_task(pr_id,task_id):
    mod_obj.set_current_L_var(30, [pr_id])
    mod_obj.set_current_L_var(33, [task_id])
    mod_obj.set_current_L_var(74, [ppms.search_record(461, [pr_id], ['main_pr_id'], True).main_pr_id.get_value()])
    mod_obj.set_current_L_var(70, [ppms.search_record(461, [pr_id], ['psp_code'], True).psp_code.get_value()+"*"])

    if len(mod_obj.task_area.get_records()) >0:
        mod_obj.menu(19)
        mod_obj.focus()
    else:
        mod_obj.menu(12)

    task_resp_rec = ppms.search_record(463, [pr_id[0], task_id[0]], ['person_resp_for_ta'])

    if task_resp_rec:
        task_resp = task_resp_rec.person_resp_for_ta.get_value()
        task_resp_areas = ('050736', '050735', '040562', '040563', '040560', '040561', '048008', '048009')

        if task_resp == ppms.uvar_get(SYS_VAR_LOGGED_IN_USER):
            for da in mod_obj.get_das():
               if da.get_id() not in task_resp_areas:
                   right_setting[0].append(da.get_id())

    project_rights.exec_project_rights(mod_obj,right_setting)

mod_obj=ppms.get_target_module()

"Define data area settings"
right_setting={}
right_setting[0]=[]

def on_load():
    mod_obj.show_task=show_task
    mod_obj.actualize_by_invoker = True

def on_initial_focus():
    mod_obj.menu(12)

def on_focus():
    pass

def on_reset():
    project_rights.exec_project_rights(mod_obj,right_setting)
    on_initial_focus()"""

        if self.verify_value(rec, 'py_macro_text', old_value_earth):
            if self.change_value(rec, 'py_macro_text', None, """
from ppms import ppms_cu
from ppms import project_rights
from ppms.constants import *

def show_task(pr_id,task_id):
    mod_obj.set_current_L_var(30, [pr_id])
    mod_obj.set_current_L_var(33, [task_id])
    mod_obj.set_current_L_var(74, [ppms.search_record(461, [pr_id], ['main_pr_id'], True).main_pr_id.get_value()])
    mod_obj.set_current_L_var(70, [ppms.search_record(461, [pr_id], ['psp_code'], True).psp_code.get_value()+"*"])

    if len(mod_obj.task_area.get_records()) >0:
        mod_obj.menu(19)
        mod_obj.focus()
    else:
        mod_obj.menu(12)

    task_resp_rec = ppms.search_record(463, [pr_id, task_id], ['person_resp_for_ta'])

    if task_resp_rec:
        task_resp = task_resp_rec.person_resp_for_ta.get_value()
        task_resp_areas = ('050736', '050735', '040562', '040563', '040560', '040561', '048008', '048009')

        if task_resp == ppms.uvar_get(SYS_VAR_LOGGED_IN_USER):
            for da in mod_obj.get_das():
               if da.get_id() not in task_resp_areas:
                   right_setting[0].append(da.get_id())

    project_rights.exec_project_rights(mod_obj,right_setting)

mod_obj=ppms.get_target_module()

"Define data area settings"
right_setting={}
right_setting[0]=[]

def on_load():
    mod_obj.show_task=show_task
    mod_obj.actualize_by_invoker = True

def on_initial_focus():
    mod_obj.menu(12)

def on_focus():
    pass

def on_reset():
    project_rights.exec_project_rights(mod_obj,right_setting)
    on_initial_focus()"""):
                return self.success()
            return self.fail()
        else:
            self.log("Value is already correct or has been manually modified, nothing to do!")
            return self.success()