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

BEFORE_HASH = {DBMS_ORACLE: '7a594c721b397b5c4e55f9478d5baef8',
               DBMS_MSSQL: '3bf583ac4ca3a9f230ec4dce4a40b062'}
AFTER_HASH = {DBMS_ORACLE: '75f8fbb9a7fd5347bdbf826bf921f9ab',
              DBMS_MSSQL: '6bcc7ad26c10205985ecd92dee1dd49f'}

CODE_TO_REPLACE = """                module.set_new_L_var(4, [pr_id])"""
CODE_TO_ADD = """                module.set_new_L_var(4, [pr_id])
                # Temporarily restore the old value so the tcalc filter will not ignore this project
                di.set_value(oldvalue)"""

DBMS = ppms.db_get_dbms_name()
                
class FixProjectCalculation(MigratePythonPacket):
    '''Fix projects not being calculated when setting their status to 9'''
    wi_number = 9789
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        fix_func = lambda code: code.replace(CODE_TO_REPLACE, CODE_TO_ADD)

        if self.migrate_python(BEFORE_HASH[DBMS], AFTER_HASH[DBMS],
                               self.CU_TYPE_DATAITEM, '001042', fix_func):
            return self.success()

        return self.fail('')

