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

    File:               res_multi_create_save.asp
    Description:        save-script van res_multi_create
    Parameters:
        rsv_ruimte_key  bron-deelreservering
        urole
        newDates
    Context:
    Note:
*/
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="./res.inc" -->
<!-- #include file="../bez/bez.inc" -->
<%

protectRequest.validateToken();

var urole = getQParamSafe("urole");
var rsv_ruimte_key = getQParamInt("rsv_ruimte_key");
res.res_set_dialect(rsv_ruimte_key);

var this_res = res.func_enabled(rsv_ruimte_key); // wat mocht ik zoal op deze reservering
user.auth_required_or_abort(this_res.couldCreate);
// =========================================
// Eerst wat info ophalen
sql = "SELECT rr.res_reservering_key"
    + "     , rr.res_reservering_ispool"
    + "     , r.res_ruimte_vervaldatum"
    + "  FROM res_reservering rr"
    + "     , res_rsv_ruimte rrr"
    + "     , res_ruimte_opstelling ro"
    + "     , res_ruimte r"
    + " WHERE rr.res_reservering_key = rrr.res_reservering_key"
    + "   AND rrr.res_ruimte_opstel_key = ro.res_ruimte_opstel_key(+)"
    + "   AND ro.res_ruimte_key = r.res_ruimte_key(+)"
    + "   AND res_rsv_ruimte_key = " + rsv_ruimte_key;
oRs = Oracle.Execute(sql);
var reservering_key = oRs("res_reservering_key").Value;
var ispool = (oRs("res_reservering_ispool").Value) == 1;
var ruimte_vervaldatum = oRs("res_ruimte_vervaldatum").Value;
if (ruimte_vervaldatum != null)
  ruimte_vervaldatum = new Date(ruimte_vervaldatum);
oRs.Close();

var nrPAF = 0;
if (!ispool) // Dan gaan bezoekers toch niet mee
{
    var sql = "SELECT bez_afspraak_key"
            + "  FROM bez_afspraak ba"
            + " WHERE ba.res_rsv_ruimte_key = " + rsv_ruimte_key;
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof)
    {
        var this_bez = bez.func_enabled_afspraak(oRs("bez_afspraak_key").Value);
        nrPAF = this_bez.nrPAF;
    }
    oRs.Close();
}

var form_dates = Request.Form("newDates");
var sql = "DECLARE"
        + "    new_key NUMBER;"
        + "BEGIN"
for (var i = 1; i <= form_dates.Count; i++)
{
    var datum = new Date(parseInt(form_dates(i),10));
    if (ruimte_vervaldatum != null && ruimte_vervaldatum < datum)
    {
      abort_with_warning(L("lcl_res_room_expired").format(toDateString(ruimte_vervaldatum)));
    }
    if (ispool) // Hele reservering
    {
        sql += " new_key := res.res_copy_res(" + reservering_key + "," + datum.toSQL() + "," + user_key + ");";
    }
    else // Gewone
    {
        sql += " new_key := res.res_copy_deelres(" + rsv_ruimte_key + "," + datum.toSQL() + "," + user_key + ");";
        if (S("res_fe_allow_dirty") != 1 && urole == 'fe')
        {
          sql += " IF res.dirty_level_all(new_key) <> 0 THEN"
               + "    raise_application_error (-20000, 'res_m960 " + toDateString(datum) + ": " + L("lcl_res_fe_no_dirty") + "');"
               + " END IF;"
        }
        sql += " UPDATE res_rsv_artikel SET res_rsv_artikel_prijs = NULL WHERE res_rsv_ruimte_key = new_key;";
    }
}
sql += " END;"
var err = Oracle.Execute(sql, true);
if (err.friendlyMsg)
    abort_with_warning(err.friendlyMsg);

shared.trackaction("RESCPY", rsv_ruimte_key);
var msg = [ L("lcl_res_copied").format(form_dates.Count) ];
if (nrPAF > 0)
{
    msg.push(L("lcl_bez_paf_not_copied").format(nrPAF));
}

var result = { message: msg.join('\n'), success: true, refresh: true };
Response.Write(JSON.stringify(result));
%>