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

from ppms.migration import ChangeDTPPacket, SQLPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType
from ppms.constants import *
from ppms import ppms

expected_value_051878 = {DBMS_ORACLE: "from ppms import ppms_cu\nfrom ppms.exits import project_exits\nfrom ppms.exits import transfer_interface\n\ndef checkInput(di, oldvalue):\n    rec = di.get_dtp_record()\n\n    if transfer_interface.Interface_lock(rec).check_status() == False: return False\n\n    # init variables\n    reporting_lock=1\n\n    # get reporting_lock settings of active module\n    active_mod_obj=ppms.get_active_module()\n    if active_mod_obj:\n        rec405=ppms.search_record(405, [active_mod_obj.get_id()], [6101], True)\n        if rec405: reporting_lock=rec405.reporting_lock.get_value()\n\n    \"\"\" check reporting lock vs active model \"\"\"\n    if reporting_lock == 0:\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False: return False\n        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False: return False\n\n    elif reporting_lock == 2: # check deadline_date\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n            return False\n\n    elif reporting_lock == 1: # check lock of project and task\n        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n            return False\n\n    count_load = rec.count_load.get_value()\n    if count_load > 1:\n        for df in di.get_dfs():\n            # set projected fields to output\n            prof_df=df.get_record().get_df(\"proj_act_load_dt085\")\n            if prof_df: prof_df.make_output()\n            if df: df.make_output()\n        ppms.ui_message_id(\"0851\")\n        return False\n\n    return True\n\ncheckInput.deps = (\"DI001519\",\"DI001395\",\"DI001516\")\n\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n\n    for df in di.get_dfs():\n        df_rec = df.get_record()\n        target_date = df_rec.date_calendar.get_raw_value()\n        task_id = df_rec.task_id.get_raw_value()\n        res_id = df_rec.res_id.get_raw_value()\n        pr_id = df_rec.pr_id.get_raw_value()\n        for da in ppms.get_active_module().get_das():\n            if da.get_id() == '045341':\n                for rec in da.get_records():\n                    if rec.pr_id.get_raw_value() == pr_id and \\\n                       rec.task_id.get_raw_value() == task_id and \\\n                       rec.res_id.get_raw_value() == res_id and \\\n                       rec.date.get_raw_value() == target_date:\n                        rec.get_dtp_record().load_act.set_value(di.get_value())\n                        return di.get_value()\n\n        return di.get_value()\n\nprocessInput.deps = (\"DI059672\",)",
                         DBMS_MSSQL: '''from ppms import ppms_cu
from ppms.exits import project_exits
from ppms.exits import transfer_interface

def checkInput(di, oldvalue):
    rec = di.get_dtp_record()

    if transfer_interface.Interface_lock(rec).check_status() == False: return False

    # init variables
    reporting_lock=1

    # get reporting_lock settings of active module
    active_mod_obj=ppms.get_active_module()
    if active_mod_obj:
        rec405=ppms.search_record(405, [active_mod_obj.get_id()], [6101], True)
        if rec405: reporting_lock=rec405.reporting_lock.get_value()

    """ check reporting lock vs active model """
    if reporting_lock == 0:
        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False: return False
        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check="load_act") == False: return False

    elif reporting_lock == 2: # check deadline_date
        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:
            return False

    elif reporting_lock == 1: # check lock of project and task
        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check="load_act") == False:
            return False

    count_load = rec.count_load.get_value()
    if count_load > 1:
        for df in di.get_dfs():
            # set projected fields to output
            prof_df=df.get_record().get_df("proj_act_load_dt085")
            if prof_df: prof_df.make_output()
            if df: df.make_output()
        ppms.ui_message_id("0851")
        return False

    return True

checkInput.deps = ("DI001519","DI001395","DI001516")

def processInput(di, oldvalue):
    rec = di.get_dtp_record()

    for df in di.get_dfs():
        df_rec = df.get_record()
        target_date = df_rec.date_calendar.get_raw_value()
        task_id = df_rec.task_id.get_raw_value()
        res_id = df_rec.res_id.get_raw_value()
        pr_id = df_rec.pr_id.get_raw_value()
        for da in ppms.get_active_module().get_das():
            if da.get_id() == '045341':
                for rec in da.get_records():
                    if rec.pr_id.get_raw_value() == pr_id and                        rec.task_id.get_raw_value() == task_id and                        rec.res_id.get_raw_value() == res_id and                        rec.date.get_raw_value() == target_date:
                        rec.get_dtp_record().load_act.set_value(di.get_value())
                        return di.get_value()

        return di.get_value()

processInput.deps = ("DI059672",)'''}

