<%@language = "javascript" %>
<%
/*
    $Revision$
    $Id$

    File:               pda/ins_deel_new.asp
    Description:        Add object (Mobile version)
    Parameters:         ins_deel_key voor bestaand deel
                        ins_deel_key=-1 voor nieuw deel

    Note:

*/
%>

<% REGISTERMENU_Disable = true; %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/Suggest/plaatsFilter.inc" -->
<!-- #include file="../Shared/Suggest/disciplineFilter.inc" -->
<!-- #include file="../Shared/Suggest/srtgroepFilter.inc" -->
<!-- #include file="../Shared/Suggest/srtdeelFilter.inc" -->
<!-- #include file="../Shared/discxalg3d.inc" -->
<!-- #include file="../INS/ins.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="../shared/data_recordset.inc" -->
<!-- #include file="./resultset_table.inc" -->


<%
FCLTHeader.Requires({ js: ["jquery.timepicker-table.js"],
                      css: ["timePicker-table.css"] });

var module  = "INS";
var urole   = getQParamSafe("urole", "fe");
var qrc     = getQParamInt("qrc", 0) != 0;
var ins_key = getQParamInt("ins_key", -1);
var return_from = getQParam("return_from", "");

var prs_obj = new Perslid(user_key);
var ins_soort = getAllSrtInstallatie();
var ins_plaats = getInsPlaats();
var alg_typ = ins_plaats.alg_type;
var alg_key = ins_plaats.alg_key;
var transitParam_arr = ["urole", "qrc", "ins_key", "alg_key", "alg_typ", "disc", "srtgrp", "srtobj", "srtbnd"];

function setTransitParams(org_transit, change_transit_obj, fromstart)
{   // change_transit_obj: {"alg_key": <new value>, ..}
    var t = buildTransitParam(org_transit, change_transit_obj);
    if (fromstart && t.length && t.substr(0,1) == "&")
    {   // haal het eerste &-teken weg
        t = t.replace("&", "");
    }
    return t;
}

function getAllSrtInstallatie()
{
    var asi = { disc_key:     getQParamInt("disc", -1)
              , srtgroep_key: getQParamInt("srtgrp", -1)
              , srtdeel_key:  getQParamInt("srtobj", -1)
              , binding:      getQParamInt("srtbnd", 0)
              };
    if (  asi.binding != BIND_RUIMTE && asi.binding != BIND_WERKPL && asi.binding != BIND_TERREI
       && asi.binding != BIND_AFDELI && asi.binding != BIND_PERSOO && asi.binding != BIND_INSDEE
       )
    {   // Geen eenduidige binding (moet een macht van 2 zijn).
        asi.binding = 0;
    }

    var sql = "SELECT NVL(a.ins_discipline_key, -1) disc"
            + "     , NVL(a.ins_srtgroep_key, -1)   srtg"
            + "     , NVL(a.ins_srtdeel_key, -1)    srtd"
            + "     , NVL(s.ins_srtdeel_binding, 0) bind"
            + "  FROM ins_v_allsrtinstallatie a"
            + "     , ins_srtdeel s"
            + " WHERE a.ins_srtdeel_key = s.ins_srtdeel_key(+)";
    if (asi.srtdeel_key > -1)
        sql += " AND a.niveau = 'S' AND a.ins_srtdeel_key = " + asi.srtdeel_key;
    else if (asi.srtgroep_key > -1)
        sql += " AND a.niveau = 'G' AND a.ins_srtgroep_key = " + asi.srtgroep_key;
    else if (asi.disc_key > -1)
        sql += " AND a.niveau = 'D' AND a.ins_discipline_key = " + asi.disc_key;
    else
        sql += " AND 0=1 ";
    var oRs = Oracle.Execute(sql);
    if (!oRs.eof)
    {   // in ieder geval 1 geldige key
        asi.disc_key     = oRs("disc").Value;
        asi.srtgroep_key = oRs("srtg").Value;
        asi.srtdeel_key  = oRs("srtd").Value;
        asi.hasbindings  = oRs("bind").Value;
        asi.binding      = asi.binding & asi.hasBindings;
    }
    oRs.Close();
    return asi;
}

