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

     File:               CNT/cnt_edit_scope.asp (was: algins4cnt.asp/alg4cnt.asp)
     Description:        Afhankelijk van het type contract kan hier de bijbehorende scope worden aangepast
                         cnt_srtcontract_type                Scope
                         [1] Onderhoud/Maintenance           Objecten
                         [2] Huur/Rental                     Onroerend goed (ruimten/terreinen)
                         [3] Ondersteuning/Support           Plaats (locatie, gebouw, verdieping, ruimte, terrein) en/of objecten
                         [4] Algemeen/Generic                Geen
                         [5] Leverancier/Supplier            Plaats (locatie, gebouw, verdieping, ruimte, terrein)
                         [6] Mantel/Master                   Geen
     Parameters:         cnt_key   Contract key
     Context:
     Note:               Weet niet of status=2 wel afgehandeld moet worden als scope; dat was eerder niet zo.
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                           js: ["./cnt_edit_scope.js"]
                    });

var cnt_key = getQParamInt("cnt_key");

var autfunction = "WEB_CNTMAN";
if (S("cnt_contract_approval") == 1)
    autfunction = "WEB_CNTUSE";

var authparams = user.checkAutorisation(autfunction);

// TODO: Hier zelf uitzoeken welke scope selectoren getoond worden afhankelijk van contracttype
// only_loc niet meer gebruiken
sql = "SELECT cdp.cnt_srtcontract_type"
    + " FROM cnt_contract c"
    + ", cnt_disc_params cdp"
    + " WHERE c.ins_discipline_key = cdp.cnt_ins_discipline_key"
    + " AND c.cnt_contract_key = " + cnt_key;
oRs = Oracle.Execute(sql);
var cnt_srtcontract_type = oRs("cnt_srtcontract_type").value;

// TODO: Voor hetcnt_srtcontract_type == 1 moet de object scope nog toegevoegd worden!!!
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">
      PRSreadlevel = "<%=authparams.PRSreadlevel%>";

<%    if (cnt_srtcontract_type == 3 || cnt_srtcontract_type == 5)
      { // Locatie/gebouw scope
%>
        $(document).ready(function ()
                          {
                            updateBld(<%=authparams.ALGwritelevel%>);
<%                          if (cnt_srtcontract_type == 3)
                            { // Objectensoort scope
%>
                              updateGroup("WEB_INSMAN");
<%                          } %>
                          });
<%    } %>
    </script>
  </head>

  <body id="editbody">
    <!-- <form name=u2 target="hidFrame" method="post"> -->
    <form id="u2" name="u2" action="cnt_edit_scope_save.asp" target="hidFrame" method="post" onSubmit="cnt_submit();">
      <input type="hidden" id="cnt_key" name="cnt_key" value="<%=cnt_key%>">
<%    var buttons = [ {title: L("lcl_submit"), action: "cnt_submit()", icon: "opslaan.png" },
                      {title: L("lcl_cancel"), action: "cnt_cancel()", icon: "undo.png" } ];
      IFRAMER_HEADER(L("lcl_cnt_frame_algemeen"), buttons);
%>
      <table border=0>
      <tr>
        <td>
          <table border=0>

