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

DELETE_STATEMENTS = ["DELETE FROM DT411 WHERE DI000306 = '049593' AND DI000213 IN ('059003', '059003')"]

class FixPortfolioModuleCustomizingErrors(SQLPacket):
    """Gets rid of customizing errors when opening module 0099FV 'Portfolio'"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 15904

    filename = 'wi15904_portfolio_fix.sql'

    def get_dependencies(self):
        return MaxVersionDependency(version='39.4'),

    def delete_old_data(self):
        self.log_heading('Wiping data that could cause conflicts')
        for statement in DELETE_STATEMENTS:
            self.modify_and_log(statement)

    def insert_new_data(self):
        self.log_heading('Inserting new data')
        self.apply_dml_file(self.sql_planta_de_dbms, self.filename)

    def fix(self, runner):
        with ppms.autocommit_disabled():
            self.delete_old_data()
            self.insert_new_data()

        return self.success()

