// ******************************************
// * $Id$
// *
// *   sendOrder()
// *
// * private functies:
// *   GetExtension()
// *   connectFile()
// *   connectFtp()
// *   connectHTTP()
// *   connectMail()
// *
// ******************************************

function GetExtension(xmlDoc, xslDoc)
{
    // Company has a stylesheet defined. Use this stylesheet to generate a formatted order
    var Extension = "html";

    var xslt   = new ActiveXObject("Msxml2.XSLTemplate.6.0");
    xslDoc.resolveExternals = true; // anders op de volgende regel al:
                                    // A reference to variable or parameter 'srtnotificatiecode' cannot be resolved
    xslt.stylesheet = xslDoc;

    var xslProc = xslt.createProcessor();
    xslProc.input = xmlDoc;
    xslProc.addParameter("mode", "getExtension");
    xslProc.transform();
    var fileExt = xslProc.output;

    Log2File(2, "File ext: " + fileExt);
    // Was in Putorders.exe fout: als je outputmode xsl:output method="text" gebruitk
    // krijg je geen xml-node met bijvoorbeeld <format><extension>csv</extension></format>
    // maar gewoon csv
    // Uiteindelijk eindigde je dan met fallback naar txt!!
    // var extDoc = SafeLoadTextXML(fileExt);
    var extDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
    extDoc.async = false;
    extDoc.loadXML(fileExt);

    if (extDoc.parseError.errorCode != 0)
    {
        // no valid xml structure, doctype of output is probably in text format
        Log2File(1, "Output format is text");
        Extension = "txt";
    }
    else
    {
        extDoc.setProperty("SelectionLanguage", "XPath");
        var extensionNode = extDoc.selectSingleNode("//format/extension")
        if (!extensionNode)
        {
            Log2File(2, "extensionNode Is Nothing");
            Extension = "html";
        }
        else
        {
            Log2File(2, "extensionNode is defined");
            Extension = extensionNode.text;
        }
        Log2File(2, "Extension: " + Extension);
    }
    return Extension;
}

// JGL Volgens mij is er juni 2014 geen enkele klant die dit gebruikt.
function connectFile(p_connect, p_textstream, p_filename)
{
        var filename = p_connect.CurrentAddress + p_filename;
        Log2File(1, "Sending File as: " + filename + "...");

        p_textstream.SaveToFile(filename, 2); // adSaveCreateOverWrite

        var resultcode = 0;
        var errText = "";
        Log2File(2, "*< connectFile");

        return {resultcode: resultcode, errtext: errText};
}


function connectHTTP( p_connect
                    , p_textstream
                    , p_xsldoc
                    , p_bedrijfadres
                    , p_filename
                    )
{
    Log2File(1, "Sending HTTP as: " + p_filename + "...");

    var resultcode = -1;
    var errText = "";
    var mode = "";
    var submitResult;

    if (p_bedrijfadres.ordermode == 0)
    {
        mode = "HTTP";
        Log2File(2, "Before SendOrder submitHTTP");
        submitResult = submitHTTP( p_connect
                                 , p_textstream
                                 , p_bedrijfadres);
        Log2File(2, "HTTPresult: " + submitResult);
    }
    else if (p_bedrijfadres.ordermode & 1)
    {
        mode = "SOAP";
        Log2File(2, "Before SendOrder SubmitDOMSOAP");
        submitResult = submitDOMXSLSOAP( p_connect
                                       , p_textstream
                                       , p_xsldoc
                                       , p_bedrijfadres
                                       );
        Log2File(2, "(base64-decoded) response: " + submitResult)
        // SOAPresult bevat nu doorgaans een cXML response, anders was er wel een error geraised
        //
        // Van cxml gaan we nog weer controleren of het verzendresultaat goed is
    }
    else
    {
        Log2File(0, "Ongeldig order_mode: " + p_bedrijfadres.ordermode);
    }

    if (mode = "SOAP")
    {
        resultcode = 0;
        // xml maar even eruit gehaald omdat API requests soms falen en deze geven dan geen geldige
        // xml terug.
        if (p_bedrijfadres.extension == "cxml" || p_bedrijfadres.extension == "xxml")
        {
            var XMLresult = new ActiveXObject("Msxml2.DOMDocument.6.0");
            var xmlDoc = SafeLoadTextXML(submitResult);

            TestAndRaiseAnXMLError("Error in xXML "+mode+" response", xmlDoc);
            if (p_xsldoc)
            {
                xmlDoc.transformNodeToObject(p_xsldoc, XMLresult);

                Log2File(3, "(c)XML response: " + XMLresult.xml);
                TestAndRaiseAnXMLError("Analyseren (c)XML response", XMLresult);
                if (XMLresult.text)
                {
                    Log2File(1, "(c)XML resultaat error: " + XMLresult.xml);
                    resultcode = 1;
                    errText = XMLresult.text;
                }
            }
            else
            {
                Log2File(3, "(c)XML response: niet gegenereerd");
            }
        }
    }
    Log2File(2, "*< connectHTTP");

    return {resultcode: resultcode, errtext: errText};
}


