一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码

注明:有abap c# 2种开发语言的接口

 1.首先进入sap系统

建立function

ZTEST_SIMPLE_02

一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第1张图片一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第2张图片一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第3张图片一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第4张图片一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第5张图片一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第6张图片

源代码

data: maxprice1 like sflight-price.
DATA: LSBL3(10) .                                           "临时变量3
data HS1 TYPE I.
DATA: l_msg(50).
TYPES: BEGIN OF tp_out.
TYPES: YGMC TYPE KNA1-NAME1.
TYPES: BM TYPE CSKT-KTEXT.  "部门
TYPES: YFXJ TYPE ZFIT_GZYSJ01-SJ1.  "应发小计
TYPES: END OF tp_out.
DATA:
gt_out TYPE TABLE OF tp_out WITH HEADER LINE.
DATA gt_wa like line of gt_out.
DATA: gt_outHB TYPE TABLE OF tp_out WITH HEADER LINE.
data wb type char20.
**********定义一个BDC内表***********************
DATA g_itb_bdcdata TYPE TABLE OF bdcdata.
DATA g_wa_bdcdata LIKE LINE OF g_itb_bdcdata.
data p_BUKRS LIKE ZFIT_GZYSJ02-BUKRS  .
data p_GJAHR LIKE ZFIT_GZYSJ02-GJAHR .
data p_MONAT LIKE ZFIT_GZYSJ02-MONAT.


FUNCTION ZTEST_SIMPLE_02.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(CARRID) LIKE  SFLIGHT-CARRID
*"     VALUE(CURRENCY) TYPE  S_CURRCODE OPTIONAL
*"  EXPORTING
*"     VALUE(PRICE) TYPE  S_PRICE
*"     VALUE(MESSAGE) TYPE  BUTXT
*"  TABLES
*"      AIRDATA STRUCTURE  SFLIGHT
*"  EXCEPTIONS
*"      UNIT_NOT_FOUND
*"----------------------------------------------------------------------
  select * from sflight into table airdata where carrid = carrid and currency = currency.

  if sy-subrc <> 0.
    raise unit_not_found.
  else.
    perform getmaxprice1 tables airdata.
    price = maxprice1.
    MESSAGE = l_msg.
  endif.

ENDFUNCTION.
*&---------------------------------------------------------------------*
*&      Form  getmaxprice1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->L_TAB      text
*----------------------------------------------------------------------*
form getmaxprice1 tables l_tab structure sflight.
  read table  l_tab  index 1.
  maxprice1 = l_tab-price.
  perFORM frm_post_fp.

endform.                    "getmaxprice1

*&---------------------------------------------------------------------*
*&      Form  frm_bdc_input
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P1         text
*      -->P2         text
*      -->P3         text
*----------------------------------------------------------------------*
FORM frm_bdc_input USING p1 p2 p3.
  CLEAR g_wa_bdcdata.
  IF p1 = 'X'.
    g_wa_bdcdata-program = p2.
    g_wa_bdcdata-dynpro = p3.
    g_wa_bdcdata-dynbegin = p1.
    APPEND g_wa_bdcdata TO g_itb_bdcdata.
  ELSE.
    g_wa_bdcdata-fnam = p2.
    g_wa_bdcdata-fval = p3.
    APPEND g_wa_bdcdata TO g_itb_bdcdata.
  ENDIF.
ENDFORM.                    "frm_bdc_input
*&---------------------------------------------------------------------*
*&      Form  frm_post_fp
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_post_fp.
  p_BUKRS = '1000'.
  p_GJAHR = '2009'.
  p_MONAT = '10'.
  DATA l_itb_message TYPE TABLE OF bdcmsgcoll.
  DATA l_wa_message LIKE LINE OF l_itb_message.
  DATA l_message TYPE char100.
  DATA l_date TYPE bkpf-budat.
  DATA l_wrbtr TYPE char16.
  DATA l_str   TYPE string.
  DATA l_mode TYPE c VALUE 'N'.
*  DATA l_mode TYPE c VALUE 'A'.
*  DATA l_mode TYPE c VALUE 'E'.
  DATA: l_datum TYPE datum.
  CONCATENATE '这是用rfc处理的' p_MONAT '月的凭证' into wb.
  l_datum+0(4) = p_GJAHR.
  l_datum+4(2) = p_MONAT.
  l_datum+6(2) = '15'.
  IF GT_outhb[] IS INITIAL.
  CLEAR:g_wa_bdcdata,g_itb_bdcdata.
*先设置科目编码
    LSBL3 = '6601050000'."记住科目编码的状态变式非常重要
    l_wrbtr = '100.00'.
    PERFORM frm_bdc_input USING: 'X' 'SAPLF040'     '0100',
                                 '' 'BDC_OKCODE'   '/00',
                                 ''  'BKPF-BLDAT'   l_datum,
                                 ''  'BKPF-BLART'   'SA',
                                 ''  'BKPF-BUKRS'   p_BUKRS,   "输入公司代码
                                ''  'BKPF-BUDAT'   l_datum,    "日期
                                 ''  'BKPF-WAERS'   'RMB',
                                  ''  'BKPF-BKTXT'  wb  ,       "文本抬头
                                 ''  'VBKPF-XBWAE'  'X',
                                 ''  'FS006-DOCID'  '*'.
    PERFORM frm_bdc_input USING: ''  'RF05V-NEWBS'  '40'.
    PERFORM frm_bdc_input USING: ''  'RF05V-NEWKO'  LSBL3.    "科目编码
    PERFORM frm_bdc_input USING: 'X' 'SAPLF040'     '0300',
                                 ''  'BDC_OKCODE'   '=ZK',
                                 ''  'BSEG-WRBTR'   l_wrbtr,    "金额为文本格式
                                 'X' 'SAPLKACB'     '0002',
                                 ''        'BDC_OKCODE'   '/EESC',
                                 ''        'BDC_CURSOR'   'COBL-KOSTL',
                                 'X' 'SAPLF040'     '0330',
                                 ''        'BDC_CURSOR'   'BSEG-VBUND',
                                 ''        'BDC_OKCODE'   '=ZK',
                                 'X' 'SAPLF040'     '0300',
                                 ''  'BDC_OKCODE'   '=ZK',
                                 'X' 'SAPLKACB'     '0002',
                                 ''       'BDC_OKCODE'   '=ENTE',
                                 ''    'COBL-KOSTL'   '1000002' , "成本中心
                                 'X'      'SAPLF040'     '0330',
                                 ''       'BDC_OKCODE'   '=S+',
