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

class FixDFAlignment(SQLPacket):
    '''Set alignment of all customer DFs to '0' if they had a value of '2'.

    This parameter hasn't worked before and might lead to unexpected behaviour.
    OLEs, however, need this set to '2' to still be displayed in the middle.
    '''
    version = 1
    done_after_success = True
    run_on_startup = True
    wi_number = 11047
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        query_dfs = "UPDATE DT411 SET DI022738 = '0' WHERE DI022738 = '2' AND DI001862 NOT IN ('011', '000', 'ZZZ')"
        query_oles = "UPDATE DT411 SET DI022738 = '2' WHERE DI000221 = 'a' AND DI000213 IN (SELECT DI000238 FROM DT412 WHERE DI001700 = '13')"

        self.log("Setting DF alignment.")
        self.modify_and_log(query_dfs)
        self.log("Adjusting OLEs.")
        self.modify_and_log(query_oles)
        return self.success()