<% /*
    $Revision$
    $Id$

    File:           model_prs_kostensoort.inc

    Description:    Model voor prs_kostensoort

    Notes:          Wellicht onlogisch, maar technisch geen bezwaar om group later nog te kunnen wijzigen?
*/
%>
<!-- #include file="../api2/model_bgt_budget.inc" -->
<!-- #include file="../api2/model_prs_kostencombinatie.inc" -->
<%

function model_prs_kostensoort()
{
    this.records_name = "costtypes";
    this.record_name = "costtype";
    this.table = "prs_kostensoort";
    this.audit = { // Parameters voor fac_audit.
        "childtable": "prs_kostencombinatie",
        "childaudit": {"sql":   "SELECT prs_kostencombinatie_key"  // Huidige kosten combinaties.
                              + "  FROM prs_kostencombinatie"
                              + " WHERE prs_kostensoort_key IN ({0})"
                              + " UNION "
                              + "SELECT fac_audit_tabelkey"  // Verwijderde kosten combinaties.
                              + "  FROM fac_audit"
                              + " WHERE fac_audit_tabelnaam = 'prs_kostencombinatie'"
                              + "   AND fac_audit_veldnaam = 'prs_kostensoort_key'"
                              + "   AND fac_audit_waarde_oud IN ('{1}')"
        }
    };
    this.primary = "prs_kostensoort_key";
    this.autfunction = (S("bgt_enabled")==1 ? ["WEB_FINMSU", "WEB_BGTMGT"] : ["WEB_FINMSU"]);
    this.record_title = L("prs_kostensoort");
    this.records_title = L("prs_kostensoort_m");
    this.soft_delete = "prs_kostensoort_verwijder";
    this.soft_expire = "prs_kostensoort_eind";

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


    fields_main = {
        "id": {
            "dbs": "prs_kostensoort_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "hidden_fld": true,
            "seq": "prs_s_prs_kostensoort_key"
        },
        "costtypegroup": {
            "dbs": "prs_kostensoortgrp_key",
            "label": L("prs_kostensoortgrp_key"),
            "required": true,
            "typ": "key",
            "foreign": bgt_costtypegroup_foreign(),
            "LOVinit": "",
            "multiedit": true,
            "showtransit": true
        },
        "name": {
            "dbs": "prs_kostensoort_oms",
            "label": (S("bgt_enabled") ? L("prs_kostensoort_opmerking") : L("prs_kostensoort_oms")),
            "typ": "varchar",
            "required": true,  /* proberen */
            "translate": true,
            "filter": "like",
            "multiedit": true
        },
        "remark": {
            "dbs": "prs_kostensoort_opmerking",
            "label": (S("bgt_enabled") ? L("prs_kostensoort_oms") : L("prs_kostensoort_opmerking")),
            "typ": "varchar",
            "required": (S("bgt_enabled") ? false : true),
            "hidden_fld": (S("bgt_enabled") ? true : false),
            "filter": "like"
        },
        "altcode": {
            "dbs": "prs_kostensoort_altcode",
            "label": (S("bgt_enabled") ? L("prs_kostensoort_oms") : L("prs_kostensoort_altcode")),
            "typ": "varchar",
            "required": (S("bgt_enabled") ? true : false)
        },
        "refcode": {
            "dbs": "prs_kostensoort_refcode",
            "label": L("prs_kostensoort_refcode"),
            "typ": "varchar",
            "translate": true
        },
        "charge": {
            "dbs": "prs_kostensoort_doorbelasten",
            "label": L("prs_kostensoort_doorbelasten"),
            "typ": "check",
            "multiedit": true
        },
        "inclvat": {
            "dbs": "prs_kostensoort_btw",
            "label": L("prs_kostensoort_btw"),
            "typ": "check",
            "multiedit": true
        },
        "vat": {
            "dbs": "fin_btwtabelwaarde_key",
            "label": L("lcl_btw_percentage"),
            "typ": "key",
            "foreign": {
                "tbl": "fin_btwtabelwaarde",
                "key": "fin_btwtabelwaarde_key",
                "desc": "fin_btwtabelwaarde_oms"
            }
        },
        "costcentre": {
            "dbs": "prs_kostenplaats_key",
            "label": L("prs_kostenplaats_key"),
            "typ": "key",
            "foreign": bgt_account_foreign(),
            "multiedit": true
        },
        "end": {
            "dbs": "prs_kostensoort_eind",
            "label": L("prs_kostensoort_eind"),
            "typ": "date",
            "multiedit": true
        }
    };


    if (S("bgt_enabled")) // alleen voor budgetcontrole
    {
        // 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
            },
            "budgetcostcategory": {
                "dbs": "bgt_kostenrubriek.bgt_kostenrubriek_key",
                "label": L("bgt_kostenrubriek"),
                "typ": "key",
                "required": true,
                "foreign": bgt_budgetcostcategory_foreign(),
                "showtransit": true
            },
            "costcategorycode": {
                "dbs": "bgt_kostenrubriek.bgt_kostenrubriek_code",
                "label": L("bgt_kostenrubriek_code"),
                "typ": "varchar",
                "readonly": true
            }
        };
        this.fields = object_merge({}, fields_ext, fields_main);

        delete this.fields.charge;
        delete this.fields.refcode;
        this.fields.inclvat.hidden_fld = true;
        this.fields.costcentre.label = L("bgt_budget_account");

        this.fields.vat.foreign.tbl = "(SELECT fin_btwtabelwaarde_key"
                                    + "      , fin_btwtabelwaarde_oms"
                                    + "   FROM fin_btwtabel t"
                                    + "      , fin_btwtabelwaarde w"
                                    + "  WHERE t.fin_btwtabel_key = w.fin_btwtabel_key"
                                    + "    AND t.fin_btwtabel_default = 1"
                                    + ")";

        this.includes = {
            "budgets": {
                "model": new model_bgt_budget(),
                "joinfield": "costtype",
                "enable_update": true
            }
        };
//        this.print = { xmlnode: "budget", where: "kostensoort"};

        this.hook_pre_show = function (obj, fld)
        {
            delete this.fields.costcategorycode;
        }

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

                delete this.fields.costcategorycode;
            }

            if (obj.costtypegroup && obj.costtypegroup.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"
                        + "     , r.bgt_kostenrubriek_oms oms_rubr"
                        + "  FROM bgt_disc_params    dp"
                        + "     , ins_tab_discipline di"
                        + "     , bgt_project        p"
                        + "     , bgt_kostenrubriek  r"
                        + "     , prs_kostensoortgrp g"
                        + " 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 = g.bgt_kostenrubriek_key"
                        + "   AND g.prs_kostensoortgrp_key = " + obj.costtypegroup.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};
                    obj.budgetcostcategory = {id: oRs("bgt_kostenrubriek_key").Value, name: oRs("oms_rubr").Value};
                }
                oRs.Close();
            }
            var prj_key = (obj.budgetproject ? obj.budgetproject.id : -1);
            var disc_key = (obj.budgetdiscipline ? obj.budgetdiscipline.id : -1);
            fld["costcentre"].foreign.tbl = bgt_account_foreign_tbl(disc_key);
            fld["costcentre"].multiedit = false;

            %>
            <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",
                    "prs_kostensoortgrp"
                ],
                "wheres": [
                    "bgt_project.bgt_project_key = bgt_kostenrubriek.bgt_project_key",
                    "bgt_kostenrubriek.bgt_kostenrubriek_key = prs_kostensoortgrp.bgt_kostenrubriek_key",
                    "prs_kostensoortgrp.prs_kostensoortgrp_key = prs_kostensoort.prs_kostensoortgrp_key"
                ]
            }
        });

    }
    else
    {
        this.fields = fields_main;
        delete this.fields.vat;
        this.includes = {
            "costcombinations": {
                "model": new model_prs_kostencombinatie(),
                "joinfield": "costtype",
                "enable_update": true,
                "multiadd": "group"
            }
        };
        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);
}
%>