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

    File:        itemsearch.asp (voorheen onderdeel van prodsearch.asp)
    Description: Zoekt een res, mld, bes etc. op basis van ingetikt nummer
    Parameters:  string s
    Context:     We komen hier alleen met server.transfer vanuit prodsearch.asp
                 als die X12345 formaat detecteert
    Note:
*/ %>
<!--#include file="../Shared/common.inc" -->
<!--#include file="fac.inc" -->

<%
  FCLTHeader.Requires({js: []});

  var urole = getQParamSafe("urole", "fe");
  var s = getQParam("s","");
  var supper = s.toUpperCase().replace(/^\s+|\s+$/g,"");
  var mobiel = getQParamInt("mobiel", -1);

// Search string has format Xnnnnn (assumed), which is short for finding an identified entity
// like a Melding or a Reservering
// The second part must be a integer then
// Of: #abcde voor object met identificatie abcde

// This is a proof of concept, needs more subtility (prefixes? other separator)
// and a location replace, not an url.
// sometimes support for subentities /2 /3 etc

url = "";
var letter = supper.substring(0,1);
var fullreference = supper.substring(1);

if (letter == '#')
{
    var ttl = L("lcl_ins_object");
    tsql = "SELECT ins_deel_key"
         + "  FROM ins_deel "
         + " WHERE ins_deel_verwijder IS NULL "
         + "   AND ins_deel_parent_key IS NULL "
         + "   AND ins_deel_upper LIKE " + safe.quoted_sql_upper("%" + fullreference + "%");
    toRs = Oracle.Execute(tsql);
    if (!toRs.eof)
    {
        url = (mobiel > 0 ? "../pda/" : "../ins/") + "ins_deel.asp?urole=bo&ins_key="+toRs(0).value;
        toRs.MoveNext();
        if (!toRs.eof) // Er zijn er meerdere. Te ingewikkeld.
        {
            url = (mobiel > 0 ? "../pda/ins_list" : "../ins/ins_search") + ".asp?urole=bo&autosearch=1&deel=" + safe.url(fullreference);
        }
    }
}
else
{
  var volgnr = -1; // Mag met punt of slash
  var arr = fullreference.split(/[\/.]/)
  if (arr.length > 1)
  {
    fullreference = arr[0];
    volgnr = parseInt(arr[1], 10) || -1;
  }
  var reference_key = parseInt(fullreference, 10);
  var ttl = "";
  if (!isNaN(reference_key) || letter == 'C')
  {
    switch (letter) {
        case 'B':
             var ttl = L("lcl_bes_bestelling");
             tsql = "SELECT bes_bestelling_key FROM bes_bestelling "
                  + " WHERE bes_bestelling_key="+reference_key  //@verwijder?
                  + "   AND fac_activiteit_key IS NULL";

             toRs = Oracle.Execute(tsql);
             if (!toRs.eof)
             {
                 url = (mobiel > 0 ? "../pda/" : "../bes/bes_") + "bestelling.asp?urole="+urole+"&bes_key="+toRs(0).value;
             }
             break;
        case 'M':
             if (volgnr == -1)
             { // De melding zelf.
               var ttl = L("lcl_complain");
               tsql = "SELECT mld_melding_key"
                    + "  FROM mld_melding"
                    + " WHERE mld_melding_key="+reference_key // @verwijder?
                    + "   AND fac_activiteit_key IS NULL";
               toRs = Oracle.Execute(tsql);
               if (!toRs.eof) {
                   url = (mobiel > 0 ? "../pda/melding.asp?urole="+urole+"&mld_key="+toRs(0).value : "../mld/mld_melding.asp?urole=xx&mld_key="+toRs(0).value);
               }
             }
             else
             { // Een specifieke opdrachtnummer bij een melding.
               var ttl = L("lcl_mld_opdr_number");
               tsql = "SELECT mld_opdr_key FROM mld_opdr"
                    + " WHERE mld_melding_key = " + reference_key
                    + "   AND mld_opdr_bedrijfopdr_volgnr = " + volgnr
                    + "   AND fac_activiteit_key IS NULL";

               toRs = Oracle.Execute(tsql);
               if (!toRs.eof) {
                   url = (mobiel > 0 ? "../pda/order" : "../mld/mld_opdr") + ".asp?opdr_key=" + toRs(0).value; // Urole zoekt mld_opdr.asp zelf wel uit.
               }
             }
             break;
        case 'R':
             var ttl = L("lcl_reservation");
             tsql = "SELECT MIN(res_rsv_ruimte_key) from res_rsv_ruimte "
                  + "WHERE res_rsv_ruimte_verwijder IS NULL AND res_reservering_key="+reference_key;
             if (volgnr > 0)
                tsql += " AND res_rsv_ruimte_volgnr = " + volgnr;
             toRs = Oracle.Execute(tsql);
             if (toRs(0).value != null) {
                url = (mobiel > 0 ? "../pda/reservering.asp?urole="+urole+"&rsv_ruimte_key="+toRs(0).value : "../res/res_reservering.asp?urole=xx&rsv_ruimte_key="+toRs(0).value);
             }
             break;
        case 'A':
             var ttl = L("lcl_vis_appointment");
             tsql = "SELECT bez_afspraak_key FROM bez_afspraak "
                  + "WHERE bez_afspraak_key="+reference_key; // @verwijder
             toRs = Oracle.Execute(tsql);
             if (!toRs.eof) {
                 url = (mobiel > 0 ? "../pda/afspraak.asp?urole="+urole+"&afs_key="+toRs(0).value : "../bez/bez_afspraak.asp?urole=xx&afspr_key="+toRs(0).value);
             }
             break;
        case 'C': // Merk op dat we alleen numerieke concractnummers kunnen vinden!
             var ttl = L("lcl_contract");
             tsql = "SELECT cnt_contract_key FROM cnt_contract "
                  + "WHERE cnt_contract_verwijder IS NULL"
                  + "  AND (cnt_contract_nummer_intern="+safe.quoted_sql(String(fullreference))
                  + "    OR cnt_contract_nummer="+safe.quoted_sql(String(fullreference)) +")";
             if (volgnr > 0)
                tsql += " AND cnt_contract_versie = " + volgnr;

             tsql += " ORDER BY cnt_contract_status";  // actieve eerst als er dubbele nummers zijn

             toRs = Oracle.Execute(tsql);
             if (!toRs.eof && toRs(0).value) {
                 url = (mobiel > 0 ? "../pda/" : "../cnt/cnt_") + "contract.asp?urole=xx&cnt_key="+toRs(0).value;
             }
             break;
        case 'X':
             /* Voorstel is om hier te gaan zoeken wat er met deze key bestaat, en dat te tonen,
                mits dat binnen mijn autorisatie valt.
              */
             break;
    }
  }
}

if (url != "")
{
    Response.Redirect(url);
}
else
{
    if (mobiel > 0)
    {
        var url = "../pda/productsearch.asp?mobiel=1&forcesave=0&emptyitemsearch=1&urole="+urole+"&s="+safe.url(s);
        Response.Redirect(url);
    }
    else
    {
        if (ttl)
            s = ttl + ": " + fullreference;
        shared.simpel_page(L("lcl_fac_emptyprodset1") + "<cite>"+ safe.html(s) + "</cite>" + L("lcl_fac_emptyprodset2"));
    }
}
%><% ASPPAGE_END(); %> 