function getInsPlaats()
{
    var alg_key = getQParamInt("alg_key", -1);
    var alg_typ = getQParamSafe("alg_typ", "");
    if (alg_typ != "" && alg_key > 0)
    {   // alg_type en alg_key zijn al bekend
        var gip = { alg_type: alg_typ
                  , alg_key: alg_key
                  , isnew: false
                  };
    }
    else
    {   // Van plaatsselector terug gekregen:
        var loc_key = getQParamInt("loc_key", -1);
        var bld_key = getQParamInt("bld_key", -1);
        var flr_key = getQParamInt("flr_key", -1);
        var roo_key = getQParamInt("roo_key", -1);
        var wrk_key = getQParamInt("wrk_key", -1);

        //alg_typ = "";
        alg_key = -1;
        if (bld_key > 0 && flr_key == -1) { alg_typ = "T"; alg_key = bld_key; }
        if (roo_key > 0 && wrk_key == -1) { alg_typ = "R"; alg_key = roo_key; }
        if (wrk_key > 0)                  { alg_typ = "W"; alg_key = wrk_key; }

        var gip = { alg_type: alg_typ
                  , alg_key: alg_key
                  , loc_key: (inArray(alg_typ, ["T", "R", "W"]) ? loc_key : null)
                  , isnew: true
                  , selector: { loc: loc_key
                              , bld: bld_key
                              , flr: flr_key
                              , roo: roo_key
                              , wrk: wrk_key
                              }
                  }
    }
    return gip;
}

var autfunction = (urole == "fo"? "WEB_INSFOF" : "WEB_INSMAN");
if (ins_soort.disc_key > 0)
{   // Als er discipline key is meegegeven waar de user op geen enkele write rechten op heeft, dan mag de user het object niet toevoegen.
    var authparams = user.checkAutorisation(autfunction, false, ins_soort.disc_key);
    var PRSALGwrite = authparams.PRSwritelevel < 9 && authparams.ALGwritelevel < 9;
    user.anything_todo_or_abort(PRSALGwrite);
}
else
{
    var authparams = user.checkAutorisation(autfunction);
}


var subject = L("lcl_mobile_newdeel");
var curPage = 0;
if (ins_key == -1)
{
    if (ins_soort.disc_key == -1)
    {
        curPage = 1;
        subject = L("lcl_mobile_selectinsdisc");
    }
    else if (ins_soort.srtgroep_key == -1)
    {
        curPage = 2;
        subject = L("lcl_mobile_selectsrtgroep");
    }
    else if (ins_soort.srtdeel_key == -1)
    {
        curPage = 3;
        subject = L("lcl_mobile_selectsrtdeel");
    }
    else if (ins_plaats.alg_key == -1 && ins_plaats.alg_type == "")
    {
        curPage = 4;
        subject = L("lcl_obj_bind");
    }
    else if (ins_plaats.alg_key == -1 && ins_plaats.alg_type != "")
    {
        switch (ins_plaats.alg_type)
        {  case "R":
           case "T":
           case "W": curPage = 5; break; // plaatsselector
           case "A": curPage = 6; subject = L("lcl_mobile_selectprsafdeling"); break;
           case "P": curPage = 7; subject = L("lcl_mobile_selectprsperslid");  break;
        }
    }
    else
    {   // Alle basis gegevens zijn nu bekend.
        curPage = 9;
    }
}
lcl.set_dialect(ins_soort.disc_key, 'INS_DISCIPLINE_KEY');


%>
<html>
  <head>
<% FCLTMHeader.Generate({title: subject }); %>
  </head>
  <body>
