<% /*
    $Revision$
    $Id$

    File:               cad_contour_list.inc
    Description:        Toon contouren zoals ingescannen gematched met de ruimtes
    Parameters:
    Context:            Search action from cad_contour_search_list.asp form
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../SlnkDWF/DWFProperties.inc" -->
<!-- #include file='../CAD/FGII.inc' -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["jquery-ui.js"] });

function contour_list(pautfunction, params)
{
  var addButton = [];

  autfunction = "WEB_CADBOF"; // TODO: Scope via user.func_enabled("CAD", null, ????)
  var authparams = user.checkAutorisation(autfunction); // minstens leesrechten
  var canChange = (authparams.ALGwritelevel < 9);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var print = (outputmode == 1 || outputmode == 5);
  var excel = (outputmode == 2 || outputmode == 6);
  var showall = params.showall;

  var loc_key = params.loc_key;
  var bld_key = params.bld_key;
  var flr_key = params.flr_key;
  var bttype = params.bttype;
  var bldcount = params.bldcount;
  var concept = params.concept; // tabblaadje CAD, minder kolommen
  var highlight_arr = params.highlight_arr||[];

  if (bttype == "T")
  {
    var inoutMode = MODE_OUT;
  }
  else
  {
    var inoutMode = MODE_IN;
  }

  if (flr_key || (bld_key > 1 && bttype == "T"))
  {
    sql =  "SELECT cad_tekening_key,"
    sql += "       cad_tekening_filenaam,"
    sql += "       cad_tekening_key"
    sql += "  FROM cad_tekening ct"
    sql += " WHERE cad_tekening_verwijder IS NULL"
    sql += "   AND cad_tekening_type = 'P'"
    sql += (flr_key > 0
           ? "   AND ct.alg_verdieping_key = " + flr_key
           : "   AND ct.alg_locatie_key = " + loc_key
             + " AND ct.alg_verdieping_key IS NULL");
    oRs = Oracle.Execute(sql);
    if (oRs.Eof)
    {   // Resulteert in melding 'Tekening niet gevonden' wat feitelijk moet zijn
        // tekening niet geconfigureerd. Ach...
        var hasActual = false;
        var hasConcept = false;
    }
    else
    {
        var fname = oRs("cad_tekening_filenaam").value;
        fname = fname.replace(/.dwg$/i,'');
        fname = fname.replace(/.dwf$/i,'');
        var fullnameActual = S("fg_dwf_path") + fname + ".dwf";
        var fullnameConcept = S("fg_dwf_path_concept") + fname + ".dwf";
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var hasActual = fso.FileExists(fullnameActual);
        var hasConcept = fso.FileExists(fullnameConcept);
    }
    if (!concept && !hasActual)
        shared.simpel_page(L("lcl_fg_drawingnotfound"))
    else if (concept && !hasConcept)
        shared.simpel_page(L("lcl_fg_conceptnotfound"))
  }

  function fnrowData(oRs)
        {
          if (inoutMode == MODE_IN)
          {
          var ruimte_key = oRs("KEY").value;
          var ruimte_nr  = oRs("alg_ruimte_nr").value;
          var verdieping_key = oRs("alg_verdieping_key").value;
          var gebouw_key = oRs("alg_gebouw_key").value;
          var locatie_key = oRs("alg_locatie_key").value;

          var data = {ruimte_key: ruimte_key,
                      gebouw_key: gebouw_key,
                      verdieping_key: verdieping_key,
                      ruimte_nr: ruimte_nr,
                      cad_ruimte_nr: oRs("cad_ruimte_nr").value,
                      locatie_key: locatie_key};

          return JSON.stringify(data);
          }
        }
%>

  <html>
    <head>
<%    FCLTHeader.Generate({outputmode: outputmode}); %>
      <script type="text/javascript">

      function editRoom(row)
      {
        var cadData = eval('(' + row.getAttribute("ROWDATA") + ')');
        var url = "appl/alg/alg_ruimte.asp?key="+cadData.ruimte_key+"&cad_ruimte_nr="+cadData.cad_ruimte_nr+"&verdiepingkey="+cadData.verdieping_key;
        FcltMgr.openDetail(url, "<%=L("lcl_room")%>");
      }



    function contour_update()
    {
        if (anyDouble)
        {
            FcltMgr.alert(L("lcl_fg_tek_meerdere_disable"));
            return;
        }

        var loc = -1;
        var bld = -1;
        var bttype = ""
        var flo = -1;

        var loc_key = <%=(loc_key?loc_key:-1)%>;
        var bld_key = <%=(bld_key?bld_key:-1)%>;
        var flr_key = <%=(flr_key?flr_key:-1)%>;
        var bttype = "<%=bttype%>";

        if (loc_key) {
            loc = loc_key
        }
        if (bld_key) {
            bld = bld_key
        }
        if (flr_key) {
            flo = flr_key
        }

        // Bij doorvoeren krijg je de optie
        //   [ ] Concept tekening naar actuele tekening
        //   [ ] Concept oppervlakte naar actuele oppervlakte
        // Na het doorvoeren van concept naar actuele tekening wordt er direct ook gescand.
        url = "../CAD/contour_update.asp?location_key=" + loc + "&building_key=" + bld + "&floor_key=" + flo + "&bttype=" + bttype;
        FcltMgr.openModalDetail(url, L("lcl_cad_doorvoeren"), {callback: FcltMgr.reload});
    }
      </script>
    </head>
    <body id="listbody">
<%
    sql_select_contouren = "(select c.CAD_IMP_CONTOUR_NR, SUM(c.CAD_IMP_CONTOUR_OPP) CAD_IMP_CONTOUR_OPP, "
                        + "         SUM(c.CAD_IMP_CONTOUR_OPP_ALT1) CAD_IMP_CONTOUR_OPP_ALT1,"
                        + "         SUM(c.CAD_IMP_CONTOUR_OPP_ALT2) CAD_IMP_CONTOUR_OPP_ALT2,"
                        + "         t.alg_locatie_key, t.alg_verdieping_key, count(*) aantal"
                        + "      from cad_imp_contour c, cad_tekening t "
                        + "     where c.cad_tekening_key = t.cad_tekening_key "
                        + "       and cad_imp_contour_concept = " + (concept?'1':'0');

    if (inoutMode == MODE_OUT)
        sql_select_contouren += " and t.alg_verdieping_key is null";
    sql_select_contouren+= "     group by c.CAD_IMP_CONTOUR_NR, t.alg_locatie_key, t.alg_verdieping_key)";


    if (inoutMode == MODE_IN)
    {
        dbTable  = "ALG_V_AANWEZIGRUIMTE";
        dbLabel  = "ALG_RUIMTE_NR";
        colKey   = "ALG_RUIMTE_KEY";
        syncview = S("fg_syncruimteview"); //"ALG_V_AANWEZIGRUIMTE";
        colLevel = "ALG_VERDIEPING_KEY";
        oppField = "ALG_RUIMTE_BRUTO_VLOEROPP";
    }
    else
    {
        dbTable  = "ALG_V_AANWEZIGTERREINSECTOR";
        dbLabel  = "ALG_TERREINSECTOR_CODE";
        colKey   = "ALG_TERREINSECTOR_KEY";
        syncview = S("fg_syncterreinview");
        colLevel = "ALG_LOCATIE_KEY";
        oppField = "ALG_TERREINSECTOR_OPPERVLAK";
    }

    sqln = "SELECT   l.alg_locatie_code, "
         + "         COALESCE (r."+colKey+", -1) KEY, "
         + "         r.cad_imp_contour_opp, "
         + "         r.cad_imp_contour_opp_alt1, "
         + "         r.cad_imp_contour_opp_alt2, "
         + "         r.alg_ruimte_nr cad_ruimte_nr, "
         + "           COALESCE (r.cad_imp_contour_opp, 0) "
         + "         - COALESCE (a."+oppField+", 0) diff, "
         + "         r.aantal, "
         + "         a."+dbLabel+", "
         + "         a."+oppField+", ";
if (inoutMode == MODE_IN)
{
    sqln +="         v.alg_verdieping_key, "
         + "         v.alg_gebouw_key, "
         + "         g.alg_locatie_key, "
         + "         g.alg_gebouw_code, "
         + "            DECODE (v.alg_verdieping_code, "
         + "                    NULL, '', "
         + "                    v.alg_verdieping_code || ' - ' "
         + "                   ) "
         + "         || v.alg_verdieping_omschrijving verd, "
         + lcl.xsqla("s.alg_srtruimte_omschrijving", "s.alg_srtruimte_key")+","
         + "         a.alg_ruimte_omschrijving, "
         + "         v.alg_verdieping_volgnr "
}
else
{
    sqln +="         -1 alg_verdieping_key, "
         + "         -1 alg_gebouw_key, "
         + "         a.alg_locatie_key, "
         + "         alg_terreinsector_omschrijving alg_gebouw_omschrijving, "  //niet code?
         + "         '("+L("lcl_terra")+")' verd, "
         + "         ' ' alg_srtruimte_omschrijving, "
         + "         ' ' alg_ruimte_omschrijving, "
         + "         1 alg_verdieping_volgnr ";
}

if (inoutMode == MODE_IN)
{
   sqln += "    FROM alg_locatie l, "
         + "         alg_gebouw g, "
         + "         alg_verdieping v, "
         + "         (SELECT TO_NUMBER (NULL) alg_ruimte_key, "
         + "                 ir.alg_verdieping_key, "
         + "                 ir.cad_imp_contour_nr alg_ruimte_nr, "
         + "                 ir.cad_imp_contour_opp, "
         + "                 ir.cad_imp_contour_opp_alt1, "
         + "                 ir.cad_imp_contour_opp_alt2, "
         + "                 ir.aantal "
         + "            FROM "+syncview+" sync, "
         +          sql_select_contouren +"  ir "
         + "           WHERE ir."+colLevel+" = sync."+colLevel+"(+) "
         + "             AND ir.cad_imp_contour_nr = sync.cadlabel(+) "
         + "             AND sync."+colLevel+" IS NULL "
         + "          UNION ALL "
         + "          SELECT sync.alg_ruimte_key, "
         + "                 sync.alg_verdieping_key, "
         + "                 sync.cadlabel, "
         + "                 ir.cad_imp_contour_opp, "
         + "                 ir.cad_imp_contour_opp_alt1, "
         + "                 ir.cad_imp_contour_opp_alt2, "
         + "                 ir.aantal "
         + "            FROM "+syncview+" sync, "
         +          sql_select_contouren + " ir "
         + "           WHERE ir."+colLevel+"(+) = sync."+colLevel
         + "                 AND ir.cad_imp_contour_nr(+) = sync.cadlabel) r, "
         + "         alg_v_aanwezigruimte a, "
         + "         alg_srtruimte s "
         + "   WHERE v.alg_verdieping_key = r.alg_verdieping_key "
         + "     AND g.alg_gebouw_key = v.alg_gebouw_key "
         + "     AND l.alg_locatie_key = g.alg_locatie_key "
         + "     AND a.alg_ruimte_key(+) = r.alg_ruimte_key "
         + "     AND s.alg_srtruimte_key(+) = a.alg_srtruimte_key ";
        if (highlight_arr.length > 0 && highlight_arr.join(",").indexOf("-1") == -1)
            sqln += " AND a.alg_ruimte_key IN (" + params.highlight_arr.join(",") + ")";
}
else
{
   sqln += "    FROM alg_locatie l, "
         + "         (SELECT TO_NUMBER (NULL) alg_terreinsector_key, "
         + "                 ir.alg_locatie_key, "
         + "                 ir.cad_imp_contour_nr alg_ruimte_nr, "
         + "                 ir.cad_imp_contour_opp, "
         + "                 ir.cad_imp_contour_opp_alt1, "
         + "                 ir.cad_imp_contour_opp_alt2, "
         + "                 ir.aantal "
         + "            FROM "+syncview+" sync, "
         +        sql_select_contouren +"  ir "
         + "           WHERE ir."+colLevel+" = sync."+colLevel+"(+) "
         + "             AND ir.cad_imp_contour_nr = sync.cadlabel(+) "
         + "             AND sync."+colLevel+" IS NULL "
         + "          UNION ALL "
         + "          SELECT sync.alg_terreinsector_key, "
         + "                 sync.alg_locatie_key, "
         + "                 sync.cadlabel, "
         + "                 ir.cad_imp_contour_opp, "
         + "                 ir.cad_imp_contour_opp_alt1, "
         + "                 ir.cad_imp_contour_opp_alt2, "
         + "                 ir.aantal "
         + "             FROM "+syncview+" sync, "
         +          sql_select_contouren + " ir "
         + "           WHERE ir."+colLevel+"(+) = sync."+colLevel
         + "                 AND ir.cad_imp_contour_nr(+) = sync.cadlabel) r, "
         + "         alg_v_aanwezigterreinsector a "
         + "   WHERE l.alg_locatie_key = r.alg_locatie_key "
         + "     AND a.alg_terreinsector_key(+) = r.alg_terreinsector_key "
}
    if (authparams.ALGreadlevel>-1) {
      sqln += "AND v.ALG_VERDIEPING_KEY IN (SELECT alg_verdieping_key FROM fac_v_my_floors "
            + "WHERE prs_perslid_key="+user_key+" AND niveau="+authparams.ALGreadlevel+")";
    }

    if (loc_key) {
      sqln += " AND l.alg_locatie_key = " + loc_key;
    }
    if (inoutMode == "G" && bld_key) {
      sqln += " AND g.alg_gebouw_key = " + bld_key;
    }

    if (flr_key) {
      sqln += " AND v.alg_verdieping_key = " + flr_key;
    }

    sqln += " ORDER BY alg_gebouw_omschrijving, "
         + "         alg_verdieping_volgnr, "
         + "         alg_ruimte_nr ";

    var buttons = [];
    if (canChange)
        var buttons = [{ icon: "doorvoeren.png", title: L("lcl_estate_room_submit"), action: "contour_update()" }];

    var rst = new ResultsetTable({sql: sqln,
                                 keyColumn: "KEY",
                                 ID: "workTable",
                                 showAll: showall,
                                 rowData: fnrowData,
                                 title: params.concept?L("lcl_fg_concept"):L("lcl_fg_actueel"),
                                 outputmode: outputmode,
                                 filterParams: params,
                                 buttons: buttons
                                });
    // TODO: Onderstaande input regel zou m.b.v. summaryCalc en summaryShow in de titelbalk moeten worden weergegeven.
    if (outputmode == 0)
    { %>
      <table width='100%' border=0>
        <tr>
          <td colspan=2 align="right">
            <label><%=L("lcl_fg_room_opp_dif_tot")%>:&nbsp;</label>
            <input class="fldnum" type=text name="tot_dif" id="tot_dif"  readonly>
            <b>&nbsp;m<sup>2</sup></b>
          </td>
        </tr>
      </table>
<%  }

    var v_total_dif = 0;
    var records_found = 0;

    function increasetotaldiff(thisdiff)
    {
      records_found = records_found + 1;
      v_total_dif = v_total_dif + thisdiff;
    }

    function formatdiff(oRs) {
      // trick: sum these values for later by calling increasetotaldiff
      increasetotaldiff (oRs("DIFF").Value);
      if (oRs("DIFF").Value == 0)
        return "";
      return "<span class='area_diff'>" + (oRs("DIFF").Value > 0?'+':'')+safe.curr(oRs("DIFF").Value) + "</span>";
    }

    // Waarschuw voor dubbele voorkomens
    var anyDouble = false; // Dan mogen we niet doorvoeren
    function cad_ruimte(oRs) {
      // show cad ruimte_nr
      if (oRs("aantal") > 1)
      {
        anyDouble = true;
        return "<div style='background-color:red;' title='"+oRs("aantal")+" "+L("lcl_fg_tek_meerdere")+"'>"+oRs("cad_ruimte_nr").Value+"</div>";
      }
      else
      {
        if (oRs("CAD_IMP_CONTOUR_OPP").value == null)
            return "<div style='color:red;' title='"+L("lcl_fg_tek_cadnotfound")+"'>"+oRs("cad_ruimte_nr").Value+"</div>";
        else
            return oRs("cad_ruimte_nr").Value;
      }
    }

    function alg_ruimte(oRs)
    {
        var rnr = oRs(inoutMode == MODE_IN?"alg_ruimte_nr":"alg_terreinsector_code").Value;
        if (rnr)
            return safe.html(rnr);

        return "<div style='color:red;' title='"+L("lcl_fg_tek_algnotfound")+"'>---</div>";
    }

    if (!flr_key)
    {
        rst.addColumn(new Column({caption: L("lcl_fg_tek_loc"), content: "ALG_LOCATIE_CODE"}));
        if (inoutMode == MODE_IN)
          rst.addColumn(new Column({caption: L("lcl_bld_terra"), content: "ALG_GEBOUW_CODE"}));
        rst.addColumn(new Column({caption: L("lcl_fg_tek_floor"), content: "VERD"}));
    }
    rst.addColumn(new Column({caption:(inoutMode == MODE_IN?L("lcl_estate_ruimte_man_nr"):L("lcl_estate_terreinsector_man_code")), content: alg_ruimte}));
    rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_nr_CAD"), content: cad_ruimte, hasActions: true}));
    rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_sort"), content: "ALG_SRTRUIMTE_OMSCHRIJVING"}));
    rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_descr"), content: "ALG_RUIMTE_OMSCHRIJVING"}));
    rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp"), datatype: "float", content: oppField}));
    rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_g"), datatype: "float", content: "CAD_IMP_CONTOUR_OPP"}));
    rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_dif"), datatype: "float", content: formatdiff}));
if (S("fg_contourlayers_alt1")!=""&&S("fg_labellayers_alt1")!="" && inoutMode == MODE_IN)
{
    rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_g_alt1"), datatype: "float", content: "CAD_IMP_CONTOUR_OPP_ALT1"}));
}
if (S("fg_contourlayers_alt2")!=""&&S("fg_labellayers_alt2")!="" && inoutMode == MODE_IN)
{
    rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_g_alt2"), datatype: "float", content: "CAD_IMP_CONTOUR_OPP_ALT2"}));
}

    if (inoutMode == MODE_IN)
      rst.addAction({ action: "editRoom", caption: L("lcl_goto_ruimte_edit"), isDefault:true});

    var cnt = rst.processResultset();
    if (canChange && cnt <= S("qp_maxrows") && !anyDouble) // vind eventuele nog onzichtbare dubbelen
    {
        var sql = "SELECT count(*) dubbel FROM ("+sqln+") WHERE aantal > 1";
        oRs = Oracle.Execute(sql);
        anyDouble = oRs("dubbel").Value > 0;
    }
%>
<script  type="text/javascript" >
  var anyDouble = <%=anyDouble? "true" : "false"%>;
<%if (outputmode == 0)
  { %>
    jQuery(document).ready(function()
                           {
                             $("#tot_dif").val(<%=safe.curr(v_total_dif)%>);
                           });
<%}%>
</script>
</body>
</html>
<%}%>

