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

    File:               get_bgt_info_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         init_key
                        niveau
    Context:
    Note:
    Sample:             appl/bgt/get_bgt_info_ajax.asp?parent_key=1&niveau=G

*/ %>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc"-->
<%
var req_info = getQParam("req_info");
var niveau = getQParam("niveau");
var root_key = getQParamInt("root_key", -1);
var parent_key = getQParamInt("parent_key");
var child_key = getQParamInt("child_key", -1);
var init_key = getQParamInt("init_key", -1);
var sel_mode = getQParam("sel_mode", "");
var sql = "";
var order = "";
var result = {};

switch (req_info)
{
    case "initsearch":
    {
        switch (niveau)
        {
            case "D":
            {   sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key  disc_key"
                    + "     , -1 project_key"
                    + "     , -1 rubriek_key"
                    + "     , -1 groep_key"
                    + "     , -1 soort_key"
                    + "     , -1 opdracht_key"
                    + "     , -1 bedrijf_key"
                    + "     , NULL kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , ins_tab_discipline t"
                    + " WHERE t.ins_discipline_key = d.bgt_ins_discipline_key"
                    + "   AND t.ins_discipline_module = 'BGT'"
                    + "   AND t.ins_discipline_verwijder IS NULL"
                    + (init_key != -1 ? " AND d.bgt_ins_discipline_key = " + init_key : "");
                break;
            }
            case "P":
            {   sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , -1 rubriek_key"
                    + "     , -1 groep_key"
                    + "     , -1 soort_key"
                    + "     , -1 opdracht_key"
                    + "     , -1 bedrijf_key"
                    + "     , COALESCE(p.prs_kostenplaats_key, -1) kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_verwijder IS NULL"
                    + (init_key != -1 ? " AND p.bgt_project_key = " + init_key : "");
                break;
            }
            case "O":
            {
                sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , " + (sel_mode == "order_edit" || sel_mode == "order_new" ? "r.bgt_kostenrubriek_key" : "-1") + " rubriek_key"
                    + "     , g.prs_kostensoortgrp_key  groep_key"
                    + "     , s.prs_kostensoort_key  soort_key"
                    + "     , o.mld_opdr_key opdracht_key"
                    + "     , b.prs_bedrijf_key bedrijf_key"
                    + "     , o.prs_kostenplaats_key kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + "     , prs_kostensoortgrp g"
                    + "     , prs_kostensoort s"
                    + "     , mld_opdr o"
                    + "     , prs_bedrijf b"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_key        = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key  = g.bgt_kostenrubriek_key"
                    + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                    + "   AND s.prs_kostensoort_key    = o.prs_kostensoort_key"
                    + "   AND o.mld_uitvoerende_keys   = b.prs_bedrijf_key"
                    + "   AND p.bgt_project_verwijder	     IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND s.prs_kostensoort_verwijder	 IS NULL"
                    + "   AND b.prs_bedrijf_verwijder	     IS NULL"
                    + (init_key != -1 ? " AND o.mld_opdr_key = " + init_key : "");
                break;
            }
            case "B":
            {
                sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , -1 rubriek_key"
                    + "     , -1 groep_key"
                    + "     , -1 soort_key"
                    + "     , -1 opdracht_key"
                    + "     , b.prs_bedrijf_key bedrijf_key"
                    + "     , NULL kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + "     , prs_kostensoortgrp g"
                    + "     , prs_kostensoort s"
                    + "     , mld_opdr o"
                    + "     , prs_bedrijf b"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_key        = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key  = g.bgt_kostenrubriek_key"
                    + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                    + "   AND s.prs_kostensoort_key    = o.prs_kostensoort_key"
                    + "   AND o.mld_uitvoerende_keys   = b.prs_bedrijf_key"
                    + "   AND p.bgt_project_verwijder        IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND s.prs_kostensoort_verwijder	 IS NULL"
                    + "   AND b.prs_bedrijf_verwijder	     IS NULL"
                    + (parent_key != -1 ? " AND p.bgt_project_key = " + parent_key : "")  // Een bedrijf kan in meerdere disciplines/projecten gebruikt worden!
                    + (init_key != -1 ? " AND b.prs_bedrijf_key = " + init_key : "");
                break;
            }
            case "R":
            {   sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , r.bgt_kostenrubriek_key rubriek_key"
                    + "     , -1 groep_key"
                    + "     , -1 soort_key"
                    + "     , NULL opdracht_key"
                    + "     , NULL bedrijf_key"
                    + "     , NULL kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_key        = r.bgt_project_key"
                    + "   AND p.bgt_project_verwijder IS NULL"
                    + (init_key != -1 ? " AND r.bgt_kostenrubriek_key = " + init_key : "");
                break;
            }
            case "G":
            {   sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , r.bgt_kostenrubriek_key rubriek_key"
                    + "     , g.prs_kostensoortgrp_key groep_key"
                    + "     , -1 soort_key"
                    + "     , NULL opdracht_key"
                    + "     , NULL bedrijf_key"
                    + "     , NULL kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + "     , prs_kostensoortgrp g"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_key        = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key  = g.bgt_kostenrubriek_key"
                    + "   AND p.bgt_project_verwijder        IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + (init_key != -1 ? " AND g.prs_kostensoortgrp_key = " + init_key : "");
                break;
            }
            case "S":
            {   sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , r.bgt_kostenrubriek_key rubriek_key"
                    + "     , g.prs_kostensoortgrp_key groep_key"
                    + "     , s.prs_kostensoort_key soort_key"
                    + "     , -1 opdracht_key"
                    + "     , -1 bedrijf_key"
                    + "     , COALESCE(s.prs_kostenplaats_key, p.prs_kostenplaats_key, -1) kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + "     , prs_kostensoortgrp g"
                    + "     , prs_kostensoort s"
                    + " WHERE d.bgt_ins_discipline_key = p.ins_discipline_key"
                    + "   AND p.bgt_project_key        = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key  = g.bgt_kostenrubriek_key"
                    + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                    + "   AND p.bgt_project_verwijder        IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND s.prs_kostensoort_verwijder	 IS NULL"
                    + (init_key != -1 ? " AND s.prs_kostensoort_key = " + init_key : "");
                break;
            }
            case "KP":
            {
                sql = "SELECT DISTINCT"
                    + "       d.bgt_ins_discipline_key disc_key"
                    + "     , p.bgt_project_key project_key"
                    + "     , NULL rubriek_key"
                    + "     , NULL groep_key"
                    + "     , NULL soort_key"
                    + "     , NULL opdracht_key"
                    + "     , NULL bedrijf_key"
                    + ( init_key == -1
                      ? ", (SELECT COALESCE(T2.kpn_key, T1.kpn_key, -1)"
                      + "     FROM (SELECT MAX(prs_kostenplaats_key) kpn_key FROM bgt_project     WHERE bgt_project_key     = " + parent_key + ") T1"
                      + "        , (SELECT MAX(prs_kostenplaats_key) kpn_key FROM prs_kostensoort WHERE prs_kostensoort_key = " + child_key  + ") T2"
                      + "  )"
                      : ", -1"
                      ) + " kostenplaats_key"
                    + "  FROM bgt_disc_params d"
                    + "     , bgt_project p"
                    + "     , prs_kostenplaats s"
                    + " WHERE d.bgt_ins_discipline_key  = p.ins_discipline_key"
                    + "   AND s.ins_discipline_key(+)   = p.ins_discipline_key"
                    + "   AND s.prs_kostenplaats_verwijder IS NULL"
                    + "   AND p.bgt_project_verwijder      IS NULL"
                    + "   AND s.prs_kostenplaats_verwijder IS NULL"
                    + (parent_key != -1 ? " AND p.bgt_project_key = " + parent_key : "")
                    + (init_key   != -1 ? " AND (s.prs_kostenplaats_key = " + init_key + " OR p.prs_kostenplaats_key = " + init_key + ")" : "");
                break;
            }
        }
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            result.parents = {"D": oRs("disc_key").Value,
                              "P": oRs("project_key").Value,
                              "R": oRs("rubriek_key").Value,
                              "G": oRs("groep_key").Value,
                              "S": oRs("soort_key").Value,
                              "O": oRs("opdracht_key").Value,
                              "B": oRs("bedrijf_key").Value,
                              "K": oRs("kostenplaats_key").Value
                             }
        }
        oRs.Close();
        result.niveau = niveau;
        result.sel_mode = sel_mode;
        break;
    }
    case "projectsearch":
    {
        switch (niveau)
        {
            case "D":
            {
                sql = "SELECT d.ins_discipline_key sel_key"
                    + "     , d.ins_discipline_omschrijving sel_oms"
                    + "     , d.ins_discipline_btw btw_inc"
                    + "     , NULL btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , NULL kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM ins_tab_discipline d"
                    + "     , bgt_disc_params p"
                    + " WHERE p.bgt_ins_discipline_key = d.ins_discipline_key"
                    + "   AND ins_discipline_module = 'BGT'"
                    + "   AND ins_discipline_verwijder IS NULL"
                    + (parent_key != -1 ? "   AND d.ins_discipline_key = " + parent_key : "")
                    + "   AND ins_discipline_key IN"
                          + " (SELECT ins_discipline_key FROM fac_v_webgebruiker"
                          + "   WHERE prs_perslid_key = " + user_key
                          + "     AND (fac_gebruiker_prs_level_read<9 OR fac_gebruiker_alg_level_read<9))";

                    order = " ORDER BY NVL(ins_discipline_volgnr, 0)"
                          + "        , d.ins_discipline_omschrijving";
                break;
            }
            case "P":
            {
                sql = "SELECT p.bgt_project_key sel_key"
                    + "     , p.bgt_project_code ||' - '|| p.bgt_project_omschrijving sel_oms"
                    + "     , (SELECT d1.ins_discipline_btw"
                            + "  FROM ins_tab_discipline d1"
                            + " WHERE d1.ins_discipline_key = p.ins_discipline_key"
                            + ") btw_inc"
                    + "     , NULL btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , p.prs_kostenplaats_key kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM bgt_project p"
                    + " WHERE p.bgt_project_verwijder IS NULL"
                    + "   AND p.ins_discipline_key = " + parent_key;
                order = " ORDER BY NVL(p.bgt_project_volgnr, 0)"
                      + "     , p.bgt_project_code"
                      + "     , p.bgt_project_code ||' - '|| p.bgt_project_omschrijving";
                break;
            }
            case "R":
            {
                sql = "SELECT r.bgt_kostenrubriek_key sel_key"
                    + "     , r.bgt_kostenrubriek_oms sel_oms"
                    + "     , (SELECT d1.ins_discipline_btw"
                            + "  FROM ins_tab_discipline d1"
                            + "     , bgt_project p1"
                            + " WHERE d1.ins_discipline_key = p1.ins_discipline_key"
                            + "   AND p1.bgt_project_key = r.bgt_project_key"
                            + ") btw_inc"
                    + "     , (SELECT f1.fin_btwtabelwaarde_perc"
                            + "  FROM fin_btwtabelwaarde f1"
                            + " WHERE f1.fin_btwtabelwaarde_key = r.fin_btwtabelwaarde_key"
                            + ") btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , NULL kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM bgt_kostenrubriek r"
                    + " WHERE r.bgt_project_key = " + parent_key;
                break;
            }
            case "G":
            {
                sql = "SELECT g.prs_kostensoortgrp_key sel_key"
                    + "     , g.prs_kostensoortgrp_oms sel_oms"
                    + "     , (SELECT d1.ins_discipline_btw"
                            + "  FROM ins_tab_discipline d1"
                            + "     , bgt_project p1"
                            + "     , bgt_kostenrubriek r1"
                            + " WHERE d1.ins_discipline_key = p1.ins_discipline_key"
                            + "   AND p1.bgt_project_key = r1.bgt_project_key"
                            + "   AND r1.bgt_kostenrubriek_key = g.bgt_kostenrubriek_key"
                            + ") btw_inc"
                    + "     , NULL btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , NULL kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM prs_kostensoortgrp g"
                    + " WHERE g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND g.bgt_kostenrubriek_key = " + parent_key;
                break;
            }
            case "S":
            {
                sql = "SELECT s.prs_kostensoort_key sel_key"
                    + "     , s.prs_kostensoort_oms sel_oms"
                    + "     , s.prs_kostensoort_btw btw_inc"
                    + "     , (SELECT f1.fin_btwtabelwaarde_perc"
                            + "  FROM fin_btwtabelwaarde f1"
                            + " WHERE f1.fin_btwtabelwaarde_key = s.fin_btwtabelwaarde_key"
                            + ") btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , COALESCE(s.prs_kostenplaats_key, p.prs_kostenplaats_key, -1) kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM prs_kostensoort s"
                    + "     , prs_kostensoortgrp g"
                    + "     , bgt_kostenrubriek r"
                    + "     , bgt_project p"
                    + " WHERE p.bgt_project_key = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key = g.bgt_kostenrubriek_key"
                    + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                    + "   AND s.prs_kostensoort_verwijder    IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND p.bgt_project_verwijder        IS NULL"
                    + "   AND s.prs_kostensoortgrp_key = " + parent_key
                    + ( sel_mode == "budget_edit"
                      ? " AND NOT EXISTS (SELECT '' FROM bgt_budget b WHERE b.prs_kostensoort_key = s.prs_kostensoort_key)"
                      : ""
                      );
                break;
            }
            case "C":
            {
                sql = "SELECT kp.prs_kostenplaats_key sel_key"
                    + "     , kp.prs_kostenplaats_nr ||' - '|| kp.prs_kostenplaats_omschrijving sel_oms"
                    + "     , NULL btw_inc"
                    + "     , NULL btw_val"
                    + "     , NULL btw_off"
                    + "     , NULL grp_key"
                    + "     , NULL srt_key"
                    + "     , NULL kpn_key"
                    + "     , NULL bdr_key"
                    + "  FROM prs_kostenplaats kp"
                    + "     , ins_tab_discipline d"
                    + "     , bgt_project p"
                    + " WHERE p.ins_discipline_key = d.ins_discipline_key"
                    + "   AND kp.ins_discipline_key = d.ins_discipline_key"
                    + "   AND kp.prs_kostenplaats_verwijder IS NULL"
                    + "   AND d.ins_discipline_verwijder    IS NULL"
                    + "   AND p.bgt_project_verwijder       IS NULL"
                    + "   AND p.bgt_project_key = " + parent_key;
                break;
            }
            case "B":
            {
                if (sel_mode == "order_edit" || sel_mode == "order_new")
                {
                    sql = "SELECT DISTINCT"
                        + "       sel_key"
                        + "     , sel_oms"
                        + "     , NULL  btw_inc"
                        + "     , ABS(btw_off-1) * btw_val  btw_val"
                        + "     , btw_off"
                        + "     , NULL  grp_key"
                        + "     , NULL  srt_key"
                        + "     , NULL  kpn_key"
                        + "     , NULL  bdr_key"
                        + "  FROM ("
                        + "        SELECT ba.prs_bedrijf_key                                        sel_key"
                        + "             , ba.prs_leverancier_nr || ' - ' || ba.prs_bedrijf_naam     sel_oms"
                        + "             , CASE WHEN fa.fin_btwtabel_key IS NULL THEN 0 ELSE 1 END   btw_off"
                        + "             , (SELECT bt_a.fin_btwtabelwaarde_perc"
                        + "                  FROM fin_btwtabelwaarde      bt_a"
                        + "                     , prs_kostensoort         ks_a"
                        + "                 WHERE ks_a.fin_btwtabelwaarde_key = bt_a.fin_btwtabelwaarde_key"
                        + "                   AND ks_a.prs_kostensoort_key = " + child_key
                        + "               ) btw_val"
                        + "          FROM prs_bedrijf  ba"
                        + "             , fin_btwtabel fa"
                        + "         WHERE ba.fin_btwtabel_key = fa.fin_btwtabel_key(+)"
                        + "           AND ba.prs_bedrijf_verwijder IS NULL"
                        + "           AND fa.fin_btwtabel_verwijder IS NULL"
                        + "           AND fa.fin_btwtabel_default IS NULL"
                        + ((child_key > 0 || sel_mode == "order_new") ? "" : " AND ba.prs_bedrijf_key = -1")
                        + "       )";
                }
                else
                {
                    sql = "SELECT DISTINCT"
                        + "       b.prs_bedrijf_key sel_key"
                        + "     , b.prs_leverancier_nr ||' - '|| b.prs_bedrijf_naam sel_oms"
                        + "     , NULL btw_inc"
                        + "     , NULL btw_val"
                        + "     , NULL btw_off"
                        + "     , NULL grp_key"
                        + "     , NULL srt_key"
                        + "     , NULL kpn_key"
                        + "     , NULL bdr_key"
                        + "  FROM bgt_project p"
                        + "     , bgt_kostenrubriek r"
                        + "     , prs_kostensoortgrp g"
                        + "     , prs_kostensoort s"
                        + "     , mld_opdr o"
                        + "     , prs_bedrijf b"
                        + " WHERE p.bgt_project_key = r.bgt_project_key"
                        + "   AND r.bgt_kostenrubriek_key = g.bgt_kostenrubriek_key"
                        + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                        + "   AND s.prs_kostensoort_key = o.prs_kostensoort_key"
                        + "   AND o.mld_uitvoerende_keys = b.prs_bedrijf_key"
                        + "   AND p.bgt_project_verwijder IS NULL"
                        + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                        + "   AND s.prs_kostensoort_verwijder IS NULL"
                        + "   AND b.prs_bedrijf_verwijder IS NULL"
                        + (parent_key > 0 ? "   AND p.ins_discipline_key = " + parent_key : "")
                        + (child_key  > 0 ? "   AND p.bgt_project_key = " + child_key : "");
                }
                break;
            }
            case "O":
            {
                sql = "SELECT DISTINCT"
                    + "       o.mld_opdr_key sel_key"
                    + "     , o.mld_opdr_ordernr ||'/'|| mld_opdr_bedrijfopdr_volgnr ||' - '|| mld_opdr_omschrijving sel_oms"
                    + "     , NULL btw_inc"
                    + "     , (SELECT f1.fin_btwtabelwaarde_perc"
                            + "  FROM fin_btwtabelwaarde f1"
                            + " WHERE f1.fin_btwtabelwaarde_key = s.fin_btwtabelwaarde_key"
                            + "   AND f1.fin_btwtabelwaarde_verwijder IS NULL"
                            + ") kostensoort_btw_val"
                    + "     ,(SELECT COALESCE(w.fin_btwtabelwaarde_perc,0)"
                    + "         FROM prs_bedrijf b"
                    + "            , fin_btwtabel f"
                    + "            , fin_btwtabelwaarde w"
                    + "        WHERE b.fin_btwtabel_key = f.fin_btwtabel_key(+)"
                    + "          AND f.fin_btwtabel_key = w.fin_btwtabel_key(+)"
                    + "          AND w.fin_btwtabelwaarde_verwijder IS NULL"
                    + "          AND b.prs_bedrijf_key = o.mld_uitvoerende_keys"
                    + "          AND f.fin_btwtabel_default IS NULL"
                    + "          AND f.fin_btwtabel_verwijder IS NULL"
                    + "          AND b.prs_bedrijf_verwijder IS NULL"
                    + "     ) bedrijf_btw_val"
                    + "     , (SELECT COUNT(*)"
                            + "  FROM prs_bedrijf b"
                            + "     , fin_btwtabel f"
                            + " WHERE b.fin_btwtabel_key = f.fin_btwtabel_key"
                            + "   AND b.prs_bedrijf_key = o.mld_uitvoerende_keys"
                            + "   AND f.fin_btwtabel_default IS NULL"
                            + "   AND f.fin_btwtabel_verwijder IS NULL"
                            + "   AND b.prs_bedrijf_verwijder IS NULL"
                            + ") btw_off"
                    + "     , s.prs_kostensoortgrp_key grp_key"
                    + "     , o.prs_kostensoort_key srt_key"
                    + "     , NULL kpn_key"
                    + "     , " + (child_key > 0 ? "NULL" : "o.mld_uitvoerende_keys") + " bdr_key"
                    + "     , o.mld_opdr_ordernr"
                    + "     , o.mld_opdr_bedrijfopdr_volgnr"
                    + "  FROM bgt_project p"
                    + "     , bgt_kostenrubriek r"
                    + "     , prs_kostensoortgrp g"
                    + "     , prs_kostensoort s"
                    + "     , mld_opdr o"
                    + " WHERE p.bgt_project_key = r.bgt_project_key"
                    + "   AND r.bgt_kostenrubriek_key = g.bgt_kostenrubriek_key"
                    + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
                    + "   AND s.prs_kostensoort_key = o.prs_kostensoort_key"
                    + "   AND p.bgt_project_verwijder        IS NULL"
                    + "   AND g.prs_kostensoortgrp_verwijder IS NULL"
                    + "   AND s.prs_kostensoort_verwijder    IS NULL"
                    + (child_key  > 0 ? "   AND o.mld_uitvoerende_keys = " + child_key : "")
                    + (parent_key > 0 ? "   AND p.bgt_project_key = " + parent_key : "");
                sql = "SELECT sel_key"
                    + "     , sel_oms"
                    + "     , btw_inc"
                    + "     , DECODE(btw_off, 0, kostensoort_btw_val, bedrijf_btw_val) btw_val"
                    + "     , btw_off"
                    + "     , grp_key"
                    + "     , srt_key"
                    + "     , kpn_key"
                    + "     , bdr_key"
                    + "     , mld_opdr_ordernr"
                    + "     , mld_opdr_bedrijfopdr_volgnr"
                    + "  FROM (" + sql + ")"
                order = " ORDER BY mld_opdr_ordernr"
                      + "        , mld_opdr_bedrijfopdr_volgnr";
                break;
            }
        }
        sql += (order == "" ? " ORDER BY 2" : order);
        var new_lov = [];
        var oRs = Oracle.Execute(sql);
        while(!oRs.eof)
        {
            new_lov.push({key: oRs("sel_key").Value,
                          oms: oRs("sel_oms").Value,
                          sel: oRs("sel_key").Value == init_key,
                          atr: { btw_inc: oRs("btw_inc").Value,
                                 btw_val: oRs("btw_val").Value,
                                 btw_off: oRs("btw_off").Value,
                                 grp_key: oRs("grp_key").Value,
                                 srt_key: oRs("srt_key").Value,
                                 kpn_key: oRs("kpn_key").Value,
                                 bdr_key: oRs("bdr_key").Value
                               }
                         });
            oRs.MoveNext();
        }
        oRs.Close();
        result.niveau = niveau;
        result.lov = new_lov;
        break;
    }
    case "plaatssearch":
    {
        switch (niveau)
        {
            case "KG":
            {
                sql = "SELECT DISTINCT kg.prs_kostenplaatsgrp_key sel_key"
                    + "     , kg.prs_kostenplaatsgrp_nr ||' - '|| kg.prs_kostenplaatsgrp_oms sel_oms"
                    + "  FROM prs_kostenplaatsgrp kg"
                    + "     , prs_kostenplaats kp"
                    + "     , ins_tab_discipline d"
                    + "     , bgt_project p"
                    + " WHERE d.ins_discipline_key = p.ins_discipline_key"
                    + "   AND d.ins_discipline_key = kp.ins_discipline_key"
                    + "   AND kp.prs_kostenplaatsgrp_key = kg.prs_kostenplaatsgrp_key(+)"
                    + "   AND kp.prs_kostenplaats_verwijder IS NULL"
                    + "   AND p.bgt_project_verwijder IS NULL"
                    + "   AND ins_discipline_verwijder IS NULL"
                    + "   AND p.bgt_project_key = " + parent_key;
                break;
            }
            case "KP":
            {
                if (!init_key)
                {   // De kostenplaats moet worden gekozen.
                    // Initieel moet deze op de kostenplaats van de (deel)project staan.
                }

                sql = "SELECT kp.prs_kostenplaats_key sel_key"
                    + "     , kp.prs_kostenplaats_nr ||' - '|| kp.prs_kostenplaats_omschrijving sel_oms"
                    + "     , kp.prs_kostenplaatsgrp_key par_key"
                    + "     , NULL btw_inc"
                    + "     , NULL btw_val"
                    + "     , NULL kpn_key"
                    + "     , ks.prs_kostensoort_key"
                    + "  FROM prs_kostenplaats kp"
                    + "     , prs_kostensoort ks"
                    + " WHERE kp.prs_kostenplaats_key = ks.prs_kostenplaats_key(+)"
                    + "   AND kp.prs_kostenplaats_verwijder IS NULL"
                    + (root_key   > 0 ? " AND kp.ins_discipline_key = " + root_key : "")
                    + (parent_key > 0 ? " AND kp.ins_discipline_key = (SELECT ins_discipline_key FROM bgt_project WHERE bgt_project_key = " + parent_key + ")" : "")
                    + (init_key   > 0 ? " AND kp.ins_discipline_key = (SELECT ins_discipline_key FROM prs_kostenplaats WHERE prs_kostenplaats_key = " + init_key + ")" : "");
            }
        }
        sql += " ORDER BY 2";
        var new_lov = [];
        var last_lov = "";
        var oRs = Oracle.Execute(sql);
        while(!oRs.eof)
        {
            if (last_lov != oRs("sel_oms").Value)
            {
                new_lov.push({key: oRs("sel_key").Value,
                              oms: oRs("sel_oms").Value,
                              sel: oRs("sel_key").Value == (init_key == -1 ? oRs("prs_kostensoort_key").Value : init_key),
                              atr: { par_key: oRs("par_key").Value}
                             });
                last_lov = oRs("sel_oms").Value;
            }
            else
            {
                var opt = new_lov[new_lov.length-1];
                opt.sel = opt.sel || (oRs("sel_key").Value == (init_key == -1 ? oRs("prs_kostensoort_key").Value : init_key));
            }
            oRs.MoveNext();
        }
        oRs.Close();
        result.niveau = niveau;
        result.lov = new_lov;
        break;
    }
    case "budgetmutation":
    {
        var mainniveau = niveau.substr(0,2);
        var subniveau = niveau.substr(2,1);
        var rubrieksql = "";
        switch (subniveau)
        {
            case "D": rubrieksql = "SELECT r.bgt_kostenrubriek_key"
                                 + "  FROM bgt_kostenrubriek r"
                                 + "     , bgt_project       p"
                                 + " WHERE p.bgt_project_key = r.bgt_project_key"
                                 + "   AND p.bgt_project_verwijder IS NULL"
                                 + "   AND p.ins_discipline_key = " + parent_key;
                      break;
            case "P": rubrieksql = "SELECT bgt_kostenrubriek_key"
                                 + "  FROM bgt_kostenrubriek r"
                                 + " WHERE r.bgt_project_key = " + parent_key;
                      break;
            case "R": rubrieksql = "SELECT r.bgt_kostenrubriek_key"
                                 + "  FROM bgt_kostenrubriek r"
                                 + " WHERE r.bgt_project_key ="
                                 + "       (SELECT p.bgt_project_key"
                                 + "          FROM bgt_kostenrubriek p"
                                 + "         WHERE p.bgt_kostenrubriek_key = " + parent_key
                                 + "       )"
                                 + "   AND (  r.bgt_kostenrubriek_key = " + parent_key
                                 + "       OR r.bgt_kostenrubriek_code = 'R'"
                                 + "       )";
                      break;
            case "":  rubrieksql = "SELECT p.bgt_project_key"
                                 + "  FROM bgt_budget p"
                                 + " WHERE p.bgt_budget_key = " + parent_key;
        }

        sql = "SELECT b.bgt_budget_key"
            + "     , " + (subniveau == "D" ? "(SELECT p.bgt_project_omschrijving"
                                            + "   FROM bgt_project p"
                                            + "  WHERE p.bgt_project_key = r.bgt_project_key"
                                            + ") ||': '||"
                                            : ""
                          )
            + "       CASE WHEN b.prs_kostensoort_key IS NOT NULL"
                      + "  THEN s.prs_kostensoort_oms"
                      + "  ELSE CASE WHEN b.prs_kostensoortgrp_key IS NOT NULL"
                                + "  THEN g.prs_kostensoortgrp_oms"
                                + "  ELSE CASE WHEN b.bgt_kostenrubriek_key IS NOT NULL"
                                          + "  THEN r.bgt_kostenrubriek_oms"
                                          + "  ELSE " + safe.quoted_sql(L("bgt_budgetmutatie_reserve"))
                                     + "  END"
                           + "  END"
            + "       END bgt_budget_naam"
            + "     , s.prs_kostensoort_btw"
            + "     , f.fin_btwtabelwaarde_perc"
            + "  FROM bgt_budget         b"
            + "     , bgt_kostenrubriek  r"
            + "     , prs_kostensoortgrp g"
            + "     , prs_kostensoort    s"
            + "     , fin_btwtabelwaarde f"
            + " WHERE b.bgt_kostenrubriek_key  = r.bgt_kostenrubriek_key(+)"
            + "   AND b.prs_kostensoortgrp_key = g.prs_kostensoortgrp_key(+)"
            + "   AND b.prs_kostensoort_key    = s.prs_kostensoort_key(+)"
            + "   AND COALESCE(s.fin_btwtabelwaarde_key, r.fin_btwtabelwaarde_key) = f.fin_btwtabelwaarde_key(+)"
            + ( subniveau == ""
              ? "   AND b.bgt_project_key = (" + rubrieksql + ")"
              : "   AND b.bgt_kostenrubriek_key IN (" + rubrieksql + ")"
              )
            + " ORDER BY 2";

        sql = "SELECT NULL bgt_budget_key"
            + "     , 'E '||" + safe.quoted_sql(L("bgt_budgetmutatie_extbudget")) + " bgt_budget_naam"
            + "     , NULL prs_kostensoort_btw"
            + "     , NULL fin_btwtabelwaarde_perc"
            + "  FROM DUAL UNION "
            + sql;

        var new_lov = [];
        var oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        {
            new_lov.push({ key: oRs("bgt_budget_key").Value
                         , oms: oRs("bgt_budget_naam").Value
                         , sel: init_key
                         , atr: { btw_inc: oRs("prs_kostensoort_btw").Value
                                , btw_val: oRs("fin_btwtabelwaarde_perc").Value
                                }
                         });
            oRs.MoveNext();
        }
        oRs.Close();
        result.niveau = mainniveau;
        result.lov = new_lov;
        break;
    }
    case "order":
    {
        switch (niveau)
        {
            case "x":
                sql = "SELECT s.prs_kostensoort_btw"
                    + "     , f.fin_btwtabelwaarde_perc"
                    + "  FROM mld_opdr o"
                    + "     , prs_kostensoort s"
                    + "     , fin_btwtabelwaarde f"
                    + " WHERE s.fin_btwtabelwaarde_key = f.fin_btwtabelwaarde_key"
                    + "   AND o.prs_kostensoort_key = s.prs_kostensoort_key"
                    + "   AND s.prs_kostensoort_verwijder IS NULL"
                    + "   AND f.fin_btwtabelwaarde_verwijder IS NULL"
                    + "   AND o.mld_opdr_key = " + parent_key;
                var oRs = Oracle.Execute(sql);
                result.inclbtw = (oRs("prs_kostensoort_btw").Value == 1);
                break;
            case "OP":
                sql = "SELECT o.mld_opdr_key"
                    + "     , b.prs_leverancier_nr ||' '|| o.mld_opdr_omschrijving opdr_naam"
                    + "     , s.prs_kostensoort_btw"
                    + "     , f.fin_btwtabelwaarde_perc"
                    + "     , o.prs_kostensoort_key"
                    + "  FROM mld_opdr o"
                    + "     , prs_kostensoort s"
                    + "     , fin_btwtabelwaarde f"
                    + "     , prs_bedrijf b"
                    + " WHERE o.prs_kostensoort_key = s.prs_kostensoort_key"
                    + "   AND s.fin_btwtabelwaarde_key = f.fin_btwtabelwaarde_key(+)"
                    + "   AND o.mld_uitvoerende_keys = b.prs_bedrijf_key"
                    + "   AND s.prs_kostensoort_verwijder IS NULL"
                    + "   AND f.fin_btwtabelwaarde_verwijder IS NULL"
                    + "   AND b.prs_bedrijf_verwijder IS NULL"
                    + "   AND s.prs_kostensoortgrp_key = " + parent_key
                    + " ORDER BY 2";
                var new_lov = [];
                var oRs = Oracle.Execute(sql);
                while(!oRs.eof)
                {
                    new_lov.push({ key: oRs("mld_opdr_key").Value
                                 , oms: oRs("opdr_naam").Value
                                 , sel: init_key
                                 , atr: { btw_inc: oRs("prs_kostensoort_btw").Value
                                        , btw_val: oRs("fin_btwtabelwaarde_perc").Value
                                        , srt_key: oRs("prs_kostensoort_key").Value
                                        }
                                 });
                    oRs.MoveNext();
                }
                oRs.Close();
                result.niveau = niveau;
                result.lov = new_lov;
                break;
        }
        break;
    }
}
Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
