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

class FixEarnedValueForProjectLevelNull(SQLPacket):
    '''Updates a procedure needed for the earned value analysis'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 15138

    fix_earned_value = 'WI10922_P_RUNNINGSUM_827.sql'

    DROP_PROCEDURE_STATEMENT = """IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'P_RunningSum_827') AND type in (N'P', N'PC'))
DROP PROCEDURE P_RunningSum_827"""

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

    def fix(self, runner):
        # fix earned value for project level null
        if ppms.db_get_dbms_name() == DBMS_MSSQL:
            self.modify_and_log(self.DROP_PROCEDURE_STATEMENT)

        self.apply_dml_file(self.sql_planta_de_dbms, self.fix_earned_value, split=False)
        return self.success()