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

     File:               CNT/cnt_show_contract.asp
     Description:        Functie bestaand contractrecord toont of nieuwe laat editen
     Parameters:         cnt_key   Contract key
     Context:
     Note:
        Afhankelijk van het type contract heeft een contract een bijbehorende scope
        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


*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../fin/fin_verkoopfactuur.inc" -->
<%
FCLTHeader.Requires({ plugins: ["suggest","jQuery"],
                           js: ["jquery-ui.js"],
                          css: ["../shared/suggest/suggest.css"] // css voor child frames
                    });

var verynew = getQParamInt("verynew", 0) == 1;
var cnt_key = getQParamInt("cnt_key", -1);

var transitParam = "";
if (verynew)
  transitParam += "&verynew=1";

var tabtitle=L("lcl_contract");
if (cnt_key > 0) {
     sql = "SELECT cnt_contract_nummer_intern, cnt_contract_versie"
         + "  FROM cnt_contract"
         + " WHERE cnt_contract_key = " + cnt_key;
     oRs = Oracle.Execute (sql);
     if (oRs.eof)
        shared.record_not_found();
  tabtitle += " "+ oRs("cnt_contract_nummer_intern").Value + (oRs("cnt_contract_versie").Value ? "."+oRs("cnt_contract_versie").Value : "");
  oRs.close();
}
%>
<html>
  <head>
<%
    FCLTHeader.Generate();
%>
    <script type="text/javascript">
      $(document).ready(function () {
            FcltMgr.setTitle("<%=safe.jsstring(tabtitle)%>", {hot: false});
<%          if (cnt_key == -1) { %>FcltMgr.startEdit(window);<% } %>
      })

      function contractClose(params)
      {
        if (params.close)
        {
          FcltMgr.closeDetail(window, { cancel: true } );
          return true;;
        }
        if (params.keepForm)
            return;
<%      if (cnt_key == -1)
        { %>
          if (params.cancel)
          {
              FcltMgr.closeDetail(window, params );
              return true;
          }

          FcltMgr.setTitle("<%=L("lcl_contract")%> " + params.cnt_key, {hot:false});
          // TODO geleend van MLD. Is dit hier nodig? Ook is cnt_key niet de juiste tabtekst, maar zien we die?
          // nieuw contract, ga verder in 'verynew' modus
          // Altijd complete reload, we hebben nu een cnt_key namelijk
          window.location.href = "cnt_contract.asp?verynew=1&cnt_key=" + params.cnt_key;
<%      }
        else
        { // Bestaand contract bewerkt, switch naar show-mode
%>
          $("#cntFrame")[0].src = "cnt_show_contract.asp?cnt_key=<%=cnt_key%>";
<%      } %>
      }

      function scopeClose(params)
      {
        $("#scopeFrame")[0].src = "cnt_show_scope.asp?cnt_key=<%=cnt_key%>";
        if (!params.cancel)
            $("#cntFrame")[0].src = "cnt_show_contract.asp?cnt_key=<%=cnt_key%>";
      };
    </script>
  </head>

  <body>