function connectFTP( p_connect
                   , p_bedrijfadres
                   , p_textstream
                   , p_filename
                   , p_attachfolder
                   )
{
    Log2File(1, "Sending FTP as: " + p_filename + "...");

    // De opdracht:
    Log2File(2, "Sending data as file: " + p_filename + " (" + p_textstream.Size + " bytes)");
    var FTPresult = sendFTP(p_connect, p_filename, p_textstream); // send text data
    //
    // Stuur alle flexfiles (type M(ap) en F(file)) ook mee
    if (FTPresult && p_attachfolder)
    {
        var objFso  = new ActiveXObject("Scripting.FileSystemObject");
        if (objFso.FolderExists(p_attachfolder))
        {
            var objFolders = objFso.GetFolder(p_attachfolder);
            var subFolders = new Enumerator(objFolders.SubFolders);
            for (; !subFolders.atEnd(); subFolders.moveNext())
            {
                var objFiles = objFso.GetFolder(subFolders.item());
                var allFiles = new Enumerator(objFiles.files);
                for (; !allFiles.atEnd(); allFiles.moveNext())
                {
                    var attFile = allFiles.item();
                    Log2File(1, "Sending attachment: " + attFile.Path + " (" + attFile.Size + " bytes)");
                    var filename = safe.filename(attFile.Name, "/", "_");

                    FTPresult = sendFTP(p_connect, filename, "", attFile.Path);
                }
            }
        }
    }

    var resultcode = -1;
    var errText = "";
    if (FTPresult)
    {
            resultcode = 2;
        }
        Log2File(2, "*< connectFTP");

    return {resultcode: resultcode, errtext: errText};
}


