Factura salvata intr-un fisier PDF si trimitere mail la client –draft 1

Lasă un comentariu

14 Aprilie 2010 de liviustoica55


 

void  printJournal()
{
    SalesShippingStat   salesShippingStat;
    boolean             newline;

    CompanyInfo companyInfo;
    fileNameOpen filePathForAttachement;
    ;

    if (journalList.len() > 0)
    {
        if (printFormletter)
        {
            this.sendAsXML();

            custInvoiceJour.printJournal(this, journalList);
        }

        if (printCODLabel)
            this.printSalesCODLabel();

        if (printFreightSlip)
            this.printoutFreightSlip();

        if (printShippingLabel && SalesParameters::find().ShippingStat != SalesShipping::PackingSlip  &&  ! this.proforma())
        {
            newline = journalList.first(custInvoiceJour);
            while (newline)
            {
                this.initShippingStat(custInvoiceJour);

                if (recordListShippingLine && recordListShippingLine.len() != 0)
                    salesShippingStat.printJournal(this, recordListShippingLine);

                newline = journalList.next(custInvoiceJour);
            }
        }

        if (this.proforma())
            this.removeProforma();
        // Kumon: Invoice layout - begin
        else
        {
            if(KumParameters::useInvoiceLayouts())
            {
                companyInfo = CompanyInfo::find();
                if(companyInfo.KUMGeneratePDF)
                {
                    //create PDF from invoice
                    filePathForAttachement = this.makePDFDocument(companyInfo);
                    
                    if(companyInfo.KUMSendMail)
                        //send mail to customer
                        this.sendMailCustomer(companyInfo,custInvoiceJour,filePathForAttachement);
                }
            }
        }
        // Kumon: Invoice layout - end

    }
}

void sendMailCustomer(CompanyInfo _companyInfo,CustInvoiceJour _custInvoiceJour,fileNameOpen _filePathForAttachement)
{
    //get setup
    SysEmailParameters eMailParameters = SysEmailParameters::find();

    //>NET
    System.Net.Mail.SmtpClient smtpClient;
    System.Net.Mail.MailMessage mail;
    System.Net.NetworkCredential networkCredential;
    System.Net.Mail.SmtpDeliveryMethod deliveryMethod;

    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;
    System.Exception clrException;

    str password =  SysEMailParameters::password();
    boolean ret;

    InteropPermission interopPermission;

    //destination's mail
    Email recipients ; 
    str subject, body;
    fileNameOpen  fileNameForEmail;
   
    ;
    //get the destination's mail
    recipients = CustTable::find(_custInvoiceJour.InvoiceAccount).Email;
    
    if(recipients && _filePathForAttachement)
    {
        fileNameForEmail = _filePathForAttachement;

        //create subject
         subject = 'Invoice number: ' + _custInvoiceJour.InvoiceId;

         //create body
         body =   'This email was send from ' + CompanyInfo::name() + ', using Dynamics AX. \n';
         body +=  'You are receiving this informational e-mail as part of the services agreed with Kumon Company.';


        interopPermission = new InteropPermission(InteropKind::ClrInterop);
        interopPermission.assert();

        deliveryMethod = ClrInterop::parseClrEnum('System.Net.Mail.SmtpDeliveryMethod', 'Network');
        // TwC: used API CLRObject
        // BP Deviation Documented
        smtpClient = new System.Net.Mail.SmtpClient(eMailParameters.SMTPRelayServerName ?
        eMailParameters.SMTPRelayServerName : eMailParameters.SMTPServerIPAddress);
        smtpClient.set_Port(eMailParameters.SMTPPortNumber);
        // TwC: used API CLRObject
        // BP Deviation Documented
        networkCredential = new System.Net.NetworkCredential(eMailParameters.SMTPUserName, password);
        //recipients = con2str(str2con(recipients, ";"));

        //mail = new System.Net.Mail.MailMessage(sender,recipients);
        // TwC: used API CLRObject
        // BP Deviation Documented
        mail = new  System.Net.Mail.MailMessage(eMailParameters.SMTPUserName,recipients);
        //exchange server 2007 require -> sender id == login user name ???
        mail.set_Subject(subject);
        mail.set_Body(body);
        smtpClient.set_Credentials(networkCredential);
        smtpClient.set_DeliveryMethod(deliveryMethod);
        mail.set_IsBodyHtml(true);

        attachementCollection = mail.get_Attachments();
        // TwC: used API CLRObject
        // BP Deviation Documented
        attachment = new System.Net.Mail.Attachment(fileNameforEmail);
        attachementCollection.Add(attachment);


        try
        {
            smtpClient.Send(mail);
            ret = true;
        }
        catch (Exception::CLRError)
        {
            // BP deviation documented
            clrException = CLRInterop::getLastException();
            if( clrException )
            {
                //BP Deviation Documented
                info( CLRInterop::getAnyTypeForObject(clrException.get_Message() ) );
                ret = false;
            }
        }
    }
}

 

 

/// <summary>
/// Save the invoice like PDF file
/// </summary>
/// <param name="_companyInfo">
/// get the path and type company in companyInfo record
/// </param>
/// <remarks>
/// Kumon customization
/// </remarks>

str makePDFDocument(CompanyInfo _companyInfo)
{
    ReportRun report;
    str nameFile;
    str path;
    str dateInvoice;
;
    dateInvoice = num2Str0(dayofmth(custInvoiceJour.InvoiceDate),2)+num2Str0(mthofyr(custInvoiceJour.InvoiceDate),2)+ num2Str0(year(custInvoiceJour.InvoiceDate),4);
    nameFile = custInvoiceJour.InvoiceAccount +'_' + dateInvoice + '_'+ custInvoiceJour.InvoiceId;

    switch (_companyInfo.KumInvoiceFormat)
        {
            case KUMInvoiceFormat::US :
                report = new ReportRun(new Args(ReportStr(SalesInvoice_US)));
                break;
            case KUMInvoiceFormat::Canada :
                report = new ReportRun(new Args(ReportStr(SalesInvoice_CAN)));
                break;
            case KUMInvoiceFormat::Mexic :
                 report = new ReportRun(new Args(ReportStr(SalesInvoice_MEX)));
                break;
        }

    report.args().caller(this);
    report.args().parmEnum(1);
    report.args().parmEnumType(920);
    report.args().object(journalList);

    report.printJobSettings().setTarget(PrintMedium::File);
    report.printJobSettings().format(PrintFormat::PDF);
    report.printJobSettings().viewerType(ReportOutputUserType::PDF);
    report.printJobSettings().warnIfFileExists(False);

    // get the path in which you will save the PDF files for this company
    path = _companyInfo.KUMDepositoryPDFFilesPath + '\\';      //path = "C:\\Test\\";
    path += nameFile + '.PDF';
    report.printJobSettings().fileName(path);

    report.report().interactive(false);

    // lock the print job settings so can't be changed
    report.printJobSettings().lockDestinationProperties(true);

    report.report().interactive(false);

    // Initialize the report
    report.init();

    report.run();
    report.finalize();
    
    return path;
}

Lasă un răspuns

Completează mai jos detaliile despre tine sau dă clic pe un icon pentru autentificare:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s

%d blogeri au apreciat asta: