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

    File:           bgt_project.asp

    Description:    aanroep van model_bgt_project

    Context:

    Notes:
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_bgt_project.inc" -->
<script language="javascript" src="../imp/imp_shared.js" runat="server"></script>
<%
var p_ins_discipline_key = getQParamInt("budgetdiscipline", -1);
var p_bgt_project_key = getQParamInt("id", -1);

var  this_model = new model_bgt_project();

if (mode == "search" || mode == "edit")
{
    // Onderstaand script is nodig om vanuit budgetbeheer naar het scherm Budget te
    // gaan, waarbij de selectievelden ingevuld worden met de bekende waarden.
%>
    <script>
        if (<%=p_ins_discipline_key%>  >= -1) var budgetdiscipline_key = <%=p_ins_discipline_key%>;
        if (<%=p_bgt_project_key%>     >= -1) var budgetproject_key = <%=p_bgt_project_key%>;
    </script>
<%
}

scaffolding(this_model,
    {
        "search": {
            "autosearch": true,
            "filters": [
                "budgetdiscipline",
                "name"
            ]
        },
        "list": {
            "columns": [
                "budgetdiscipline",
                "code",
                "name",
                "pricedate"
            ],
            "orderby": [
                "budgetdiscipline",
                "sequence",
                "code",
                "name"
            ]
        },
        "edit": {
            "requires": {
                "js": ["../bgt/bgt_budget.js"]
            }
        },
        "wrap": {
            "frames": [{ url: "../bgt/bgt_kostenrubriek.asp?mode=list&budgetproject={0}",
                         title: L("bgt_kostenrubriek_m"),
                         autfunction: "WEB_BGTMGT"
                      }]
        },
        "transit": {
            "budgetdiscipline": p_ins_discipline_key
        }
    });


function create_default_projectdata(prj_key, def_prj_key)
{
    // Kopieer de standaard deel-projectdata naar een nieuw project.
    var err = "";
    // Rubriek:
    var sql_rub = "SELECT bgt_kostenrubriek_key"
                + "     , bgt_kostenrubriek_code"
                + "     , bgt_kostenrubriek_oms"
                + "  FROM bgt_kostenrubriek"
                + " WHERE bgt_project_key = " + def_prj_key;
    var oRs_rub = Oracle.Execute(sql_rub);
    while (!oRs_rub.eof)
    {
        var rub_key = get_sequence("bgt_s_bgt_kostenrubriek_key");
        var ins_rub = "INSERT INTO bgt_kostenrubriek"
                    + "( bgt_project_key"
                    + ", bgt_kostenrubriek_key"
                    + ", bgt_kostenrubriek_code"
                    + ", bgt_kostenrubriek_oms"
                    + ") "
                    + "VALUES "
                    + "(" + prj_key
                    + "," + rub_key
                    + "," + safe.quoted_sql(oRs_rub("bgt_kostenrubriek_code").Value)
                    + "," + safe.quoted_sql(oRs_rub("bgt_kostenrubriek_oms").Value)
                    + ")";
        err = Oracle.Execute(ins_rub, true);
        if (err.friendlyMsg)
            abort_with_warning(err.friendlyMsg);
        var def_rub_key = oRs_rub("bgt_kostenrubriek_key").Value;
        // Kostensoortgroep:
        var sql_grp = "SELECT prs_kostensoortgrp_key"
                    + "     , prs_kostensoortgrp_altcode"
                    + "     , prs_kostensoortgrp_oms"
                    + "  FROM prs_kostensoortgrp"
                    + " WHERE bgt_kostenrubriek_key = " + def_rub_key;
        var oRs_grp = Oracle.Execute(sql_grp);
        while (!oRs_grp.eof)
        {
            var grp_key = get_sequence("prs_s_prs_kostensoortgrp_key");
            var ins_grp = "INSERT INTO prs_kostensoortgrp"
                        + "( bgt_kostenrubriek_key"
                        + ", prs_kostensoortgrp_key"
                        + ", prs_kostensoortgrp_altcode"
                        + ", prs_kostensoortgrp_oms"
                        + ") "
                        + "VALUES "
                        + "(" + rub_key
                        + "," + grp_key
                        + "," + safe.quoted_sql(oRs_grp("prs_kostensoortgrp_altcode").Value)
                        + "," + safe.quoted_sql(oRs_grp("prs_kostensoortgrp_oms").Value)
                        + ")";
            err = Oracle.Execute(ins_grp, true);
            if (err.friendlyMsg)
                abort_with_warning(err.friendlyMsg);
            var def_grp_key = oRs_grp("prs_kostensoortgrp_key").Value;
            // Kostensoort:
            var sql_srt = "SELECT prs_kostensoort_key"
                        + "     , prs_kostensoort_altcode"
                        + "     , prs_kostensoort_oms"
                        + "     , fin_btwtabelwaarde_key"
                        + "  FROM prs_kostensoort"
                        + " WHERE prs_kostensoortgrp_key = " + def_grp_key;
            var oRs_srt = Oracle.Execute(sql_srt);
            while (!oRs_srt.eof)
            {
                var srt_key = get_sequence("prs_s_prs_kostensoort_key");
                var ins_srt = "INSERT INTO prs_kostensoort"
                            + "( prs_kostensoortgrp_key"
                            + ", prs_kostensoort_key"
                            + ", prs_kostensoort_altcode"
                            + ", prs_kostensoort_opmerking"
                            + ", prs_kostensoort_oms"
                            + ", fin_btwtabelwaarde_key"
                            + ") "
                            + "VALUES "
                            + "(" + grp_key
                            + "," + srt_key
                            + "," + safe.quoted_sql(oRs_srt("prs_kostensoort_altcode").Value)
                            + "," + safe.quoted_sql(oRs_srt("prs_kostensoort_altcode").Value)
                            + "," + safe.quoted_sql(oRs_srt("prs_kostensoort_oms").Value)
                            + "," + oRs_srt("fin_btwtabelwaarde_key").Value
                            + ")";
                err = Oracle.Execute(ins_srt, true);
                if (err.friendlyMsg)
                    abort_with_warning(err.friendlyMsg);
                oRs_srt.MoveNext();
            }
            oRs_srt.Close()
            oRs_grp.MoveNext();
        }
        oRs_grp.Close();
        oRs_rub.MoveNext();
    }
    oRs_rub.Close();
}

function get_sequence(seq)
{
    var sql = "SELECT " +seq+ ".nextval FROM dual";
    var oRs = Oracle.Execute( sql );
    var seq_key = oRs(0).Value;
    oRs.Close();
    return seq_key;
}

%><% ASPPAGE_END(); %> 