<%        // Objecten scope
          // TODO: Voor het cnt_srtcontract_type == 1 moet de object scope nog toegevoegd worden!!!
          if (cnt_srtcontract_type == 1)
          { %>
            <tr>
              <td>
                <table>
<%                FCLTplaatsselector(authparams.ALGreadlevel, { locatiekey: -1,
                                                                gebouwkey: -1,
                                                                verdiepingkey: -1,
                                                                ruimtekey: -1,
                                                                startlevel: 2, // locatie
                                                                eindlevel: 5, // ruimte
                                                                autlevel: authparams.ALGwritelevel,
                                                                onLocChange: "onPlaatsChange()",
                                                                onGebChange: "onPlaatsChange()",
                                                                onVerChange: "onPlaatsChange()",
                                                                onRuiChange: "onPlaatsChange()",
                                                                whenEmpty: L("lcl_search_generic") // want filter
                                                              }); %>
                </table>
              </td>
              <td>
                <table>
                  <!-- Organisatie -->
<%                FCLTafdelingselector("sDept_key", // organisation
                                     "sgDept",
                                     { departmentKey: -1,
                                       label: L("lcl_prs_organisatie"),
                                       autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                                       onChange: "onAfdelingChange",
                                       whenEmpty: L("lcl_search_generic")  // want filter
                                     }); %>
                  <!-- Persoon -->
<%                FCLTpersoonselector("sName_key", // Persoon
                                       "sgPerson",
                                       { perslidKey: -1,
                                         label: L("lcl_search_pers"),
                                         autlevel: authparams.ALGwritelevel9,
                                         filtercode: "OBJP",
                                         urlAdd: [{urlParam: "afd_key", field: "sDept_key"}],
                                         extracode: "AK",
                                         extraParamField: "prsafd_key",
                                         onChange: "onPersoonChange",
                                         whenEmpty: L("lcl_search_generic") // want filter
                                       }); %>
                </table>
              </td>
            </tr>

            <tr>
              <td>
                <br>
              </td>
            </tr>

            <tr>
              <td>
                <b><%=L("lcl_cnt_object_scope")%></b>
              </td>
              <td>&nbsp;</td>
            </tr>

            <tr>
              <td class="resultkolom">
                <span id="objpass">
                  <table id="cntobjscope_list">
<%                // Works only for Objects now; query columns match shared/load_robjs.asp
                  var plaatsaanduiding = (S("prs_wpprefix_objectid") == 1
                                         ? " CASE"
                                           + " WHEN o.prs_werkplek_omschrijving IS NOT NULL"
                                           + " THEN ' ' || o.prs_werkplek_omschrijving"
                                           + " ELSE ''"
                                           + " END"
                                         : "''");
                  sql = "SELECT co.cnt_contract_object_key"
                      + "     , d.ins_deel_key"
                      + "     , d.ins_deel_omschrijving || " + plaatsaanduiding + " || ' (' || " + lcl.xsql('s.ins_srtdeel_omschrijving','s.ins_srtdeel_key') +" || ')' omschrijving"
                      + "     , COALESCE(d.ins_alg_locatie_key, -1) loc_key"
                      + "     , COALESCE(ao.alg_geb_ter_key, -1) gebter_key"
                      + "     , COALESCE(ao.alg_verdieping_key, -1) ver_key"
                      + "     , COALESCE(d.ins_alg_ruimte_key, -1) rui_key"
                      + "     , d.ins_srtdeel_key"
                      + " FROM ins_v_aanwezigdeel d"
                      + "    , ins_srtdeel s"
                      + "    , cnt_contract_object co"
                      + "    , ins_v_alg_onroerendgoed ao"
                      + "    , ins_v_alg_overzicht o"
                      + " WHERE d.ins_srtdeel_key = s.ins_srtdeel_key"
                      + "   AND d.ins_alg_ruimte_key = ao.alg_onrgoed_keys(+)" // (+) afdelings/persoons gebonden objecten hebben geen onrgoed link
                      + "   AND (d.ins_deel_vervaldatum IS NULL OR d.ins_deel_vervaldatum > TRUNC(SYSDATE))"
                      + "   AND co.cnt_ins_deel_key IS NOT NULL"
                      + "   AND co.cnt_ins_deel_key = d.ins_deel_key"
                      + "   AND co.cnt_contract_object_verwijder IS NULL"
                      + " AND d.ins_alg_ruimte_key = o.alg_onroerendgoed_keys(+)"
                      + " AND d.ins_alg_ruimte_type = o.alg_onroerendgoed_type(+)"
                      + " AND d.ins_alg_locatie_key = o.alg_locatie_key(+)"
                      + "   AND co.cnt_contract_key = " + cnt_key
                      + " ORDER BY 3";
                  oRs = Oracle.Execute(sql);
                  j = 1;
                  while (!oRs.eof)
                  {
%>                  <tr id="row<%=j+1000%>" OnClick="javascript:selectObjRow(<%=j%>);">
                      <td class="selectable" id="obj"><%/*note:this content is matched in itemExist, watch out! */ %>
                          <%=oRs("omschrijving").value%></td>
                      <td>
                        <input type=hidden id=k<%=j%> name=k<%=j%> value=<%=oRs("cnt_contract_object_key").value%>>
                        <input type=hidden id=kd<%=j%> name=kd<%=j%> value=<%=oRs("ins_deel_key").value%>>
                        <input type=hidden id=ks<%=j%> name=ks<%=j%> value=<%=oRs("ins_srtdeel_key").value%>>
                      </td>
                    </tr>
<%                  j++;
                    oRs.MoveNext();
                  }
                  oRs.close(); %>
                  </table>
                </span>

                <input type="hidden" id="objcnt" name="objcnt" value="<%=j-1%>">
              </td>
              <td>
                <table>
                <tr>
                  <td>
                    <span class="mover" onclick="addObj()">&lt;</span><br><br>
                    <span class="mover" onclick="removeObj()">&gt;</span><br><br>
                    <span class="mover" onclick="removeAllObj()">&gt;&gt;</span>
                  </td>
                  <td>
                    <table>
                    <!-- Objecten -->
                    <tr>
<%                    sql = "";
                      FCLTselector("objs",
                                   sql,
                                   { label: L("lcl_cnt_objects"),
                                     startmulti: true,
                                     size: 10,
                                     onDblClick: "showObjDetail(event);"
                                   }); %>
                    </tr>
                    </table>
                  </td>
                </tr>
                </table>
              </td>
            </tr>


<%        } %>

<%        // Locatie/gebouw scope
          if (cnt_srtcontract_type == 3 || cnt_srtcontract_type == 5)
          { %>
            <tr>
              <td></td>
              <td></td>
            </tr><%/* to force space for 1st column if empty */%>

            <tr>
              <td>
                <b><%=L("lcl_cnt_location_scope")%> <%=L("lcl_cnt_verdeling")%></b>
              </td>
              <td>&nbsp;</td>
            </tr>

            <tr>
              <td class="resultkolom">
                <span id="locpass">
                  <table id="cntlocscope_list">
<%                // Works only for Objects now; query columns match shared/load_robjs.asp
                  sql = "SELECT cp.cnt_contract_plaats_key"
                      + ", cp.cnt_alg_plaats_key"
                      + ",  CASE cp.cnt_alg_plaats_code"
                             + " WHEN 'L'"
                             + " THEN lo.alg_locatie_omschrijving"
                             + " WHEN 'G'"
                             + " THEN gg.alg_locatie_omschrijving ||"
                               + " ' (' || "
                               + " gg.alg_locatie_code || "
                               + " ' - ' || "
                               + " g.alg_gebouw_code || "
                               + " ' ' || "
                               + " gg.alg_gebouw_naam || ')'"
                             + " WHEN 'T'"
                             + " THEN lo2.alg_locatie_omschrijving ||"
                               + " ' (' ||"
                               + " t.alg_terreinsector_code || ' - ' || t.alg_terreinsector_naam || ')'"
                             + " END plaats"
                      + ", cp.cnt_alg_plaats_code"
                      + ", cp.cnt_contract_plaats_gewicht"
                      + " FROM cnt_contract_plaats cp"
                      + ", alg_v_aanweziglocatie lo"
                      + ", alg_v_gebouw_gegevens gg"
                      + ", alg_v_aanweziggebouw g"
                      + ", alg_v_aanwezigterreinsector t"
                      + ", alg_v_aanweziglocatie lo2"
                      + " WHERE (cp.cnt_alg_plaats_code = 'L' OR cp.cnt_alg_plaats_code = 'G' OR cp.cnt_alg_plaats_code = 'T')"
                      + " AND cp.cnt_alg_plaats_key = lo.alg_locatie_key(+)"
                      + " AND cp.cnt_alg_plaats_key = gg.alg_gebouw_key(+)"
                      + " AND gg.alg_gebouw_key = g.alg_gebouw_key(+)"
                      + " AND cp.cnt_alg_plaats_key = t.alg_terreinsector_key(+)"
                      + " AND t.alg_locatie_key = lo2.alg_locatie_key(+)"
                      + " AND cp.cnt_contract_plaats_verwijder IS NULL"
                      + " AND cp.cnt_contract_key = " + cnt_key
                      + " ORDER BY 3";
                  oRs = Oracle.Execute(sql);
                  j = 1;
                  while(!oRs.eof)
                  {
%>                  <tr id="row<%=j%>" OnClick="javascript:selectLocRow(<%=j%>);">
                      <td class="selectable" id="loc"><%/*note:this content is matched in itemExist, watch out!*/%>
                        <%=safe.html("" + oRs("plaats").value)%></td>
                      <td class=selectable>
                        <input type="text" class="fldshort float" maxlength="12" id=akn<%=j%> name=akn<%=j%> id=akn onBlur="checkWeight(event);" value=<%=safe.editablefloat(oRs("cnt_contract_plaats_gewicht").value)%>>
                        <input type="hidden" id="ak<%=j%>" name="ak<%=j%>" value="<%=oRs("cnt_contract_plaats_key").value%>">
                        <input type="hidden" id="akd<%=j%>" name="akd<%=j%>" value="<%=oRs("cnt_alg_plaats_key").value%>">
                        <input type="hidden" id="aks<%=j%>" name="aks<%=j%>" value="<%=safe.htmlattr(oRs("plaats").value)%>">
                        <input type="hidden" id="akt<%=j%>" name="akt<%=j%>" value="<%=oRs("cnt_alg_plaats_code").value%>">
                      </td>
                    </tr>
<%                  j++;
                    oRs.MoveNext();
                  }
                  oRs.close(); %>
                  </table>
                </span>

                <input type="hidden" id="loccnt" name="loccnt" value="<%=j-1%>">
              </td>
              <td>
                <table>
                <tr>
                  <td>
                    <span class="mover" onclick="addLoc()">&lt;</span><br><br>
                    <span class="mover" onclick="removeLoc()">&gt;</span><br><br>
                    <span class="mover" onclick="removeAllLoc()">&gt;&gt;</span>
                  </td>
                  <td>
                    <table>

                    <!-- Locatie -->
                    <tr>
<%                    sql = "SELECT alg_locatie_key"
                          + "     , alg_locatie_omschrijving"
                          + "  FROM alg_locatie"
                          + " WHERE alg_locatie_verwijder IS NULL"
                          + ((authparams.ALGwritelevel > -1)
                            ? " AND alg_locatie_key IN (SELECT alg_locatie_key FROM fac_v_my_locations "
                              + " WHERE prs_perslid_key = " + user_key + " AND niveau = " + authparams.ALGwritelevel + ")"
                            : "")
                          + " ORDER BY alg_locatie_upper";
                      FCLTselector("locs",
                                   sql,
                                   { label: L("lcl_location"),
                                     startmulti: true,
                                     size: 10,
                                     onChange: 'updateBld("' + authparams.ALGwritelevel + '");'
                                   }); %>
                    </tr>

                    <!-- Gebouw -->
                    <tr>
                      <td class="label"><label for="blds"><%=L("lcl_building")%>:</label></td>
                      <td>
                        <div id="selectblddiv">
<%                        FCLTselector("blds",
                                       "", // sql: lege selectbox die onload wordt geladen
                                       { startmulti: true,
                                         size: 10
                                       }); %>
                        </div>
                      </td>
                    </tr>
                    </table>
                  </td>
                </tr>
                </table>
              </td>
            </tr>
<%        } %>