function connectMail( p_connect
                    , p_textstream
                    , p_xmlData
                    , p_bedrijfadres
                    , p_xslPath
                    , p_ordernr
                    , p_order_key
                    , p_attachfolder
                    , p_sender
                    , XMLnode
                    , p_filename
                    )
{
    Log2File(1, "Sending email as: " + p_filename + "...");
    var resultcode = -1;
    var errText = "";

    if ( S("puo_fromaddress") && S("puo_fromaddress") != "null" && S("puo_fromaddress") != "nodefault")
    {
        // Compose mail
        Log2File(2, "Before SendMail");
        var body = "Opdracht nr. " + p_ordernr + " is middels Facilitor bij u geplaatst."
                 + " De specificatie van de bestelling is bijgevoegd als document: " + p_filename;
        var mailResult  = false;
        var encryptpass = "";

        if ((p_bedrijfadres.ordermode & 2) != 0) // Ooit bedacht om vertaaldocumenten encryped te versturen
        {                                         // Dat werd echter niet door virusscanners geaccepteerd dus
                                                  // nooit in gebruik genomen
            encryptpass = p_bedrijfadres.certificateName;
        }

        var mailsubject = getMailSubject(XMLnode, p_order_key, p_ordernr);

        // Alleen txt en html kunnen eventueel in de mailbody
        var mail_to = p_connect.mailto;
        var params = { attachFolder:   p_attachfolder
                     , attachPassword: encryptpass
                     , include_file:   p_bedrijfadres.AttachFile
                     , cc          :   S("puo_cc")
                     , bcc         :   S("puo_bcc")
                     , receiptto   :   S("puo_receiptto")
                     };
        if ((p_bedrijfadres.ordermode & 1) == 0)  // In body
        {
            if (p_bedrijfadres.extension == "html")
            {
                // Use the mailer translation function in case the html is added to the body
                // to be able to add the logo and to include the email parameter
                var content = XML2HTML( p_xmlData
                                      , p_xslPath
                                      , ""
                                      , "email"
                                      );
                params.attachFileName = p_filename; //JGL Volgens mij don't care parameter
                mailResult = sendMail( p_sender
                                     , mail_to
                                     , mailsubject
                                     , ""
                                     , content
                                     , params
                                     );
            }
            else
            {
                mailResult = sendMail( p_sender
                                     , mail_to
                                     , mailsubject
                                     , stream2text(p_textstream)
                                     , ""
                                     , params
                                     );
            }
        }
        else // Plain body met verwijzing naar de attachment, waarschijnlijk een XML
        {
            params.attachFileName = p_filename;
            params.attachStream   = p_textstream;
            mailResult = sendMail( p_sender
                                 , mail_to
                                 , mailsubject
                                 , body
                                 , ""
                                 , params
                                 );
        }

        if (mailResult)
        {
            resultcode = 2;
        }
        else
        {
            Log2File(0, "Ordering email not sent..");
        }
    }
    else
    {
            Log2File(0, "Ordering email not sent...");
        }
        Log2File(2, "*< connectMail");

    return {resultcode: resultcode, errtext: errText};
}


