<% /*
     $Revision$
     $Id$

    File:               referentieFilter.inc
    Description:        functies voor referentie suggest velden
    Globals:
    Context:
    Note:
*/ %>


<%
function getReferenceNameReferentie(prefkey, preftype)
{
  var lreferenceName = "";
  if (preftype && prefkey && prefkey > 0)
  {
    //var filtClause = getFiltClauseReferentie(params.filtercode, filt_params);
    //var extraInf = getExtraInfReferentie(params.extracode);

    var sql = "";
    switch (preftype)
    {
      case "M": sql = "SELECT sd.ins_srtdiscipline_prefix || TO_CHAR(mld_melding_key) || ' - ' ||"
                      + lcl.xsql("md.ins_discipline_omschrijving", "md.ins_discipline_key") + " || '/' ||"
                      + lcl.xsql("sm.mld_stdmelding_omschrijving", "sm.mld_stdmelding_key") + " refname"
                    + "  FROM mld_melding m"
                    + "     , mld_stdmelding sm"
                    + "     , mld_discipline md"
                    + "     , ins_srtdiscipline sd"
                    + " WHERE sm.mld_ins_discipline_key = md.ins_discipline_key"
                    + "   AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
                    + "   AND sd.ins_srtdiscipline_key = md.ins_srtdiscipline_key"
                    + "   AND m.mld_melding_key = " + prefkey;
                break;
      case "O": sql = "SELECT sd.ins_srtdiscipline_prefix || TO_CHAR (o.mld_melding_key) || '/' ||o.mld_opdr_bedrijfopdr_volgnr || ' - ' ||"
                      + lcl.xsql("md.ins_discipline_omschrijving", "md.ins_discipline_key") + " || '/' || "
                      + lcl.xsql("std.mld_stdmelding_omschrijving", "std.mld_stdmelding_key") + " || ' - ' ||"
                      + "     (SELECT u.naam FROM mld_v_uitvoerende u WHERE u.mld_uitvoerende_key = o.mld_uitvoerende_keys) refname"
                    + "  FROM mld_opdr o"
                    + "     , mld_stdmelding std"
                    + "     , mld_melding m"
                    + "     , mld_discipline md"
                    + "     , ins_srtdiscipline sd"
                    + " WHERE m.mld_melding_key = o.mld_melding_key"
                    + "   AND m.mld_stdmelding_key = std.mld_stdmelding_key"
                    + "   AND std.mld_ins_discipline_key = md.ins_discipline_key"
                    + "   AND sd.ins_srtdiscipline_key = md.ins_srtdiscipline_key"
                    + "   AND o.mld_opdr_key = " + prefkey;
                break;
      case "R": sql = "SELECT 'R'"
                      + "     || rm.res_reservering_key"
                      + "     || '/'"
                      + "     || rm.res_rsv_ruimte_volgnr"
                      + "     || ' - '"
                      + "     || " + lcl.xsql('r.res_ruimte_nr', 'r.res_ruimte_key') + " refname"
                    + "  FROM res_rsv_ruimte rm"
                    + "     , res_ruimte_opstelling ro"
                    + "     , res_ruimte r"
                    + " WHERE rm.res_ruimte_opstel_key = ro.res_ruimte_opstel_key"
                    + "   AND ro.res_ruimte_key = r.res_ruimte_key"
                    + "   AND rm.res_rsv_ruimte_key = " + prefkey;
                break;
      case "B": sql = "SELECT 'B' || b.bes_bestelling_key refname"
                    + "  FROM bes_bestelling b"
                    + " WHERE b.bes_bestelling_key = " + prefkey;
                break;
      case "X": sql = "SELECT 'B' || b.bes_bestelopdr_id || ' - ' || be.prs_bedrijf_naam refname"
                    + "  FROM bes_bestelopdr b"
                    + "     , prs_bedrijf be"
                    + " WHERE be.prs_bedrijf_key = b.prs_bedrijf_key"
                    + "   AND b.bes_bestelopdr_key = " + prefkey;
                break;
      case "C": sql = "SELECT 'C'"
                    + "       || c.cnt_contract_nummer_intern"
                    + "       || ' - '"
                    + "       || c.cnt_contract_nummer"
                    + "       || ' - '"
                    + "       || c.cnt_contract_omschrijving refname"
                    + "  FROM cnt_v_aanwezigcontract c"
                    + " WHERE c.cnt_contract_key = " + prefkey;
                break;
      case "F": sql = "SELECT f.fin_factuur_key refkey"
                    + "     , 'F' || f.fin_factuur_key || ' - ' ||"
                      + "      COALESCE(sd.ins_srtdiscipline_prefix || o.mld_melding_key,"
                      + "               CASE WHEN c.cnt_contract_nummer_intern IS NULL THEN '' ELSE 'C' END || c.cnt_contract_nummer_intern,"
                      + "               bo.bes_bestelopdr_id) || ' (' ||"
                      + "     COALESCE((SELECT b.prs_bedrijf_naam"
                      + "                 FROM prs_bedrijf b"
                      + "                WHERE prs_bedrijf_key = COALESCE (o.mld_uitvoerende_keys, c.cnt_prs_bedrijf_key, bo.prs_bedrijf_key)),"
                      + "              (SELECT " + S("prs_dep_string")
                      + "                 FROM prs_v_afdeling d"
                      + "                WHERE d.prs_afdeling_key = c.cnt_prs_afdeling_key),"
                      + "              (SELECT " + S("prs_pers_string")
                      + "                 FROM prs_perslid p"
                      + "                WHERE p.prs_perslid_key = c.cnt_prs_perslid_key),"
                      + "              '-') || ')' refname"
                    + "     , TO_CHAR(f.fin_factuur_datum, 'DD-MM-YYYY') refdesc"
                    + "     , f.fin_factuur_opmerking reftitle"
                    + "     , f.fin_factuur_datum datum"
                    + "     , 'F' extra"
                    + "     , '' AS restype"
                    + "  FROM fin_factuur f"
                    + "     , mld_opdr o"
                    + "     , mld_melding m"
                    + "     , mld_stdmelding sm"
                    + "     , mld_discipline d"
                    + "     , ins_srtdiscipline sd"
                    + "     , cnt_contract c"
                    + "     , bes_bestelopdr bo"
                    + "     , bes_bestelopdr_item boi"
                    + "     , bes_bestelling_item bi"
                    + "     , bes_bestelling b"
                    // Zoektekst op meldingnummer, contractnummer, bestellingnummer, factuurnummer, factuurkey of factuur opmerking.
                    + " WHERE f.mld_opdr_key            = o.mld_opdr_key(+)"         // vast staat in welke tak we zitten
                    + "   AND o.mld_melding_key         = m.mld_melding_key(+)"
                    + "   AND m.mld_stdmelding_key      = sm.mld_stdmelding_key(+)"
                    + "   AND sm.mld_ins_discipline_key = d.ins_discipline_key(+)"
                    + "   AND d.ins_srtdiscipline_key   = sd.ins_srtdiscipline_key(+)"
                    + "   AND f.cnt_contract_key        = c.cnt_contract_key(+)"
                    + "   AND f.bes_bestelopdr_key      = bo.bes_bestelopdr_key(+)"
                    + "   AND bo.bes_bestelopdr_key = boi.bes_bestelopdr_key(+)"
                    + "   AND boi.bes_bestelopdr_item_key = bi.bes_bestelopdr_item_key(+)"
                    + "   AND bi.bes_bestelling_key = b.bes_bestelling_key(+)"
                    + "   AND f.fin_factuur_key = " + prefkey;
                break;
    }
    var oRs = Oracle.Execute(sql);
    if (!oRs.eof)
      lreferenceName = oRs("refname").value;
  }
  return lreferenceName;
}

