Page 1 of 1

Sales Order printing, changing rpt on the fly

PostPosted: Thu May 19, 2016 4:54 pm
by DannyC
I have been given the task of writing a plugin which will change the sales order report based on the debtor.

Basically the client will be printing an invoice as per their normal workflow, but when a sales order has a specific debtor, then a delivery docket must print out instead.
They do not have the print selection popup, nor do they have print to screen turned on. This means that as soon as they click the print icon in the toolbar, the output spits out of the printer, no dialogs at all.

I can add their delivery docket into the sales order print collection, but I am finding that both the invoice and delivery docket print out automatically. I just need one or the other.
I think I have successfully swapped from the invoice to the delivery docket based on the debtor (see code below).

I am hooking into the salesOrderForm.SalesOrder.Printing event as that seems to be the only event where I can capture the debtor and change the report before printing.

I can't find a way to just get one report to print out, even though there could be multiple reports in the collection. I thought of incrementing a counter, but that didn't seem to go well, and the ClientCancelledException aborts the whole print job.

Unfortunately, the client has a deadline to get something implemented tomorrow. Yeah, I know!

This is my effort so far.
Code: Select all
    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
        Dim salesOrderForm As JiwaSalesUI.SalesOrder.SalesOrderEntryForm = DirectCast(JiwaForm, JiwaApplication.IJiwaForm)
       AddHandler salesOrderForm.SalesOrder.Printing, AddressOf SOPrinting
  End Sub

   Sub SOPrinting (sender As Object, e As System.eventargs, ByRef Report As JiwaApplication.PrintGroup.SalesOrderReports.SalesOrderReport)
      msgbox(PrintedReportCounter)
      If PrintedReportCounter = 0 Then  '  Just print a single report no matter how many are in the print collection.
         Dim salesOrder As JiwaSales.SalesOrder.SalesOrder = DirectCast( sender , JiwaSales.SalesOrder.SalesOrder)
         If salesOrder.Debtor.AccountNo = "1140" Then
            For Each salesOrderReport As JiwaApplication.PrintGroup.SalesOrderReports.SalesOrderReport In salesOrder.SalesOrderReports
               If salesOrderReport.ReportDefinition.Report.FileName = "MNT30000 - My Delivery Docket.rpt" Then
                  Report = salesOrderReport
                  Exit For
               End If
            Next   
         End If
      End If
      PrintedReportCounter = PrintedReportCounter + 1
      If PrintedReportCounter > 0 Then
         Throw New JiwaApplication.Exceptions.ClientCancelledException
   End If         
   End Sub


Any feedback would be appreciated.

Cheers

Danny

Re: Sales Order printing, changing rpt on the fly  Topic is solved

PostPosted: Mon May 23, 2016 8:30 pm
by Mike.Sheen
You probably want to hook into the PrintingMany event, instead of the Printing event.

In there you can examine all the reports in the ReportCollection passed in as a parameter to that event, and remove the reports you don't want to print.

Just remember that removing items in a collection whilst inside a for each loop will product unexpected results, so use a For Next loop instead - starting from the Count back to 1, with a Step -1.

Re: Sales Order printing, changing rpt on the fly

PostPosted: Thu May 26, 2016 12:24 pm
by DannyC
Thx Mike.
Have sorted out this one using your suggestion re PrintingMany.

Cheers.