<% /*
    $Revision$
    $Id$

    File:           model_bgt_project.inc

    Description:   Model voor bgt_project

    Context:

    Notes:
*/
%>
<!-- #include file="../api2/model_bgt_kostenrubriek.inc" -->
<!-- #include file="../bgt/bgt_tools.inc" -->
<%

function model_bgt_project()
{
    this.records_name = "budgetprojects";
    this.record_name = "budgetproject";
    this.table = "bgt_project";
    this.primary = "bgt_project_key";
    this.soft_delete = "bgt_project_verwijder";
    this.autfunction = "WEB_BGTMGT";
    this.record_title = L("bgt_project");
    this.records_title = L("bgt_project_m");


    this.fields = {
        "id": {
            "dbs": "bgt_project_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "hidden_fld": true,
            "seq": "bgt_s_bgt_project_key",
            "defaultvalue": null
        },
        "budgetdiscipline": {  /* TODO linkt nu naar algemene discpline, moet bgt-variant in scf zijn? */
            "dbs": "ins_discipline_key",
            "label": L("bgt_discipline_omschrijving"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": {
                "tbl": "ins_tab_discipline",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving",
                "where": "ins_discipline_module = 'BGT' AND ins_discipline_verwijder IS NULL"
            },
            "showtransit": true
        },
        "name": {
            "dbs": "bgt_project_omschrijving",
            "label": L("bgt_project_omschrijving"),
            "typ": "varchar",
            "required": true,
            "translate": true
        },
        "code": {
            "dbs": "bgt_project_code",
            "label": L("bgt_project_code"),
            "typ": "varchar",
            "required": true
        },
        "sequence": {
            "dbs": "bgt_project_volgnr",
            "label": L("bgt_project_volgnr"),
            "typ": "number"
        },
        "account": {
            "dbs": "prs_kostenplaats_key",
            "label": L("bgt_budget_account"),
            "typ": "key",
            "foreign": bgt_account_foreign()
        },
        "pricedate": {
            "dbs": "bgt_project_prijspeildatum",
            "label": L("bgt_project_prijspeildatum"),
            "typ": "date"
        },
        "info": {
            "dbs": "bgt_project_info",
            "label": L("bgt_project_info"),
            "typ": "memo"
        },
        "notes": {
            "dbs": "bgt_project_notes",
            "label": L("bgt_project_notes"),
            "typ": "memo"
        }
    };

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

    this.hook_pre_edit = function (obj, fld)
    {
        if (mode != "save")
        {
            var sql = "SELECT count(*)"
                    + "  FROM bgt_kostenrubriek"
                    + " WHERE bgt_project_key = " + obj.id;
            var oRs = Oracle.Execute(sql);
            if (oRs(0).Value == 0) // Een nieuw project, of een project waaraan nog geen rubrieken zijn gekoppeld.
            {   // Voeg de selectie voor het standaardproject toe.
                fld["import"] = {
                    "dbs": "bgt_v_project_full.bgt_project_key",
                    "label": "Standaard projectdata",
                    "typ": "key",
                    "foreign": {
                        "tbl": "(SELECT DISTINCT bgt_project_key"
                             + "      , pcode ||' - '|| subprojectnaam default_project"
                             + "   FROM bgt_v_project_full"
                             + "  WHERE pcode LIKE 'SPD%'"
                             + "    AND ins_discipline_verwijder IS NULL"
                             + "    AND bgt_project_verwijder IS NULL"
                             + ")",
                        "key": "bgt_project_key",
                        "desc": "default_project"
                    }
                };
            }
            oRs.Close();

            if (obj.id > 0)
            {
                fld["account"].foreign.tbl = bgt_account_foreign_tbl(obj.budgetdiscipline.id);
            }
            %>
            <script>
                <% if (obj.budgetdiscipline)   { %> var budgetdiscipline   = "<%=obj.budgetdiscipline.id%>";   <% } %>
            </script>
            <%
        }
    }

    this.hook_pre_post = function(params, obj)
    {
        // Controle op, en wijzigingen aan data voordat het record wordt toegevoegd.
    }

    this.hook_post_post = function(params, obj, key)
    {
        // Nadat het project-record is toegevoegd is de key hiervan bekend.
        var data_key = getFParamInt("import", -1);
        if (data_key > 0)
        {
           create_default_projectdata(key, data_key);
           params.toaster = "Standaard projectdata toegevoegd."
        }
    }

    this.hook_pre_put = function(params, obj, key)
    {
        // Wordt alleen gebruikt bij wijzigen.
        var data_key = getFParamInt("import", -1);
        if (data_key > 0)
        {
            create_default_projectdata(key, data_key);
            params.toaster = "Standaard projectdata toegevoegd."
        }
    }

    this.hook_pre_delete = function(params, key)
    {
        // Alle opdrachten van dit deelproject 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 bgt_project_key = " + key
                + " )"
        Oracle.Execute(sql);
    }

    // if (!S("bgt_enabled"))
    // {
    //      this.fields.budgetdiscipline.hidden_fld = true
    //      this.fields.budgetdiscipline.default_value = 1
    // }

    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);
}
%>