*按员工核算则BSEG-XREF2不为空
*                               ''       'BSEG-XREF2'   gt_wa_out-kunnr.
                                 ''       'BSEG-XREF2'   ''.
    PERFORM frm_bdc_input USING: ''  'RF05V-NEWBS'  '50',
                                 ''  'RF05V-NEWKO'  '2211030101',
                                 'X' 'SAPLF040'     '0300',

                                 ''  'BDC_OKCODE'   '=AB',
                                 ''  'BSEG-WRBTR'   '*',
                                 'X' 'SAPLKACB'     '0002',

                                 ''  'BDC_OKCODE'   '=ENTE',
                                 'X' 'SAPLF040'     '0700',
                                 ''  'BDC_CURSOR'   'BKPF-XBLNR',
                                 ''  'BDC_OKCODE'   '=PBBP'.
    "break-point.
    CALL TRANSACTION 'F-65' USING g_itb_bdcdata MODE l_mode MESSAGES INTO l_itb_message.
    READ TABLE l_itb_message INTO l_wa_message WITH KEY  msgtyp = 'S' msgid = 'FP' msgnr = '001'.
    IF sy-subrc = 0.
      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = l_wa_message-msgid
          lang      = '1'
          no        = l_wa_message-msgnr
          v1        = l_wa_message-msgv1
          v2        = l_wa_message-msgv2
          v3        = l_wa_message-msgv3
          v4        = l_wa_message-msgv4
        IMPORTING
          msg       = l_msg
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
*      WRITE:/ l_msg.
    ELSE.
      LOOP AT l_itb_message INTO l_wa_message WHERE  msgtyp = 'E' OR  msgtyp = 'A'.
        CALL FUNCTION 'FORMAT_MESSAGE'
          EXPORTING
            id        = l_wa_message-msgid
            lang      = '1'
            no        = l_wa_message-msgnr
            v1        = l_wa_message-msgv1
            v2        = l_wa_message-msgv2
            v3        = l_wa_message-msgv3
            v4        = l_wa_message-msgv4
          IMPORTING
            msg       = l_msg
          EXCEPTIONS
            not_found = 1
            OTHERS    = 2.
*        WRITE:/ l_msg.
      ENDLOOP.
    ENDIF.
  ELSE.
*    WRITE:/ '没有需要结转的数据'.
  ENDIF.
ENDFORM.                    " FRM_POST

 2.然后进入c#vs2010系统

新建c#-项目

新建form2个label、1个textbox

form后台代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SAP.Middleware.Connector;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RfcConfigParameters rfcPar = new RfcConfigParameters();
            rfcPar.Add(RfcConfigParameters.Name, "dxx");
            rfcPar.Add(RfcConfigParameters.AppServerHost, "11x.xs.ee.45");
            rfcPar.Add(RfcConfigParameters.Client, "660");
            rfcPar.Add(RfcConfigParameters.User, "FI2sd");
            rfcPar.Add(RfcConfigParameters.Password, "734569");
            rfcPar.Add(RfcConfigParameters.SystemNumber, "00");
            rfcPar.Add(RfcConfigParameters.Language, "EN");
            RfcDestination dest = RfcDestinationManager.GetDestination(rfcPar);
            RfcRepository rfcrep = dest.Repository;
            IRfcFunction myfun = null;
            myfun = rfcrep.CreateFunction("ZTEST_SIMPLE_02");
            myfun.SetValue("CARRID", "AA");//SAP里面的传入参数
            myfun.SetValue("CURRENCY", "USD");//SAP里面的传入参数
            myfun.Invoke(dest);

            string ERMSG = myfun.GetValue("PRICE").ToString();

            string message = myfun.GetValue("MESSAGE").ToString();

            this.label1.Text = ERMSG;
            this.label2.Text = message;

            dest = null;
            rfcrep = null;

            IRfcTable IrfTable = myfun.GetTable("AIRDATA");
            //提前实例化一个空的表结构出来
            DataTable dt = new DataTable();
            dt.Columns.Add("CARRID");
            dt.Columns.Add("CONNID");
            dt.Columns.Add("FLDATE");
            for (int i = 0; i < IrfTable.Count; i++)
            {
                IrfTable.CurrentIndex = i;
                DataRow dr = dt.NewRow();
                dr["CARRID"] = IrfTable.GetString("CARRID");
                dr["CONNID"] = IrfTable.GetString("CONNID");
                dr["FLDATE"] = IrfTable.GetString("FLDATE");
                textBox1.AppendText(IrfTable.GetString("CARRID")+IrfTable.GetString("CONNID")+IrfTable.GetString("FLDATE")+'\n');
                    dt.Rows.Add(dr);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

执行效果:

一套通过c# sap-rfc 完整处理一个bdc凭证的程序代码_第7张图片

 

你可能感兴趣的:(ABAP)