# -*- coding: utf-8 -*-
from ppms.migration.packet import *

class FixApprovedEffort(ChangeDTPPacket):
    '''Add approved effort to the mapping dict of the budget module macro.'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 8361

    def fix(self, runner):
        OLD_VAL = """from ppms import ppms_cu
from ppms import project_rights

def set_project_budget():
    # copies th approved budget values to the project budget
    inv_rec=ppms.get_context_df().get_record()
    pr_id=inv_rec.get_dtp_record().get_di("pr_id").get_value()
    if project_rights.current_user_is_mpm() == False: ppms.ui_message_id("0051")
    else:
        mapping_dict={
        "appr_internal_vdi": "int_personnel_budget",
        "appr_external_vdi": "ext_personnel_budget",
        "appr_mat_costs_vdi": "material_costs_bdgt",
        "appr_invest_vdi": "investments_budget",
        "appr_misc_vdi": "misc_budget"
        }
        for source,target in mapping_dict.items(): inv_rec.get_df(target).set_raw_value(inv_rec.get_df(source).get_raw_value())

def get_pr_areas():
    from ppms import ppms_cu
    pr_id_list=[]
    
    #get project ids of current module
    for da_obj in mod_obj.get_das():
        if ppms_cu.Access.get_dt_id_of_ddi(da_obj) == "461":
            for mts_rec_obj in da_obj.get_records():
                try:
                    if mts_rec_obj.get_dtp_record().pr_id.get_value() not in pr_id_list:
                        pr_id_list.append(mts_rec_obj.get_dtp_record().pr_id.get_value()) 
                except: pass
    
    return pr_id_list

def set_permission_for_cost_details(mv_id=""):
    applied_variant=mv_id
    if project_rights.check_cost_details_toggle() == False:
        variant_id=ppms_cu.Helper.get_global_setting("budget_variant_cost_from_reports").parameter.get_value()
        if applied_variant != variant_id:
            ppms.ui_message_id("0796")
            mod_obj.apply_mv_by_id(variant_id)
    else:
        if applied_variant:
            pass
        else:
            mod_obj.reset_cu()

def refresh_module():
    mod_obj.menu(19)
    
def check_changes_in_panel():
    if hasattr(mod_obj, 'changes_in_panel'):
        if mod_obj.changes_in_panel == True:
            #mod_obj.get_da("changes_in_panel").get_customizing().get_dfc("dummy_id").set_window(1)
            mod_obj.get_da("changes_in_panel").get_customizing().set_display_behavior(1)
    
def refresh_costs_details():
    if hasattr(mod_obj, 'pr_id'): project_rights.refresh_costs_details(mod_obj.pr_id)
    
def make_budget_visible():
    for rec in mod_obj.get_da("cost_effort_details").get_records():
        if rec.get_dtp_record().pr_id.get_value() == mod_obj.get_panel().get_main_module().pr_id:
            rec.get_df("background").focus()
            
def sum_costs_by_year_for_manual_costs():
    mod_obj.menu(34)
    from ppms import project_rights, ppms_cu
    if project_rights.check_cost_details_toggle() == True:
        for pr_id in get_pr_areas():
            ppms.db_modify(ppms.get_query("000368").format(pr_id))
        mod_obj.refresh_dts((461,))

mod_obj=ppms.get_target_module()

#Define data area settings
right_setting={}
#watcher
right_setting[0]=[]

def on_load():
    mod_obj.refresh_costs_details=refresh_costs_details
    mod_obj.refresh_module=refresh_module
    mod_obj.check_changes_in_panel=check_changes_in_panel
    mod_obj.set_permission_for_cost_details=set_permission_for_cost_details
    mod_obj.sum_costs_by_year_for_manual_costs=sum_costs_by_year_for_manual_costs
    mod_obj.set_project_budget=set_project_budget
    
    ppms.echo_off()
    mod_obj.set_permission_for_cost_details()
    ppms.echo_on()
    try:
        mod_obj.get_invoker_module().focus_specific_module_by_invoker(ppms_cu.Helper.get_global_setting("startup_module_cost_reports").alpha120.get_value(),mod_obj)
    except:
        pass

def on_initial_focus():
    project_rights.exec_project_rights(mod_obj,right_setting)
    mod_obj.menu(12)
    make_budget_visible()
    
def on_focus():
    mod_obj.check_changes_in_panel()
    
def on_reset():  
    project_rights.exec_project_rights(mod_obj,right_setting)
    ppms.echo_off()
    mod_obj.set_permission_for_cost_details()
    ppms.echo_on()
    mod_obj.refresh_costs_details()
    mod_obj.menu(12)
    make_budget_visible()

def on_after_mv_switch(old_mv, new_mv):
    mod_obj.set_permission_for_cost_details(new_mv.get_id())
    project_rights.exec_project_rights(mod_obj,right_setting)
    make_budget_visible()"""

        NEW_VAL = """from ppms import ppms_cu
from ppms import project_rights

def set_project_budget():
    # copies th approved budget values to the project budget
    inv_rec=ppms.get_context_df().get_record()
    pr_id=inv_rec.get_dtp_record().get_di("pr_id").get_value()
    if project_rights.current_user_is_mpm() == False: ppms.ui_message_id("0051")
    else:
        mapping_dict={
        "appr_internal_vdi": "int_personnel_budget",
        "appr_external_vdi": "ext_personnel_budget",
        "appr_mat_costs_vdi": "material_costs_bdgt",
        "appr_invest_vdi": "investments_budget",
        "appr_misc_vdi": "misc_budget",
        "vdi_appr_effort_total":"effort_budget"
        }
        for source,target in mapping_dict.items(): inv_rec.get_df(target).set_raw_value(inv_rec.get_df(source).get_raw_value())

def get_pr_areas():
    from ppms import ppms_cu
    pr_id_list=[]
    
    #get project ids of current module
    for da_obj in mod_obj.get_das():
        if ppms_cu.Access.get_dt_id_of_ddi(da_obj) == "461":
            for mts_rec_obj in da_obj.get_records():
                try:
                    if mts_rec_obj.get_dtp_record().pr_id.get_value() not in pr_id_list:
                        pr_id_list.append(mts_rec_obj.get_dtp_record().pr_id.get_value()) 
                except: pass
    
    return pr_id_list

def set_permission_for_cost_details(mv_id=""):
    applied_variant=mv_id
    if project_rights.check_cost_details_toggle() == False:
        variant_id=ppms_cu.Helper.get_global_setting("budget_variant_cost_from_reports").parameter.get_value()
        if applied_variant != variant_id:
            ppms.ui_message_id("0796")
            mod_obj.apply_mv_by_id(variant_id)
    else:
        if applied_variant:
            pass
        else:
            mod_obj.reset_cu()

def refresh_module():
    mod_obj.menu(19)
    
def check_changes_in_panel():
    if hasattr(mod_obj, 'changes_in_panel'):
        if mod_obj.changes_in_panel == True:
            #mod_obj.get_da("changes_in_panel").get_customizing().get_dfc("dummy_id").set_window(1)
            mod_obj.get_da("changes_in_panel").get_customizing().set_display_behavior(1)
    
def refresh_costs_details():
    if hasattr(mod_obj, 'pr_id'): project_rights.refresh_costs_details(mod_obj.pr_id)
    
def make_budget_visible():
    for rec in mod_obj.get_da("cost_effort_details").get_records():
        if rec.get_dtp_record().pr_id.get_value() == mod_obj.get_panel().get_main_module().pr_id:
            rec.get_df("background").focus()
            
def sum_costs_by_year_for_manual_costs():
    mod_obj.menu(34)
    from ppms import project_rights, ppms_cu
    if project_rights.check_cost_details_toggle() == True:
        for pr_id in get_pr_areas():
            ppms.db_modify(ppms.get_query("000368").format(pr_id))
        mod_obj.refresh_dts((461,))

mod_obj=ppms.get_target_module()

#Define data area settings
right_setting={}
#watcher
right_setting[0]=[]

def on_load():
    mod_obj.refresh_costs_details=refresh_costs_details
    mod_obj.refresh_module=refresh_module
    mod_obj.check_changes_in_panel=check_changes_in_panel
    mod_obj.set_permission_for_cost_details=set_permission_for_cost_details
    mod_obj.sum_costs_by_year_for_manual_costs=sum_costs_by_year_for_manual_costs
    mod_obj.set_project_budget=set_project_budget
    
    with ppms.echo_disabled():
        mod_obj.set_permission_for_cost_details()
    
    try:
        mod_obj.get_invoker_module().focus_specific_module_by_invoker(ppms_cu.Helper.get_global_setting("startup_module_cost_reports").alpha120.get_value(),mod_obj)
    except:
        pass

def on_initial_focus():
    project_rights.exec_project_rights(mod_obj,right_setting)
    mod_obj.menu(12)
    make_budget_visible()
    
def on_focus():
    mod_obj.check_changes_in_panel()
    
def on_reset():  
    project_rights.exec_project_rights(mod_obj,right_setting)
    with ppms.echo_disabled():
        mod_obj.set_permission_for_cost_details()
    mod_obj.refresh_costs_details()
    mod_obj.menu(12)
    make_budget_visible()

def on_after_mv_switch(old_mv, new_mv):
    mod_obj.set_permission_for_cost_details(new_mv.get_id())
    project_rights.exec_project_rights(mod_obj,right_setting)
    make_budget_visible()
    """

        record = self.get_record(405, ['0099D6'], [40819])
        if not self.change_value(record, 'py_macro_text', OLD_VAL, NEW_VAL):
            return self.fail()

        return self.success()
