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


class UpdatePeriodCalculationProcedure(SQLPacket):
    """Update the procedure P_CALC_PERIODS with a few bug fixes"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13121

    FILES = ('wi13121_P_CALC_PERIODS.sql', 'wi13121_PLANTA_F_CALC_PERIOD_FACTOR.sql')
    CREATE_COLUMNS = {DBMS_ORACLE: """alter table
   DT468_TEMP
add
   (
   FAK_3 NUMBER,
   FAK_4 NUMBER,
   FAK_5 NUMBER
   )""", DBMS_MSSQL: """ALTER TABLE DT468_TEMP ADD
FAK_3 float,
FAK_4 float,
FAK_5 float"""}

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

    def fix(self, packet_runner):
        self.modify_and_log('drop function "F_CALC_CAPACITY"')
        self.modify_and_log('drop procedure "P_CALC_PERIODS"')
        self.modify_and_log(self.CREATE_COLUMNS[ppms.db_get_dbms_name()])

        for file_path in self.FILES:
            self.apply_dml_file(self.sql_planta_de_dbms, file_path, split=False)

        return self.success()