function getFiltClauseReferentie(pfiltcode, params)
{
  //var lfiltClause = {from: "", where: ""};
  var lfiltClause = {M: "",
                     O: "",
                     R: "",
                     B: "",
                     X: "",
                     C: "",
                     F: ""};
  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode)
    {
      case 'M':    // Alle meldingen mogelijk als referentie
                   var lfiltClause = { M: " AND EXISTS (SELECT 1"
                                          + "  FROM fac_v_webgebruiker g"
                                          + " WHERE g.prs_perslid_key = " + user_key
                                          + "   AND g.ins_discipline_key = sm.mld_ins_discipline_key"
                                          + "   AND g.fac_functie_key IN (" + (params.funcKeys ? params.funcKeys : "''") + ")" // ('WEB_MLDFOF', 'WEB_MLDBOF')
                                          + "   AND g.fac_gebruiker_alg_level_read < 9)"
                                     };
                   break;
      case 'O':    // Weergeven van alle mogelijke eigen referenties
                   var lfiltClause = { M: " AND (m.prs_perslid_key = " + params.perslidKey
                                          + " OR m.prs_perslid_key_voor = " + params.perslidKey + ")",
                                       O: " AND (m.prs_perslid_key = " + params.perslidKey
                                          + " OR m.prs_perslid_key_voor = " + params.perslidKey + ")",
                                       R: " AND (rm.res_rsv_ruimte_contact_key = " + params.perslidKey
                                          + " OR rm.res_rsv_ruimte_host_key = " + params.perslidKey + ")",
                                       B: " AND (b.prs_perslid_key = " + params.perslidKey
                                          + " OR b.prs_perslid_key_voor = " + params.perslidKey + ")",
                                       X: " AND bst.prs_perslid_key = " + params.perslidKey,
                                       C: " AND (c.prs_perslid_key_eig = " + params.perslidKey
                                          + " OR c.prs_perslid_key_beh = " + params.perslidKey + ")",
                                       F: " AND (m.prs_perslid_key = " + params.perslidKey
                                          + " OR m.prs_perslid_key_voor = " + params.perslidKey
                                          + " OR c.prs_perslid_key_eig = " + params.perslidKey
                                          + " OR c.prs_perslid_key_beh = " + params.perslidKey
                                          + " OR b.prs_perslid_key = " + params.perslidKey
                                          + " OR b.prs_perslid_key_voor = " + params.perslidKey + ")"
                                     };
                   break;
      case 'F':    // Filteren op bestaande ingevoerde referenties
                   var lfiltClause = { M: " AND m.mld_melding_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                           FROM mld_melding m1"
                                          + "                          WHERE m1.mld_melding_kto_type LIKE 'M'"
                                          + "                            AND m1.mld_melding_kto_key > 0)",
                                       O: " AND o.mld_opdr_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                        FROM mld_melding m1"
                                          + "                       WHERE m1.mld_melding_kto_type LIKE 'O'"
                                          + "                         AND m1.mld_melding_kto_key > 0)",
                                       R: " AND rm.res_rsv_ruimte_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                               FROM mld_melding m1"
                                          + "                              WHERE m1.mld_melding_kto_type LIKE 'R'"
                                          + "                                AND m1.mld_melding_kto_key > 0)",
                                       B: " AND b.bes_bestelling_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                              FROM mld_melding m1"
                                          + "                             WHERE m1.mld_melding_kto_type LIKE 'B'"
                                          + "                               AND m1.mld_melding_kto_key > 0)",
                                       X: " AND b.bes_bestelopdr_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                              FROM mld_melding m1"
                                          + "                             WHERE m1.mld_melding_kto_type LIKE 'X'"
                                          + "                               AND m1.mld_melding_kto_key > 0)",
                                       C: " AND c.cnt_contract_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                            FROM mld_melding m1"
                                          + "                           WHERE m1.mld_melding_kto_type LIKE 'C'"
                                          + "                             AND m1.mld_melding_kto_key > 0)",
                                       F: " AND f.fin_factuur_key IN (SELECT m1.mld_melding_kto_key"
                                          + "                           FROM mld_melding m1"
                                          + "                          WHERE m1.mld_melding_kto_type LIKE 'F'"
                                          + "                            AND m1.mld_melding_kto_key > 0)"
                                     };
                   break;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where = " AND 1 = 2";
                   break;
    }
  }

  return lfiltClause;
}

function getExtraInfReferentie(pextracode)
{
  var lExtraInf = {M: ", 'Extra' extra",
                   O: ", 'Extra' extra",
                   R: ", 'Extra' extra",
                   B: ", 'Extra' extra",
                   X: ", 'Extra' extra",
                   C: ", 'Extra' extra",
                   F: ", 'Extra' extra"};
  if (pextracode && pextracode != "")
  {
    switch(pextracode)
    {
      case 'RT': lExtraInf = {M: ", 'M' extra",
                              O: ", 'O' extra",
                              R: ", 'R' extra",
                              B: ", 'B' extra",
                              X: ", 'X' extra",
                              C: ", 'C' extra",
                              F: ", 'F' extra"};
                 break;
    }
  }
  return lExtraInf;
}
%>
