.net 连接SAP for NCO 3.0

来源:互联网 发布:java 过滤换行 编辑:程序博客网 时间:2024/06/05 09:44
Imports System.Data
Imports System.IO
Imports System.Data.OracleClient
Imports System.Diagnostics
Imports System.Collections
Imports System.Collections.Generic
Imports OracleHelper
Imports ADOR


Public Class SAPRFC
    Implements IDestinationConfiguration   '继承接口
    Public Destination As RfcDestination     'SAP Connector3.0 的RFC源
    Public SapFunction As IRfcFunction       'SAP RFC函数调用
    Public mystruct As IRfcStructure
    Public datatbl As IRfcTable              '调用到的RFC SAP table
    Public optionstbl As IRfcTable              '调用到的OPTIONS table
    Public fieldtbl As IRfcTable             '调用到的FIELD table


    Public Function ChangeEventsSupported() As Boolean Implements IDestinationConfiguration.ChangeEventsSupported
        Return False
    End Function


    Public Event ConfigurationChanged(ByVal destinationName As String, ByVal args As RfcConfigurationEventArgs) Implements IDestinationConfiguration.ConfigurationChanged  '事件


    Public Function GetParameters(ByVal destinationName As String) As RfcConfigParameters Implements IDestinationConfiguration.GetParameters
        Dim parms As RfcConfigParameters = New RfcConfigParameters()
        If ("qas_000".Equals(destinationName)) Then
            parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.SystemNumber, "70")  'SAP实例
            parms.Add(RfcConfigParameters.User, "")      ' 用户名
            parms.Add(RfcConfigParameters.Password, "")  '密码
            parms.Add(RfcConfigParameters.Client, "777")       ' Client
            parms.Add(RfcConfigParameters.Language, "EN")      '登陆语言
            parms.Add(RfcConfigParameters.PoolSize, "5")
            parms.Add(RfcConfigParameters.MaxPoolSize, "10")
            '  parms.Add(RfcConfigParameters.IdleTimeout, "60")
            Return parms
        ElseIf ("prd_000".Equals(destinationName)) Then
            parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.SystemNumber, "02")  'SAP实例
            parms.Add(RfcConfigParameters.User, "")      ' 用户名
            parms.Add(RfcConfigParameters.Password, "")  '密码
            parms.Add(RfcConfigParameters.Client, "777")       ' Client
            parms.Add(RfcConfigParameters.Language, "EN")      '登陆语言
            parms.Add(RfcConfigParameters.PoolSize, "5")
            parms.Add(RfcConfigParameters.MaxPoolSize, "10")
            '  parms.Add(RfcConfigParameters.IdleTimeout, "60")
            Return parms
        Else
            Return Nothing
        End If
    End Function


    'SAP3.0 连接SAP 
    Function ConnectSAP() As Boolean
        Dim GetSapValue = System.Configuration.ConfigurationManager.AppSettings("SAP_Destination")
        Dim ID As IDestinationConfiguration = New SAPRFC()         '继承
        Try
            RfcDestinationManager.RegisterDestinationConfiguration(ID)
            If GetSapValue = "QAS" Then
                Destination = RfcDestinationManager.GetDestination("qas_000")          '连接SAP, SAPConnector 3.0方式 QAS SYSTEM


            ElseIf GetSapValue = "PRDALL" Then
                Destination = RfcDestinationManager.GetDestination("prd_000")          '连接SAP, SAPConnector 3.0方式 PRD SYSTEM
            End If
            RfcDestinationManager.UnregisterDestinationConfiguration(ID)
            Return True
        Catch ex As Exception
            Loger.Error(ex.Message.ToString)
            Return False
        End Try


    End Function


    Public Function GetDataTableFromRfcTable(ByVal rfcTable As IRfcTable) As DataTable
        Dim dt As New DataTable()
        If rfcTable Is Nothing Then
            dt = Nothing
        Else
            Dim liElement As Integer = 0
            Dim rfcEMD As RfcElementMetadata
            Dim dr As DataRow
            Dim row As IRfcStructure
            For liElement = 0 To rfcTable.ElementCount - 1
                rfcEMD = rfcTable.GetElementMetadata(liElement)
                dt.Columns.Add(rfcEMD.Name)
            Next
            For Each row In rfcTable
                dr = dt.NewRow()
                For liElement = 0 To rfcTable.ElementCount - 1
                    rfcEMD = rfcTable.GetElementMetadata(liElement)
                    dr(rfcEMD.Name) = row.GetString(rfcEMD.Name)
                Next
                dt.Rows.Add(dr)
            Next
        End If
        Return dt
    End Function


    Public Sub TEST()
        Try
            Destination = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("BAPI_EXCHANGERATE_GETDETAIL")     '建立一个函数对象
                SapFunction.SetValue("RATE_TYPE", "M")                             '传递参数
                SapFunction.SetValue("FROM_CURR", "RMB")
                SapFunction.SetValue("TO_CURRNCY", "USD")
                SapFunction.SetValue("DATE", "2012-05-03")


                SapFunction.Invoke(Destination)                                        '激活提交
                mystruct = SapFunction.GetStructure("EXCH_RATE")
                Dim Exch_Rate As String = mystruct.GetValue("EXCH_RATE").ToString()
                ' GetTable = SapFunction.GetTable("YTABLE")                   '返回表 The backend has added one more line to it.
            End If
        Catch ex As Exception
            Loger.Error(ex.Message.ToString)
        Finally
            Destination = Nothing
        End Try
    End Sub


    Public Function ReadPlantLocationFromSap() As DataTable
        Try
            datatbl = Nothing
            Destination = Nothing
            SapFunction = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                'If SapFunction Is Nothing Then
                '    Exit Function
                'End If


                SapFunction.SetValue("QUERY_TABLE", "T001L")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WERKS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "LGORT")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "WERKS = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND DISKZ = '1'")
                End If




                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function GetSapinventory(ByRef partnum As ArrayList) As DataTable
        Dim strGJAHR As String = ""
        Dim strOption As String = ""
        Try
            strGJAHR = CStr(Year(Today))


            Dim cmd As New DataBean
            Dim ds As New DataSet
            Dim str As String = "select location from kaizan_transfer"


            cmd.ConnOracle()
            ds = cmd.GetDataSet(str)
            cmd.CloseConn()


            Destination = Nothing
            SapFunction = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                'If SapFunction Is Nothing Then
                '    Exit Function
                'End If


                SapFunction.SetValue("QUERY_TABLE", "MARD")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WERKS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "LABST")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "INSME")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "SPEME")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KLABS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KINSM")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KSPEM")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "WERKS = 'CSU1'")
                    strOption += "WERKS = 'CSU1'"
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND MATNR IN (''")
                    strOption += "AND MATNR IN (''"
                    'optionstbl.SetValue(0, "AND MATNR IN (")
                    'strOption += " AND MATNR IN ("
                    For i As Integer = 0 To partnum.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", partnum(i).ToString.Trim))
                        strOption += String.Format(",'{0}'", partnum(i).ToString.Trim)
                        'If i = partnum.Count - 1 Then  '最后一个PartNO
                        '    optionstbl.SetValue(0, String.Format("'{0}'", partnum(i).ToString.Trim))
                        '    strOption += String.Format("'{0}'", partnum(i).ToString.Trim)
                        'Else
                        '    optionstbl.SetValue(0, String.Format("'{0}',", partnum(i).ToString.Trim))
                        '    strOption += String.Format("'{0}',", partnum(i).ToString.Trim)
                        'End If
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                    strOption += ")"
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND LGORT NOT IN (''")
                    strOption += "AND LGORT NOT IN (''"
                    'optionstbl.SetValue(0, "AND LGORT NOT IN (")
                    'strOption += " AND LGORT NOT IN ("
                    For J As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim))
                        strOption += String.Format(",'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim)
                        'If J = ds.Tables(0).Rows.Count - 1 Then  '最后一个lOCATION
                        '    optionstbl.SetValue(0, String.Format("'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim))
                        '    strOption += String.Format("'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim)
                        'Else
                        '    optionstbl.SetValue(0, String.Format("'{0}',", ds.Tables(0).Rows(J).Item(0).ToString.Trim))
                        '    strOption += String.Format("'{0}',", ds.Tables(0).Rows(J).Item(0).ToString.Trim)
                        'End If
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                    strOption += ")"
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function ReadPoFromSap(ByVal partnum As ArrayList) As DataTable
        Try
            Destination = Nothing
            SapFunction = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "AFPO")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "PWERK")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "AUFNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "PSMNG")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WEMNG")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "IAMNG")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "MATNR IN (''")
                    For i As Integer = 0 To partnum.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", partnum(i).ToString.Trim))
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND DAUTY = '10'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND PWERK = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND ELIKZ <> 'X'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND DNREL <> 'X'")
                End If




                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function ReadprepackFromSap(ByVal partnum As ArrayList) As DataTable
        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                Dim cmd As New DataBean
                Dim ds As New DataSet
                Dim str As String = "select location from kaizan_transfer"
                cmd.ConnOracle()
                ds = cmd.GetDataSet(str)
                cmd.CloseConn()


                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "MCHB")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WERKS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "LGORT")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CHARG")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CLABS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CINSM")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CSPEM")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "MATNR IN (''")
                    For i As Integer = 0 To partnum.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", partnum(i).ToString.Trim))
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND WERKS = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND ( CLABS > 0")
                    optionstbl.Append()
                    optionstbl.SetValue(0, " OR CINSM > 0")
                    optionstbl.Append()
                    optionstbl.SetValue(0, " OR CSPEM > 0 )")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND LGORT NOT IN (''")
                    For J As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim))
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function GetSapprepack1() As DataTable
        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                Dim cmd As New DataBean
                Dim ds As New DataSet
                Dim str As String = "select location from kaizan_transfer"
                cmd.ConnOracle()
                ds = cmd.GetDataSet(str)
                cmd.CloseConn()


                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "MSKA")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WERKS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CHARG")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KALAB")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KAINS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "KASPE")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "LGORT")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "WERKS = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND ( KALAB > 0")
                    optionstbl.Append()
                    optionstbl.SetValue(0, " OR KAINS > 0")
                    optionstbl.Append()
                    optionstbl.SetValue(0, " OR KASPE > 0 )")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND LGORT NOT IN (''")
                    For J As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        optionstbl.Append()
                        optionstbl.SetValue(0, String.Format(",'{0}'", ds.Tables(0).Rows(J).Item(0).ToString.Trim))
                    Next
                    optionstbl.Append()
                    optionstbl.SetValue(0, ")")
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function callBAPI_GOODSMVT_GETITEMS(ByRef fromdate As String, ByRef todate As String) As DataSet
        Dim ds As New DataSet
        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("BAPI_GOODSMVT_GETITEMS")     '建立一个函数对象
                'Dim API As IRfcFunction = Destination.Repository.CreateFunction("BAPIALM_ORDERHEAD_GET_LIST")
                'Dim myStructure As IRfcStructure = Destination.Repository.GetStructureMetadata("BAPI2017_GM_PSTNG_DATE_RATable").CreateStructure
                'myStructure.SetValue("FIELD_NAME", "OPTIONS_FOR_DOC_TYPE")
                'myStructure.SetValue("OPTION", "EQ")
                'myStructure.SetValue("LOW_VALUE", "ZM01")
                'Dim myTable As IRfcTable = API.GetTable("IT_RANGES")
                'myTable.Append(myStructure)
                'API.SetValue("IT_RANGES", myTable)
                'API.Invoke(Destination)




                '建立Material_Ra
                Dim materialtable As IRfcTable
                materialtable = SapFunction.GetTable("MATERIAL_RA")
                materialtable.Append()
                materialtable.SetValue("LOW", "XFP10GS")
                materialtable.Append()
                materialtable.SetValue("LOW", "XFP10GEOC192IR2")
                materialtable.Append()
                materialtable.SetValue("LOW", "JN-BASE-B")


                '建立Plant_Ra
                Dim planttable As IRfcTable
                planttable = SapFunction.GetTable("PLANT_RA")
                planttable.Append()
                planttable.SetValue("LOW", "CSU1")


                '建立Move_Type_Ra
                Dim movetypetable As IRfcTable
                movetypetable = SapFunction.GetTable("MOVE_TYPE_RA")
                movetypetable.Append()
                movetypetable.SetValue("LOW", "601")
                movetypetable.Append()
                movetypetable.SetValue("LOW", "602")


                '建立Pstng_Date_Ra
                Dim PstngDatetable As IRfcTable
                PstngDatetable = SapFunction.GetTable("PSTNG_DATE_RA")
                PstngDatetable.Append()
                PstngDatetable.SetValue("SIGN", "I")
                PstngDatetable.SetValue("OPTION", "BT")
                'PstngDatetable.SetValue("LOW", "20120101")
                'PstngDatetable.SetValue("HIGH", "20120331")
                PstngDatetable.SetValue("LOW", fromdate)
                PstngDatetable.SetValue("HIGH", todate)


                SapFunction.SetValue("MATERIAL_RA", materialtable)
                SapFunction.SetValue("MOVE_TYPE_RA", movetypetable)
                SapFunction.SetValue("PLANT_RA", planttable)
                SapFunction.SetValue("PSTNG_DATE_RA", PstngDatetable)




                SapFunction.Invoke(Destination) '激活提交
                Dim GOODSMVT_ITEMS, GOODSMVT_HEADER As IRfcTable
                GOODSMVT_HEADER = SapFunction.GetTable("GOODSMVT_HEADER")
                GOODSMVT_ITEMS = SapFunction.GetTable("GOODSMVT_ITEMS")


                Dim dthead, dtitem As DataTable
                dthead = GetDataTableFromRfcTable(GOODSMVT_HEADER)
                dtitem = GetDataTableFromRfcTable(GOODSMVT_ITEMS)
                ds.Tables.Add(dthead)
                ds.Tables.Add(dtitem)
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return ds
    End Function


    Public Function GetSapbom1(ByRef partnum As String, ByRef batch As String, ByRef specialstock As String) As DataTable


        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "MSEG")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                SapFunction.SetValue("ROWCOUNT", "1")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "WERKS")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "CHARG")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "SOBKZ")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "AUFNR")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "WERKS = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND BWART = '101'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND CHARG = '" & batch & "'")
                    If specialstock <> "" Then
                        optionstbl.Append()
                        optionstbl.SetValue(0, "AND SOBKZ = '" & specialstock & "'")
                    End If
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND MATNR = '" & partnum & "'")
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function GetSapbom2(ByRef ponum As String) As DataTable
        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "AFKO")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "AUFNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "RSNUM")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "GAMNG")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AUFNR = '" & ponum & "'")
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function


    Public Function GetSapbom3(ByRef ponum As String, ByRef rsnum As String) As DataTable
        Try
            SapFunction = Nothing
            Destination = Nothing
            datatbl = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("RFC_READ_TABLE")     '建立一个函数对象
                SapFunction.SetValue("QUERY_TABLE", "RESB")
                SapFunction.SetValue("DELIMITER", ";")
                'SapFunction.SetValue("NO_DATA", "X")
                fieldtbl = SapFunction.GetTable("FIELDS")
                If fieldtbl.ElementCount = 0 Then
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "RSNUM")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "MATNR")
                    fieldtbl.Append()
                    fieldtbl.SetValue(0, "BDMNG")
                End If
                optionstbl = SapFunction.GetTable("OPTIONS")
                If optionstbl.ElementCount = 0 Then
                    optionstbl.Append()
                    optionstbl.SetValue(0, "RSNUM = '" & rsnum & "'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND BDART = 'AR'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND WERKS = 'CSU1'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND AUFNR = '" & ponum & "'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND BDMNG > 0")
                    optionstbl.Append()
                    optionstbl.SetValue(0, "AND ( XLOEK <> 'X'")
                    optionstbl.Append()
                    optionstbl.SetValue(0, " OR ENMNG > 0 )")
                End If
                SapFunction.Invoke(Destination)             '激活提交
                datatbl = SapFunction.GetTable("DATA")     '返回表 
            End If
        Catch ex As Exception
            datatbl = Nothing
            optionstbl = Nothing
            fieldtbl = Nothing
            Loger.Error(ex.Message.ToString)
        Finally
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcTable(datatbl)
    End Function
End Class
原创粉丝点击