Export to PDF button for Sales Order  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Export to PDF button for Sales Order

Postby Ernst » Thu May 06, 2021 10:44 am

My customer, recently migrated from XERO to JIWA, says XERO had this fantastic function, they could click button on the sales order screen, and it would create PDF invoice.

Anybody tried this in JIWA, or is it even possible?
User avatar
Ernst
Kohai
Kohai
 
Posts: 219
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 12

Re: Export to PDF button for Sales Order

Postby Mike.Sheen » Thu May 06, 2021 1:07 pm

Ernst wrote:My customer, recently migrated from XERO to JIWA, says XERO had this fantastic function, they could click button on the sales order screen, and it would create PDF invoice.

Anybody tried this in JIWA, or is it even possible?


Of course it's possible - anything is possible - it all depends how much effort and time it will take.

Are you familiar with the plugin "Email Report"? It's in demo data and adds a button to the report previewer to generate the currently viewed report as a PDF and attach that to a new email.

If the customer doesn't want to have to preview the report first, then you've got another problem to solve first - how do you know which report they want to use for the invoice? As you would be aware, we facilitate any number of reports to be configured to print from the sales order form - so a decision would need to be made on which report would be used.

Then you need to know if they generate this PDF direct to file system or do they want to preview it first? If they want to preview it first, then the "Email Report" plugin has done a lot of the heavy lifting for you already. If they want to just save it to file system, then it's not terribly difficult either, but I would question the value of that - I mean, they want to do something with that PDF - saving to file system first is probably just extra steps.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Export to PDF button for Sales Order  Topic is solved

Postby nsbandara » Thu May 06, 2021 1:09 pm

Hi,

Yes, its possible to export invoice to pdf.

You can create button in sales order menu, Invoke a method to export invoice to pdf by loading invoice report and then export report to pdf using crystal report library method.

Please see exmple code segment below.

Code: Select all
JiwaFinancials.Jiwa.JiwaApplication.Report.Configuration.Report reportRec = _.Manager.CollectionItemFactory.CreateCollectionItem<JiwaFinancials.Jiwa.JiwaApplication.Report.Configuration.Report>();
reportRec.ReadRecord(_invoice_report_rec_id);
JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport jiwaPrintReport = _.Manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport>(null);
jiwaPrintReport.LoadReport(reportRec);

//update report parameters

IEnumerator en = jiwaPrintReport.JiwaReportRangeCollection.GetEnumerator();
while (en.MoveNext())
{
   JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaReportRange range = (JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaReportRange)en.Current;
   if (string.IsNullOrEmpty(range.FormulaKey) == false)
   {
      string parameterName = jiwaPrintReport.JiwaPrintFormulaCollection[range.FormulaKey].Name;

      switch (parameterName)
      {
         case "Pass_CurrentHistNo":
            range.Value = _HistoryNo_;
            break;
         case "Pass_SP_CurrentHistNo":
            range.Value = _HistoryNo_;
            break;
         case "Pass_InvoiceNo":
            range.Value = _InvoiceNo_;
            break;
         case "Pass_SP_InvoiceNo":
            range.Value = _InvoiceNo_;
            break;
         case "Pass_InvoiceHistoryID":
            range.Value = _InvoiceHistoryID_;
            break;
         case "Pass_SP_InvoiceHistoryID":
            range.Value = _InvoiceHistoryID_;
            break;
         default:
            break;
      }
   }
}

//export options, update report
string filename = _InvoiceNo_ + "-D" + _HistoryNo_.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd]") + ".pdf";
string exportPath = Path.Combine(_exportFolder_, filename);
jiwaPrintReport.UpdateReport();
//export to disk using crystal report lib
jiwaPrintReport.CrystalReportObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, exportPath);
User avatar
nsbandara
Occasional Contributor
Occasional Contributor
 
Posts: 43
Joined: Tue Jul 16, 2013 5:02 pm
Location: Sri Lanka
Topics Solved: 11

Re: Export to PDF button for Sales Order

Postby pricerc » Thu May 06, 2021 1:13 pm

Ahh Xero. It has some nice bells and whistles, but is a bit of a gilded cage.

We've got a plugin that adds a "Process and Email or Print" button for one of our customers.

Email or Print decided from some flag on the customer that escapes me for the moment, but for email it uses SalesOrder.Email function - it's really quite easy.
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 504
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 20

Re: Export to PDF button for Sales Order

Postby pricerc » Thu May 06, 2021 2:09 pm

Ernst,

I know you prefer VB. Here's the key bits from our plugin.

I have a whole plugin for enhancing Jiwa's standard email templates, but that's a bit out of scope for the question. So there's just a shell of a ParseEmailTemplate function that you'd need to flesh it out a bit.

