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

    File:               contour_update.asp
    Description:        Kopieert de oppervlaktes uit CAD_IMP_CONTOUR naar ALG_RUIMTE
                        en de tekening vanuit de conceptfolder 'omhoog'
    Parameters:
        location_key
        building_key
        floor_key
    Context:            Ajax vanuit contour-overzicht-lijst
    Note:
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file='../CAD/FGII.inc'-->
<!-- #include file='./cad_scan_dwf.inc' -->
<!-- #include file="../Shared/json2.js" -->

<%
var message = L("lcl_fg_contour_synced");
var submitting = getQParamInt("submit", 0) == 1;
var doorvoeren_opp = (getFParam("cad_doorvoeren_opp", "off") == "on");
var doorvoeren_tek = (getFParam("cad_doorvoeren_tek", "off") == "on");
var message = doorvoeren_opp && doorvoeren_tek? L("lcl_cad_doorgevoerd_tekopp") : (doorvoeren_tek? L("lcl_cad_doorgevoerd_tek"): (doorvoeren_opp? L("lcl_cad_doorgevoerd_opp") : ""))

var location_key  = getQParamInt("location_key", -1);
var TransitParam = buildTransitParam(["mode", "location_key", "building_key", "floor_key", "bttype"]);

var building_key  = getQParamInt("building_key", -1);
var floor_key   = getQParamInt("floor_key", -1);
var bttype = getQParam("bttype", "");
var inoutMode = bttype == "T"? MODE_OUT : MODE_IN;

var hasConcept = false;
if (floor_key > 0 || (building_key > 1 && bttype == "T")) // alvast een en ander bepalen
{
    var sql = "SELECT cad_tekening_key"
            + "     , cad_tekening_filenaam"
            + "     , cad_tekening_key"
            + "  FROM cad_tekening ct"
            + " WHERE cad_tekening_verwijder IS NULL"
            + "   AND cad_tekening_type = 'P'"
            + (floor_key > 0
              ? " AND ct.alg_verdieping_key = " + floor_key
              : " AND ct.alg_locatie_key = " + location_key
                + " AND ct.alg_verdieping_key IS NULL");
    oRs = Oracle.Execute(sql);
    var cad_tek_key = oRs("cad_tekening_key").value;
    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");
    hasConcept = fso.FileExists(fullnameConcept);
    oRs.Close();
}
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>

