<% /*
    $Revision$
    $Id$

    File:           model_bgt_discipline.inc

    Description:    Model voor budget disciplines

    Context:

    Notes:          intern hebben we een apart model voor bgt_disc_params
                    Naar de buitenwereld presenteren we het als één model
*/
%>
<!-- #include file="model_bgt_disc_params.inc" -->
<%

function model_bgt_discipline(disc_key, params)
{
    // Eén model voor de buitenwereld
    var disc = new _model_bgt_discipline();
    api2.merge_disc_params_model(this, disc);

    this.hook_pre_delete = function(params, key)
    {
        // Alle deelprojecten ook op verwijderd zetten.
        var sql = "UPDATE bgt_project"
                + "   SET bgt_project_verwijder = SYSDATE"
                + " WHERE ins_discipline_key = " + key;
        Oracle.Execute(sql);
        //
        // Alle opdrachten van de deelprojecten op "Verwerkt" zetten.
        var sql = "UPDATE mld_opdr"
                + "   SET mld_statusopdr_key = 7"
                + " WHERE prs_kostensoort_key IN"
                + " (SELECT prs_kostensoort_key"
                + "    FROM bgt_v_project_full"
                + "   WHERE ins_discipline_key = " + key
                + " )"
        Oracle.Execute(sql);
    }
    this.REST_GET    = generic_REST_GET(disc,  // Let op: de originele _model
                                           { "GET": {
                                                     wheres: [ "ins_tab_discipline.ins_discipline_module = 'BGT'"
                                                             ]
                                                    }
                                            }
                                      )

    this.REST_POST   = generic_REST_POST(disc);
    this.REST_PUT    = generic_REST_PUT(disc);
    this.REST_DELETE = generic_REST_DELETE(this, {});
}

function _model_bgt_discipline()
{
  this.records_name = "budgetdisciplines";
  this.record_name = "budgetdiscipline";
  this.table = "ins_tab_discipline";
  this.audit = { "childtable": "bgt_disc_params" }, // Parameters voor fac_audit.
  this.primary = "ins_discipline_key";
  this.soft_delete = "ins_discipline_verwijder";  // ik wil er eigenlijk liever niet standaard op kunnen filteren
  this.autfunction = "WEB_BGTMGT";
  this.record_title = L("bgt_discipline");
  this.records_title = L("bgt_discipline_m");

    this.fields = {
        "id": {
            "dbs": "ins_discipline_key",
            "label": L("lcl_key"),
            "typ": "key",
            "hidden_fld": true,
            "required": true,
            "seq": "ins_s_ins_discipline_key"
        },
        "module": {
            "dbs": "ins_discipline_module",
            "label": L("bgt_discipline_module"),
            "typ": "varchar",
            "hidden_fld": true,
            "defaultvalue": "BGT"
        },
        "name": {
            "dbs": "ins_discipline_omschrijving",
            "label": L("bgt_discipline_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "required": true
        },
        "sequence": {
            "dbs": "ins_discipline_volgnr",
            "label": L("bgt_discipline_volgnr"),
            "typ": "number"
        },
        "vat": {
            "dbs": "ins_discipline_btw",
            "label": L("bgt_discipline_btw"),
            "typ": "check",
            "default": "false",
            "hidden_fld": true
        },
        "symbolname": {
            "dbs": "ins_discipline_image",
            "typ": "icon",
            "module": "BGTD",
            "label": L("lcl_bgt_symbol_name")
        },
        "externnr": {
            "dbs": "ins_discipline_externnr",
            "typ": "varchar",
            "label": L("extern_nr")
        },
        "externsyncdate": {
            "dbs": "ins_discipline_externsyncdate",
            "typ": "datetime",
            "label": L("extern_syncdate"),
            "hidden_fld": true,
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        }
    };

//    this.print = { xmlnode: "budget", where: "discipline"};

    /* Velden van BGT_DISC_PARAMS komen er dynamisch bij */
    this.disc_params = {
        model: model_bgt_disc_params,
        joinfield: "budgetdiscipline"
    };
}
%>