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

class ChangeLicenseTaskCount(SQLPacket):
    '''Change license task count to only include tasks of active projects.'''

    wi_number = 11108
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    TEXTCONSTANTS = {'DE':'PLANTA Project:\n   {tasks} Vorgänge\n\nPLANTA Portfolio:\n   {projects} Projekte\n\nPLANTA Request:\n   {srqs} SRQs\n   {rsos} RSOs\n\nPLANTA Sales:\n   {leads} Leads\n   {offers} Angebote\n   {assignments} Aufträge\n\nPLANTA Content:\n   {contents} Contents\n   {elements} Elemente',
                     'EN':'PLANTA Project:\n   {tasks} Tasks\n\nPLANTA Portfolio:\n   {projects} Projects\n\nPLANTA Request:\n   {srqs} SRQs\n   {rsos} RSOs\n\nPLANTA Sales:\n   {leads} Leads\n   {offers} Offers\n   {assignments} Orders\n\nPLANTA Content:\n   {contents} Contents\n   {elements} Elements',
                     'FR':'PLANTA Project:\n   {tasks} Tâches\n\nPLANTA Portfolio:\n   {projects} Projets\n\nPLANTA Request:\n   {srqs} SRQs\n   {rsos} RSOs\n\nPLANTA Sales:\n   {leads} Prospects\n   {offers} Offres\n   {assignments} Ordres\n\nPLANTA Content:\n   {contents} Contenus\n   {elements} Eléments'}
    ITEXT = """UPDATE DT435 SET DI001549 = '%s' WHERE DI001538 = '754581' AND DI001550 = '%s'"""
    code = """SELECT COUNT (*) FROM DT463 A WHERE EXISTS (SELECT * FROM DT461 B WHERE B.DI001042 <> 9 AND A.DI001097=B.DI001001)"""
    STATEMENT = """UPDATE DT343 SET DI040109 = '%s' WHERE DI024373 = '000225'""" % code
    SQL = """UPDATE DT406 SET DI059051 = '0', DI009341 = '0' WHERE DI000969 = '049403'"""

    def fix(self, runner):
        failures = 0
        for lang, const in self.TEXTCONSTANTS.items():
            if not self.modify_and_log(self.ITEXT % (const, lang)):
                failures += 1
        if not self.modify_and_log(self.STATEMENT):
            failures += 1
        if not self.modify_and_log(self.SQL):
            failures += 1
        if not failures:
            return self.success()
        return self.fail()