new_value_051878 = "from ppms import ppms_cu\nfrom ppms.exits import project_exits\nfrom ppms.exits import transfer_interface\n\ndef checkInput(di, oldvalue):\n    rec = di.get_dtp_record()\n\n    if transfer_interface.Interface_lock(rec).check_status() == False:\n        return False\n    # init variables\n    reporting_lock=1\n\n    # get reporting_lock settings of active module\n    active_mod_obj=ppms.get_active_module()\n    if active_mod_obj:\n        rec405=ppms.search_record(405, [active_mod_obj.get_id()], [6101], True)\n        if rec405: reporting_lock=rec405.reporting_lock.get_value()\n    \"\"\" check reporting lock vs active model \"\"\"\n    if reporting_lock == 0:\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n            return False\n\n        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n            return False\n\n    elif reporting_lock == 2: # check deadline_date\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n            return False\n\n    elif reporting_lock == 1: # check lock of project and task \n        if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n            return False\n\n    if not project_exits.reporting_lock(rec).check_max_h():\n        return False\n\n    return True\n\ncheckInput.deps = (\"DI061217\", \"DI051873\")\n\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if rec.count_load.get_value() > 1:\n        for df in di.get_dfs():\n            # set projected fields to output\n            prof_df=df.get_record().get_df(\"proj_act_load_dt085\")\n            if prof_df: prof_df.make_output()\n            if df: df.make_output()\n        ppms.ui_message_id(\"0851\")\n        return oldvalue\n    else:\n        return di.get_value()\nprocessInput.deps = (\"DI059672\",)"


