<% /*
    $Revision$
    $Id$

    File:           mgt_tools.inc

    Description:

    Context:

    Notes:
*/
%>
<%
function object_merge(){
    for (var i=1; i<arguments.length; i++)
       for (var a in arguments[i])
         arguments[0][a] = arguments[i][a];
   return arguments[0];
}

function bgt_budgetdiscipline_foreign(p_checkauth)
    {
        var tbl = "(SELECT d.ins_discipline_key"
                 + "     , d.ins_discipline_omschrijving oms"
                 + "  FROM ins_tab_discipline d"
                 + "     , bgt_disc_params p"
                 + " WHERE d.ins_discipline_key = p.bgt_ins_discipline_key"
                 + "   AND d.ins_discipline_module = 'BGT'"
                 + "   AND d.ins_discipline_verwijder IS NULL";
        if (p_checkauth)
        {
            tbl += ""
                 + "    AND ins_discipline_key IN"
                        + " (SELECT ins_discipline_key FROM fac_v_webgebruiker"
                        + "   WHERE prs_perslid_key = " + user_key
                        + "     AND (fac_gebruiker_prs_level_read<9 OR fac_gebruiker_alg_level_read<9))";
        }
        tbl += ")"

        var foreign = {
            "tbl": tbl,
            "key": "ins_discipline_key",
            "desc": "oms",
            "order": "oms"
        };

        return foreign;
    }

function bgt_budgetproject_foreign()
    {
        return {
            "tbl": "(SELECT bgt_project_key"
                 + "      , bgt_project_code ||' - '|| bgt_project_omschrijving oms"
                 + "   FROM bgt_project"
                 + "  WHERE bgt_project_verwijder IS NULL"
                 + "  ORDER BY bgt_project_volgnr"
                 + "         , bgt_project_code)",
            "key": "bgt_project_key",
            "desc": "oms"
        };
    }

function bgt_budgetcostcategory_foreign()
    {
        return {
            "tbl": "bgt_kostenrubriek",
            "key": "bgt_kostenrubriek_key",
            "desc": "bgt_kostenrubriek_oms"
        };
    }

function bgt_costtypegroup_foreign()
    {
        return {
            "tbl": "prs_kostensoortgrp",
            "key": "prs_kostensoortgrp_key",
            "desc": "prs_kostensoortgrp_oms"
        };
    }

function bgt_costtype_foreign()
    {
        return {
            "tbl": "prs_kostensoort",
            "key": "prs_kostensoort_key",
            "desc": "prs_kostensoort_oms"
        };
    }

function bgt_orderstatus_foreign()
    {
        return {
            "tbl": "mld_statusopdr",
            "key": "mld_statusopdr_key",
            "desc": "mld_statusopdr_omschrijving"
        };
    }

function bgt_orderstatus_foreign_tbl(isNew)
    {
        var tbl = "(SELECT mld_statusopdr_key"
                + "      , mld_statusopdr_omschrijving"
                + "   FROM mld_statusopdr"
                + (isNew ? "  WHERE mld_statusopdr_key IN (3,5,10)" : "")
                + ")";
        return tbl;
    }

function bgt_invoicestatus_foreign()
    {
        return {
            "tbl": "fin_factuur_statuses",
            "key": "fin_factuur_statuses_key",
            "desc": "fin_factuur_statuses_omschr"
        };
    }

function bgt_account_foreign()
    {
        return {
            "tbl": "(SELECT prs_kostenplaats_key"
                 + "      , prs_kostenplaats_nr ||' - '|| prs_kostenplaats_omschrijving oms"
                 + "   FROM prs_kostenplaats"
                 + "  WHERE prs_kostenplaats_verwijder IS NULL)",
            "key": "prs_kostenplaats_key",
            "desc": "oms"
        };
    }

function bgt_account_foreign_tbl(discipline_key)
    {
        var tbl = "(SELECT kp.prs_kostenplaats_key"
                + "      , kp.prs_kostenplaats_nr ||' - '|| kp.prs_kostenplaats_omschrijving oms"
                + "   FROM prs_kostenplaats kp"
                + "  WHERE kp.prs_kostenplaats_verwijder IS NULL"
                + (discipline_key && discipline_key > 0 ? " AND kp.ins_discipline_key = " + discipline_key : "")
                + ")";
        return tbl;
    }

function bgt_accountgroup_foreign()
    {
        return {
            "tbl": "(SELECT prs_kostenplaatsgrp_key"
                 + "      , DECODE (prs_kostenplaatsgrp_nr, NULL, '', prs_kostenplaatsgrp_nr || ' - ') || prs_kostenplaatsgrp_oms oms"
                 + "      , COALESCE(prs_kostenplaatsgrp_limperiode, 0) prs_kostenplaatsgrp_limperiode"
                 + "   FROM prs_kostenplaatsgrp)",
            "key": "prs_kostenplaatsgrp_key",
            "desc": "oms",
            "extravalue": "prs_kostenplaatsgrp_limperiode"
        };
    }

function bgt_company_foreign(p_oms)
    {
        var v_oms = p_oms || 0;
        var tbl = "(SELECT prs_bedrijf_key"
                + "      , prs_leverancier_nr" + (v_oms == 0 ? " ||' - '|| prs_bedrijf_naam" : "") + " oms"
                + "   FROM prs_bedrijf"
                + "  WHERE prs_bedrijf_verwijder IS NULL"
                + "    AND (prs_bedrijf_intern IS NULL))"; // intern=1 wil ik nooit zien.
        var foreign = {
            "tbl": tbl,
            "key": "prs_bedrijf_key",
            "desc": "oms"
        };
        return foreign;
    }

function bgt_order_foreign()
    {
        return {
            "tbl": "(SELECT DISTINCT"
                 + "       o.mld_opdr_key"
                 + "     , o.mld_opdr_ordernr ||'/'|| mld_opdr_bedrijfopdr_volgnr ||' - '|| mld_opdr_omschrijving oms"
                 + "     , o.mld_opdr_ordernr"
                 + "     , mld_opdr_bedrijfopdr_volgnr"
                 + "  FROM bgt_project p"
                 + "     , bgt_kostenrubriek r"
                 + "     , prs_kostensoortgrp g"
                 + "     , prs_kostensoort s"
                 + "     , mld_opdr o"
                 + " 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 = o.prs_kostensoort_key)",
            "key": "mld_opdr_key",
            "desc": "oms",
            "extravalue": "mld_opdr_ordernr, mld_opdr_bedrijfopdr_volgnr",
            "order": "mld_opdr_ordernr, mld_opdr_bedrijfopdr_volgnr, oms"
        };
    }

function bgt_invoice_foreign()
    {
        return {
            "tbl": "(SELECT DISTINCT"
                 + "        d.fac_usrdata_volgnr"
                 + "      , d.fac_usrdata_code"
                 + "   FROM fac_usrdata d"
                 + "      , fac_usrtab t"
                 + "  WHERE t.fac_usrtab_key = d.fac_usrtab_key"
                 + "    AND t.fac_usrtab_naam = " + safe.quoted_sql(S("bgt_usrtab_vaste_factuurnummer"))
                 + "  ORDER BY d.fac_usrdata_volgnr"
                 + ")",
            "key": "fac_usrdata_volgnr",
            "desc": "fac_usrdata_code"
        };
    }

%>