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


class FixTaskRights(ChangeDTPPacket):
    '''Enables the person responsible for a task ('person_resp_for_ta' in task table 463) to access/edit it'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12940

    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

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], [1052], True).main_pr_id.get_value()])
    mod_obj.set_current_L_var(70, [ppms.search_record(461, [pr_id], [23220,264,29448], 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)

    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()"""
        old_value_venus = """from ppms import ppms_cu
from ppms import project_rights

def show_task(pr_id,task_id,add=False):
    new_task_id = task_id
    main_pr_id = ppms.search_record(461, [pr_id], [1052], True).main_pr_id.get_value()
    psp_code = ppms.search_record(461, [pr_id], [23220,264,29448], True).psp_code.get_value()+"*"
    if add == True:
        # show task successor additive
        projects_shown = mod_obj.get_current_L_var()[30]
        tasks_shown = mod_obj.get_current_L_var()[33]
        main_pr_shown = mod_obj.get_current_L_var()[74]
        psp_code_shown = mod_obj.get_current_L_var()[70]
        if pr_id not in projects_shown:
            projects_shown.append(pr_id)
        if task_id not in tasks_shown:
            tasks_shown.append(task_id)
        if main_pr_id not in main_pr_shown:
            main_pr_shown.append(main_pr_id)
        if psp_code not in psp_code_shown:
            psp_code_shown.append(psp_code)
        pr_id = projects_shown
        task_id = tasks_shown
        main_pr_shown = main_pr_shown
        psp_code_shown = psp_code_shown
    else:
        # only show the chosen task
        pr_id = [pr_id]
        task_id = [task_id]
        main_pr_id = [main_pr_id]
        psp_code = [psp_code]
    # set new filter
    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, main_pr_id)
    mod_obj.set_current_L_var(70, psp_code)
    if len(mod_obj.task_area.get_records()) > 0:
        mod_obj.menu(19)
        mod_obj.focus()
    else:
        mod_obj.menu(12)
    # focus the latest inserted task
    for rec in mod_obj.task_area.get_records():
        dtp_rec = rec.get_dtp_record()
        this_task_id = dtp_rec.task_id.get_value()
        if this_task_id == new_task_id:
            rec.pr_technical.scroll_to()      
            rec.pr_technical.focus()

    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()
        elif self.verify_value(rec, 'py_macro_text', old_value_venus):
            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,add=False):
    new_task_id = task_id
    main_pr_id = ppms.search_record(461, [pr_id], ['main_pr_id'], True).main_pr_id.get_value()
    psp_code = ppms.search_record(461, [pr_id], ['psp_code'], True).psp_code.get_value()+"*"
    if add == True:
        # show task successor additive
        projects_shown = mod_obj.get_current_L_var()[30]
        tasks_shown = mod_obj.get_current_L_var()[33]
        main_pr_shown = mod_obj.get_current_L_var()[74]
        psp_code_shown = mod_obj.get_current_L_var()[70]
        if pr_id not in projects_shown:
            projects_shown.append(pr_id)
        if task_id not in tasks_shown:
            tasks_shown.append(task_id)
        if main_pr_id not in main_pr_shown:
            main_pr_shown.append(main_pr_id)
        if psp_code not in psp_code_shown:
            psp_code_shown.append(psp_code)
        pr_id = projects_shown
        task_id = tasks_shown
        main_pr_shown = main_pr_shown
        psp_code_shown = psp_code_shown
    else:
        # only show the chosen task
        pr_id = [pr_id]
        task_id = [task_id]
        main_pr_id = [main_pr_id]
        psp_code = [psp_code]
    # set new filter
    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, main_pr_id)
    mod_obj.set_current_L_var(70, psp_code)
    if len(mod_obj.task_area.get_records()) > 0:
        mod_obj.menu(19)
        mod_obj.focus()
    else:
        mod_obj.menu(12)
    # focus the latest inserted task
    for rec in mod_obj.task_area.get_records():
        dtp_rec = rec.get_dtp_record()
        this_task_id = dtp_rec.task_id.get_value()
        if this_task_id == new_task_id:
            rec.pr_technical.scroll_to()      
            rec.pr_technical.focus()

    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()"""):
                return self.success()
        return self.fail()