<% /*
    $Revision$
    $Id$

    File:               contour_update.inc
    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:            include voor contour_update.asp en api_files.asp
    Note:
*/
%>

<!-- #include file='../CAD/FGII.inc'-->
<!-- #include file='./cad_scan_dwf.inc' -->

<%

var contour = {
    make_actual: function _make_actual(cad_tek_key, fname) {
        var fullnameActual = S("flexfilespath") + "/cad/" + fname + ".dwf";
        var fullnameConcept = S("flexfilespath") + "/cad/concept/" + fname + ".dwf";
        var success = false;
        var message;
        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) {
            __DoLog("Doorvoeren concept " + fullnameConcept + " mislukt: \n" + e.description + "<br>" + safe.UserContext(), "#ffff00");
            message = "Doorvoeren concept /cad/concept/" + fname + ".dwf mislukt: \n" + e.description;
        }
        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");
        }
        return { success: success, message: message };
    },

    doorvoeren_opp: function _doorvoeren_opp(isTerrein, params) {
        params = params || {};
        if (isTerrein) {
            var 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 "
                           + "    AND r.alg_locatie_key = " + params.location_key
                           + ")";
            var sql = "UPDATE alg_terreinsector ru "
                    + "   SET ru.alg_terreinsector_oppervlak = " + sql_select
                    + " WHERE EXISTS " + sql_select;
            oRs = Oracle.Execute(sql);
        } else {
            var sql_from = " FROM alg_verdieping v"
                         + "    , alg_gebouw g"
                         + "    , alg_locatie l"
                         + "    , cad_imp_contour c"
                         + "    , cad_tekening t";

            var sql_where = " WHERE cad_imp_contour_concept = 0"
                          + "   AND g.alg_gebouw_key = v.alg_gebouw_key"
                          + "   AND l.alg_locatie_key = g.alg_locatie_key"
                          + "   AND c.cad_tekening_key = t.cad_tekening_key"
                          + (params.location_key && params.location_key > -1
                            ? " AND g.alg_locatie_key = " + params.location_key
                            : "")
                          + (params.building_key && params.building_key > -1
                            ? " AND v.alg_gebouw_key = " + params.building_key
                            : "")
                          + (params.floor_key && params.floor_key > -1
                            ? " AND v.alg_verdieping_key  = " + params.floor_key
                            : "");

            var sql_from_room = ", " + S("fg_syncruimteview") + " r";

            var sql_where_room = "   AND v.alg_verdieping_key = r.alg_verdieping_key"
                               + "   AND r.alg_ruimte_key = ru.alg_ruimte_key"
                               + "   AND c.cad_imp_contour_nr = r.cadlabel"
                               + "   AND t.alg_verdieping_key = r.alg_verdieping_key";

            var sql_where_floor = "   AND c.cad_imp_contour_nr = '" + L("lcl_floor") + " (' || v.alg_verdieping_code || ')'"
                                + "   AND t.alg_verdieping_key = v.alg_verdieping_key";

            var sql_track_room = "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_from
                               + sql_from_room
                               + "     , alg_ruimte ru"
                               + sql_where
                               + sql_where_room;
            var oRs_tracking = Oracle.Execute(sql_track_room);

            /* Track ruimte updates */
            var trackArray;
            while (!oRs_tracking.EoF)
            {
              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 === null ? L("lcl_trackfromempty") : oRs_tracking("alg_ruimte_bruto_vloeropp").Value)
                              + " => " + oRs_tracking("cad_imp_contour_opp").Value);
              }
              if (S("fg_contourlayers_alt1") != "" && 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 === null ? L("lcl_trackfromempty") : oRs_tracking("alg_ruimte_opp_alt1").Value)
                              + " => " + oRs_tracking("cad_imp_contour_opp_alt1").Value);
              }
              if (S("fg_contourlayers_alt2") != "" && 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 === null ? L("lcl_trackfromempty") : oRs_tracking("alg_ruimte_opp_alt2").Value)
                              + " => " + oRs_tracking("cad_imp_contour_opp_alt2").Value);
              }
              if (trackArray.length > 0) { // Wijzigingen van deze ruimte tracken, elk record kan een andere ruimte zijn
                shared.trackaction("ALGRUP",
                                  oRs_tracking("alg_ruimte_key").Value,
                                  L("lcl_alg_is_algrup") + "\n" + trackArray.join("\n"));
              }
              oRs_tracking.MoveNext();
            }
            oRs_tracking.Close();

            /* Update ruimte oppervlakte(s) */
            var sql_update_select_room = "SELECT c.cad_imp_contour_opp"
                                       + (S("fg_contourlayers_alt1") ? "     , c.cad_imp_contour_opp_alt1" : "")
                                       + (S("fg_contourlayers_alt2") ? "     , c.cad_imp_contour_opp_alt2" : "")
                                       + sql_from
                                       + sql_from_room
                                       + sql_where
                                       + sql_where_room;

            var sql_room_update = "UPDATE alg_ruimte ru "
                                + "   SET (ru.alg_ruimte_bruto_vloeropp"
                                + (S("fg_contourlayers_alt1") ? "     ,  ru.alg_ruimte_opp_alt1" : "")
                                + (S("fg_contourlayers_alt2") ? "     ,  ru.alg_ruimte_opp_alt2" : "")
                                + "     ) = (" + sql_update_select_room + ")"
                                + " WHERE EXISTS (" + sql_update_select_room + ")";
            Oracle.Execute(sql_room_update);

            /* Track verdieping update */
            var sql_track_floor = "SELECT v.alg_verdieping_key"
                                + "     , v.alg_verdieping_bruto_vloeropp"
                                + "     , c.cad_imp_contour_opp"
                                + sql_from
                                + sql_where
                                + sql_where_floor;
            oRs_tracking = Oracle.Execute(sql_track_floor);

            trackArray = [];
            if (!oRs_tracking.EoF &&
                oRs_tracking("alg_verdieping_bruto_vloeropp").Value != oRs_tracking("cad_imp_contour_opp").Value) { // Verdieping
              shared.trackaction("ALGVUP",
                                oRs_tracking("alg_verdieping_key").Value,
                                L("lcl_alg_is_algvup") + "\n"
                              + L("lcl_estate_verdieping_bruto_vloeropp") + ": "
                              + (oRs_tracking("alg_verdieping_bruto_vloeropp").Value === null ? L("lcl_trackfromempty") : oRs_tracking("alg_verdieping_bruto_vloeropp").Value)
                              + " => " + oRs_tracking("cad_imp_contour_opp").Value);
            }
            oRs_tracking.Close();

            /* Update verdieping oppervlakte */
            var sql_update_select_floor = "SELECT c.cad_imp_contour_opp"
                                        + sql_from
                                        + sql_where
                                        + sql_where_floor
                                        + "   AND ver.alg_verdieping_key = v.alg_verdieping_key";

            var sql_floor_update = "UPDATE alg_verdieping ver"
                                 + "   SET (ver.alg_verdieping_bruto_vloeropp"
                                 + "     ) = (" + sql_update_select_floor + ")"
                                 + " WHERE EXISTS (" + sql_update_select_floor + ")";
            Oracle.Execute(sql_floor_update);
        }
    }
}

%>