# -*- coding: utf-8 -*-
import os

from ppms.migration.packet import *

BEFORE_HASH = 'af28e51704c85b879b9bc7b188e73288'
AFTER_HASH = '24c75bc60c017c957db27dd58e5d05b6'

SUB = lambda _: r"""from ppms import ppms_cu

def get_amount(query):
    query = ppms.get_query(query)
    results = ppms.db_select(query)
    
    try:
        return str(int(results[0][0]))
    except IndexError:
        return '0'
    
def computeOutput(di):
    projects = get_amount('000226')
    tasks = get_amount('000225')
    
    srqs = get_amount('000227')
    rsos = get_amount('000228')
    
    leads = get_amount('000229')
    offers = get_amount('000230')
    
    assignments = get_amount('000231')
    
    contents = get_amount('000232')
    elements = get_amount('000233')
    
    text = ppms_cu.Access.get_text_constant(None, '001562')
    text = text.format(**locals())
    text = text.replace(r'\n', '\n')

    return text
 
computeOutput.deps = ('*',)"""

class FixLicenseLevel(MigratePythonPacket):
    '''Correct the license level value range'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9581

    def fix(self, runner):
        if not self.migrate_python(BEFORE_HASH, AFTER_HASH,
                                   self.CU_TYPE_DATAITEM, '059024', SUB):
            return self.fail()
                                   
        return self.success()
        
class PortLicenseTranslations(SQLPacket):
    '''Port translations for the license level module'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9581
    
    FNAME = 'wi9581_fix_licenselevel.sql'
    DELETE_QUERY = 'DELETE FROM DT435 WHERE DI001538 = 754581'
    
    def fix(self, runner):
        rows = self.modify_and_log(self.DELETE_QUERY)
        
        path = self.get_sql_file_path(self.sql_planta_de_dbms, self.FNAME)
        for query in self.get_queries_from_file(path):
            self.modify_and_log(query)
        
        return self.success()