from ppms.migration.packet import *
from ppms.migration import ChangeDTPPacket, MaxVersionDependency, VersionType

NEW_VALUE = '''from ppms import ppms_cu

def computeOutput(di):
    rec = di.get_dtp_record()
    cur_user=None
    
    cVars=ppms.get_active_module().get_current_L_var()
    if 5 in cVars:
        if len(cVars[5]):
            cur_user=cVars[5][0]
    
    if not cur_user: cur_user=ppms.uvar_get("@1")
    pr_id = rec.pr_id.get_value()
    pr_type = rec.pr_type.get_value()
    
    count_team_change_rights=ppms.db_select(ppms.get_query("000236").format(pr_id,cur_user,1))[0][0]
    count_team_no_change_rights=ppms.db_select(ppms.get_query("000236").format(pr_id,cur_user,0))[0][0]
       
    if cur_user == rec.pr_manager.get_value():
        if pr_type == 0:
            if rec.level.get_value() > 0: const_id="001245"
            else: const_id="001244"
        elif pr_type == 1: const_id="001323"
        elif pr_type == 2: const_id="001324"
        elif pr_type == 10: const_id="001325"
        else: const_id="001244"
        
    elif count_team_change_rights > 0: const_id="001246"
    elif count_team_no_change_rights > 0: const_id="001319"
    else:
        if pr_type == 1: const_id="001326"
        elif pr_type == 2: const_id="001327"
        elif pr_type == 10: const_id="001328"
        else: const_id="001243"
    
    return ppms_cu.Helper.get_const_title(const_id)

computeOutput.deps = ("DI001062","DI007823","DI001053","DI023220","*")'''

OLD_VALUE = '''from ppms import ppms_cu

def computeOutput(di):
    rec = di.get_dtp_record()

    cur_user=ppms.uvar_get("@1")
    pr_id = rec.pr_id.get_value()
    pr_type = rec.pr_type.get_value()
    
    count_team_change_rights=ppms.db_select(ppms.get_query("000236").format(pr_id,cur_user,1))[0][0]
    count_team_no_change_rights=ppms.db_select(ppms.get_query("000236").format(pr_id,cur_user,0))[0][0]
       
    if cur_user == rec.pr_manager.get_value() and count_team_change_rights ==0 and count_team_no_change_rights == 0:
        if pr_type == 0:
            if rec.level.get_value() > 0: const_id="001245" # sub projects
            else: const_id="001244" # main projects
        elif pr_type == 1: const_id="001323"
        elif pr_type == 2: const_id="001324"
        elif pr_type == 10: const_id="001325"
        else: const_id="001244"
        
    elif count_team_change_rights > 0: const_id="001246" # stakeholder with change rights
    elif count_team_no_change_rights > 0: const_id="001319" # stakeholder without change rights
    else:
        if pr_type == 1: const_id="001326"
        elif pr_type == 2: const_id="001327"
        elif pr_type == 10: const_id="001328"
        else: const_id="001243"
    
    return ppms_cu.Helper.get_const_title(const_id)

computeOutput.deps = ("DI001062","DI007823","DI001053","DI023220")'''

class WhenProjectManagerIsStakeholderToo(ChangeDTPPacket):
    '''Correct the ValueRange of DI059194'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12550

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

    def fix(self, runner):
        record = self.get_record(412, ['059194'], [27352])
        if not self.change_value(record, 'valuerange', OLD_VALUE, NEW_VALUE):
            return self.fail()
            
        return self.success()
