Robin,
Ok, here it is.
Once you've followed the instructions below, the sales order form will have a new menu option under the record menu : "Process and Email or Print".
When selected, the current sales order is processed, and then either emailed or printed, depending on the value of the debtor email address (if blank print, if not blank, then email).
The report and printer to use is drawn from the printer setup as defined in the sales order form. The first invoice type report configured is what is printed. It will print the number of copies configured to print (if printing), but if emailing only attaches the invoice once.
The subject and body will need to be customised - i've just put a rather simple example in there.
There are 3 code fragments to paste into the breakout scripts for sales order entry. I advise you to work on a copy of your live data first.
There is one thing you will have to change - the sending address needs to be configured. Find the line which reads
- Code: Select all
From = "[email protected]"
and replace it with an appropriate address.
1. Add this code to the very start of the Form Custom Menu Clicked breakout of the sales order form, before any existing code.
- Code: Select all
If MenuIndex = 4 Then
ProcessAndEmailOrPrintInvoice SalesOrderObject, FormObject, MDIParentObject
End If
2. Add this code to the very start of the Form Loaded breakout of the sales order entry form (before any other existing code)
- Code: Select all
FormObject.mRecordCustom(4).Caption = "Process and Email or Print"
FormObject.mRecordCustom(4).Visible = True
FormObject.mRecordCustom(4).Checked = False
3. Add this code to the Form Loaded breakout of the sales order entry form (copy the code below AFTER the End Sub of the existing breakout code)
- Code: Select all
Public Function ProcessSalesOrder(FormObject, SalesOrderObject)
Dim SOProcObject
Dim lErrorString
Dim lErrorModule
ProcessSalesOrder = True
If ProcessSalesOrder = True Then
Set SOProcObject = CreateObject("JiwaProcessSO.clsSalesOrderProcess")
Set SOProcObject.Database = SalesOrderObject.Database
Set SOProcObject.CommonLib = SalesOrderObject.JiwaCommLib
Set SOProcObject.JLib = SalesOrderObject.JLib
Set SOProcObject.MDIParent = FormObject.MDIParent
Set SOProcObject.SystemProfile = SalesOrderObject.SystemProfile
End If
If ProcessSalesOrder = True Then
If SOProcObject.Setup = 0 Then
ProcessSalesOrder = False
lErrorString = SOProcObject.ErrorString
lErrorModule = SOProcObject.ErrorModule
End If
End If
If ProcessSalesOrder = True Then
If SOProcObject.ProcessSalesOrder(SalesOrderObject.InvoiceID) = 0 Then
ProcessSalesOrder = False
lErrorString = SOProcObject.ErrorString
lErrorModule = SOProcObject.ErrorModule
End If
End If
If Not (SOProcObject Is Nothing) Then
SOProcObject.CleanUp
Set SOProcObject = Nothing
End If
If ProcessSalesOrder = False Then
MsgBox "Error occurred whilst processing : " & lErrorString & vbCrLf & "Module : " & lErrorModule
End If
End Function
Public Function EmailReportDirect(MDIParentObject, SalesOrderObject, ReportFileName, From, EmailTo, Subject, CC, BCC, Message, NameToGiveAttachment, EM_Main_RecID, SourceID, SourceType, SourceDisplayNo, SourceDrillDownIndex, rtnErrorModule, rtnErrorString)
Dim PrintReportObject
Dim m_JiwaReportRange
Dim MyLoop
Dim OldCriticalSectionFlag
EmailReportDirect = True
EM_Main_RecID = ""
If EmailReportDirect = True Then
InternetMailControlLicenseKey = "IPELLICSJMMBMVTC"
Set InternetMailControl = CreateObject("SocketTools.InternetMail")
' Initialize the control with the license key
nError = InternetMailControl.Initialize(InternetMailControlLicenseKey)
If nError > 0 Then
rtnErrorModule = "InternetMailControl.Initialize"
rtnErrorString = "Unable to initialize control:" & vbCrLf & InternetMailControl.LastErrorString
EmailReportDirect = False
End If
End If
If EmailReportDirect = True Then
Set PrintReportObject = CreateObject("JiwaPrintReport.clsJiwaPrintReport")
Set PrintReportObject.Database = SalesOrderObject.Database
Set PrintReportObject.JiwaCommLib = SalesOrderObject.JiwaCommLib
Set PrintReportObject.JLib = SalesOrderObject.JLib
Set PrintReportObject.CrystalApplicationObject = MDIParentObject.GetJWPrintObject
Set PrintReportObject.SystemProfile = SalesOrderObject.SystemProfile
Set PrintReportObject.InternetMailControl = InternetMailControl
End If
If EmailReportDirect = True Then
PrintReportObject.ClearError
PrintReportObject.SetUp
End If
If EmailReportDirect = True Then
If PrintReportObject.ErrorString <> "" Then
rtnErrorString = PrintReportObject.ErrorString
rtnErrorModule = PrintReportObject.ErrorModule
EmailReportDirect = False
End If
End If
If EmailReportDirect = True Then
If PrintReportObject.LoadReport(ReportFileName) = 0 Then
rtnErrorString = PrintReportObject.ErrorString
rtnErrorModule = PrintReportObject.ErrorModule
EmailReportDirect = False
End If
End If
If EmailReportDirect = True Then
'PrintReportObject.OverrideRecordSelectionCriteria = RecordSelectionFormula
For Each m_JiwaReportRange In PrintReportObject.JiwaPrintReportRanges
Select Case UCase(Trim(PrintReportObject.JiwaPrintFormulas(m_JiwaReportRange.FormulaKey).Name))
Case UCase("{@Pass_CurrentHistNo}")
m_JiwaReportRange.Value = SalesOrderObject.Database.FormatNumber(Format(SalesOrderObject.SelectedHistoryNo, "00"))
Case UCase("{@Pass_InvoiceNo}")
m_JiwaReportRange.Value = SalesOrderObject.InvoiceNo
Case UCase("{@Pass_SP_InvoiceHistoryID}")
m_JiwaReportRange.Value = SalesOrderObject.SalesOrderHistorys(SalesOrderObject.SelectedHistoryNo).InvoiceHistoryID
End Select
Next
End If
If EmailReportDirect = True Then
If PrintReportObject.UpdateReport = 0 Then
rtnErrorString = PrintReportObject.ErrorString
rtnErrorModule = PrintReportObject.ErrorModule
EmailReportDirect = False
End If
End If
If EmailReportDirect = True Then
If PrintReportObject.EmailReport(From, EmailTo, Subject, CC, BCC, Message, NameToGiveAttachment, EM_Main_RecID, False, SourceID, SourceType, SourceDisplayNo, SourceDrillDownIndex) = 0 Then
rtnErrorString = PrintReportObject.ErrorString
rtnErrorModule = PrintReportObject.ErrorModule
EmailReportDirect = False
End If
End If
If Not (PrintReportObject Is Nothing) Then
PrintReportObject.CleanUp
Set PrintReportObject = Nothing
End If
End Function
Public Function GetReportDetails(SalesOrderObject, rtnReportFileName, rtnReportDescription, rtnLogicalPrinterDeviceName, rtnLogicalPrinterDriverName, rtnLogicalPrinterPort, rtnNumberCopies, rtnErrorString, rtnErrorModule)
GetReportDetails = True
Set PrintSetupObject = CreateObject("JiwaSOPrintSetup.clsSalesOrderReports")
Set PrintSetupObject.SystemProfile = SalesOrderObject.SystemProfile
If PrintSetupObject.ReadRecords = False Then
rtnErrorString = PrintSetupObject.ErrorString
rtnErrorModule = PrintSetupObject.ErrorModule
GetReportDetails = False
Else
For Each lReport In PrintSetupObject
If lReport.ReportType = 0 Then
rtnReportFileName = lReport.ReportPath & "\" & lReport.ReportFileName
rtnReportDescription = lReport.ReportDescription
Set LogicalPrinter = SalesOrderObject.SystemProfile.LogicalPrinters.GetLogicalPrinterFromName(lReport.Printer)
rtnLogicalPrinterDeviceName = LogicalPrinter.DeviceName
rtnLogicalPrinterDriverName = LogicalPrinter.DriverName
rtnLogicalPrinterPort = LogicalPrinter.Port
rtnNumberCopies = lReport.NumberOfCopies
Exit For
End If
Next
End If
End Function
Public Sub ProcessAndEmailOrPrintInvoice(SalesOrderObject, FormObject, MDIParentObject)
NumberCopies = 1
If GetReportDetails(SalesOrderObject, ReportFileName, ReportDescription, LogicalPrinterDeviceName, LogicalPrinterDriverName, LogicalPrinterPort, NumberCopies, ErrorString, ErrorModule) = 0 Then
MsgBox ErrorString
Else
If Trim(ReportFileName) = "" Then
MsgBox "No report has been defined."
Else
If ProcessSalesOrder(FormObject, SalesOrderObject) = True Then
FormObject.ReadRecord 0
If Trim(SalesOrderObject.SalesOrderHistorys(SalesOrderObject.SelectedHistoryNo).EmailAddress) <> "" Then
From = "[email protected]"
EmailTo = SalesOrderObject.SalesOrderHistorys(SalesOrderObject.SelectedHistoryNo).EmailAddress
Subject = "Invoice " & SalesOrderObject.InvoiceNo
CC = ""
BCC = ""
Message = "Dear " & SalesOrderObject.DebtorContactName & "," & vbrclf & vbcrlf & "Please find attached your invoice."
NameToGiveAttachment = SalesOrderObject.InvoiceNo & ".pdf"
SourceID = SalesOrderObject.InvoiceID
SourceType = "Sales Order"
SourceDisplayNo = SalesOrderObject.InvoiceNo
If EmailReportDirect(MDIParentObject, SalesOrderObject, ReportFileName, From, EmailTo, Subject, CC, BCC, Message, NameToGiveAttachment, EM_Main_RecID, SourceID, SourceType, SourceDisplayNo, 3, ErrorModule, ErrorString) = 0 Then
MsgBox ErrorString
End If
SalesOrderObject.AddEmailLog Subject, EmailTo, From, CC, BCC, NameToGiveAttachment, "", Message, EM_Main_RecID, False, 0
If SalesOrderObject.SaveOrder(, True) = 0 Then
MsgBox SalesOrderObject.ErrorString
End If
Else
MDIParentObject.ResetReportSelectionFormulaTokens
MDIParentObject.AddReportSelectionFormulaToken "{@Pass_CurrentHistNo}", SalesOrderObject.SelectedHistoryNo
MDIParentObject.AddReportSelectionFormulaToken "{@Pass_InvoiceNo}", SalesOrderObject.InvoiceNo
MDIParentObject.AddReportSelectionFormulaToken "{@Pass_SP_InvoiceHistoryID}", SalesOrderObject.SalesOrderHistorys(SalesOrderObject.SelectedHistoryNo).InvoiceHistoryID
MDIParentObject.PrintReportDirect ReportFileName, LogicalPrinterDeviceName, LogicalPrinterDriverName, LogicalPrinterPort, NumberCopies, False, ""
SalesOrderObject.AddPrintLog SalesOrderObject.SalesOrderHistorys(SalesOrderObject.SelectedHistoryNo).Ref & " - " & ReportDescription & " (Printed By " & SalesOrderObject.Database.JiwaLoginUserName & ")", 0
If SalesOrderObject.SaveOrder(, True) = 0 Then
MsgBox SalesOrderObject.ErrorString
End If
End If
FormObject.ReadRecord 0
End If
End If
End If
End Sub