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

OLD_STATEMENT = """SELECT DI000215,DI040837 FROM DT412
WHERE di000275=0 and di001700 <> 20
AND di000239='{0}'"""

NEW_STATEMENT = """SELECT DI000215,DI040837 FROM DT412
WHERE di000275=0 and di001700 <> 20
AND di000239='{0}' AND DI040837 is not NULL
"""


class PortSQLStatementForReportCreation(ChangeDTPPacket):
    """Changes SQL statement 000336
 
    Now, the statement excludes DIs without "Dataitem fuer Abgleichsquelle" filled when creating a report
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13793

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

    def fix(self, runner):
        record = self.get_record(dt_num=343, key_list=['-', '000336'], di_list=['source_text_clob'])
        if not self.change_value(record=record, attribute='source_text_clob', old_value=OLD_STATEMENT,
                                 new_value=NEW_STATEMENT):
            return self.fail('Could not change sql statement 000336 - was it individualized?')

        return self.success()