SOP Document with Taxes

This article gives an example of how to code a SOP document when the imported document only supplies header level taxes.

I'm freqently given web imports to bring into Dynamics GP, it's not unusual for them to have the taxes stated at the order level and not at the line level.

The task, then, is to back into the taxes by doing TAXAMOUNT/ORDERSUBTOTAL = TAXRATE

When you have the tax rate you can apply it to each line and build the tax import. This method will incur rounding errors, so the last detail line has to handle that.

Notes:

We bring in the DOCID, TAXSCHID and the TAXDTLID from the config file. I've never had a client want more than one tax detail ID.

 

    Sub importOrder(ByVal oGreatPlainsIntegration As GreatPlainsIntegration)
        Dim oCustomer As Customer = oGreatPlainsIntegration.Batch.Customer
        Dim oDocumentHeader As DocumentHeader = oCustomer.DocumentHeader
        Dim strLocationCode As String = ""
        Dim decSubtotal As Decimal = 0
 
        Try
            'get the server and database from the config
            Dim strServer As String = System.Configuration.ConfigurationManager.AppSettings("SQLServer")
            Dim strDocID As String = System.Configuration.ConfigurationManager.AppSettings("DOCID")
            Dim strDatabase As String = "TWO"
            Dim strTaxScheduleID As String = System.Configuration.ConfigurationManager.AppSettings("TAXSCHID")
            Dim strTaxDetailID As String = System.Configuration.ConfigurationManager.AppSettings("TAXDTLID")
 
            Dim intCurrentLine As Int16 = 0
 
            'declare our eConnect classes
            'this example uses our eConnect Functions class located here: http://dyndeveloper.com/thread.aspx?Threadid=1117
            Dim oeConnectType As New Microsoft.Dynamics.GP.eConnect.Serialization.eConnectType
            Dim oSOPTransactionType As New Microsoft.Dynamics.GP.eConnect.Serialization.SOPTransactionType
            Dim oeConnectFunctions As New eConnectFunctions(strServer, strDatabase)
 
            'TAX RATE
            'loop through the lines to get the document subtotal
            For Each oDocumentLine As DocumentLine In oDocumentHeader.DocumentLine
                decSubtotal += oDocumentLine.Quantity * oDocumentLine.UnitPrice
            Next
            'get the tax rate
            Dim decTaxRate As Decimal = oDocumentHeader.TaxAmount / decSubtotal
 
            'initialize
            Dim decSalesTaxTotal As Decimal = 0
 
            'create the document details
            For Each oDocumentLine As DocumentLine In oDocumentHeader.DocumentLine
                '
                ' handle sales taxes
                '
                ' we know the amount of the taxes at the order level, we'll back into the taxes
                Dim otaSopLineIvcTaxInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopLineIvcTaxInsert_ItemsTaSopLineIvcTaxInsert
                With otaSopLineIvcTaxInsert
                    .SOPNUMBE = oDocumentHeader.DocumentNumber
                    .SOPTYPE = oDocumentHeader.SopType
                    .LNITMSEQ = oDocumentLine.LineNumber
                    .CUSTNMBR = oCustomer.CustomerNumber
                    .SALESAMT = oDocumentLine.Quantity * oDocumentLine.UnitPrice
                    .TAXDTLID = strTaxDetailID
                    If intCurrentLine + 1 = oDocumentHeader.DocumentLine.Count Then
                        'we're on the last detail line, so back into the amount to handle rounding errors
                        .STAXAMNT = oDocumentHeader.TaxAmount - decSalesTaxTotal
                    Else
                        .STAXAMNT = Math.Round(decTaxRate * .SALESAMT, 2)
                        decSalesTaxTotal += .STAXAMNT
                    End If
                End With
                ReDim Preserve oSOPTransactionType.taSopLineIvcTaxInsert_Items(intCurrentLine)
                oSOPTransactionType.taSopLineIvcTaxInsert_Items(intCurrentLine) = otaSopLineIvcTaxInsert
 
                '
                'handle the lines
                '
                'declare an object for the detail line
                Dim oItemsTaSopLineIvcInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopLineIvcInsert_ItemsTaSopLineIvcInsert
 
                'populate the detail line
                oItemsTaSopLineIvcInsert.SOPNUMBE = oDocumentHeader.DocumentNumber
                oItemsTaSopLineIvcInsert.SOPTYPE = oDocumentHeader.SopType
                oItemsTaSopLineIvcInsert.ITEMNMBR = oDocumentLine.ItemNumber
                oItemsTaSopLineIvcInsert.QUANTITY = oDocumentLine.Quantity
                oItemsTaSopLineIvcInsert.CUSTNMBR = oCustomer.CustomerNumber
                oItemsTaSopLineIvcInsert.DOCDATE = oDocumentHeader.DocumentDate
                oItemsTaSopLineIvcInsert.LOCNCODE = oDocumentLine.Location
                oItemsTaSopLineIvcInsert.UOFM = oDocumentLine.UOFM
                oItemsTaSopLineIvcInsert.UNITPRCE = oDocumentLine.UnitPrice
                oItemsTaSopLineIvcInsert.LNITMSEQ = oDocumentLine.LineNumber
                oItemsTaSopLineIvcInsert.TAXAMNT = otaSopLineIvcTaxInsert.STAXAMNT
                oItemsTaSopLineIvcInsert.XTNDPRCE = oDocumentLine.Quantity * oDocumentLine.UnitPrice
                strLocationCode = oDocumentLine.Location
 
                ReDim Preserve oSOPTransactionType.taSopLineIvcInsert_Items(intCurrentLine)
                oSOPTransactionType.taSopLineIvcInsert_Items(intCurrentLine) = oItemsTaSopLineIvcInsert
 
                intCurrentLine += 1
            Next
 
            '
            '
            'create the document header
            '
            '
            Dim otaSopHdrIvcInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopHdrIvcInsert
 
            'populate the header
            With otaSopHdrIvcInsert
                .SOPNUMBE = oDocumentHeader.DocumentNumber
                .SOPTYPE = oDocumentHeader.SopType
                .DOCID = strDocID
                .CUSTNMBR = oCustomer.CustomerNumber
                .BACHNUMB = "WEB" & Now.ToString("yyyyMMdd")
                .DOCDATE = oDocumentHeader.DocumentDate
 
                .ReqShipDate = Now.ToShortDateString
                .FREIGHT = oDocumentHeader.FreightAmount
                .MISCAMNT = oDocumentHeader.MiscAmount
                .TRDISAMT = oDocumentHeader.TradeDiscountAmount
                If .TRDISAMT <> 0 Then
                    .TRDISAMTSpecified = 1
                End If
                .TAXAMNT = oDocumentHeader.TaxAmount
                .TAXSCHID = strTaxScheduleID
 
                .ADDRESS1 = oDocumentHeader.ShippingAddress1
                .ADDRESS2 = oDocumentHeader.ShippingAddress2
                .ADDRESS3 = oDocumentHeader.ShippingAddress3
                .CITY = oDocumentHeader.ShippingCity
                .STATE = oDocumentHeader.ShippingState
                .ZIPCODE = oDocumentHeader.ShippingZip
                .COUNTRY = oDocumentHeader.ShippingCountry
                .LOCNCODE = strLocationCode
 
                .SUBTOTAL = decSubtotal
                .DOCAMNT = .FREIGHT + .MISCAMNT + decSubtotal - .TRDISAMT + .TAXAMNT
            End With
 
            'assign the header to the master
            oSOPTransactionType.taSopHdrIvcInsert = otaSopHdrIvcInsert
 
            'create the document header user def fields
            Dim otaSopUserDefined As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopUserDefined
 
            'populate the header
            With otaSopUserDefined
                .SOPNUMBE = oDocumentHeader.DocumentNumber
                .SOPTYPE = oDocumentHeader.SopType
                .USRDAT01 = oDocumentHeader.UserDate1
                .USRDAT02 = oDocumentHeader.UserDate2
                .USERDEF1 = oDocumentHeader.UserDefined1
                .USERDEF2 = oDocumentHeader.UserDefined2
                .USRDEF03 = oDocumentHeader.UserDefined3
                .USRDEF04 = oDocumentHeader.UserDefined4
                .USRDEF05 = oDocumentHeader.UserDefined5
            End With
 
            'assign the header to the master
            oSOPTransactionType.taSopUserDefined = otaSopUserDefined
 
            ReDim Preserve oeConnectType.SOPTransactionType(0)
            oeConnectType.SOPTransactionType(0) = oSOPTransactionType
            'this example uses our eConnect Functions class located here: http://dyndeveloper.com/thread.aspx?Threadid=1117
            oeConnectFunctions.CreateTransactionEntity(oeConnectType)
 
        Catch ex As Exception
            Throw ex
        End Try
 
    End Sub

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences