<?xml version="1.0" encoding="windows-1252"?>
<![CDATA[
/*
    $Id$

    File:       puo_putorders.WSC
    Context:    Vanuit de ASP code
    Note:       (de file puo_putorders.wsf is degene die vanuit
                 de scheduled task aangeroepen wordt)

*/
]]>
<component>
<?component error="true" debug="true"?>
<registration
	description="puo_putorders"
	progid="puo_putorders.WSC"
	version="1.00"
	classid="{6D817B6F-9D08-4636-AAD9-8BD7C5EFF56A}"
>
</registration>

<public>

<!-- properties -->

<!-- methods -->
	<method name="initialize">
		<PARAMETER name="params"/>
	</method>
	<method name="sendnotifications">
		<PARAMETER name="ref_key"/>
		<PARAMETER name="pcode"/>
	</method>
	<method name="sendMLDorders">
		<PARAMETER name="opdr_key"/>
	</method>
	<method name="puo_sendMail">
		<PARAMETER name="adrTo"/>
		<PARAMETER name="strSubj"/>
        <PARAMETER name="strBody"/>
        <PARAMETER name="params"/>
	</method>
	<method name="puo_sendSMS">
		<PARAMETER name="telnr"/>
		<PARAMETER name="msg"/>
	</method>
</public>

<script language="javascript" src="../wsf_shared.js"></script>
<script language="javascript" src="puo_shared.js"></script>
<script language="javascript" src="puo_crackURL.js"></script>
<script language="javascript" src="puo_xmltools.js"></script>
<script language="javascript" src="puo_base64.js"></script>
<script language="javascript" src="puo_connector.js"></script>
<script language="javascript" src="puo_sendfile.js"></script>
<script language="javascript" src="puo_sendmail.js"></script>
<script language="javascript" src="puo_sendhttp.js"></script>
<script language="javascript" src="puo_sendsoap.js"></script>
<script language="javascript" src="puo_sendftp.js"></script>
<script language="javascript" src="puo_sendsms.js"></script>
<script language="javascript" src="puo_allorders.js"></script>
<script language="javascript" src="puo_notifications.js"></script>

<script language="javascript">
<![CDATA[

/*	properties	*/


/*	methods	*/
/*
/*
    $Revision$
    $Id$

    File:   puo_putorders.wsc
*/
/* Globals */
var S;
var Oracle;
var customerId;
var PUO_LOGPATH;
var custabspath;

function initialize(params)
{
    S = params.S;
    Oracle = params.Oracle;
    customerId = params.customerId;
    custabspath = params.custabspath;
    PUO_LOGPATH = params.PUO_LOGPATH;
}

// Als ref_key meegeven dan alleen notificaties met die referentie (en module zoals afgeleid uit pcode)
function sendnotifications(ref_key, pcode)
{
    if (ref_key > 0 && pcode) // Voorkom terugval naar Scheduled opdrachten
        sendNotification(ref_key, pcode);
}

// Forceer verzenden van één melding opdracht (vanuit ASP code)
// return false bij errors
function sendMLDorders(opdr_key)
{
    var sql = "SELECT b.prs_bedrijf_key, o.mld_melding_key || '/' || o.mld_opdr_bedrijfopdr_volgnr reference,"
            + "       COALESCE (l1.alg_locatie_email, l2.alg_locatie_email) Sender"
            + "  FROM prs_bedrijf b, mld_opdr o, mld_melding m, mld_adres ma, alg_locatie l1, alg_locatie l2"
            + " WHERE b.prs_bedrijf_key = o.mld_uitvoerende_keys"
            + "   AND o.mld_melding_key = m.mld_melding_key"
            + "   AND m.mld_adres_key = ma.mld_adres_key(+)"
            + "   AND ma.alg_locatie_key = l1.alg_locatie_key(+)"
            + "   AND m.mld_alg_locatie_key = l2.alg_locatie_key(+)"
            + "   AND EXISTS (SELECT '' FROM prs_bedrijfadres ba where ba.prs_bedrijf_key= b.prs_bedrijf_key AND ba.prs_bedrijfadres_type='O')"
            + "   AND (o.mld_statusopdr_key = 5 OR o.mld_statusopdr_key = 4)"
            // + "   AND o.mld_opdr_teverzenden = 1" // Die staat nog op 0 om te voorkomen dat geschedulede putorders hem oppikt
            + "   AND o.mld_opdr_verzonden IS NULL"
            + "   AND o.mld_opdr_key = " + opdr_key;
    var oRs = Oracle.Execute(sql);
    if (oRs.Eof) // Misschien nog status==10, ter fiattering
    {   // Dan maar later door scheduled putorders laten oppikken
        oRs.Close();
        var sql = "UPDATE mld_opdr SET mld_opdr_teverzenden = 1 WHERE mld_opdr_key = " + opdr_key;
        Oracle.Execute(sql);
        return true; // niet echt een error
    }

    var Bedrijf_key = oRs("prs_bedrijf_key").value;
    var XMLnode = 'opdracht';
    var OpdrKey = opdr_key;
    var ordernr = oRs("reference").value;
    var Sender  = oRs("Sender").value||"";
    var cust    = 0;
    oRs.Close();

    if ((S("puo_flags") & 4) != 4) // Wis bitje +4 voor alles scheduled
    {
        Log2File(1, "\n====== Opdracht: key: " + OpdrKey + " nr: " + ordernr + " (" + toDateTimeString(new Date()) + ")");
        Log2File(1, "       Skipping immediate sending because of puo_flags & 4. Scheduled putorders may retry.");

        var sql = "UPDATE mld_opdr SET mld_opdr_teverzenden = 1 WHERE mld_opdr_key = " + opdr_key;
        Oracle.Execute(sql);
        return true; // niet echt een error
    }

    var result = send1Order(Bedrijf_key, XMLnode, OpdrKey, ordernr, Sender, cust)
    if (result)
    {
        Log2File(1, "====== " + XMLnode + " done (" + toDateTimeString(new Date()) + ")");
    }
    else
    {   // Dan maar opnieuw door scheduled putorders laten proberen
        Log2File(1, "====== " + XMLnode + " sending failed. Retry by scheduled putorders (" + toDateTimeString(new Date()) + ")");
        var sql = "UPDATE mld_opdr SET mld_opdr_teverzenden = 1 WHERE mld_opdr_key = " + opdr_key;
        Oracle.Execute(sql);
    }
    return result;
}

// Vanuit ASP shared/send_mail.inc
function puo_sendMail(adrTo, strSubj, strBody, params)
{
    params = params || {};
    Log2File(1, "\n====== Sending e-mail (" + toDateTimeString(new Date()) + ")");

    sendMail( params.FromAddress || S("puo_fromaddress") //p_mailfrom
              , adrTo
              , strSubj
              , null // p_body
              , strBody // p_bodyhtml
              , { replyTo: params.ReplyAddress }
              )
}

// Vanuit ASP shared/send_sms.inc
function puo_sendSMS(telnr, msg)
{
    Log2File(1, "\n====== Sending SMS (" + toDateTimeString(new Date()) + ")");
    sendSMS( telnr, msg )
}


]]>
</script>
</component>

