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


class PortRemainingLoad(SQLPacket):
    """Ports the multiplexing on 'remaining load'"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 19539

    filename = 'WI19539_port_remaining_load.sql'

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


    def fix(self, runner):
        if self.update_value_range():
            return self.success()
        else:
            return self.fail("update of DI001511 failed")

    def update_value_range(self):
        valuerange = """
from ppms import ppms
def processInput(di, old_value):
    new_value = di.get_value()
    rec = di.get_dtp_record()
    pr_id = rec.pr_id.get_value()
    task_id = rec.task_id.get_value()
    res_id = rec.res_id.get_value()
    rec_466 = ppms.search_record(466,[pr_id, task_id,res_id],['actual_start'])

    if rec_466:
        date= rec_466.actual_start.get_value()
    else:
        date =0
    if not date:
        rec.planned_load.set_value(new_value)

    return new_value
processInput.deps = ("planned_load",'pr_id', 'task_id', 'res_id')"""
        di_rec = ppms.search_record(412,['001511'],['valuerange', 'wb_type'])
        di_rec.wb_type.set_value('PY')
        di_rec.valuerange.set_value(valuerange)
        return di_rec.save()