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

    File:               loadUitvoerendenOptions.asp
    Description:        Aanmaken van de options voor een listbox met (meerdere) uitvoerenden
    Parameters:
    Context:            Vanuit edit opdrachten scherm via uitvoerenden button
    Note:
*/ %>

<%
DOCTYPE_Disable = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/selector.inc" -->

<%
// Bouw een materiaal lijst
var uitvkeyArray = getFParamIntArray("uitvkeystr", []);
var uitvcpersArray = getFParamIntArray("uitvcpersstr", []);
var uitvcntArray = getFParamIntArray("uitvcntstr", []);
var mld_key = getFParamInt("mld_key");
var filter = getFParam("filter");
var lutype = getFParam("lutype");

var sql_contract1 = "  FROM cnt_v_aanwezigcontract cnt"
                  + "     , cnt_contract_object co"
                  + "     , mld_melding_object mo "
                  + " WHERE cnt.cnt_contract_key = co.cnt_contract_key"
                  + "   AND co.cnt_ins_deel_key = mo.ins_deel_key"
                  + "   AND mo.mld_melding_key = " + mld_key
                  + "   AND cnt.cnt_prs_bedrijf_key = b.prs_bedrijf_key"
                  + "   AND cnt.cnt_contract_key IN (" + (uitvcntArray.length > 0? uitvcntArray.join(",") : "-1") + ")"
                  + "   AND cnt.cnt_contract_status IN (0, 1)"

var sql_contract2 = "  FROM cnt_v_aanwezigcontract cnt"
                  + "     , cnt_contract_plaats cp"
                  + "     , mld_melding me"
                  + "     , cnt_disc_params cpa "
                  + " WHERE cnt.cnt_contract_key = cp.cnt_contract_key"
                  + "   AND cnt.ins_discipline_key = cpa.cnt_ins_discipline_key"
                  + "   AND cnt.cnt_contract_status = 0" // actief
                  + "   AND ((cp.cnt_alg_plaats_key = me.mld_alg_locatie_key"
                    + " AND cp.cnt_alg_plaats_code = 'L')"
                    + "  OR (cp.cnt_alg_plaats_key = (SELECT alg_gebouw_key FROM alg_v_onroerendgoed og"
                      + "                              WHERE og.alg_onroerendgoed_keys = me.mld_alg_onroerendgoed_keys)"
                      + "                                AND cp.cnt_alg_plaats_code = 'G'))"
                  + "   AND me.mld_melding_key = " + mld_key
                  + "   AND cnt.cnt_prs_bedrijf_key = b.prs_bedrijf_key"
                  + "   AND cnt.cnt_contract_key IN (" + (uitvcntArray.length > 0? uitvcntArray.join(",") : "-1") + ")"
                  + "   AND cnt.cnt_contract_status IN (0, 1)";

var sql_contractkey = " SELECT cnt.cnt_contract_key"
                    + sql_contract1
                    + " UNION"
                    + " SELECT cnt.cnt_contract_key"
                    + sql_contract2;

var sql_contractoms = " SELECT '* ' || cnt.cnt_contract_nummer_intern || '-' || cnt.cnt_contract_omschrijving"
                    + sql_contract1
                    + " UNION"
                    + " SELECT cnt.cnt_contract_nummer_intern || '-' || cnt.cnt_contract_omschrijving"
                    + sql_contract2;

var sqlP = " SELECT p.prs_perslid_key uitv_key"
         + "      , " + safe.qL("lcl_mld_person_prefix") + " || " + S("prs_pers_string") + " naam"
         + "      , -1 cpkey"
         + "      , -1 cntkey"
         + "   FROM prs_v_aanwezigperslid p"
         + "  WHERE p.prs_perslid_key IN (" + (uitvkeyArray.length > 0? uitvkeyArray.join(",") : "-1") + ")"

var sqlD = " SELECT b.prs_bedrijf_key uitv_key"
         + "      , " + safe.qL("lcl_mld_bedrijf_prefix") + " || b.prs_bedrijf_naam"
         + "        || ' (' "
         + "        || COALESCE((SELECT " + S("prs_contactpers_string")
         + "                       FROM prs_contactpersoon cp"
         + "                      WHERE cp.prs_bedrijf_key = b.prs_bedrijf_key"
         + "                        AND cp.prs_contactpersoon_key IN (" + (uitvcpersArray.length > 0? uitvcpersArray.join(",") : "-1") + ")), '-')"
         + "        || ' / '"
         + "        || COALESCE((" + sql_contractoms + "), '-')"
         + "        || ')' naam"
         + "      , COALESCE((SELECT cp.prs_contactpersoon_key"
         + "                    FROM prs_contactpersoon cp"
         + "                   WHERE cp.prs_bedrijf_key = b.prs_bedrijf_key"
         + "                     AND cp.prs_contactpersoon_key IN (" + (uitvcpersArray.length > 0? uitvcpersArray.join(",") : "-1") + ")), -1) cpkey"
         + "      , COALESCE((" + sql_contractkey + "), -1) cntkey"
         + "   FROM " + (lutype == "L"? "prs_v_leverancierbedrijf" : "prs_v_uitvoerendebedrijf") + " b"
         + "  WHERE b.prs_bedrijf_key IN (" + (uitvkeyArray.length > 0? uitvkeyArray.join(",") : "-1") + ")"

var sql = sqlP + " UNION " + sqlD + " ORDER BY 2";

FCLTselectorOptions(sql,
                    { startmulti: true,
                      size: 10,
                      extraParamValue: "cpkey, cntkey",
                      optgroup: "naam"
                    });
%>

