Create Invoice from Sales Order

Top  Previous  Next

[Visual Basic] The following example demonstrates how to create and save a new Customer Invoice document from an existing Sales Order document. (This example uses the CustomerInvoiceWebService.)

 

' ----------------------------------------

' Step 1: Create an instance of the web service.

' ----------------------------------------

Dim salesOrderWs As New WebServices.CustomerInvoiceWebService("http://localhost/objacctwebservices/")

 

' ----------------------------------------

' Step 2: Set the LoginKey. (Use Login.GetLoginKey to

' login to a specific company and generate a LoginKey.)

' For this example, the LoginKey is being stored in a 

' public variable called PublicLoginKey.

' ----------------------------------------

StdLib.PopulateObjAcctSoapHeader(salesOrderWs, PublicLoginKey)

 

' ----------------------------------------

' Step 3: Populate a DataSet with the existing (copy from) document.

' For this example, we're retrieving a Sales Order document, so we

' will use the ObjAcct.WebServices.DataSets.CustomerInvoiceDataSet.

' ----------------------------------------

Dim salesOrderDs As DataSet = New WebServices.DataSets.CustomerInvoiceDataSet()

Dim docID As String = "SO5000"

 

' Retrieve the Sales Order Document.

salesOrderDs = salesOrderWs.GetDocumentByDocumentID(docID, "2.0", StdLib.DOCUMENT_Type_SALES_ORDER, "RowID", ReturnTypeEnum.Standard)

 

' Check for errors.

If StdLib.GetErrorNumber(salesOrderDs) < 0 Then

       MsgBox(StdLib.GetErrorAsString(salesOrderDs))

Else

       ' ----------------------------------------

       ' Step 4: Create an instance of the web service.

       ' ----------------------------------------

       Dim invoiceWs As New WebServices.CustomerInvoiceWebService("http://localhost/objacctwebservices/")

 

       ' ----------------------------------------

       ' Step 5: Set the LoginKey. (Use Login.GetLoginKey to

       ' login to a specific company and generate a LoginKey.)

       ' For this example, the LoginKey is being stored in a 

       ' public variable called PublicLoginKey.

       ' ----------------------------------------

       StdLib.PopulateObjAcctSoapHeader(invoiceWs, PublicLoginKey)

 

       ' ----------------------------------------

       ' Step 6: Create and populate a DataSet with the new (copy to) document.

       ' For this example, we're creating a new Customer Invoice document from a specific

       ' Sales Order document, so we will simply copy from the salesOrderDs DataSet.

       ' ----------------------------------------

       Dim invoiceDs As DataSet = salesOrderDs.Copy

 

       ' -------------------

       ' Document Header.

       ' -------------------

       Dim headerDr As DataRow = invoiceDs.Tables("Header").Rows(0)

 

       headerDr("RowID") = ""

       headerDr("DocID") = ""

       headerDr("DocDate") = Today.ToShortDateString

       headerDr("EnteredDate") = Today.ToShortDateString

       headerDr("EntityDocDate") = Today.ToShortDateString        ' Invoice Date.

       headerDr("JournalDocTypeRowID") = StdLib.DOCUMENT_Type_CUSTOMER_INVOICE

       headerDr("ReverseDocRowID") = salesOrderDs.Tables("Header").Rows(0).Item("RowID").ToString.Trim

 

       ' -------------------

       ' Transaction.

       ' -------------------

       Dim invoiceTransDr As DataRow

       Dim i As Integer

 

       For Each invoiceTransDr In invoiceDs.Tables("Transaction").Rows

 

               ' Set the XRefJournalTranRowID to the RowID on the Sales Order transaction.

               invoiceTransDr("XRefJournalTranRowID") = invoiceTransDr("RowID")

 

               invoiceTransDr("RowID") = StdLib.GetTempRowID & CStr(i)

               i += 1

 

       Next

 

       ' ----------------------------------------

       ' Step 7: Save the Customer Invoice (copy to) document.

       ' ----------------------------------------

       Dim returnDs As DataSet = invoiceWs.SaveDocument(invoiceDs, CustomerInvoiceSaveType.ConvertSalesOrderToInvoice)

 

       ' Check for errors.

       If StdLib.GetErrorNumber(returnDs) < 0 Then

               MsgBox(StdLib.GetErrorAsString(returnDs))

       End If

End If