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

    File:               ins_create.asp
    Description:        Maak een nieuwe ins_deel aan op een locatie (XY).
    Parameters:         locKey
                        mode
                        OF: deel_key (voor kopieren) OF: srtdeel_key
                        OF: room_key OF: wp_key
                        dwgX, dwgY
    Context:            default.asp/Ajax.Updater
    Result:             X-JSON header
    Note:               Vooralsnog *moet* in een ruimte gegooid worden of op een werkpleksymbool.
                        XY is wel optioneel.

    Result:
*/
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="FGII.inc" -->

<%
protectRequest.validateToken();
var inoutMode = getQParamInt("mode");

var scenario_key = getQParamInt("scenario_key"); // current scenario.

var srtdeel_key = -1;
var deel_key = getQParam("deel_key", ""); // (string, kan I:xxx, P:xxx of IW:xxx bevatten)
if (deel_key != "") // Kopie
{
  if (deel_key.match(/^I:/)) deel_key = deel_key.substring(2);
  if (deel_key.match(/^P:/)) deel_key = deel_key.substring(2);
  if (deel_key.match(/^IW:/)) deel_key = deel_key.substring(3);
  deel_key = parseInt(deel_key, 10);
  if (scenario_key == 0) // I:
  {
    var sql = "SELECT ins_srtdeel_key key, ins_deel_dwgrotatie rot, ins_deel_dwgschaal scl"
            + "  FROM ins_deel"
            + " WHERE ins_deel_key = " + deel_key;
  }
  else // P:
  {
    var sql = "SELECT ins_srtdeel_key key, prj_deel_dwgrotatie rot, prj_deel_dwgschaal scl"
            + "  FROM prj_deel"
            + " WHERE prj_deel_key = " + deel_key;
  }
  var oRs=Oracle.Execute(sql);
  if (!oRs.Eof)
  {
    srtdeel_key = oRs("key").value;
    rot = oRs("rot").value;
    scl = oRs("scl").value;
  }
  oRs.Close();
}
if (srtdeel_key == -1)
{
  var srtdeel_key = getQParamInt("srtdeel_key");
  rot = 0;
  scl = 1.0;
}

if (Request.QueryString("dwgX").Count == 0)
{
  var newX = "NULL"; // Droppen met ctrlKey ingedrukt liet XY achterwege
  var newY = "NULL";
}
else
{
  var newX = parseFloat(getQParamFloat("dwgX"));
  var newY = parseFloat(getQParamFloat("dwgY"));
}

var ins_type=(inoutMode == MODE_IN? 'R' : 'T');

var roomKey = getQParamInt("room_key", -1);
var wpKey = "NULL";
if (roomKey == -1) // Niet gedropt op ruimte maar werkplek?
{
  wpKey = getQParamInt("wp_key"); // Dan moet die er zijn
  ins_type = "W";
  if (scenario_key == 0)
  {
      roomKey = wpKey;
      var sql = "SELECT prs_werkplek_dwgx dwgx, prs_werkplek_dwgy dwgy,"
              + "       prs_werkplek_dwgrotatie rot, prs_werkplek_dwgschaal scl"
              + "  FROM prs_werkplek pw"
              + " WHERE pw.prs_werkplek_key = " + wpKey;
              oRs = Oracle.Execute(sql);
  }
  else
  {
      var sql = "SELECT prj_werkplek_dwgx dwgx, prj_werkplek_dwgy dwgy,"
              + "       prj_werkplek_dwgrotatie rot, prj_werkplek_dwgschaal scl,"
              + "       prj_ruimte_key"
              + "  FROM prj_werkplek pw"
              + " WHERE pw.prj_werkplek_key = " + wpKey;
      oRs = Oracle.Execute(sql);
      roomKey = oRs("prj_ruimte_key").value;
  }
  if (oRs("dwgX").value == null)
  {
      Response.AddHeader("X-JSON", "{ success: false, message: \""+L("lcl_fg_place_wp_first").replace(/\n/g,'\\n')+"\"}");
      Response.End;
  }
  else
  {
      newX = newX - oRs("dwgX").value; // Coordinaten zijn relatief tov werkplek
      newY = newY - oRs("dwgY").value;
  }
}

var BIND_RUIMTE = 0x01;
var BIND_WERKPL = 0x02;
var BIND_TERREI = 0x04;
var BIND_AFDELI = 0x08;
var BIND_PERSOO = 0x10;
var BIND_INSDEE = 0x20; // Onderdeel

// We hebben srtdeel_key. Nu binding controleren
sql = "Select ins_srtdeel_binding "
    + "  from ins_srtdeel"
    + " where ins_srtdeel_key = " + srtdeel_key;
oRs = Oracle.Execute(sql);
srtdeel_binding=oRs(0).value;

if ((ins_type=="R"&& !(srtdeel_binding&BIND_RUIMTE)) ||
    (ins_type=="W"&& !(srtdeel_binding&BIND_WERKPL)) ||
    (ins_type=="T"&& !(srtdeel_binding&BIND_TERREI))
   )
{
  var msg = ins_type=="R"?L("lcl_obj_bind_r"):ins_type=="T"?L("lcl_obj_bind_t"):L("lcl_obj_bind_w");
  Response.Write(JSON.stringify({ success: false, message: L("lcl_fg_obj_bind_error")+msg }));
  Response.End;
}

var locKey = getQParamInt("loc_key");

if (scenario_key == 0)
{
  sql = "SELECT ins.get_description('LPN_INS_DEEL', "+locKey+", ins_srtdeel_code_upper)"
      + "  FROM ins_srtdeel WHERE ins_srtdeel_key="+srtdeel_key;
}
else
{
  sql =  "SELECT max(ins_srtdeel_code_upper)||NVL "
       + "          (LPAD "
       + "              (TO_CHAR "
       + "                    (  MAX "
       + "                          (TO_NUMBER (SUBSTR (prj_deel_omschrijving, "
       + "                                                LENGTH (ins_srtdeel_code_upper) "
       + "                                              + 1, "
       + "                                                LENGTH (prj_deel_omschrijving) "
       + "                                              - LENGTH (ins_srtdeel_code_upper) "
       + "                                             ) "
       + "                                     ) "
       + "                          ) "
       + "                     + 1 "
       + "                    ), "
       + "               MAX (  LENGTH (prj_deel_omschrijving) "
       + "                    - LENGTH (ins_srtdeel_code_upper) "
       + "                   ), "
       + "               '0' "
       + "              ), "
       + "           '001' "
       + "          ) "
       + "  FROM prj_deel, prj_ruimte, ins_srtdeel "
       + " WHERE prj_deel_omschrijving LIKE ins_srtdeel_code_upper || '%' "
       + "   AND fac.safe_to_number (SUBSTR (prj_deel_omschrijving, "
       + "                                   LENGTH (ins_srtdeel_code_upper) + 1, "
       + "                                     LENGTH (prj_deel_omschrijving) "
       + "                                   - LENGTH (ins_srtdeel_code_upper) "
       + "                                  ) "
       + "                          ) IS NOT NULL "
       + "   AND prj_deel_verwijder IS NULL "
       + "   AND prj_deel.prj_ruimte_key = prj_ruimte.prj_ruimte_key "
       + "   AND ins_srtdeel.ins_srtdeel_key = " + srtdeel_key
       + "   AND prj_ruimte.prj_scenario_key = " + scenario_key
}
oRs = Oracle.Execute(sql);
var desc = oRs(0).value;

if (scenario_key == 0)
  sql = "INSERT INTO INS_DEEL (ins_srtdeel_key, ins_deel_omschrijving, "
      + "                      ins_alg_ruimte_type, ins_alg_ruimte_key, "
      + "                      ins_deel_dwgx, ins_deel_dwgy,"
      + "                      ins_deel_dwgrotatie, ins_deel_dwgschaal)"
      + " VALUES (" + srtdeel_key + ","
      +            safe.quoted_sql(desc) + ", "
      +            safe.quoted_sql(ins_type) + ","
      +            roomKey + ","
      +            newX + "," + newY + ","
      +            rot + "," + scl
      + ")"
else
  sql = "INSERT INTO PRJ_DEEL (ins_srtdeel_key, prj_deel_omschrijving, "
      + "                      prj_ruimte_key, prj_werkplek_key,"
      + "                      prj_deel_dwgx, prj_deel_dwgy,"
      + "                      prj_deel_dwgrotatie, prj_deel_dwgschaal)"
      + " VALUES (" + srtdeel_key + ","
      +            safe.quoted_sql(desc) + ", "
      +            roomKey+"," + wpKey + ","
      +            newX + "," + newY + ","
      +            rot + "," + scl
      + ")"
Oracle.Execute(sql);

Response.Write(JSON.stringify({ success: true }));
%>
