Skip to Content
0
Former Member
May 17, 2005 at 08:04 PM

How to update oOrder and oInvoice Items' COGM?

45 Views

I am attempting to update the COGM and Profit Margin values for Non-Stock items when an associated Purchase Invoice for these non-stock items is received. This is being done to have more accurate booked order and invoiced order reports. The code I have written is shown below. As yet, I am unable to find a way to reach the RDR1.GrossBuyPr column (labled as COGM in the UI) and INV1.GrossBuyPr column (also, COGM in the UI) from the DI API. Is there another way to update these values. Also, how would I update the Profit Margin for the respective lines?

Any help would be greatly appreciated!

    Private Sub oApplication_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles oApplication.ItemEvent

        If pVal.FormType = 141 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK And _
           pVal.Before_Action = False And pVal.FormUID = 1 And pVal.Action_Success = True Then

            oForm = oApplication.Forms.GetForm(pVal.ForType, pVal.FormTypeCount)
            updateNonStockCOGM()

        End If

    End Sub


    Private Sub updateNonStockCOGM()

        Dim oItems As SAPbouiCOM.Items
        Dim oItem As SAPbouiCOM.Item
        Dim oEditText As SAPbouiCOM.EditText
        Dim sPI_DocNum As String
        Dim row, row2 As Integer
        Dim oPI As SAPbobsCOM.Documents
        Dim oPI_Lines As SAPbobsCOM.Document_Lines
        Dim uiPI_Item As SAPbobsCOM.Items
        Dim oPO As SAPbobsCOM.Documents
        Dim oPO_Lines As SAPbobsCOM.Document_Lines
        Dim uiPO_Item As SAPbobsCOM.Items
        Dim oSO As SAPbobsCOM.Documents
        Dim oSO_Lines As SAPbobsCOM.Document_Lines
        Dim uiSO_Item As SAPbobsCOM.Items
        Dim oSI As SAPbobsCOM.Documents
        Dim oSI_Lines As SAPbobsCOM.Document_Lines
        Dim uiSI_Item As SAPbobsCOM.Items

        ' Obtain OPCH.DocNum ... contained in Item # 8  
        oItems = oForm.Items
        oItem = oItems.Item("8")
        oEditText = oItem.Specific
        sPI_DocNum = oEditText.Value

        oPI = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseInvoices)
        oPI.GetByKey(sPI_DocNum)

        If oPI.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items Then

            oPI_Lines = oPI.Lines

            For row = 0 To oPI_Lines.Count
                oPI_Lines.SetCurrentLine(row)
                uiPI_Item.GetByKey(oPI_Lines.ItemCode)

                If uiPI_Item.InventoryItem = SAPbobsCOM.BoYesNoEnum.tNO And oPI_Lines.BaseEntry() > 0 Then
                    ' Update PO Price
                    oPO = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
                    oPO.GetByKey(oPI_Lines.BaseEntry())
                    oPO_Lines = oPO.Lines
                    oPO_Lines.SetCurrentLine(oPI_Lines.BaseLine())
                    oPO_Lines.Price = oPI_Lines.Price()
                    oPO.Update()
                    If oPO_Lines.BaseEntry() > 0 Then
                        ' Update SO Price
                        oSO = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)
                        oSO.GetByKey(oPO_Lines.BaseEntry())
                        oSO_Lines = oSO.Lines
                        oSO_Lines.SetCurrentLine(oPO_Lines.BaseLine())
                        oSO_Lines.Price = oPI_Lines.Price()    '<== INCORRECT!!!  NOT SURE HOW TO SET THE COST OF THE ITEM...
                        ' How do I recompute the COGM value?
                        oSO.Update()
                    End If
                    Dim oRecordSet As SAPbobsCOM.Recordset
                    oSI = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices)
                    oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("Select DocNum from OINV where BaseDocNum = '" & oSO.DocNum & "'")
                    If oRecordSet.RecordCount > 0 Then
                        oSI.Browser.Recordset = oRecordSet
                        oSI_Lines = oSI.Lines
                        For row2 = 0 To oSI_Lines.Count
                            oSI_Lines.SetCurrentLine(row2)
                            If oSI_Lines.BaseLine.Equals(oSO_Lines.LineNum) = True Then
                                oSI_Lines.cost()    '<== INCORRECT!!!!  NOT SURE HOW TO SET THE COST OF THE ITEM....
                                ' How do I recompute the COGM value?
                                oSI.Update()
                            End If
                        Next
                    End If
                End If
            Next
        End If
    End Sub