<%        // Objectensoort scope
          if (cnt_srtcontract_type == 3)
          { %>
            <tr>
              <td><b><%=L("lcl_cnt_objectsrt_scope")%></b></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td class="resultkolom">
                <span id=ospass>
                  <table id="cntobjsortscope_list">
<%                // Works only for Objects now; query columns match shared/load_robjs.asp
                  sql = "SELECT DISTINCT cos.cnt_contract_object_key"
                      + ", s.ins_srtdeel_key"
                      + ", "+ lcl.xsqla('s.ins_srtdeel_omschrijving','s.ins_srtdeel_key')
                      + " FROM ins_v_srtdeel_gegevens s"
                      + ", cnt_contract_object cos"
                      + ", ins_v_alg_onroerendgoed ao"
                      + " WHERE "
                      + " cos.cnt_ins_srtdeel_key = s.ins_srtdeel_key AND "
                      + " cos.cnt_ins_deel_key IS NULL AND "
                      + " cos.cnt_contract_object_verwijder IS NULL AND"
                      + " cos.cnt_contract_key = " + cnt_key
                      + " ORDER BY 3";
                  oRs = Oracle.Execute(sql);
                  j = 1;
                  while (!oRs.eof)
                  {
%>                  <tr id="row<%=j+1000%>" OnClick="javascript:selectObjSortRow(<%=j + 1000%>);">
                      <td class="selectable" id="obsort"><%/*note:this content is matched in itemExist, watch out!*/%>
                          <%=oRs("ins_srtdeel_omschrijving").value%></td>
                       <td>
                       <input type="hidden" id="sk<%=j%>" name="sk<%=j%>" value="<%=oRs("cnt_contract_object_key").value%>">
                       <input type="hidden" id="skd<%=j%>" name="skd<%=j%>" value="<%=oRs("ins_srtdeel_key").value%>">
                       <input type="hidden" id="sks<%=j%>" name="sks<%=j%>" value="<%=oRs("ins_srtdeel_omschrijving").value%>">
                      </td>
                    </tr><%
                    j++;
                    oRs.MoveNext();
                  }
                  oRs.close(); %>
                  </table>
                </span>

                <input type=hidden id=oscnt name=oscnt value=<%=j-1%>>
              </td>
              <td>
                <table>
                <tr>
                  <td>
                    <span class="mover" onclick="addObjSort()">&lt;</span><br><br>
                    <span class="mover" onclick="removeObjSort()">&gt;</span><br><br>
                    <span class="mover" onclick="removeAllObjSort()">&gt;&gt;</span>
                  </td>
                  <td>
                    <table>

                    <!-- Discipline -->
<%                  sql = "SELECT ins_discipline_key"
                        + ", "+ lcl.xsqla('ins_discipline_omschrijving','ins_discipline_key')
                        + " FROM ins_v_aanwezigdiscipline"
                        + " WHERE ins_discipline_key IN"
                          + " (SELECT ins_discipline_key"
                          + " FROM fac_v_webgebruiker g"
                          + ", fac_functie f"
                          + " WHERE g.prs_perslid_key = " + user_key
                          + " AND f.fac_functie_key = g.fac_functie_key "
                          + " AND f.fac_functie_code = 'WEB_INSMAN'"
                          + " AND (g.fac_gebruiker_alg_level_read < 9 OR g.fac_gebruiker_prs_level_read < 9)" + ")"
                        + " ORDER BY "+ lcl.xsql('ins_discipline_omschrijving','ins_discipline_key') +" ASC";
                    FCLTselector("disc",
                                 sql,
                                 { label: L("lcl_discipline"),
                                   startmulti: true,
                                   size: 10,
                                   onChange: "updateGroup('WEB_INSMAN');"
                                 }); %>

                    <!-- Groep -->
<%                  FCLTselector("srtgroup",
                                 "", // sql: lege selectbox die onload wordt geladen
                                 { label: L("lcl_obj_group"),
                                   startmulti: true,
                                   size: 10,
                                   onChange: "updateSrtDeel('WEB_INSMAN');"
                                 }); %>

                    <!-- Objectsoort -->
<%                  FCLTselector("srtdeelmulti",
                                 "", // sql: lege selectbox die onload wordt geladen
                                 { label: L("lcl_obj_sort"),
                                   startmulti: true,
                                   size: 10
                                 }); %>
                    </table>
                  </td>
                </tr>
                </table>
              </td>
            </tr>
<%        } /* if (only_loc) */ %>
          </table>
        </td>
      </tr>
      </table>
<% IFACE.FORM_END(); %>
    </form>
    <iframe src="../Shared/empty.asp" frameborder="0" name="hidFrame" id="hidFrame" style="display:none"></iframe>
  </body>
</html>
