*-----------BAPI参数定义 DATA:ls_documentheader TYPE bapiache09, lt_accountgl TYPE STANDARD TABLE OF bapiacgl09, " 总账 lt_accountreceivable TYPE STANDARD TABLE OF bapiacar09, " 客户 lt_accountpayable TYPE STANDARD TABLE OF bapiacap09, " 供应商 lt_currencyamount TYPE STANDARD TABLE OF bapiaccr09, " 金额 lt_extension2 TYPE STANDARD TABLE OF bapiparex, lt_return TYPE STANDARD TABLE OF bapiret2, lt_bsed TYPE TABLE OF zsfi0030, ls_zsfi0030 TYPE zsfi0030. DATA: BEGIN OF ls_key, belnr LIKE bkpf-belnr, "会计凭证号码 bukrs LIKE bkpf-bukrs, "公司代码 gjahr LIKE bkpf-gjahr, "财年 xblnr LIKE bkpf-xblnr, "参考凭证号 END OF ls_key. DATA:ls_enh2 TYPE zsfi_fidoc_enh2. DATA:lv_obj_type LIKE bapiache09-obj_type, lv_obj_key LIKE bapiache09-obj_key, lv_obj_sys LIKE bapiache09-obj_sys. DATA:ls_accountgl TYPE bapiacgl09, " 总账 ls_accountreceivable TYPE bapiacar09, " 客户 ls_accountpayable TYPE bapiacap09, " 供应商 ls_currencyamount TYPE bapiaccr09, " 金额 ls_extension2 TYPE bapiparex, ls_return TYPE bapiret2, ls_extension TYPE zsfi_extension. DATA:lv_posnr TYPE posnr_acc, lv_wrbtr TYPE bseg-wrbtr, lv_lifnr TYPE bseg-lifnr, lv_kunnr TYPE bseg-kunnr, lv_sgtxt TYPE bseg-sgtxt, lv_ktogr TYPE anla-ktogr, lv_sgl_fld TYPE char1. *-----------抬头数据 ls_documentheader-comp_code = p_head-bukrs."公司代码 ls_documentheader-doc_date = p_head-bldat."凭证日期 ls_documentheader-pstng_date = p_head-budat."过账日期 ls_documentheader-doc_type = p_head-blart."凭证类型 ls_documentheader-fisc_year = p_head-gjahr."会计年度 ls_documentheader-fis_period = p_head-monat."会计期间 ls_documentheader-username = sy-uname."过账人 * ls_documentheader-username = p_head-usnam."过账人 ls_documentheader-header_txt = p_head-bktxt."抬头凭证文本 ls_documentheader-ref_doc_no = p_head-xblnr."共享凭证号 ls_documentheader-obj_type = 'BKPFF'. "参考过程 ls_documentheader-bus_act = 'RFBU'. "业务事务 *---------行项目赋值 SORT t_item BY buzei. LOOP AT t_item INTO DATA(ls_item). CLEAR lv_posnr. ls_item-lifnr = |{ ls_item-lifnr ALPHA = IN }|. ls_item-hkont = |{ ls_item-hkont ALPHA = IN }|. ls_item-kostl = |{ ls_item-kostl ALPHA = IN }|. ls_item-aufnr = |{ ls_item-aufnr ALPHA = IN }|. ls_item-kunnr = |{ ls_item-kunnr ALPHA = IN }|. ls_item-anln1 = |{ ls_item-anln1 ALPHA = IN }|. ls_item-anln2 = |{ ls_item-anln2 ALPHA = IN }|. ls_item-prctr = |{ ls_item-prctr ALPHA = IN }|. lv_posnr = ls_item-buzei. lv_posnr = |{ ls_item-buzei ALPHA = IN }|. IF ls_item-bschl >= '01' AND ls_item-bschl <= '20'. *-------客户 CLEAR ls_accountreceivable. IF ls_item-kunnr IS NOT INITIAL. ls_accountreceivable-itemno_acc = lv_posnr. "行项目号 ls_accountreceivable-customer = ls_item-kunnr."客户号 ls_accountreceivable-gl_account = ls_item-hkont. "付款科目 * ls_accountreceivable-bus_area = co_bus_area. "业务范围 ls_accountreceivable-pmnttrms = ls_item-zterm. "付款条件 ls_accountreceivable-bline_date = ls_item-zfbdt. "基准日期 ls_accountreceivable-dsct_days1 = ls_item-zbd1t."现在折扣天数1 ls_accountreceivable-dsct_days2 = ls_item-zbd2t."现在折扣天数2 ls_accountreceivable-netterms = ls_item-zbd3t."现在折扣天数3 ls_accountreceivable-dsct_pct1 = ls_item-zbd1p."现金折扣百分率1 ls_accountreceivable-dsct_pct2 = ls_item-zbd2p."现在折扣百分率2 ls_accountreceivable-pymt_meth = ls_item-zlsch."付款方式 ls_accountreceivable-pmnt_block = ls_item-zlspr."冻结付款 ls_accountreceivable-paymt_ref = ls_item-kidno. ls_accountreceivable-sp_gl_ind = ls_item-umskz. "特别总账标记 ls_accountreceivable-ref_key_1 = ls_item-xref1. ls_accountreceivable-ref_key_2 = ls_item-xref2. ls_accountreceivable-ref_key_3 = ls_item-xref3. ls_accountreceivable-alloc_nmbr = ls_item-zuonr."分配 ls_accountreceivable-item_text = ls_item-sgtxt. "项目文本 ls_accountreceivable-profit_ctr = ls_item-prctr."利润中心 APPEND ls_accountreceivable TO lt_accountreceivable. ENDIF . ELSEIF ls_item-bschl >= '21' AND ls_item-bschl <= '39'. CLEAR ls_accountpayable. *---------供应商 IF ls_item-lifnr IS NOT INITIAL. ls_accountpayable-itemno_acc = lv_posnr. "行项目号 ls_accountpayable-vendor_no = ls_item-lifnr. "供应商号 ls_accountpayable-gl_account = ls_item-hkont. "付款科目 * ls_accountpayable-bus_area = co_bus_area. "业务范围 ls_accountpayable-pmnttrms = ls_item-zterm. "付款条件 ls_accountpayable-dsct_days1 = ls_item-zbd1t."现在折扣天数1 ls_accountpayable-dsct_days2 = ls_item-zbd2t."现在折扣天数2 ls_accountpayable-netterms = ls_item-zbd3t."现在折扣天数3 ls_accountpayable-dsct_pct1 = ls_item-zbd1p."现金折扣百分率1 ls_accountpayable-dsct_pct2 = ls_item-zbd2p."现在折扣百分率2 ls_accountpayable-pymt_meth = ls_item-zlsch."付款方式 ls_accountpayable-pmnt_block = ls_item-zlspr."冻结付款 ls_accountpayable-sp_gl_ind = ls_item-umskz. "特别总账标记 ls_accountpayable-ref_key_1 = ls_item-xref1. ls_accountpayable-ref_key_2 = ls_item-xref2. ls_accountpayable-ref_key_3 = ls_item-xref3. ls_accountpayable-alloc_nmbr = ls_item-zuonr."分配 ls_accountpayable-item_text = ls_item-sgtxt. "项目文本 ls_accountpayable-profit_ctr = ls_item-prctr."利润中心 APPEND ls_accountpayable TO lt_accountpayable. ENDIF. ELSEIF ls_item-bschl = '40' OR ls_item-bschl = '50' OR ls_item-bschl = '80' OR ls_item-bschl = '81' OR ls_item-bschl = '83' OR ls_item-bschl = '84' OR ls_item-bschl = '85' OR ls_item-bschl = '86' OR ls_item-bschl = '89' OR ls_item-bschl = '90' OR ls_item-bschl = '91' OR ls_item-bschl = '93' OR ls_item-bschl = '94' OR ls_item-bschl = '95' OR ls_item-bschl = '96' OR ls_item-bschl = '99' . *-------总账 CLEAR ls_accountgl. IF ls_item-hkont IS NOT INITIAL. * ls_accountgl-bus_area = co_bus_area. "业务范围 ls_accountgl-itemno_acc = lv_posnr. "行项目号 ls_accountgl-gl_account = ls_item-hkont. "科目 ls_accountgl-orderid = ls_item-aufnr."内部订单 ls_accountgl-costcenter = ls_item-kostl."成本中心 ls_accountgl-item_text = ls_item-sgtxt. "项目文本 ls_accountgl-ref_key_1 = ls_item-xref1. ls_accountgl-ref_key_2 = ls_item-xref2. ls_accountgl-ref_key_3 = ls_item-xref3. ls_accountgl-alloc_nmbr = ls_item-zuonr."分配 ls_accountgl-trade_id = ls_item-vbund. ls_accountgl-profit_ctr = ls_item-prctr."利润中心 APPEND ls_accountgl TO lt_accountgl. ENDIF. ELSEIF ls_item-bschl = '70' OR ls_item-bschl = '75' . * 固定资产 CLEAR ls_accountgl. ls_accountgl-itemno_acc = lv_posnr. "行项目号 IF ls_item-bschl = '70'. CLEAR lv_ktogr. SELECT SINGLE ktogr INTO lv_ktogr FROM anla WHERE anln1 = ls_item-anln1 AND bukrs = p_head-bukrs. "--资产科目 SELECT SINGLE ktansw INTO ls_accountgl-gl_account FROM t095 WHERE ktopl = 'JK00' AND ktogr = lv_ktogr. ENDIF. * ls_accountgl-gl_account = ls_item-hkont. "科目 * ls_accountgl-bus_area = co_bus_area. "业务范围 ls_accountgl-asset_no = ls_item-anln1. ls_accountgl-sub_number = ls_item-anln2. ls_accountgl-item_text = ls_item-sgtxt. "项目文本 ls_accountgl-acct_type = 'A'. ls_accountgl-orderid = ls_item-aufnr."内部订单 ls_accountgl-costcenter = ls_item-kostl. ls_accountgl-ref_key_1 = ls_item-xref1. ls_accountgl-ref_key_2 = ls_item-xref2. ls_accountgl-ref_key_3 = ls_item-xref3. ls_accountgl-alloc_nmbr = ls_item-zuonr."分配 ls_accountgl-trade_id = ls_item-vbund. ls_accountgl-profit_ctr = ls_item-prctr."利润中心 APPEND ls_accountgl TO lt_accountgl. ENDIF. "金额 CLEAR:ls_currencyamount. ls_currencyamount-itemno_acc = lv_posnr. "行项目 ls_currencyamount-currency = p_head-waers. "货币 ls_currencyamount-amt_doccur = ls_item-dmbtr."交易货币金额 *---------------S/H 借贷关系转换 IF ls_item-bschl = '01' OR ls_item-bschl = '02' OR ls_item-bschl = '03' OR ls_item-bschl = '04' OR ls_item-bschl = '05' OR ls_item-bschl = '06' OR ls_item-bschl = '07' OR ls_item-bschl = '08' OR ls_item-bschl = '09' OR ls_item-bschl = '0A' OR ls_item-bschl = '0B' OR ls_item-bschl = '0C' OR ls_item-bschl = '0X' OR ls_item-bschl = '0Y' OR ls_item-bschl = '0Z' OR ls_item-bschl = '21' OR ls_item-bschl = '22' OR ls_item-bschl = '24' OR ls_item-bschl = '25' OR ls_item-bschl = '26' OR ls_item-bschl = '27' OR ls_item-bschl = '28' OR ls_item-bschl = '29' OR ls_item-bschl = '40' OR ls_item-bschl = '70' OR ls_item-bschl = '80' OR ls_item-bschl = '81' OR ls_item-bschl = '83' OR ls_item-bschl = '84' OR ls_item-bschl = '85' OR ls_item-bschl = '86' OR ls_item-bschl = '89' . ls_currencyamount-amt_doccur = abs( ls_currencyamount-amt_doccur ). ELSE. ls_currencyamount-amt_doccur = ( -1 ) * abs( ls_currencyamount-amt_doccur ). ENDIF. IF p_head-kursf <> ''. ls_currencyamount-exch_rate = p_head-kursf. "汇率 ls_currencyamount-disc_amt = ls_currencyamount-amt_doccur * p_head-kursf."* ELSE. ls_currencyamount-disc_amt = ls_currencyamount-amt_doccur ."* ENDIF. APPEND ls_currencyamount TO lt_currencyamount. CLEAR ls_zsfi0030. IF ls_item-umskz = 'W'. lv_sgl_fld = 'W'. EXPORT lv_sgl_fld TO MEMORY ID 'LV_SGL_FLD'. ls_zsfi0030 = VALUE #( posnr = lv_posnr bschl = ls_item-bschl rstgr = ls_item-rstgr xnegp = ls_item-xnegp wbank = ls_item-wbank ). APPEND ls_zsfi0030 TO lt_bsed. ENDIF. CLEAR ls_enh2. ls_enh2-posnr = lv_posnr. ls_enh2-bschl = ls_item-bschl. ls_enh2-rstgr = ls_item-rstgr."原因代码 ls_enh2-xnegp = ls_item-xnegp. ls_enh2-kidno = ls_item-kidno. ls_enh2-umskz = ls_item-umskz. ls_enh2-anbwa = ls_item-anbwa. ls_enh2-zzlzbkz = ls_item-zzlzbkz. ls_item-zzcustomer = |{ ls_item-zzcustomer ALPHA = IN }|. ls_enh2-zzcustomer = ls_item-zzcustomer. ls_item-zzvendor = |{ ls_item-zzvendor ALPHA = IN }|. ls_enh2-zzvendor = ls_item-zzvendor. ls_extension2-structure = 'ZSFI_FIDOC_ENH2'. ls_extension2-valuepart1 = ls_enh2. APPEND ls_extension2 TO lt_extension2. CLEAR ls_extension2. IF ls_item-zzarea1 <> ''. ls_extension-posnr = lv_posnr. ls_extension-zzarea1 = ls_item-zzarea1. ls_extension2-structure = 'ZSFI_EXTENSION'. ls_extension2-valuepart1 = ls_extension. APPEND ls_extension2 TO lt_extension2. CLEAR ls_extension2. ENDIF. CLEAR ls_item. ENDLOOP. EXPORT lt_bsed TO MEMORY ID 'ZBSED'. "过账校验函数 CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK' EXPORTING documentheader = ls_documentheader TABLES accountgl = lt_accountgl accountreceivable = lt_accountreceivable accountpayable = lt_accountpayable currencyamount = lt_currencyamount extension2 = lt_extension2 return = lt_return. "如果返回有错误,则直接不用执行过账函数,返回错误消息至FSSC CLEAR ls_return. READ TABLE lt_return INTO ls_return WITH KEY type = 'E'. IF sy-subrc = 0. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. c_return-xblnr = p_head-xblnr. c_return-ygjahr = p_head-gjahr. c_return-type = 'E'. CLEAR ls_return. LOOP AT lt_return INTO ls_return WHERE type = 'E'. c_return-message = c_return-message && ls_return-message && ','. CLEAR ls_return. ENDLOOP. ELSE. "如果检查无错误,则可执行正式过账函数 CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. CLEAR lt_return. "凭证过账函数 CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST' EXPORTING documentheader = ls_documentheader IMPORTING obj_type = lv_obj_type obj_key = lv_obj_key obj_sys = lv_obj_sys TABLES accountgl = lt_accountgl accountreceivable = lt_accountreceivable accountpayable = lt_accountpayable currencyamount = lt_currencyamount extension2 = lt_extension2 return = lt_return. CLEAR ls_return. READ TABLE lt_return INTO ls_return WITH KEY type = 'E'. IF sy-subrc <> 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. ls_key = lv_obj_key. "过账成功返回成功的凭证编号 c_return-xblnr = p_head-xblnr. c_return-message = text-003."凭证过账成功 c_return-belnr = lv_obj_key+0(10). c_return-belnr = |{ c_return-belnr ALPHA = OUT }|. c_return-bukrs = ls_key-bukrs. c_return-ygjahr = p_head-gjahr. c_return-gjahr = ls_key-gjahr. c_return-type = 'S'. IF lv_obj_key IS NOT INITIAL. UPDATE bkpf SET ppnam = sy-uname "p_head-ppnam WHERE belnr = lv_obj_key+0(10) AND bukrs = ls_key-bukrs AND gjahr = ls_key-gjahr. IF sy-subrc = 0. COMMIT WORK. ELSE. ROLLBACK WORK. ENDIF. ENDIF. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. c_return-xblnr = p_head-xblnr. c_return-ygjahr = p_head-gjahr. c_return-type = 'E'. CLEAR ls_return. LOOP AT lt_return INTO ls_return WHERE type = 'E'. c_return-message = c_return-message && c_return-message && ','. CLEAR ls_return. ENDLOOP. ENDIF. ENDIF.
标签:02,BAPI,return,lv,item,lt,ls,TYPE From: https://www.cnblogs.com/yy-abaper/p/16726965.html