<%@ LANGUAGE = JavaScript %>
<% /*
    $Revision$
    $Id:fac_roundtrip_exp.asp 66040 2024-08-27 14:08:35Z erikg $

    File:               fac_roundtrip_exp.asp
    Description:        Exporteer de gegevens van de view behorende bij de import naar een csv-bestand.
    Parameters:         code: Een geldige fac_import_app_code.
    Note:               
*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/adovbs.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<%
FCLTHeader.Requires({ plugins:["jQuery"]});

var autfunction = "WEB_IMPORT";
var authparams = user.checkAutorisation(autfunction);
var code = getQParamSafe("app_code");

var imp_params = { success: true
                 , warning: ""
                 , app_code: code
                 };

var sql = "SELECT fac_import_app_key"
        + "     , fac_import_app_oms"
        + "     , fac_functie_key"
        + "  FROM fac_import_app"
        + " WHERE fac_import_app_prefix IS NULL"
        + "   AND fac_import_app_code = " + safe.quoted_sql(code);
var oRs = Oracle.Execute(sql);
if (!oRs.eof)
{
    imp_app_key = oRs("fac_import_app_key").Value;
}
else
{
    imp_params.success = false;
    imp_params.warning = "Unknow import code: " + code;
}
oRs.Close();

if (imp_params.success)
{

    var viewname = "fac_v_exp_" + code;
    var filename = "fac_data_" + code;
    try
    {
        var hiresTimer = new ActiveXObject("SLNKDWF.About");
        var tm_start = hiresTimer.usTimer;
        var sql = "SELECT * FROM " + viewname;
        var oRs = Oracle.RealConnection.Execute(sql);
        create_export(oRs, filename);
        oRs.Close();
    }
    catch (e)
    {
        var msg = e.description;
        switch (msg.substr(0, 9))
        {
            case "ROW-00004": msg += '\nHint: Query should not contain mld_t_uitvoertijd columns'; break;
            case "ORA-00942": msg += '\nHint: ' + safe.html(viewname.toUpperCase()); break;
        }
        imp_params.success = false;
        imp_params.warning = msg;
    }
}

function create_export(oRs, filename)
{
    var outputmode = 4; //2=excel, 4=csv
    FCLTHeader.Generate({ outputmode: outputmode, filename: filename});
    var rst = new ResultsetTable({ sql: "dummy"
                                 , pRs: oRs // oRs is al eerder bepaald
                                 , ID: "sqldatatable"
                                 , canCSV: true
                                 , showAll: true
                                 , outputmode: outputmode
                                 , title: "viewname"
                                 });

    function fnfnmyFloat(kolom)
    {
        return function (oRs)
        {
            var v = oRs(kolom).Value;
            return safe.displayfloat(v, 2, true); // true voor trimZeros, we weten helemaal niet of die 2 zo bedoeld was
        }
    }

    // kolommen maken
    var donecols = {};
    for (var i = 0; i < oRs.Fields.Count; i++)
    {
        var kolomnaam = oRs.Fields(i).Name.toLowerCase();
        if (kolomnaam in donecols)
        {
            shared.internal_error("Duplicate column name '{0}' not supported".format(kolomnaam));
        }
        donecols[kolomnaam] = true;
        var colpar = { caption: kolomnaam
                     , content: kolomnaam
                     , datatype: null
                     , align: null
                     }
        var kolomtype = oRs.Fields(i).Type;
        if (kolomtype == adInteger || kolomtype == adDecimal)
        {
            colpar.datatype = 'number';
        }
        else if (kolomtype == adCurrency || kolomtype == adNumeric || kolomtype == adVarNumeric || kolomtype == adDouble)
        {
            colpar.align = "right"
            colpar.content = fnfnmyFloat(kolomnaam);
        }
        else if (kolomtype == adDBTimeStamp)
        {
            colpar.datatype = 'datetime';
            colpar.withSeconds = true;
        }
        rst.addColumn(new Column(colpar));
    }

    var cnt = rst.processResultset();
}
%>

<% ASPPAGE_END(); %>
