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


class RemoveFilterForChartDisplaying(SQLPacket, ChangeDTPPacket):
    """This will undo an former workaround for zero values not being correctly
    read the client This affects the cost trend analysis in module 
    status/statusreport. 
    
    It's required to use the new file charthelper.py from client CS 52964 
    (see Server 39.5.18 release note)"""
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14872

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

    def fix(self, runner):
        if self.DBMS == DBMS_MSSQL:
            self.log("Only required for systems running on ORACLE.")
            return self.success()
        
        EMPTY_VALUE = ''
        OLD_LOWER_VALUE = '0.1'
        OLD_UPPER_VALUE = '99999999999999'
        
        record = self.get_record(411, ['046074',714074], ['filter_from'])
        if not self.change_value(record, 'filter_from', 
                                 OLD_LOWER_VALUE, EMPTY_VALUE):
            return self.fail()
        
        record = self.get_record(411, ['046074',714074], ['filter_to'])
        if not self.change_value(record, 'filter_to', 
                                 OLD_UPPER_VALUE, EMPTY_VALUE):
            return self.fail()
    
        record = self.get_record(411, ['046074',714075], ['filter_from'])
        if not self.change_value(record, 'filter_from', 
                                 OLD_LOWER_VALUE, EMPTY_VALUE):
            return self.fail()     
                
        record = self.get_record(411, ['046074',714075], ['filter_to'])
        if not self.change_value(record, 'filter_to', 
                                 OLD_UPPER_VALUE, EMPTY_VALUE):
            return self.fail()
    
        record = self.get_record(411, ['046074',714077], ['filter_from'])
        if not self.change_value(record, 'filter_from', 
                                 OLD_LOWER_VALUE, EMPTY_VALUE):
            return self.fail()
                
        record = self.get_record(411, ['046074',714077], ['filter_to'])
        if not self.change_value(record, 'filter_to', 
                                 OLD_UPPER_VALUE, EMPTY_VALUE):
            return self.fail()
    
        record = self.get_record(411, ['046074',714078], ['filter_from'])
        if not self.change_value(record, 'filter_from', 
                                 OLD_LOWER_VALUE, EMPTY_VALUE):
            return self.fail()
        
        record = self.get_record(411, ['046074',714078], ['filter_to'])
        if not self.change_value(record, 'filter_to', 
                                 OLD_UPPER_VALUE, EMPTY_VALUE):
            return self.fail()

        return self.success()