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

class FixCostCalculationAfterReplanning(SQLPacket):
    """Move deletion of DT861 records from P_VERDICHTE_PR_AKT to P_UPD_KOST_AUFW_ERL_AKT"""
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12618

    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):
        sql_files = ["WI12618_fix_cost_calculation_after_replanning_1.sql",
                     "WI12618_fix_cost_calculation_after_replanning_2.sql"]

        for sql_file in sql_files:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, sql_file)

        # In case we have Oracle do some extra stuff
        if ppms.db_get_dbms_name() == "000002":
            sql_file = "WI12618_fix_cost_calculation_after_replanning_3.sql"
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, sql_file, split=True)
            sql_file = "WI12618_fix_cost_calculation_after_replanning_4.sql"
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, sql_file)

        return self.success()