from ppms.migration import ChangeDTPPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType

OLDVALUE="""def computeOutput(di):
    rec = di.get_dtp_record()
    returnvalue=""
    rec823=ppms.search_record(823, [rec.pr_id.get_value(),rec.report_pls_report_id.get_value()], [8702], True)
       
    if rec823 != None:
        returnvalue = rec823.report_title.get_value()
    
    return returnvalue

computeOutput.deps = ('DI051881',)
"""

OLDVALUE_VOE="""def computeOutput(di):
    rec = di.get_dtp_record()
    returnvalue=""
    rec823=ppms.search_record(823, [rec.pr_id.get_value(),rec.report_pls_report_id.get_value()], [8702], True)
       
    if rec823 != None:
        returnvalue = rec823.report_title.get_value()
    
    return returnvalue

computeOutput.deps = ('DI051881',)"""


NEWVALUE="""def computeOutput(di):
    rec = di.get_dtp_record()
    returnvalue=""
    rec823 = None
    if not rec is None:
        rec823=ppms.search_record(823, [rec.pr_id.get_value(),rec.report_pls_report_id.get_value()], [8702], True)
       
    if rec823 != None:
        returnvalue = rec823.report_title.get_value()
    
    return returnvalue

computeOutput.deps = ('DI051881',)"""

class PythonErrorInMacro009A4T(ChangeDTPPacket):
    """Fix the Python error when pressing "Planung vergleichen" button in "Terminplan"
    """
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12714
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.5', version_type=VersionType.CUSTOMIZING))
            
    def fix(self, runner):
        report_pls_report_title_rec = self.get_record(412, ['058033'], ['valuerange'])
        if self.change_value(report_pls_report_title_rec, 'valuerange', OLDVALUE, NEWVALUE): 
            return self.success()
        elif self.change_value(report_pls_report_title_rec, 'valuerange', OLDVALUE_VOE, NEWVALUE):
            return self.success()
        else: return self.fail()