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


class AddCostCalcTime(SQLPacket):
    """Add two new project columns which save the values for the last cost
    calculation date and time"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18230

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

    def fix(self, packet_runner):

        add_columns = {DBMS_ORACLE:
                       ["""alter table DT461 add
                          (COST_CALC_DATE date
                           DEFAULT TO_DATE('01.01.1970','DD.MM.YYYY'))""",
                           """alter table DT461 add
                          (COST_CALC_TIME number(10,0) DEFAULT 0)"""],
                       DBMS_MSSQL:
                       ["""alter table DT461
                           add COST_CALC_DATE datetime2""",
                        """alter table DT461
                           add COST_CALC_TIME numeric(10,0)""",
                        """alter table DT461
                           add constraint DT461DI063979_DEFAULT
                           DEFAULT CONVERT(datetime2,'01.01.1970',104)
                           FOR [COST_CALC_DATE]""",
                        """alter table DT461
                           add constraint DT461DI063980_DEFAULT
                           DEFAULT 0 FOR [COST_CALC_TIME]"""]}

        for sql_stmt in add_columns[self.DBMS]:
            self.modify_and_log(sql_stmt)

        return self.success()