// Public functies
function SendOrder( p_connect
                   , p_xmlData
                   , p_bedrijfadres
                   , p_xslPath
                   , p_ordernr
                   , p_order_key
                   , p_attachfolder
                   , p_sender
                   , XMLnode
                   , p_code
                   )
{
    // Input:
    // Order_mode 0=in body, 1=attachment
    //           +2='encrypt' opdracht mail attachments met 'certificateName'
    //           +4='donot doublebase64encode'
    // Resultaat:
    //   0 Alles ok
    //  -1 Technische fout, bijvoorbeeld geen Certificaat, geen HTTPS verbinding of ongeldige SOAP envelop
    //   1 Logische fout in bijvoorbeeld de cXML zoals leverancier niet bekend o.i.d.
    //     errText wordt eventueel met een tekst gevuld
    //   2 OK maar geen bevestiging ontvangen bij mail, file en FTP
        Log2File(2, "*> sendOrder");
    try
    {
        if (!p_xslPath)
        {
          throw new UserException(1000, "PutOrders", "No path specified");
        }
        var resultcode = -1;
        var errText = "Onbekende fout";

        Log2File(3, "In SendOrder sender=" + p_sender);
        // Company has a stylesheet defined. Use this stylesheet to generate a formatted order
        // Voor AKZA/GTI is dit bijvoorbeeld een eigen formaat
        // Voor UWVA/Westplant is dit een cXML
        // Load stylesheet
        Log2File(1, "Load company style: " + p_xslPath);
        var xslDoc = SafeLoadFileXSL(p_xslPath);
        xslDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"); // verderop nodig
        var xmlDoc = SafeLoadTextXML(p_xmlData);

        if (!p_bedrijfadres.extension)
        {
            p_bedrijfadres.extension = GetExtension(xmlDoc, xslDoc) // Old style via XSL
        }
        if (p_bedrijfadres.extension)
        {
            var streamResult = new ActiveXObject("ADODB.Stream");
            streamResult.Type = 2; // adTypeText
            streamResult.Charset = 'utf-8';

            // Kijk of er in de XSL naar een andere encoding wordt getransformeerd
            // Naar Windows-1252 kregen we namelijk problemen bij Extended ASCII
            // karakters op Windows 2008 (AAIT#31212)
            if (xslDoc.selectSingleNode("//xsl:output"))
            {
                var enc = xslDoc.selectSingleNode("//xsl:output").getAttribute("encoding");
                if (enc)
                {
                    Log2File(2, "Forcing stream encoding to: " + enc);
                    streamResult.Charset = enc;
                }
            }

            streamResult.Open();
            if (p_bedrijfadres.extension == "xml" || p_bedrijfadres.extension == "cxml")
            {
                result = XML2HTML( p_xmlData
                                 , p_xslPath
                                 , p_code
                                 , ""
                                 )

                var XMLResult = new ActiveXObject("Msxml2.DOMDocument.6.0");
                XMLResult.async = false;
                XMLResult.loadXML(result);
                TestAndRaiseAnXMLError("Error in SendOrder XML2HTML result", XMLResult);

                // Eventuele attachements inplakken
                //Log2File(2, "Order: " + XMLResult.xml);
                XMLResult = mergeXMLAttachments(XMLResult);
                XMLResult.save(streamResult);
                //Log2File(2, "Order2: " + XMLResult.xml);
            }
            else // Bijvoorbeeld csv
            {
                xmlDoc.transformNodeToObject(xslDoc, streamResult);
            }
            LogStream2File(2, "transformed", streamResult, p_bedrijfadres.extension);
            streamResult.Position = 0;

            // file naam mag geen / bevatten
            var filename = p_ordernr + "." + p_bedrijfadres.extension;
            filename = safe.filename(filename, "/", "_");

            var connectResult = {};
            if (p_connect.Connected)
            {
                if (p_connect.isFile)
                {
                    filename = "Opdr_" + filename;
                    connectResult = connectFile( p_connect
                                               , streamResult
                                               , filename
                                               );
                }
                else if (p_connect.isHTTP || p_connect.isHTTPS)
                {
                    connectResult = connectHTTP( p_connect
                                               , streamResult
                                               , xslDoc // voor de terugweg
                                               , p_bedrijfadres
                                               , filename
                                               );
                }
                else if (p_connect.isFTP)
                {
                    filename = "o_" + filename;
                    connectResult = connectFTP( p_connect
                                              , p_bedrijfadres
                                              , streamResult
                                              , filename
                                              , p_attachfolder
                                              );
                }
                else if (p_connect.isMail)
                {
                    filename = "Opdr_" + filename;
                    connectResult = connectMail( p_connect
                                               , streamResult
                                               , p_xmlData
                                               , p_bedrijfadres
                                               , p_xslPath
                                               , p_ordernr
                                               , p_order_key
                                               , p_attachfolder
                                               , p_sender
                                               , XMLnode
                                               , filename
                                               );
                }
                else
                {
                    // no connection
                }
            } // p_connected.Connected
            resultcode = connectResult.resultcode;
            errText = connectResult.errtext;
        } //extension != ""
    }
    catch(e)
    {
        Log2File(0, "Error (SendOrder) sending " + (filename||"") + ": " + e.description);
        resultcode = -1;
        errText = e.description;
    }
    finally
    {
    }
        Log2File(2, "*< sendOrder");

    return {resultcode: resultcode, errtext: errText};
}

function getMailSubject(pXMLnode, pOpdrKey, ordernr)
{
    var sqlS = "";
    var MailSubject = "";
    switch (pXMLnode)
    {
        case "opdracht":
            {
                MailSubject = S("puo_subjectprefixmld");
                sqlS = "SELECT mld.opdrsprintf(" + safe.quoted_sql(MailSubject) + "," + pOpdrKey + ") subj FROM DUAL";
                break;
            }
        case "bestelopdr":
            {
                MailSubject = S("puo_subjectprefix");
                sqlS = "SELECT bes.opdrsprintf(" + safe.quoted_sql(MailSubject) + "," + pOpdrKey + ") subj FROM DUAL";
                break;
            }
        case "contract":
            {
                MailSubject = S("puo_subjectprefixcnt");
                sqlS = "SELECT cnt.sprintf("     + safe.quoted_sql(MailSubject) + "," + pOpdrKey + ") subj FROM DUAL";
                break;
            }
    }
    if (MailSubject.indexOf("##") == -1)
    {
        MailSubject = MailSubject + ordernr; // Geen ##, backwards compatible toch nummer erbij
    }
    else
    {
        var oRsS = Oracle.Execute(sqlS);
        MailSubject = oRsS("subj").value;
        oRsS.Close();
    }

    return MailSubject;
}