values = {'001510':("from ppms import ppms_cu\nfrom ppms.exits import project_exits\nfrom ppms.exits import transfer_interface\ndef checkInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if 'lock_rec' in ppms_cu.Access.get_ppms_builtins(\"ppms\").keys():\n        auto_var = ppms_cu.Access.get_ppms_builtins(\"ppms\")['lock_rec']\n    else:\n        auto_var = None\n    if auto_var:\n        return di.get_value()\n    else:\n        if transfer_interface.Interface_lock(rec).check_status() == False: return False\n        # init variables\n        reporting_lock=1\n\n        # get reporting_lock settings of active module\n        active_mod_obj=ppms.get_active_module()\n        if active_mod_obj:\n            rec405=ppms.search_record(405, [active_mod_obj.get_id()], [6101], True)\n            if rec405: reporting_lock=rec405.reporting_lock.get_value()\n\n        \"\"\" check reporting lock vs active model \"\"\"\n        if reporting_lock == 0:\n            if project_exits.reporting_lock(rec).check_reporting_lock_date() == False: return False\n            if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False: return False\n\n        elif reporting_lock == 2: # check deadline_date\n            if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n                return False\n\n        elif reporting_lock == 1: # check lock of project and task\n            if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n                return False\n\n        return True\n\ncheckInput.deps = (\"DI060750\",)\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if not rec.ou_cctr_of_res.get_value():\n        rec.ou_cctr_of_res.set_value(rec.curr_ou_cctr_f_res.get_value())\n    return di.get_value()\nprocessInput.deps = (\"DI009064\",\"DI007783\")",
                    "from ppms import ppms_cu\nfrom ppms.exits import project_exits\nfrom ppms.exits import transfer_interface\n\ndef checkInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if 'lock_rec' in ppms_cu.Access.get_ppms_builtins(\"ppms\").keys():\n        auto_var = ppms_cu.Access.get_ppms_builtins(\"ppms\")['lock_rec']\n    else:\n        auto_var = None    \n    if auto_var:\n        return di.get_value()\n    else:\n        if transfer_interface.Interface_lock(rec).check_status() == False:\n            return False\n        # init variables\n        reporting_lock=1\n\n        # get reporting_lock settings of active module\n        active_mod_obj=ppms.get_active_module()\n        if active_mod_obj:\n            rec405=ppms.search_record(405, [active_mod_obj.get_id()], [6101], True)\n            if rec405: reporting_lock=rec405.reporting_lock.get_value()\n        \n        \"\"\" check reporting lock vs active model \"\"\"\n        if reporting_lock == 0:\n            if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n                return False\n\n            if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n                return False\n\n        elif reporting_lock == 2: # check deadline_date\n            if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n                return False\n\n        elif reporting_lock == 1: # check lock of project and task \n            if project_exits.reporting_lock(rec).check_pr_and_task_lock(di_to_check=\"load_act\") == False:\n                return False\n\n        if not project_exits.reporting_lock(rec).check_max_h():\n            return False\n\n        return True\n\ncheckInput.deps = (\"DI060750\", \"DI001519\")\n\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if not rec.ou_cctr_of_res.get_value():\n        rec.ou_cctr_of_res.set_value(rec.curr_ou_cctr_f_res.get_value())\n    return di.get_value()\nprocessInput.deps = (\"DI009064\",\"DI007783\")"),
          '001519':("from ppms import ppms_cu\nfrom ppms.exits import project_exits\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if 'lock_rec' in ppms_cu.Access.get_ppms_builtins(\"ppms\").keys():\n        auto_var = ppms_cu.Access.get_ppms_builtins(\"ppms\")['lock_rec']\n    else:\n        auto_var = None\n    if auto_var:\n        return di.get_value()\n    else:\n        #get attributes\n        date=rec.date.get_value()\n        pr_id=rec.pr_id.get_value()\n        # check model deadline\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False: return oldvalue\n\n        # check date value\n        if date > ppms.uvar_get(\"@37\"):\n            if pr_id:\n                # editing plan values is allowed\n                if rec.load_act.get_value() == 0: return di.get_value()\n\n                # check posting_type of project\n                pr_rec=ppms.search_record(461, [pr_id], [9494], True)\n                if not pr_rec: return oldvalue\n                else:\n                    if pr_rec.posting_type.get_value() != 1:\n                        ppms.ui_message_id(\"0832\")\n                        return oldvalue\n            else:\n                return di.get_value()\n\n        return di.get_value()\n\nprocessInput.deps = (\"DI001510\",\"DI001395\",\"DI001516\",\"DI001517\",\"DI001518\")",
                    "from ppms import ppms_cu\nfrom ppms.exits import project_exits\n\ndef checkInput(di, oldvalue):\n    rec = di.get_dtp_record()\n\n    return project_exits.reporting_lock(rec).check_max_h()\n\ndef processInput(di, oldvalue):\n    rec = di.get_dtp_record()\n    if 'lock_rec' in ppms_cu.Access.get_ppms_builtins(\"ppms\").keys():\n        auto_var = ppms_cu.Access.get_ppms_builtins(\"ppms\")['lock_rec']\n    else:\n        auto_var = None    \n    if auto_var:\n        return di.get_value()\n    else:\n        #get attributes\n        date=rec.date.get_value()\n        pr_id=rec.pr_id.get_value()\n\n        # check model deadline\n        if project_exits.reporting_lock(rec).check_reporting_lock_date() == False:\n            return oldvalue\n        \n        # check date value\n        if date > ppms.uvar_get(\"@37\"):\n            if pr_id:\n                # editing plan values is allowed\n                if rec.load_act.get_value() == 0:\n                    return di.get_value()\n                \n                # check posting_type of project\n                pr_rec=ppms.search_record(461, [pr_id], [9494], True)\n                if not pr_rec:\n                    return oldvalue\n                else:\n                    if pr_rec.posting_type.get_value() != 1:\n                        ppms.ui_message_id(\"0832\")\n                        return oldvalue\n            else:\n                return di.get_value()\n        \n        return di.get_value()\n    \n\nprocessInput.deps = (\"DI001510\",\"DI001395\",\"DI001516\",\"DI001517\",\"DI001518\")")}


class FixMaxHours(ChangeDTPPacket):
    '''Fix maximum hours on resource not working'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10362

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

    def fix(self, runner):
        errors = 0

        for di, (old_value, new_value) in values.items():
            record = self.get_record(412, [di], ['valuerange'])
            if not record:
                errors += 1

            if not self.change_value(record, 'valuerange', old_value, new_value):
                errors += 1

        record = self.get_record(412, ['051878'], ['valuerange'])
        if not record:
            errors += 1

        else:
            if not self.change_value(record, 'valuerange', expected_value_051878[ppms.db_get_dbms_name()], new_value_051878):
                errors += 1

        if not errors:
            return self.success()

        return self.fail()

class AddMaxHoursTextConstant(SQLPacket):
    '''Port text constants from Venus to VoE'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10362

    SQL = 'WI10362_fix_max_hours.sql'

    def fix(self, runner):
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.SQL)

        queries = self.get_queries_from_file(sqlpath)

        for query in queries:
            self.modify_and_log(query)

        return self.success()
