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

    File:           bgt_budget.asp

    Description:    aanroep van model_bgt_budget

    Context:

    Notes:
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_bgt_budget.inc" -->
<%
var parent = getQParam("parent", "");
var project_key = getQParamInt("budgetproject", -1);
var kostenrubriek_key = getQParamInt("budgetcostcategory", -1);
var kostensoortgrp_key = getQParamInt("costtypegroup", -1);
var kostensoort_key = getQParamInt("costtype", -1);
var this_model = new model_bgt_budget();

if (mode == "show")
{
    var autparams = user.checkAutorisation(this_model.autfunction);
    var canWrite = (autparams.PRSwritelevel < 9 && autparams.ALGwritelevel < 9)
    if (!canWrite)
    {
        delete this_model.REST_PUT;
    }
    var budget_key = getQParamInt("id", -1);
    var sql = "SELECT count(*) aantal"
            + "  FROM mld_opdr o"
            + "     , bgt_budget b"
            + " WHERE o.prs_kostensoort_key = b.prs_kostensoort_key"
            + "   AND b.bgt_budget_key = " + budget_key;
    var oRs = Oracle.Execute(sql);
    if (oRs("aantal").Value > 0)
        delete this_model.REST_DELETE;
    oRs.Close();
}
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 (<%=project_key%>        >= -1) var budgetproject_key = <%=project_key%>;
        if (<%=kostenrubriek_key%>  >= -1) var budgetcostcategory_key = <%=kostenrubriek_key%>;
        if (<%=kostensoortgrp_key%> >= -1) var costtypegroup_key = <%=kostensoortgrp_key%>;
        if (<%=kostensoort_key%>    >= -1) var costtype_key = <%=kostensoort_key%>;
    </script>
<%
}

var model_params = {
        "search": {
            "autosearch": ((parent == "costtype" && kostensoort_key > 0) || (parent == "search") ? true : false),
            "filters": [
                "budgetdiscipline",
                "budgetproject",
                "budgetcostcategory",
                "costtypegroup",
                "costtype",
                "contingency"
            ],
            "transit": [
                "budgetdiscipline",
                "budgetproject",
                "budgetcostcategory",
                "costtypegroup",
                "costtype"
            ],
            "requires": {
                "js": ["../bgt/bgt_budget.js"]
            }
        },
        "list": {
            "columns": [
                "budgetproject",
                "budgetcostcategory",
                "costtypegroup",
                "costtype",
                "amountoriginal",
                "amountmutation",
                "amountreservation",
                "amount",
                "vat"
            ],
            "orderby": [
                "budgetproject",
                "budgetcostcategory",
                "costtypegroup",
                "costtype"
            ]
        },
        "edit": {
            "requires": {
                "js": ["../bgt/bgt_budget.js"]
            }
        },
        "layout": {
            "miscellaneous": {
                "icon": "fa-sack"
            }
        },
        "wrap": {
            "no_default_frames": true,
            "frames": [ { url: "./bgt_budgetmutatie.asp?mode=list&budgetcommon={0}", title: L("bgt_budgetmutatie_m") } ]
        }
    }

// Het budget-scherm wordt aangeroepen vanuit het scherm kostensoort.
// Als deze key bekend is kunnen alle parents worden ingevuld.
if (parent == "costtype" && kostensoort_key > 0)
{
    var sql = "SELECT p.ins_discipline_key"
            + "     , p.bgt_project_key"
            + "     , r.bgt_kostenrubriek_key"
            + "     , g.prs_kostensoortgrp_key"
            + "  FROM bgt_project p"
            + "     , bgt_kostenrubriek r"
            + "     , prs_kostensoortgrp g"
            + "     , prs_kostensoort s"
            + " WHERE p.bgt_project_key = r.bgt_project_key"
            + "   AND r.bgt_kostenrubriek_key = g.bgt_kostenrubriek_key"
            + "   AND g.prs_kostensoortgrp_key = s.prs_kostensoortgrp_key"
            + "   AND s.prs_kostensoort_key = " + kostensoort_key;
    var oRs = Oracle.Execute(sql);

    model_params.transit = {
        "budgetdiscipline":   oRs("ins_discipline_key").Value,
        "budgetproject":      oRs("bgt_project_key").Value,
        "budgetcostcategory": oRs("bgt_kostenrubriek_key").Value,
        "costtypegroup":      oRs("prs_kostensoortgrp_key").Value
        // Geen "costtype" omdat er slecht 1 budget per costensoort mag bestaan.
    }
}
else if (parent == "search")
{   // Scherm wordt aangeroepen vanuit bgt_search_list.
    model_params.transit = {
        "budgetproject": project_key,
        "budgetcostcategory": kostenrubriek_key,
        "costtypegroup": kostensoortgrp_key,
        "costtype": kostensoort_key
    }
}

scaffolding(this_model, model_params);
%><% ASPPAGE_END(); %>