This is at least partly based on the plugin that Mike mentioned. But it's been probably two years since I did this bit.

Code: Select all
Option Explicit On
Option Infer On
Option Strict On

Imports JiwaFinancials.Jiwa
Imports JiwaFinancials.Jiwa.JiwaApplication
Imports JiwaFinancials.Jiwa.JiwaApplication.PrintGroup.SalesOrderReports

Imports JiwaFinancials.Jiwa.JiwaApplication.Security
Imports JiwaFinancials.Jiwa.JiwaDebtors
Imports JiwaFinancials.Jiwa.JiwaSales
Imports JiwaFinancials.Jiwa.JiwaSales.SalesOrder
Imports JiwaFinancials.Jiwa.JiwaSales.SalesQuote
Imports JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder

Imports System
Imports System.Diagnostics
Imports System.Linq

Namespace JiwaForums
    Public Module SalesOrderMailer

        ''' <summary>
        ''' Format email for sending invoice or credit.
        '''
        ''' NOTE: this is just a shell and would need to be modified to suit your customer.
        ''' </summary>
        ''' <param name="salesOrder"></param>
        ''' <returns></returns>
        Private Function ParseEmailTemplate(ByVal salesOrder As SalesOrder) As FormattedEmailMessage
            Dim result As New FormattedEmailMessage
            result.AttachmentName = String.Format("{0}-D{1:00}", salesOrder.InvoiceNo, salesOrder.SelectedHistoryNo)

            '
            ' Get Debtor details
            '
            Dim debtor As JiwaDebtors.Debtor = salesOrder.Manager.BusinessLogicFactory.CreateBusinessLogic(Of JiwaDebtors.Debtor)(Nothing)
            debtor.Read(salesOrder.Debtor.DebtorID)

            If salesOrder.CreditNote Then
                ' format credit note email
                result.EmailSubject = String.Format("Credit note {0}.D{1:00} attached", salesOrder.InvoiceNo, salesOrder.CurrentHistoryNo)
                result.EmailBody = ""
                result.EmailFrom = ""
                result.EmailTo = debtor.EmailAddress
                result.EmailCC = ""
                result.EmailBCC = ""
            Else
                ' format invoice email
                result.EmailSubject = String.Format("Invoice {0}.D{1:00} attached", salesOrder.InvoiceNo, salesOrder.CurrentHistoryNo)
                result.EmailBody = ""
                result.EmailFrom = ""
                result.EmailTo = debtor.EmailAddress
                result.EmailCC = ""
                result.EmailBCC = ""
            End If

            Return result
        End Function

        ''' <summary>
        ''' Emails an invoice or credit PDF
        ''' </summary>
        ''' <remarks>
        ''' Needs ParseEmailTemplate to do something useful with the template
        ''' </remarks>
        ''' <param name="salesOrder"></param>
        ''' <param name="documentType"></param>
        ''' <param name="docName"></param>
        ''' <param name="parentForm"></param>
        Public Sub EmailSalesOrder(salesOrder As SalesOrder, documentType As SalesOrderReportCollection.SalesOrderPrintReportTypes, docName As String, parentForm As BaseSalesOrderEntryForm)
            If salesOrder Is Nothing Then
                Throw New ArgumentNullException(NameOf(salesOrder), $"{NameOf(salesOrder)} not supplied.")
            End If

            If parentForm Is Nothing Then
                Throw New ArgumentNullException(NameOf(parentForm), $"{NameOf(parentForm)} not supplied.")
            End If

            If Debugger.IsAttached Then Debugger.Break()

            If salesOrder.ChangeFlag Or salesOrder.InsertFlag Then
                Throw New ApplicationException("Cannot email an invoice until the changes have been saved.")
            End If

            If salesOrder.Manager.Staff.GetAbstractPermission(salesOrder.ClientClassName, "Email") <> UserGroup.AccessLevels.Allow Then
                Throw New ApplicationException("You do not have authority to email invoices.")
            End If

            If salesOrder.SalesOrderReports.Count = 0 Then
                Throw New ApplicationException("No sales order reports have been defined.")
            End If

            Dim candidateReports = (
                From r As SalesOrderReport In salesOrder.GetCandidateReportsToEmail().Cast(Of SalesOrderReport)
                Where CInt(r.BillingType) = CInt(salesOrder.BillType) AndAlso
                    (r.DocumentType = documentType) AndAlso
                    (String.IsNullOrWhiteSpace(docName) OrElse String.Compare(docName, r.Name, True) = 0)
                Order By r.ItemNo).ToList()

            If Not candidateReports.Any Then
                Throw New Exception("There are no reports that are applicable to this particular salesorder.")
            End If

            Dim formattedEmail = ParseEmailTemplate(salesOrder)

            Try
                Dim startParameters As New JiwaCollection(Of SalesOrderReport)
                For Each r In candidateReports
                    startParameters.Add(r)
                Next

                Dim emailReportDialog As EmailReport = salesOrder.Manager.DialogFactory.CreateDialog(Of EmailReport)({startParameters}, parentForm, "Sales Order Entry")

                emailReportDialog.EmailTo = formattedEmail.EmailTo
                emailReportDialog.From = formattedEmail.EmailFrom
                emailReportDialog.CC = formattedEmail.EmailCC
                emailReportDialog.BCC = formattedEmail.EmailBCC
                emailReportDialog.Subject = formattedEmail.EmailSubject
                emailReportDialog.Message = formattedEmail.EmailBody
                emailReportDialog.RequestReadReceipt = formattedEmail.RequestReadReceipt
                emailReportDialog.NameToGiveAttachment = formattedEmail.AttachmentName
                If emailReportDialog.ShowDialog(parentForm) <> System.Windows.Forms.DialogResult.OK Then
                    Return
                End If

                salesOrder.Email(emailReportDialog.SelectedReport,
                                 emailReportDialog.NameToGiveAttachment,
                        emailReportDialog.From,
                        emailReportDialog.EmailTo,
                        emailReportDialog.RequestReadReceipt,
                        emailReportDialog.Subject,
                        emailReportDialog.CC,
                        emailReportDialog.BCC,
                        emailReportDialog.Message,
                        CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)

                Return
            Catch ex As Exception
                Throw
            End Try
        End Sub
    End Module

    Public Structure FormattedEmailMessage
        Public Property EmailFrom As String
        Public Property EmailTo As String
        Public Property RequestReadReceipt As Boolean
        Public Property EmailCC As String
        Public Property EmailBCC As String
        Public Property EmailSubject As String
        Public Property EmailBody As String
        Public Property AttachmentName As String
    End Structure

End Namespace
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 504
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 20

Re: Export to PDF button for Sales Order

Postby Ernst » Fri May 07, 2021 8:32 am

Great thanks for all the info, will certainly check out that VB code and when successful post plugin here.

It would always be the invoice.

Would be nice to invoke one of the file save as with windows pop up, like creditor payment batch.

Thanks again.. cheers.
User avatar
Ernst
Kohai
Kohai
 
Posts: 219
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 12

Re: Export to PDF button for Sales Order

Postby pricerc » Fri May 07, 2021 11:27 am

Out of curiosity, why are they wanting to save a PDF of the invoice?

Xero do that because that's the only way you can actually print an invoice - you have to download a PDF and then print it.

I understand wanting to email a PDF with as few clicks as possible, but the saving of a PDF seems a bit superfluous as long as you have the database to pull up invoices.
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 504
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 20

Re: Export to PDF button for Sales Order

Postby Mike.Sheen » Fri May 07, 2021 11:34 am

pricerc wrote:Out of curiosity, why are they wanting to save a PDF of the invoice?

Xero do that because that's the only way you can actually print an invoice - you have to download a PDF and then print it.

I understand wanting to email a PDF with as few clicks as possible, but the saving of a PDF seems a bit superfluous as long as you have the database to pull up invoices.


Exactly.

Mike.Sheen wrote:If they want to just save it to file system, then it's not terribly difficult either, but I would question the value of that - I mean, they want to do something with that PDF - saving to file system first is probably just extra steps
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Export to PDF button for Sales Order

Postby Ernst » Tue Sep 12, 2023 6:50 pm

Have just spent a few hours getting it working. The application, was when you send a freight file via the SendFreight button on sales orders, The mail carrier, also wanted a dangerous good document PDF uploaded via FTP to the carrier website, so the driver can view it while on route, and while carrying the load.
User avatar
Ernst
Kohai
Kohai
 
Posts: 219
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 12

Re: Export to PDF button for Sales Order

Postby Mike.Sheen » Wed Sep 13, 2023 10:59 am

Ernst wrote:also wanted a dangerous good document PDF uploaded via FTP to the carrier website, so the driver can view it while on route, and while carrying the load.


I'd wager they (the carrier) also has an API to get them the PDF that way instead of FTP - which would be a cleaner way achieve this.

Code in a plugin to send a file to FTP is substantial and when I've done it I've always used a 3rd part library I have to include as an embedded reference.

An API POST in a plugin, on the other hand, is usually just one line of code - literally - and no 3rd party libraries needed!
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Next

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 37 guests