<%
    PAGE_START({id: "page-"+curPage, pclass: "deel-new"});
      HEADER({title: subject, back:!qrc, home:!qrc });
      CONTENT_START();

        if (curPage == 0)
        {
            ROFIELD("fld", "AiAi", "Verkeerde pagina");
        }

        if (curPage == 1)
        {   // discipline
            var params = { autfunctionKeyArray: ( typeof authparams.autfunctionkey == "object"
                                                ? authparams.autfunctionkey
                                                : [authparams.autfunctionkey]
                                                )
                         , autRW: "W"
                         , srtdisc_key: -1
                         , disc_key: -1
                         , disc: -1
                         , ins_key_arr: []
                         , loc_key: -1
                         , bld_key: -1
                         , flr_key: -1
                         , room_key: -1
                         , perslidKey: user_key
                         , ref_type: ""
                         , module: module
                         , filtcode: ""
                         };
            var extracode = "A";
            var cndfilter = false;
            var filtClause = getFiltClauseDiscipline(params.filtcode, params);
            var extraInf = getExtraInfDiscipline(extracode);

            var sql = "SELECT DISTINCT td.ins_discipline_key"
                    + "     , " + ( params.srtdisc_key > 0
                                  ? ""
                                  : "NVL2(sd.ins_srtdiscipline_prefix, sd.ins_srtdiscipline_prefix || '-', '') || "
                                  )
                                + lcl.xsql("td.ins_discipline_omschrijving", "td.ins_discipline_key") + " ins_discipline_omschrijving"
                    + "     , " + lcl.xsqla("sd.ins_srtdiscipline_omschrijving", "sd.ins_srtdiscipline_key")
                    +         extraInf.selectstr
                    + "     , ins_discipline_volgnr"
                    + "  FROM ins_tab_discipline td"
                    + "     , ins_srtdiscipline sd"
                    +         filtClause.from
                    +         extraInf.fromstr
                    + " WHERE td.ins_srtdiscipline_key = sd.ins_srtdiscipline_key(+)" // Alleen bij MLD is de ins_srtdiscipline_key (altijd) ingevuld
                    + "   AND td.ins_discipline_verwijder IS NULL"
                    +         filtClause.where
                    +         ( cndfilter
                              ? " AND sd.ins_srtdiscipline_key != " + S("cnd_gebreken_srtdisc_key")
                              : ""
                              )
                    +         extraInf.wherestr
                    + " ORDER BY ins_discipline_volgnr, ins_discipline_omschrijving";

            function fnDiscLink(oRs)
            {
                var disc_key = oRs("ins_discipline_key").Value;
                return "?" + setTransitParams(transitParam_arr, {disc: disc_key}, true);
            }

            var rst = new ResultsetTable({ sql: sql
                                         , keyColumn: "ins_discipline_key"
                                         , linkColumn: fnDiscLink
                                         , singleLink: true
                                         , headerColumn: "ins_discipline_omschrijving"
                                         , ID: "disctable"
                                         , showAll: true
                                         , emptySetString: L("lcl_empty")
                                        });
            var cnt = rst.processResultset();
        }

        if (curPage == 2)
        {   // groep
            var params = { autfunction: autfunction
                         , disc_key: ins_soort.disc_key
                         , module: module
                         , filtcode: ""
                         };

            var filtClause = getFiltClauseSrtgroep(params.filtcode, params);
            var sql = "SELECT sg." + module + "_srtgroep_key srtgroep_key"
                    + "     , " + lcl.xsql("sg." + module + "_srtgroep_omschrijving", "sg." + module + "_srtgroep_key") + " srtgroep_omschrijving"
                    + "     , " + lcl.xsqla("td.ins_discipline_omschrijving", "td.ins_discipline_key")
                    + "     , td.ins_discipline_key extra"
                    + "  FROM " + module + "_v_aanwezigsrtgroep sg"
                    + "     , ins_tab_discipline td"
                    + " WHERE td.ins_discipline_key = sg.ins_discipline_key"
                    + "   AND td.ins_discipline_verwijder IS NULL"
                    +         filtClause
                    + " ORDER BY " + module + "_srtgroep_omschrijving ASC";

            function fnSrtgroepLink(oRs)
            {
                var srtgroep_key = oRs("srtgroep_key").Value;
                return "?" + setTransitParams(transitParam_arr, {srtgrp: srtgroep_key}, true);
            }

            var rst = new ResultsetTable({ sql: sql
                                         , keyColumn: "srtgroep_key"
                                         , linkColumn: fnSrtgroepLink
                                         , singleLink: true
                                         , headerColumn: "srtgroep_omschrijving"
                                         , ID: "srtgroeptable"
                                         , showAll: true
                                         , emptySetString: L("lcl_empty")
                                        });
            var cnt = rst.processResultset();
        }

        if (curPage == 3)
        {   // objectsoort
            var extracode = "B";
            var params = { autfunction: autfunction
                         , disc_key: ins_soort.disc_key
                         , srtgroep_key: ins_soort.srtgroep_key
                         , module: module
                         , filtcode: ""
                         };
            var filtClause = getFiltClauseSrtdeel(params.filtcode, params);
            var extraInf = getExtraInfSrtdeel(extracode, module);

            var koppelteken = ((S("ins_srtdeelsuggest_info") == 3) ? "||' - '||" : "");
            var sql = "SELECT sd." + module + "_srtdeel_key srtdeel_key"
                    + "     , CASE WHEN sd." + module + "_srtdeel_nr IS NULL"
                       + "         THEN ''"
                       + "         ELSE sd." + module + "_srtdeel_nr || ' '"
                       + "    END ||"
                       +      lcl.xsql("sd." + module + "_srtdeel_omschrijving",  "sd." + module + "_srtdeel_key")
                    + ( module == "INS"
                      ?  " || CASE WHEN sd." + module + "_srtdeel_code IS NULL"
                       + "         THEN ''"
                       + "         ELSE ' ('||sd." + module + "_srtdeel_code ||') '"
                       + "    END "
                      :  ""
                      )+ " srtdeel_omschrijving"
                    + "     , " + (S("ins_srtdeelsuggest_info") & 2  ? lcl.xsql("id." + module + "_discipline_omschrijving", "id." + module + "_discipline_key") + koppelteken : "")
                                + (S("ins_srtdeelsuggest_info") & 1  ? lcl.xsql("sg." + module + "_srtgroep_omschrijving",   "sg." + module + "_srtgroep_key") : "")
                       + " srtgroep_omschrijving"
                    +         extraInf
                    + "  FROM " + module + "_srtdeel sd"
                    + "     , " + module + "_v_aanwezigsrtgroep sg"
                    +         filtClause.from
                    + " WHERE sd." + module + "_srtgroep_key = sg." + module + "_srtgroep_key"
                    + "   AND (  " + module + "_srtdeel_vervaldatum IS NULL"
                    + "       OR " + module + "_srtdeel_vervaldatum > SYSDATE"
                    + "       )"
                    +         filtClause.where
                    + " ORDER BY sd." + module + "_srtdeel_nr"
                    + "        , UPPER(" + lcl.xsql("sd." + module + "_srtdeel_omschrijving", "sd." + module + "_srtdeel_key") + ")";

            function fnSrtdeelLink(oRs)
            {
                var srtdeel_key = oRs("srtdeel_key").Value;
                return "?" + setTransitParams(transitParam_arr, {srtobj: srtdeel_key}, true);
            }

            var rst = new ResultsetTable({ sql: sql
                                         , keyColumn: "srtdeel_key"
                                         , linkColumn: fnSrtdeelLink
                                         , singleLink: true
                                         , headerColumn: "srtdeel_omschrijving"
                                         , ID: "srtdeeltable"
                                         , showAll: true
                                         , emptySetString: L("lcl_empty")
                                        });
            var cnt = rst.processResultset();
        }

        if (curPage == 4)
        {   // Toon alleen de bindings die gebruikt mogen worden bij het srtdeel.
            var bind_arr = [];
            if ((ins_soort.hasbindings & BIND_RUIMTE) > 0) bind_arr.push({ id: "bindR", label: L("lcl_obj_bind_r"), value: "R", click: "set_bind('R');"});
            if ((ins_soort.hasbindings & BIND_WERKPL) > 0) bind_arr.push({ id: "bindW", label: L("lcl_obj_bind_w"), value: "W", click: "set_bind('W');"});
            if ((ins_soort.hasbindings & BIND_TERREI) > 0) bind_arr.push({ id: "bindT", label: L("lcl_obj_bind_t"), value: "T", click: "set_bind('T');"});
            if ((ins_soort.hasbindings & BIND_AFDELI) > 0) bind_arr.push({ id: "bindA", label: L("lcl_obj_bind_a"), value: "A", click: "set_bind('A');"});
            if ((ins_soort.hasbindings & BIND_PERSOO) > 0) bind_arr.push({ id: "bindP", label: L("lcl_obj_bind_p"), value: "P", click: "set_bind('P');"});
            if ((ins_soort.hasbindings & BIND_INSDEE) > 0) bind_arr.push({ id: "bindI", label: L("lcl_obj_bind_i"), value: "I", click: "set_bind('I');"});
            if (bind_arr.length > 0)
            {
                CONTROLGROUP_START();
                    for (var i=0; i<bind_arr.length; i++)
                    {
                        BUTTON(bind_arr[i].label, bind_arr[i]);
                    }
                CONTROLGROUP_END();
                var v_url = "ins_deel.asp?" + setTransitParams(transitParam_arr, null, true);
            %>
            <script>
                function set_bind(c)
                {
                    var v_url = "<%=v_url%>" + "&alg_typ=" + c;
                    window.location.replace(v_url);
                }

                $(function()
                    {
                        if (<%=bind_arr.length%> == 1)
                        {   // slechts 1 binding beschikbaar
                            set_bind("<%=bind_arr[0].value%>");
                        }
                    });
            </script>
            <%
            }
            else
            {
                %><label><%=L("lcl_mobile_no_srtdeel")%></label><%
            }
        }

        if (curPage == 5)
        {   // plaats
            var return_to_param = safe.url("ins_deel_new.asp?urole="+urole+ setTransitParams(transitParam_arr, {ins_key: ins_key}, false));
            var return_from_param = (return_from.length ? "&return_from=" + safe.url(return_from) : "");
            var locatie_key = prs_obj.alg_locatie_key();
            var verplaats_params = "?x=b"
                                 + "&urole=" + urole
                                 + "&startlevel=2" // is default in plaatsselector
                                 + "&eindlevel=" + (ins_plaats.alg_type == "W" ? 6 : (ins_plaats.alg_type == "T" ? 3 : 5))
                                 + "&autlevel=-1"
                                 + "&autoopen=2"
                                 + "&filtCode=" + "INCVR"
                                 + "&bind=" + (ins_plaats.alg_type == "W" || ins_plaats.alg_type == "R" ? "G" : (ins_plaats.alg_type == "T" ? "T" : ""))
                                 + "&loc_key=" + locatie_key
                                 + "&return_to=" + return_to_param
                                 + return_from_param;
            var url = "plaatsselector.asp" + verplaats_params;
            %>
            <script>
                $(function()
                    {
                        if (<%=alg_key%> == -1)
                        {   // Ga naar de plaatsselector.
                            v_url = "<%=url%>" ;
                            window.location.replace(v_url);
                        }
                    });
            </script>
            <%
        }

        if (curPage == 6)
        { // afdeling
            var autlevel = authparams.PRSwritelevel;
            var filtClause = "";
            if (autlevel == 0)
            {   // Bedrijf niveau zit niet in prs_v_afdeling_familie
                filtClause = " AND d.prs_afdeling_key IN"
                           + "     (SELECT a.prs_afdeling_key FROM prs_v_afdeling a "
                           + "       WHERE a.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key()
                           + "     )";
            }
            else if (autlevel > 0)
            {   // geen filtercode
                filtClause = " AND d.prs_afdeling_key IN"
                           + "     (SELECT prs_afdeling_key"
                           + "        FROM prs_v_afdeling_familie a"
                           + "       WHERE a.prs_afdeling_elder_key IN"
                                    + "   (SELECT aa.prs_afdeling_elder_key"
                                    + "      FROM prs_v_afdeling_familie aa"
                                    + "     WHERE aa.prs_afdeling_key = " + user.prs_afdeling_key()
                                    + "       AND aa.niveau = " + autlevel
                                    + "   )"
                           + "     )";
            }

            var sql = "SELECT d.prs_afdeling_key"
                    + "     , " + S("prs_dep_string") + " dep"
                    + "     , " + S("prs_kpn_string") + " kpl"
                    + "     , prs_afdeling_naam3 afd"
                    + "  FROM prs_v_aanwezigafdeling d"
                    + "     , prs_v_aanwezigkostenplaats k"
                    + " WHERE d.prs_kostenplaats_key = k.prs_kostenplaats_key (+)"
                    + filtClause
                    + " ORDER BY 2"

            function fnAfdelingLink(oRs)
            {
                var afdeling_key = oRs("prs_afdeling_key").Value;
                return "?" + setTransitParams(transitParam_arr, {alg_key: afdeling_key}, true);
            }

            var rst = new ResultsetTable({ sql: sql
                                         , keyColumn: "prs_afdeling_key"
                                         , linkColumn: fnAfdelingLink
                                         , singleLink: true
                                         , headerColumn: "dep"
                                         , ID: "afdelingtable"
                                         , showAll: true
                                         , emptySetString: L("lcl_empty")
                                        });
            var cnt = rst.processResultset();
        }

        if (curPage == 7)
        {   // persoon
            var sql = "SELECT p.prs_perslid_key"
                    + "     , " + S("prs_pers_string") + " naam"
                    + "     , " + S("prs_dep_string") + " afdeling"
                    + "     , prs_perslid_telefoonnr"
                    + "  FROM prs_perslid p"
                    + "     , prs_v_afdeling d "
                    + " WHERE p.prs_afdeling_key = d.prs_afdeling_key"
                    + "   AND p.prs_perslid_verwijder IS NULL"
                    + "   AND (prs_perslid_oslogin IS NULL OR prs_perslid_oslogin NOT LIKE '\\_%' ESCAPE '\\')"
                    + "   AND BITAND(prs_perslid_flags, 2) = 0"
                    + " ORDER BY 2";

            function fnPersoonLink(oRs)
            {
                var perslid_key = oRs("prs_perslid_key").Value;
                return "?" + setTransitParams(transitParam_arr, {alg_key: perslid_key}, true);
            }

            var rst = new ResultsetTable({ sql: sql
                                         , keyColumn: "prs_perslid_key"
                                         , linkColumn: fnPersoonLink
                                         , singleLink: true
                                         , headerColumn: "naam"
                                         , ID: "persoontable"
                                         , showAll: true
                                         , emptySetString: L("lcl_empty")
                                        });
            var cnt = rst.processResultset();
        }

        if (curPage == 9)
        {   // de gegevens van het object (naar ins_deel in edit-mode)
            var url = "ins_deel.asp?urole=" + urole + "&ins_key=-1"
                    + "&disc=" + ins_soort.disc_key + "&srtgrp=" + ins_soort.srtgroep_key + "&srtobj=" + ins_soort.srtdeel_key
                    + "&alg_typ=" + ins_plaats.alg_type + "&alg_key=" + ins_plaats.alg_key;
            %>
            <script>
                $(function()
                    {
                        purl = "<%=url%>";
                        window.location.replace(purl);
                    });
            </script>
            <%
        }

      CONTENT_END();
      FOOTER();
    PAGE_END();
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