<%
    if (cnt_key == -1)
    {
      var page="cnt_edit_contract.asp"; // Maak een nieuw

      var srtdisc = getQParamInt("srtdisc", -1);

      if (srtdisc == -1)
      {

      }

      if (srtdisc > 0)
        transitParam += "&srtdisc=" + srtdisc;
      else
      {
        //Dan maar vragen
        var buttons = [];
        // Give the user a list and let him select a srtdiscipline.
        mySRTsql = "SELECT cs.ins_discipline_key"
                 + "     , "+ lcl.xsqla('cs.ins_discipline_omschrijving','cs.ins_discipline_key')
                 + "     , "+ lcl.xsqla('dp.cnt_disc_params_opmerking','dp.cnt_ins_discipline_key')
                 + "  FROM cnt_discipline cs"
                 + "     , cnt_disc_params dp"
                 + " WHERE cs.ins_discipline_key = dp.cnt_ins_discipline_key"
                 + "   AND cs.ins_discipline_verwijder IS NULL"
                 + "   AND cs.ins_discipline_key IN"
                   + " (SELECT ins_discipline_key"
                   + "    FROM fac_v_webgebruiker g"
                   + "       , fac_functie f"
                   + "   WHERE g.fac_functie_key = f.fac_functie_key"
                   + "     AND f.fac_functie_code IN ('WEB_CNTMAN'" + (S("cnt_contract_approval") == 1? ", 'WEB_CNTUSE'" : "") + ")"
                   + "     AND g.fac_gebruiker_alg_level_write < 9"
                   + "     AND g.fac_gebruiker_prs_level_write < 9" // prs en alg write rechten
                   + "     AND g.prs_perslid_key = " + user_key + ")"
                   + " ORDER BY "+ lcl.xsql('ins_discipline_omschrijving','cs.ins_discipline_key');

        oRs = Oracle.Execute(mySRTsql);
        while (!oRs.eof)
        {
          srtdisc = oRs("ins_discipline_key").Value;
          buttons.push({ title: oRs("ins_discipline_omschrijving").Value,
                         action: "srtdisc=" + srtdisc + ";$('div#dmodal').dialog('close').remove();",
                         showText: false,
                         tooltip: oRs("cnt_disc_params_opmerking").Value
                       });
          oRs.MoveNext();
        }
        if (buttons.length == 0)
          Response.write(L("lcl_mld_choose_na"));
        else if (buttons.length == 1)
        {
          transitParam += "&srtdisc=" + srtdisc;
        }
        else
        {
          var page="../shared/empty.html";
%>        <script type='text/javascript'>
            srtdisc = -1;
            function dialogClose()
            {
              if (srtdisc== -1)
                FcltMgr.closeDetail(window, { cancel: true } );
              else
                $("#cntFrame")[0].src = "cnt_edit_contract.asp?cnt_key=<%=cnt_key%><%=transitParam%>&srtdisc="+srtdisc;
            }
            $(document).ready(function()
                              {
                                var params = { title: "<%=L("lcl_cnt_frame_algemeen")%>",
                                               modal: true,
                                               width : 300,
                                               resizable : false,
                                               close : dialogClose
                                             };
                                FcltMgr.fixActiveelementError();
                                $('div#dmodal').dialog(params).dialog('open');
                              });
          </script>
          <div id="dmodal" style="display:none">
<%          if (buttons.length)
              CreateButtons(buttons, { vertical: 1, isDialog: true} )
            else
              Response.write(L("lcl_mld_choose_na")); %>
          </div>
<%      }
      }
    }
    else
    {
      var page="cnt_show_contract.asp";
    }
    // 1. Het hoofdframe
    page += "?cnt_key=" + cnt_key + transitParam;
    IFRAMER("cntFrame", page, { title: L("lcl_cnt_frame_algemeen"),
                                initHeight: "450px",
                                FcltClose: "contractClose"
                              } );
    if (cnt_key > 0)
    {
      // ===================== CONTRACTNOTES FRAME ============================
      // Future extension kan zijn hiervoor WEB_CNTUSE te vereisen,
      // en dan read om te zien en write om toe te voegen
      page = "cnt_show_note.asp?cnt_key=" + cnt_key + transitParam;
      IFRAMER("noteFrame", page, { title: L("lcl_mld_frame_notes"), refreshOnClose: true, initHide: S("note_collapsed") } );

      // ===================== CONTRACTSCOPE FRAME ============================
      // Deze alleen als er mogelijke scope is: type niet 4 (generiek) of 6 (mantel)
      var cnt_typecontract = -1;
      var sql = "SELECT dp.cnt_srtcontract_type "
              + "  FROM cnt_contract c, cnt_disc_params dp "
              + " WHERE c.ins_discipline_key = dp.cnt_ins_discipline_key "
              + "   AND cnt_contract_key =" + cnt_key ;
      var oRs = Oracle.Execute( sql );
      if(!oRs.eof){
           cnt_typecontract = oRs("cnt_srtcontract_type").value;
      }
      oRs.close();
      if (cnt_typecontract != 4 && cnt_typecontract != 6) {
          page = "cnt_show_scope.asp?cnt_key=" + cnt_key;
          IFRAMER("scopeFrame", page, { title: L("lcl_cnt_frame_scope"), initHide: true, FcltClose: "scopeClose" } );
      }

      // ===================== OPDRACHTEN FRAME    ============================
      var orderExist = false;
      var sql = "SELECT 'x'"
              + " FROM mld_opdr"
              + " WHERE cnt_contract_key =" + cnt_key ;
      var oRs = Oracle.Execute( sql );
      if(!oRs.eof){
           orderExist = true;;
      }
      oRs.close();
      var authparamsMLDBOF = user.checkAutorisation("WEB_MLDBOF", true);
      if (orderExist && authparamsMLDBOF) {  // d.i. minstens ergens readrechten op
          page = "../mld/opdr_list.asp?urole=bo&cnt_key=" + cnt_key + transitParam;
          IFRAMER("opdrFrame", page, { title: L("lcl_mld_frame_opdr"), initHide: true, refreshOnClose: true } );
      }

      // ===================== FACTUREN FRAME      ============================
      var invoiceExist = false;
      var sql = "SELECT 'x'"
              + " FROM fin_factuur"
              + " WHERE cnt_contract_key =" + cnt_key ;
      var oRs = Oracle.Execute( sql );
      if(!oRs.eof){
           invoiceExist = true;;
      }
      oRs.close();
      var authparamsFINFOF = user.checkAutorisation("WEB_FINFOF", true);
      var authparamsFINBOF = user.checkAutorisation("WEB_FINBOF", true);
      if (invoiceExist && (authparamsFINFOF || authparamsFINBOF)) {
          page = "../fin/fin_list.asp?embedded=1&cnt_key=" + cnt_key + transitParam;
          IFRAMER("finFrame", page, { initHide: true, refreshOnClose: true } );
      }

      verkoopfactuur_IFRAMER('contract', cnt_key);
    }
%>
  </body>
</html>
