<![CDATA[
/*
    $Id$

    File:       put_orders.wsf
    Calling:    cscript ..\..\..\utils\PutOrders\put_orders.wsf
    Parameters: Optioneel FORCE om te zorgen dat falende orders toch weer gestuurd worden
                Optioneel KEEP om te zorgen dat notificaties zelfs bij succes niet uit de Queue verdwijnen
    Context:    Scheduled task
    Note:       De code veronderstelt (nog) dat de current folder een cust/xxxx/tasks folder is
                Concreter: ../Oracle.udl wordt gebruikt
*/
]]>

<job id="IncludeGenExport">
  <script language="JScript" src="../wsf_shared.js"/>
  <script language="JScript" src="../utils_settings.js"/>
  <script language="JScript" src="./puo_shared.js"/>
  <script language="JScript" src="./puo_crackURL.js"/>
  <script language="JScript" src="./puo_XMLTools.js"/>
  <script language="JScript" src="./puo_base64.js"/>
  <script language="JScript" src="./puo_connector.js"/>
  <script language="JScript" src="./puo_sendfile.js"/>
  <script language="JScript" src="./puo_sendmail.js"/>
  <script language="JScript" src="./puo_sendhttp.js"/>
  <script language="JScript" src="./puo_sendsoap.js"/>
  <script language="JScript" src="./puo_sendftp.js"/>
  <script language="JScript" src="./puo_sendsftp.js"/>
  <script language="JScript" src="./puo_sendsms.js"/>
  <script language="JScript" src="./puo_sendapp.js"/>
  <script language="JScript" src="./json2.js"/>
  <script language="JScript" src="./puo_md5.js"/>
  <script language="JScript" src="puo_send2address.js" />
  <script language="JScript" src="puo_hook.js" />
  <script language="JScript" src="./puo_notifications.js"/>

  <script language="JScript">

    var applicatieRun = new Date();
    if (isOffline())
    {
        // wil dit niet gelogd zien settings.overrule_setting("puo_logdestination", 1); // Nooit naar de database
        settings["puo_logdestination"].v = 1;
        customerId = 'XXXX';
        Log2File(0, "FACILITOR is offline, putorders skipped (" + toDateTimeString(applicatieRun) + ")");
        WScript.Quit(1);
    }

    try
    {
        var oAbout = new ActiveXObject("SLNKDWF.About");
        WScript.Echo("SLNKDWF Version: " + oAbout.VersionString);
    }
    catch(e)
    {
        __Log("SLNKDWFx64.DLL is not properly installed.\n{0}".format(e.description));
        WScript.Quit(1);
    }

    var tmStart = oAbout.usTimer;
    var Oracle = Connect2Oracle('puo_Putorders.wsf');
    var otm = Math.round((oAbout.usTimer - tmStart) / 1000);

    var forceOrders = 0;
    var clearNotifications = 1;
    if (WScript.Arguments.length > 0 && WScript.Arguments(0) == 'FORCE')
        forceOrders = 1; // forceer falende orders die normaal uitgesteld zouden worden.

    if (WScript.Arguments.length > 0 && WScript.Arguments(0) == 'KEEP')
        clearNotifications = 0; // Behoud notificaties in de queue zelfs bij succesvol versturen

    var sql = "SELECT fac_version_cust FROM fac_version";
    var oRs = Oracle.Execute(sql);
    var customerId = oRs(0).Value;
    oRs.Close();
    __Log("CustomerId: " + customerId);

    var fso  = new ActiveXObject("Scripting.FileSystemObject");
    // We zijn altijd gescheduled vanuit cust/XXXX/Tasks
/* global */ var custabspath = fso.GetAbsolutePathName("../");
    var WshNetwork = new ActiveXObject("WScript.Network");

    // Let op: Log2File werkt nu pas omdat hierboven pas customerId is bepaald
    Log2File(0, "===========================================================");
    Log2File(0, "PUTORDERS STARTED (" + toDateTimeString(applicatieRun) + ") " + WshNetwork.UserDomain + "\\" + WshNetwork.UserName);
    Log2File(2, "puo_loglevel: " + S("puo_loglevel"));

    if (otm > 2500)
    {
        Log2File(1, "{0} Connect2Oracle took {1} ms".format(toDateTimeString(new Date()), otm));
    }

    // Niet vroeger plaatsen in dit bestand. Settings laden heeft customerId nodig voor flexfilespath
    if (S("tsk_master_schema"))
    {
        var stderr = fso.GetStandardStream (2);
        stderr.WriteLine("Master schema is {0}. Output is probably redirected to ../TEMP/{0}/{1}_PUTORDERS.lock".format(S("tsk_master_schema"), customerId));
        //stderr.WriteLine("Sleeping 5 seconds");
        //WScript.Sleep(5*1000);
        //stderr.WriteLine("Moving on...");
    }
    else
    {
        Log2File(0, "Master schema is not set, Putorders will run just once");
    }

    try
    {
        sendNotification();
        // cleanPopup(); gebeurt tegenwoordig in PROCEDURE fac_clean()
        var sql = "UPDATE fac_version"
                + "   SET fac_version_putorders_date = SYSDATE";
        Oracle.Execute(sql);
    }
    catch(e)
    {
        Log2File(0, "Uncaught error in puo_putorders.wsf: " + e.description, "E");
    }

    var endDate = new Date();
    var duration = Math.round((endDate - applicatieRun) / 100) / 10;
    Log2File(0, "PUTORDERS FINISHED ({0}, duration {1}s)".format(toDateTimeString(endDate), duration));

    Oracle.Close();

  </script>
</job>