<%  if (submitting)
    {
      protectRequest.validateToken();
      // eerst de tekening, voor het geval beide vinkjes zijn gezet
      if (doorvoeren_tek && hasConcept)
      {
          var success = false;
          try
          { // Moven van concept naar actual!
            __Log("Ik probeer " + fullnameConcept + " te verplaatsen naar " + fullnameActual);
            fso.CopyFile(fullnameConcept, fullnameActual, true/*overwrite*/);
            fso.DeleteFile(fullnameConcept);
            success = true;
          }
          catch (e)
          {
            message = "Doorvoeren concept " + fullnameConcept + " mislukt: \n" + e.description;
            __DoLog(message + "<br>" + safe.UserContext(), "#ffff00");
          }
          if (success)
          {
              // Nieuwe scannen (zou vaak ook wel in de database kunnen)
              message = scanDWF(cad_tek_key, 0);
              // Oude contouren opruimen
              Oracle.Execute("DELETE FROM CAD_IMP_CONTOUR"
                           + " WHERE cad_tekening_key = " + cad_tek_key
                           + "   AND cad_imp_contour_concept = 1");
          }
      }

      if (inoutMode == MODE_IN)
      {
        if (doorvoeren_opp) // altijd actuele opp naar alg_ruimte opp. Heeft niets met concepten te maken
        {
          var sql_select_from = "   FROM " + S("fg_syncruimteview") + " r"
                              + "      , alg_verdieping v"
                              + "      , alg_gebouw g"
                              + "      , alg_locatie l"
                              + "      , cad_imp_contour c"
                              + "      , cad_tekening t";

          var sql_select_where = " WHERE cad_imp_contour_concept = 0"
                               + "   AND 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 t.alg_verdieping_key = r.alg_verdieping_key"
                               + "   AND c.cad_tekening_key = t.cad_tekening_key"
                               + "   AND c.cad_imp_contour_nr = r.cadlabel"
                               + "   AND r.alg_ruimte_key = ru.alg_ruimte_key"
                               + (location_key > -1
                                 ? " AND g.alg_locatie_key = " + location_key
                                 : "")
                               + (building_key > -1
                                 ? " AND v.alg_gebouw_key = " + building_key
                                 : "")
                               + (floor_key > -1
                                 ? " AND v.alg_verdieping_key  = " + floor_key
                                 : "");


          var sql_tracking = "SELECT ru.alg_ruimte_key"
                           + "     , r.cadlabel"
                           + "     , ru.alg_ruimte_bruto_vloeropp"
                           + "     , c.cad_imp_contour_opp"
                           + "     , ru.alg_ruimte_opp_alt1"
                           + "     , c.cad_imp_contour_opp_alt1"
                           + "     , ru.alg_ruimte_opp_alt2"
                           + "     , c.cad_imp_contour_opp_alt2"
                           + sql_select_from
                           + "     , alg_ruimte ru"
                           + sql_select_where;
          var oRs_tracking = Oracle.Execute(sql_tracking);

          while (!oRs_tracking.eof)
          {
            var trackarray = [];
            if (oRs_tracking("alg_ruimte_bruto_vloeropp").Value != oRs_tracking("cad_imp_contour_opp").Value)
              trackarray.push(L("lcl_estate_ruimte_bruto_vloeropp") + ": " + oRs_tracking("alg_ruimte_bruto_vloeropp").Value + " => " + oRs_tracking("cad_imp_contour_opp").Value);
            if (oRs_tracking("alg_ruimte_opp_alt1").Value != oRs_tracking("cad_imp_contour_opp_alt1").Value)
              trackarray.push(L("lcl_estate_ruimte_opp_alt1") + ": " + oRs_tracking("alg_ruimte_opp_alt1").Value + " => " + oRs_tracking("cad_imp_contour_opp_alt1").Value);
            if (oRs_tracking("alg_ruimte_opp_alt2").Value != oRs_tracking("cad_imp_contour_opp_alt2").Value)
              trackarray.push(L("lcl_estate_ruimte_opp_alt2") + ": " + oRs_tracking("alg_ruimte_opp_alt2").Value + " => " + oRs_tracking("cad_imp_contour_opp_alt2").Value);
            if (trackarray.length > 0)
              // Wijzigingen van deze ruimte tracken.
              shared.trackaction("ALGRUP",
                                 oRs_tracking("alg_ruimte_key").Value,
                                 L("lcl_alg_is_algrup") + "\n" + trackarray.join("\n"));

            oRs_tracking.MoveNext();
          }
          oRs_tracking.close();

          var sql_update_select = "SELECT c.cad_imp_contour_opp"
                                + "     , c.cad_imp_contour_opp_alt1"
                                + "     , c.cad_imp_contour_opp_alt2"
                                + sql_select_from
                                + sql_select_where;

          var sql_update = "UPDATE alg_ruimte ru "
                         + "   SET (ru.alg_ruimte_bruto_vloeropp"
                         + "     ,  ru.alg_ruimte_opp_alt1"
                         + "     ,  ru.alg_ruimte_opp_alt2) = (" + sql_update_select + ")"
                         + " WHERE EXISTS (" + sql_update_select + ")";
          Oracle.Execute(sql_update);
        }
      }
      else
      { // Buiten, terreinsectoren
        if (doorvoeren_opp)
        {
          sql_select = "(SELECT c.cad_imp_contour_opp "
                     + "   FROM " + S("fg_syncterreinview") + " r, "
                     + "        cad_imp_contour c, "
                     + "        cad_tekening t "
                     + "  WHERE t.alg_locatie_key = r.alg_locatie_key "
                     + "    AND c.cad_tekening_key = t.cad_tekening_key "
                     + "    AND c.cad_imp_contour_nr = r.cadlabel "
                     + "    AND r.alg_terreinsector_key = ru.alg_terreinsector_key "

          sql_select += " AND r.alg_locatie_key    = " + location_key;
          sql_select += ")";

          sql = "UPDATE alg_terreinsector ru "
              + " SET   ru.alg_terreinsector_oppervlak = " + sql_select
              + " WHERE EXISTS " + sql_select;

          oRs = Oracle.Execute(sql);
        }
      }
%>
      <script type="text/javascript">
        FcltMgr.closeDetail( window,
                             { warning: "<%=safe.jsstring(message)%>"
                             });
      </script>
<%
      Response.End;
    }
    else // gewone formulier
    { %>
      <script type="text/javascript">

        function cad_cancel()
        {
            FcltMgr.closeDetail(window, { cancel: true } );
        }

        function cad_close(close)
        {
          if (!validateForm("u2"))
            return false;

          document.doorvoerenform.submit();
          result = true;
        }

        jQuery(document).ready(function()
                               {
                                 FcltMgr.resized(window);
                               });
      </script>
<%  } %>
  </head>

  <body class="modal" id="mod_caddoorvoeren">

    <form id="doorvoerenform" name="doorvoerenform" action="contour_update.asp?submit=1<%=TransitParam%>" method="post">
<%    BLOCK_START("cadDoorvoeren", L("lcl_cad_doorvoeren"));
if (hasConcept) {
%>    <tr>
        <td>
          <% CHECKBOX("fldcaddoorvoeren", "cad_doorvoeren_tek", 1 ); %>
        </td><td>
          <label for="cad_doorvoeren_tek"><%=L("lcl_cad_tek_concept_to_actueel")%></label>
        </td>
      </tr>
<% } %>
      <tr>
        <td>
          <% CHECKBOX("fldcaddoorvoeren", "cad_doorvoeren_opp", 1 ); %>
        </td><td>
          <label for="cad_doorvoeren_opp"><%=L("lcl_cad_opp_concept_to_actueel")%></label>
        </td>
      </tr>
<%    BLOCK_END();
      var buttons =  [ {title: L("lcl_doorvoeren"), icon: "accept.png", action: "cad_close()", singlepress: true, id:"btn_cad_submit"}
                     , {title: L("lcl_cancel"), icon: "cancel.png", action: "cad_cancel()"} ];
      CreateButtons(buttons);
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
