<% /*
    $Revision$
    $Id$

    File:           model_prs_kostensoortgrp.inc

    Description:    Vanuit CodeCharge gegenereerd model voor prs_kostensoortgrp

    Context:

    Notes:
*/
%>
<!-- #include file="../api2/model_bgt_budget.inc" -->
<!-- #include file="../api2/model_prs_kostensoort.inc" -->
<%

function model_prs_kostensoortgrp()
{
    this.records_name = "costtypegroups";
    this.record_name = "costtypegroup";
    this.table = "prs_kostensoortgrp";
    this.primary = "prs_kostensoortgrp_key";
    this.autfunction = (S("bgt_enabled")==1 ? ["WEB_FINMSU", "WEB_BGTMGT"] : ["WEB_FINMSU"]);
    this.record_title = L("prs_kostensoortgrp");
    this.records_title = L("prs_kostensoortgrp_m");
    this.soft_delete = "prs_kostensoortgrp_verwijder";

    var fields_main = {};
    var fields_ext = {};


    fields_main = {
        "id": {
            "dbs": "prs_kostensoortgrp_key",
            "label": L("lcl_key"),
            "typ": "key",
            "hidden_fld": true,
            "required": true,
            "seq": "prs_s_prs_kostensoortgrp_key"
        },
        "budgetcostcategory": {
            "dbs" : "bgt_kostenrubriek_key",
            "label": L("bgt_kostenrubriek"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": bgt_budgetcostcategory_foreign(),
            "showtransit": true
        },
        "name": {
            "dbs": "prs_kostensoortgrp_oms",
            "label": L("prs_kostensoortgrp_oms"),
            "typ": "varchar",
            "translate": true,
            "required": true,
            "filter": "like"
        },
        "altcode": {
            "dbs": "prs_kostensoortgrp_altcode",
            "label": L("prs_kostensoortgrp_altcode"),
            "typ": "varchar",
            "filter": "like"
        }
    };

    if (S("bgt_enabled"))
    {
        // Via Admin moet de context discipline/project toegevoegd worden.
        fields_ext = {
            "budgetdiscipline": {
                "dbs": "bgt_project.ins_discipline_key",
                "label": L("bgt_discipline_omschrijving"),
                "typ": "key",
                "required": true,
                "foreign": bgt_budgetdiscipline_foreign(),
                "showtransit": true
            },
            "budgetproject": {
                "dbs": "bgt_project.bgt_project_key",
                "label": L("bgt_project_omschrijving"),
                "typ": "key",
                "required": true,
                "foreign": bgt_budgetproject_foreign(),
                "showtransit": true
            }
        };
        this.fields = object_merge({}, fields_ext, fields_main);


        this.hook_pre_edit = function (obj, fld)
        {
            if ((obj.id > -1) || (obj.budgetcostcategory && obj.budgetcostcategory.id > -1))
            {
                this.fields.budgetdiscipline.hidden_fld = false;
                this.fields.budgetdiscipline.readonly = true;
                this.fields.budgetproject.hidden_fld = false;
                this.fields.budgetproject.readonly = true;
            }

            if (obj.budgetcostcategory && obj.budgetcostcategory.id != -1)
            {   // Nog even de parent gegevens erbij halen zodat deze readonly getoond worden.
                var sql = "SELECT p.ins_discipline_key"
                        + "     , p.bgt_project_key"
                        + "     , r.bgt_kostenrubriek_key"
                        + "     , dp.bgt_disc_params_code ||' - '|| di.ins_discipline_omschrijving oms_disc"
                        + "     , p.bgt_project_code ||' - '|| p.bgt_project_omschrijving oms_proj"
                        + "  FROM bgt_disc_params    dp"
                        + "     , ins_tab_discipline di"
                        + "     , bgt_project        p"
                        + "     , bgt_kostenrubriek  r"
                        + " WHERE dp.bgt_ins_discipline_key = p.ins_discipline_key"
                        + "   AND di.ins_discipline_key = p.ins_discipline_key"
                        + "   AND p.bgt_project_key = r.bgt_project_key"
                        + "   AND r.bgt_kostenrubriek_key = " + obj.budgetcostcategory.id;
                var oRs = Oracle.Execute(sql);
                if (!oRs.eof)
                {
                    obj.budgetdiscipline = {id:oRs("ins_discipline_key").Value, name: oRs("oms_disc").Value};
                    obj.budgetproject = {id: oRs("bgt_project_key").Value, name: oRs("oms_proj").Value};
                }
                oRs.Close();
            }

            %>
            <script>
                var budgetdiscipline_key   = <%=(obj.budgetdiscipline   ? obj.budgetdiscipline.id   : -1)%>;
                var budgetproject_key      = <%=(obj.budgetproject      ? obj.budgetproject.id      : -1)%>;
                var budgetcostcategory_key = <%=(obj.budgetcostcategory ? obj.budgetcostcategory.id : -1)%>;
                var costtypegroup_key      = <%=(obj.costtypegroup      ? obj.costtypegroup.id      : -1)%>;
            </script>
            <%
        }

        this.REST_GET = generic_REST_GET(this, {
            "GET": {
                "tables": [
                    "bgt_project",
                    "bgt_kostenrubriek"
                ],
                "wheres": [
                    "bgt_kostenrubriek.bgt_kostenrubriek_key = prs_kostensoortgrp.bgt_kostenrubriek_key",
                    "bgt_kostenrubriek.bgt_project_key = bgt_project.bgt_project_key"
                ]
            }
        });
    }
    else
    {
        this.fields = fields_main;
        delete this.fields.budgetcostcategory;
        this.REST_GET    = generic_REST_GET(this);
    }
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>