一,、創(chuàng)建憑證
主要是使用BAPI ‘BAPI_ACC_DOCUMENT_POST’創(chuàng)建憑證
由于’BAPI_ACC_DOCUMENT_POST‘對一些字段(如記賬碼,、我們自己增強的現(xiàn)金流量碼等)的傳遞使用擴展字段,,所以需要使用BAdI ‘ACC_DOCUMENT‘,。
BAdI ‘ACC_DOCUMENT‘的增強:
來自CSDN博客,,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/longgerr/archive/2010/10/22/5958210.aspx
1,、SE11創(chuàng)建結(jié)構(gòu)ZKS_OA_ACC_EXT
2、SE19 ,,實例化ACC_DOCUMENT– ZACC_DOCUMENT
參考交易使用BKPFF
3,、保存
如果保存時出現(xiàn)了下面這個消息,說明必須將做的Classic Badi遷移到New Badi,。
此時可以選擇已有的或創(chuàng)建新的Badi Enhancement Implementation,。我們選擇創(chuàng)建新的。
創(chuàng)建好之后,,系統(tǒng)會提示消息,。
4,、刪除原始Classic Badi
回到SE19。
如果進行了遷移,,則在雙擊新建的New Badi的時候會報出消息:
所以我們刪除我們之前創(chuàng)建的ZACC_DOCUMENT,,才能保證這個增強能夠被正常調(diào)用。
刪除Classic Badi時,,系統(tǒng)首先提示:
這個選擇Yes
隨后系統(tǒng)又提示:
這個提示的意思是詢問,,是否刪除從這個Classic Badi遷移過去的New Badi。顯然要保留,,這個選擇No啟用new badi
5、編輯代碼
在New Badi里面顯示ZACC_DOCUMENT,,此時將不會報出需要刪除Classic Badi的警告了,。
雙擊CHANGE方法即可進入編輯。
代碼釋義:
CHANGE方法中參數(shù)FLT_VAL,,C_ACCHD,,C_ACCIT,C_ACCCR,,C_ACCWT,,C_ACCTX均與憑證數(shù)據(jù)相關(guān)。
C_EXTENSION2中傳入的數(shù)據(jù)即為BAPI_ACC_DOCUMENT_POST中EXTENSION2參數(shù),。
BAPIPAREX-TE_STRUC為數(shù)據(jù)字典中結(jié)構(gòu)的名稱,,其余為擴展數(shù)據(jù)。
生成BAPI的EXTENTION2參數(shù)時,,就是按照ZKS_OA_ACC_EXT的數(shù)據(jù)結(jié)構(gòu)順序?qū)懭氲?,讀取時也按照相同的方式讀取即可。
根據(jù)參數(shù)里面的類型和說明來判斷某個參數(shù)對應(yīng)憑證中的什么數(shù)據(jù),,將BAPI中無法傳入的數(shù)據(jù)從EXTENTION2里面取出,,傳入CHANGE的參數(shù)里面即可。
參考代碼:
method IF_EX_ACC_DOCUMENT~CHANGE.
DATA: wa_extension TYPE bapiparex,
zext_data TYPE zks_oa_acc_ext.
FIELD-SYMBOLS: <accit> TYPE accit.
IF c_extension2[] IS NOT INITIAL.
READ TABLE c_extension2 INTO wa_extension WITH KEY structure = ’PARK’.”預(yù)制憑證
IF sy-subrc = 0.
MOVE ’2′ TO c_acchd-status_new.
DELETE c_extension2 INDEX sy-tabix.
ENDIF.
LOOP AT c_extension2 INTO wa_extension WHERE structure = ’ZKS_OA_ACC_EXT’.
zext_data = wa_extension-valuepart1.
READ TABLE c_accit ASSIGNING <accit> WITH KEY posnr = zext_data-posnr.
IF sy-subrc = 0.
<accit>-rstgr = zext_data-rstgr.
<accit>-bschl = zext_data-bschl.
<accit>-xref1_hd = zext_data-xref1_hd.
<accit>-zfbdt = zext_data-zfbdt.
<accit>-FKBER = zext_data-FKBER.
<accit>-ANBWA = zext_data-BWASL.
ENDIF.
ENDLOOP.
ENDIF.
endmethod. |
資產(chǎn)類憑證(AA)創(chuàng)建問題:
在使用BAPI_ACC_DOCUMENT_POST創(chuàng)建AA憑證時,,參數(shù)
OBJ_TYPE 參考交易
OBJ_KEY
OBJ_SYS
BUS_ACT業(yè)務(wù)事務(wù)
為必填,,根據(jù)SAP的回復(fù),OBJ_TYPE應(yīng)為自建類型,,使用SM30,,在表TTYP中拷貝行BKPF新建ZBKPF。注意:使用該參考交易的憑證無法使用FB08沖銷,。
Dear customer,
because I was not able to find an expert for BAPI area at this weekend Imyself tried to find out for you relevant information concerning the
above mentioned issue:
First I have lowered the priority of this message because the priority 1is intended for other problem cases -> please have a look at the
attached notes 19500 and 67739.
Secondly I assume that this is not a system error or program error but
rather a consulting issue.
However I will give you some hints:
Please see note 643232, there are several restrictions when you post an
asset transaction by means of a BAPI which is not from asset accounting:
1. You can only post acquisitions or credit memos to fixed asset.
2. You must maintain the acquisition value offsetting account in the
Customizing of the Asset Accounting although it is not posted.
3. You must use a customer-specific object type OBJ_TYPE that starts
with ” Z”.
4. The asset transaction type cannot be derived automatically, so it is absolutely necessary that you fill the line item using the BAPI (if
necessary via the extension method).
5. It is absolutely necessary to assign the company code currency and
possibly existing parallel currency values. The Asset Accounting
performs currency translations from transaction currencies.
The BAPI_ACC_DOCUMENT_POST (located in function group ACC9)
as a newer BAPI only uses BTE as exit technology, no SMOD/CMOD exits.
So you may copy the function module SAMPLE_INTERFACE_RWBAPI01
to something like ZRWBAPI01, then enter your program code
in ZRWBAPI01, activate it and then call transaction FIBF and make
there a new entry for event RWBAPI01 as a “process function
module of a customer” and enter ZRWBAPI01 as function module name.
You will also need to enter a product identifier here and make sure
that you activate the product in FIBF. |
以上參數(shù)在憑證頭中的位置,,其中BUS_ACT = ’RMWE’
測試發(fā)現(xiàn),不止AA類憑證需要這樣處理,,記帳碼的類型如果為A,,則也需要同樣處理。記帳碼的類型可以從表tbsl中獲得,。
參考代碼:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DATA t_tbsl LIKE TABLE OF tbsl WITH HEADER LINE.
DATA:ls_doc_head LIKE bapiache09,
lt_accountgl LIKE TABLE OF bapiacgl09 WITH HEADER LINE,
lt_amount LIKE TABLE OF bapiaccr09 WITH HEADER LINE,
lt_accountreceivable LIKE TABLE OF bapiacar09 WITH HEADER LINE,
lt_accountpayable LIKE TABLE OF bapiacap09 WITH HEADER LINE,
lt_return LIKE TABLE OF bapiret2 WITH HEADER LINE,
l_itemno LIKE bapiacgl09-itemno_acc,
l_trade_id LIKE lt_accountgl-trade_id.
DATA: wa_zks_oa_acc_ext LIKE zks_oa_acc_ext.
DATA: it_extension2 TYPE STANDARD TABLE OF bapiparex WITH HEADER LINE.
DATA: G_BELNR TYPE BSEG-BELNR,
G_GJAHR TYPE BSEG-GJAHR.
DATA: BEGIN OF IT_BSEG OCCURS 0,
BELNR TYPE BSEG-BELNR,
END OF IT_BSEG.
DATA: L_AWSYS TYPE BKPF-AWSYS,
l_OBJ_TYPE type BAPIACHE09-OBJ_TYPE,
l_OBJ_KEY type BAPIACHE09-OBJ_KEY,
l_OBJ_SYS type BAPIACHE09-OBJ_SYS.
DATA: RND_VALUE TYPE DATATYPE-CHAR0128.
DATA: WA_BSEG TYPE ZHEC_BSEG.
DEFINE SET_AA_HEAD. “ 資產(chǎn)類憑證的憑證頭的特殊處理
CALL FUNCTION ‘OWN_LOGICAL_SYSTEM_GET’ “當(dāng)前邏輯系統(tǒng)
IMPORTING
OWN_LOGICAL_SYSTEM = L_AWSYS
EXCEPTIONS
OWN_LOGICAL_SYSTEM_NOT_DEFINED = 1.
G_GJAHR = i_bkpf-pstng_date(4).
“ AA憑證需要獲得將生成憑證的憑證號 作為參考字段,。
SELECT BELNR FROM BKPF INTO TABLE IT_BSEG
WHERE BUKRS = i_bkpf-comp_code
AND GJAHR = G_GJAHR
AND BLART = I_BKPF-DOC_TYPE.
SORT IT_BSEG BY BELNR DESCENDING.
READ TABLE IT_BSEG INDEX 1.
IT_BSEG-BELNR = IT_BSEG-BELNR + 1.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
INPUT = IT_BSEG-BELNR
IMPORTING
OUTPUT = IT_BSEG-BELNR .
CALL FUNCTION ‘RANDOM_C’ “ 參考字段加上2位隨機碼與正常憑證區(qū)分
EXPORTING
LEN_MIN = 2
LEN_MAX = 2
CHAR_MAX = 27
IMPORTING
RND_VALUE = RND_VALUE
.
CONCATENATE IT_BSEG-BELNR i_bkpf-comp_code G_GJAHR RND_VALUE INTO ls_doc_head-OBJ_KEY.
ls_doc_head-OBJ_TYPE = ‘ZBKPF’.
* SHIFT ls_doc_head-OBJ_KEY LEFT DELETING LEADING SPACE.
ls_doc_head-OBJ_SYS = L_AWSYS.
ls_doc_head-BUS_ACT = ‘RMWE’.
END-OF-DEFINITION.
SELECT * INTO CORRESPONDING FIELDS OF TABLE t_tbsl FROM tbsl.
ls_doc_head-doc_type = i_bkpf-doc_type. “憑證類型
ls_doc_head-fis_period = i_bkpf-fis_period .“會計期間
ls_doc_head-doc_date = i_bkpf-doc_date. “憑證中的憑證日期
ls_doc_head-pstng_date = i_bkpf-pstng_date. “憑證中的記帳日期
ls_doc_head-header_txt = i_bkpf-header_txt. “ 憑證抬頭文本
ls_doc_head-ref_doc_no = i_bkpf-ref_doc_no. “參考憑證編號
ls_doc_head-comp_code = i_bkpf-comp_code. “公司代碼
ls_doc_head-username = i_bkpf-username. “用戶名
IF i_bkpf-doc_type = ‘AA’.
SET_AA_HEAD.
ENDIF.
SORT t_tbsl BY bschl.
LOOP AT t_bseg INTO WA_BSEG.“取供應(yīng)商客戶行貿(mào)易伙伴 供原因代碼為關(guān)聯(lián)方時候用
READ TABLE t_tbsl WITH KEY bschl = WA_bseg-bschl BINARY SEARCH.
IF sy-subrc = 0.
IF t_tbsl-koart = ‘K’.
SELECT SINGLE vbund INTO l_trade_id FROM lfa1 WHERE lifnr = WA_bseg-vendor_no
and vbund like ‘HZ%’.
EXIT.
ELSEIF t_tbsl-koart = ‘D’.
SELECT SINGLE vbund INTO l_trade_id FROM kna1 WHERE lifnr = WA_bseg-customer
and vbund like ‘HZ%’.
EXIT.
ENDIF.
ENDIF.
CLEAR WA_bseg.
ENDLOOP.
LOOP AT t_bseg INTO WA_BSEG.
CLEAR : lt_accountgl,lt_accountpayable, lt_accountreceivable.
ADD 10 TO l_itemno.
READ TABLE t_tbsl WITH KEY bschl = WA_bseg-bschl BINARY SEARCH.
IF sy-subrc = 0.
IF t_tbsl-koart = ‘S’.
lt_accountgl-itemno_acc = l_itemno. “會計憑證行項目編
lt_accountgl-gl_account = WA_bseg-gl_account.“總分類帳帳目
lt_accountgl-costcenter = WA_bseg-costcenter.“成本中心
lt_accountgl-item_text = WA_bseg-item_text.“項目文本
lt_accountgl-value_date = WA_bseg-value_date.“起息日
lt_accountgl-alloc_nmbr = WA_bseg-alloc_nmbr.“分配號
lt_accountgl-PROFIT_CTR = WA_bseg-PROFIT_CTR. “ 利潤中心
lt_accountgl-orderid = WA_bseg-orderid.“訂單號
lt_accountgl-ACCT_TYPE = ‘S’.
lt_accountgl-trade_id = l_trade_id.“貿(mào)易伙伴
lt_accountgl-REF_KEY_1 = WA_BSEG-XREF1.
APPEND lt_accountgl.
CLEAR lt_accountgl.
ELSEIF t_tbsl-koart = ‘K’.
lt_accountpayable-itemno_acc = l_itemno. “會計憑證行項目編
lt_accountpayable-vendor_no = WA_bseg-vendor_no.“供應(yīng)商或債權(quán)人的賬號
lt_accountpayable-gl_account = WA_bseg-gl_account.“總分類帳帳目
lt_accountpayable-sp_gl_ind = WA_bseg-sp_gl_ind.“特殊總分類帳標(biāo)志
lt_accountpayable-item_text = WA_bseg-item_text.“項目文本
lt_accountpayable-pmnttrms = WA_bseg-pmnttrms.“付款條件代碼
lt_accountpayable-alloc_nmbr = WA_bseg-alloc_nmbr.“分配號
lt_accountpayable-PROFIT_CTR = WA_bseg-PROFIT_CTR. “ 利潤中心
“功能范圍,在擴展字段中
LT_ACCOUNTPAYABLE-REF_KEY_1 = WA_BSEG-XREF1.
APPEND lt_accountpayable.
CLEAR lt_accountpayable.
ELSEIF t_tbsl-koart = ‘D’.
lt_accountreceivable-itemno_acc = l_itemno. “會計憑證行項目編
lt_accountreceivable-item_text = WA_bseg-item_text.“項目文本
lt_accountreceivable-customer = WA_bseg-customer. “客戶編號1
lt_accountreceivable-sp_gl_ind = WA_bseg-sp_gl_ind.“特殊總分類帳標(biāo)志
lt_accountreceivable-gl_account = WA_bseg-gl_account.“總分類帳帳目
lt_accountreceivable-pmnttrms = WA_bseg-pmnttrms.“付款條件代碼
lt_accountreceivable-alloc_nmbr = WA_bseg-alloc_nmbr.“分配號
lt_accountreceivable-PROFIT_CTR = WA_bseg-PROFIT_CTR. “ 利潤中心
lt_accountreceivable-REF_KEY_1 = WA_BSEG-XREF1.
APPEND lt_accountreceivable.
CLEAR lt_accountreceivable.
ELSEIF t_tbsl-koart = ‘A’.
lt_accountgl-itemno_acc = l_itemno. “會計憑證行項目編
lt_accountgl-GL_ACCOUNT = WA_bseg-GL_ACCOUNT. “總分類帳帳目
* lt_accountgl-CUSTOMER = t_bseg-CUSTOMER. “客戶編號1
* lt_accountgl-VENDOR_NO = t_bseg-VENDOR_NO. “供應(yīng)商或債權(quán)人的賬號
lt_accountgl-ITEM_TEXT = WA_bseg-ITEM_TEXT. “項目文本
lt_accountgl-PROFIT_CTR = WA_bseg-PROFIT_CTR. “ 利潤中心
lt_accountgl-ASSET_NO = WA_bseg-ASSET_NO. “ 資產(chǎn)號
lt_accountgl-SUB_NUMBER = WA_bseg-SUB_NUMBER. “ 資產(chǎn)次級編號
lt_accountgl-PO_NUMBER = WA_bseg-PO_NUMBER. “ 采購單號
lt_accountgl-ACCT_TYPE = ‘A’.
lt_accountgl-REF_KEY_1 = WA_BSEG-XREF1.
IF ls_doc_head-OBJ_TYPE IS INITIAL.
SET_AA_HEAD.
ENDIF.
APPEND lt_accountgl.
CLEAR lt_accountgl.
ENDIF.
lt_amount-itemno_acc = l_itemno. “會計憑證行項目編
IF t_tbsl-shkzg = ‘S’.“金額正負(fù)處理
IF WA_bseg-amt_doccur < 0.
lt_amount-amt_doccur = WA_bseg-amt_doccur * -1. “金額
ELSE.
lt_amount-amt_doccur = WA_bseg-amt_doccur. “金額
ENDIF.
ELSEIF t_tbsl-shkzg = ‘H’.
IF WA_bseg-amt_doccur > 0.
lt_amount-amt_doccur = WA_bseg-amt_doccur * -1. “金額
ELSE.
lt_amount-amt_doccur = WA_bseg-amt_doccur. “金額
ENDIF.
ENDIF.
lt_amount-currency = i_bkpf-currency. “貨幣
wa_zks_oa_acc_ext-posnr = l_itemno. “憑證行項目
wa_zks_oa_acc_ext-zfbdt = WA_bseg-bline_date. “到期日
wa_zks_oa_acc_ext-bschl = WA_bseg-bschl.“記賬碼
wa_zks_oa_acc_ext-rstgr = WA_bseg-rstgr.“付款原因代碼
wa_zks_oa_acc_ext-BWASL = WA_bseg-BWASL. “ 資產(chǎn)事務(wù)類型
wa_zks_oa_acc_ext-xref1_hd = i_bkpf-xref1_hd.“參考字段(標(biāo)題1),,說明該憑證來自HEC系統(tǒng)
* IF t_tbsl-koart = ‘K’.
wa_zks_oa_acc_ext-FKBER = WA_bseg-FKBER. “功能范圍
* ENDIF.
wa_zks_oa_acc_ext-BWASL = WA_bseg-BWASL. “ 事務(wù)類型
it_extension2-structure = ‘ZKS_OA_ACC_EXT’.
it_extension2-valuepart1 = wa_zks_oa_acc_ext.
APPEND:lt_amount,it_extension2.
CLEAR:lt_amount,it_extension2.
ELSE.
i_return-type = ‘E’.
i_return-message = ‘記賬碼有誤‘.
ENDIF.
CLEAR WA_bseg.
ENDLOOP.
CALL FUNCTION ‘BAPI_ACC_DOCUMENT_POST’
EXPORTING
documentheader = ls_doc_head
IMPORTING
OBJ_TYPE = l_OBJ_TYPE
OBJ_KEY = l_OBJ_KEY
OBJ_SYS = l_OBJ_SYS
TABLES
accountgl = lt_accountgl
currencyamount = lt_amount
accountpayable = lt_accountpayable
accountreceivable = lt_accountreceivable
return = lt_return
extension2 = it_extension2.
READ TABLE lt_return WITH KEY type = ‘E’.
IF sy-subrc = 0.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
LOOP AT lt_return.
i_return-type = ‘E’.
CONCATENATE i_return-message ‘/’ lt_return-message INTO i_return-message.
CLEAR lt_return.
ENDLOOP.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = ‘X’.
LOOP AT lt_return.
i_return-type = ‘S’.
IF lt_return-type = ‘S’.
IF i_bkpf-doc_type = ‘AA’.
SELECT SINGLE BELNR FROM BKPF INTO i_return-BELNR “ 根據(jù)參考碼得到真正的憑證號,,用于返回
WHERE BUKRS = i_bkpf-comp_code
AND GJAHR = G_GJAHR
AND AWTYP = l_OBJ_TYPE
AND AWKEY = l_OBJ_KEY.
CONCATENATE G_GJAHR ‘_’ i_bkpf-comp_code ‘_’ i_return-BELNR INTO i_return-BELNR.
ELSE.
CONCATENATE lt_return-message_v2+14(4) ‘_’ lt_return-message_v2+10(4) ‘_’ lt_return-message_v2+0(10) INTO i_return-belnr.
ENDIF.
ENDIF.
CONCATENATE i_return-message ‘/’ lt_return-message INTO i_return-message.
CLEAR lt_return.
ENDLOOP.
ENDIF.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
另外,上面BAdI ACC_DOCUMENT的參考交易為BKPFF,,這將導(dǎo)致使用ZBKPF的AA類憑證無法激活BAdI,,無法傳輸擴展字段,需要修改BAdI的過濾器值,,增加or AWTYP = ZBKPF
KZ類憑證 預(yù)付帳款-在建工程 創(chuàng)建問題:
使用BAPI ‘BAPI_ACC_DOCUMENT_POST’創(chuàng)建預(yù)付賬款-在建工程憑證(29 -J)時無法完成以下功能:
1)無法將AUC號傳給供應(yīng)商
2)無法自動生成50 70兩行項目
參考使用錄屏方式完成,,該錄屏方式號稱可以實現(xiàn)所有類型憑證的創(chuàng)建。
另外,,錄屏方式返回的失敗錯誤信息需要讀取SM35中的類似錯誤信息
1) 通過Session name查找session的屬性
SELECT * FROM apql
INTO TABLE i_apql
WHERE groupid = L_GROUPID
AND mandant = sy-mandt.
2)使用程序rsbdc_protocol的Function “read_bdc_log_plain”,,通過session的屬性查找Session日志的Message
PERFORM read_bdc_log_plain(rsbdc_protocol)
TABLES
logtable
USING
i_apql-temseid i_apql-mandant.
3)通過上面的Message - logtable 輸出Message的具體信息。
bapiret2-ID = logtable-logmessage+74(16).
bapiret2-NUMBER = logtable-logmessage+94(3).
bapiret2-message_v1 = logtable-logmessage+100(20).
CALL FUNCTION ’BAPI_MESSAGE_GETDETAIL’
EXPORTING
ID = bapiret2-ID
NUMBER = bapiret2-NUMBER
textformat = ’ASC’
message_v1 = bapiret2-message_v1
IMPORTING
MESSAGE = bapiret2-MESSAGE.
CONCATENATE bapiret2-MESSAGE ’//’ L_MESSAGE INTO L_MESSAGE.
參考代碼:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*——————————————-
* Import
** I_BKPF
DOC_TYPE BLART
CURRENCY WAERS
FIS_PERIOD MONAT
DOC_DATE BLDAT
PSTNG_DATE BUDAT
HEADER_TXT BKTXT
REF_DOC_NO XBLNR
COMP_CODE BUKRS
XREF1_HD XREF1_HD
USERNAME USNAM
XREF2_HD XREF2_HD
______________________________ ______________________________
* Table
**T_BSEG
COSTCENTER KOSTL
BSCHL BSCHL
SP_GL_IND ACPI_UMSKZ
CUSTOMER KUNNR
VENDOR_NO LIFNR
GL_ACCOUNT HKONT
ITEM_TEXT SGTXT
AMT_DOCCUR WRBTR
RSTGR RSTGR
VALUE_DATE VALUT
BLINE_DATE ACPI_ZFBDT
PMNTTRMS ACPI_ZTERM
ALLOC_NMBR ACPI_ZUONR
ORDERID AUFNR
PROFIT_CTR PRCTR
ASSET_NO ANLN1
SUB_NUMBER ANLN2
PO_NUMBER EBELN
FKBER FKBER
BWASL BWASL
XREF1 XREF1
______________________________ ______________________________
TABLES:TBSL.
DATA WA_BSEG TYPE ZHEC_BSEG.
DATA: L_NUM TYPE BKPF-NUMPG,
G_GROUPID TYPE APQI-GROUPID.
DATA: L_TCODE TYPE SY-TCODE.
DATA: L_STYPE LIKE FTPOST-STYPE.
DATA: L_COUNTER TYPE COUNT_PI.
DATA: L_FVALUE TYPE BDC_FVAL.
DATA: LT_FTPOST TYPE STANDARD TABLE OF FTPOST.
DATA: L_SUBRC TYPE SY-SUBRC.
DATA: LT_BLNTAB TYPE STANDARD TABLE OF BLNTAB.
DATA: LT_FTTAX TYPE STANDARD TABLE OF FTTAX.
DATA: RND_VALUE TYPE DATATYPE-CHAR0128.
DATA : L_GJAHR TYPE BKPF-GJAHR.
DATA: L_BELNR LIKE BKPF-BELNR.
CLEAR L_NUM.
LOOP AT T_BSEG INTO WA_BSEG.
L_NUM = L_NUM + 1.
ENDLOOP.
L_GJAHR = I_BKPF-PSTNG_DATE(4).
CALL FUNCTION ‘RANDOM_C’
EXPORTING
LEN_MIN = 2
LEN_MAX = 2
CHAR_MAX = 27
IMPORTING
RND_VALUE = RND_VALUE.
CONCATENATE ‘FI’ SY-DATUM+4(4) SY-UZEIT(4) RND_VALUE INTO G_GROUPID. “生成會話名稱session name
*開始錄屏
CALL FUNCTION ‘POSTING_INTERFACE_START’
EXPORTING
I_FUNCTION = ‘B’
I_XBDCC = ‘X’
I_KEEP = ‘X’
I_GROUP = G_GROUPID
* I_MODE = L_MODE
I_USER = SY-UNAME.
MOVE ‘FB01′ TO L_TCODE.
CLEAR L_COUNTER.
L_COUNTER = L_COUNTER + 1.
L_STYPE = ‘K’.
* fill Hearder
MOVE I_BKPF-COMP_CODE TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-BUKRS’ L_FVALUE . “公司代碼
MOVE I_BKPF-DOC_TYPE TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-BLART’ L_FVALUE . ”憑證類型
WRITE I_BKPF-PSTNG_DATE TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-BUDAT’ L_FVALUE . ” 過賬日期
WRITE I_BKPF-DOC_DATE TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-BLDAT’ L_FVALUE . “憑證日期
IF I_BKPF-FIS_PERIOD IS NOT INITIAL.
WRITE I_BKPF-FIS_PERIOD TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-MONAT’ L_FVALUE . ” 過賬期間
ENDIF.
WRITE I_BKPF-CURRENCY TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-WAERS’ L_FVALUE . ”貨幣
IF I_BKPF-HEADER_TXT IS NOT INITIAL.
WRITE I_BKPF-HEADER_TXT TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-BKTXT’ L_FVALUE . ” 憑證抬頭文本
ENDIF.
* IF I_BKPF-REF_DOC_NO IS NOT INITIAL.
* WRITE I_BKPF-REF_DOC_NO TO L_FVALUE.
* PERFORM F_FILL_FIELD
* TABLES LT_FTPOST
* USING L_STYPE L_COUNTER ‘BKPF-XBLNR’ L_FVALUE . ” 參照
* ENDIF.
* PAGES
WRITE L_NUM TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BKPF-NUMPG’ L_FVALUE . ” 頁數(shù),,取憑證行項目數(shù)
* FILL ITEM
L_STYPE = ‘P’.
CLEAR L_COUNTER.
LOOP AT T_BSEG INTO WA_BSEG.
L_COUNTER = L_COUNTER + 1.
MOVE WA_BSEG-BSCHL TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-BSCHL’ L_FVALUE . ” 過賬碼
WRITE WA_BSEG-amt_doccur TO L_FVALUE LEFT-JUSTIFIED.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-WRBTR’ L_FVALUE . ” 憑證貨幣金額
MOVE WA_BSEG-sp_gl_ind TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-UMSKZ’ L_FVALUE . ” 特殊 G/L 標(biāo)識
IF WA_BSEG-BSCHL(1) = 3. ” 科目/供應(yīng)商/客戶
MOVE WA_BSEG-VENDOR_NO TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-LIFNR’ L_FVALUE .
ELSEIF WA_BSEG-BSCHL(1) = 0.
MOVE WA_BSEG-CUSTOMER TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-KUNNR’ L_FVALUE .
ELSEIF WA_BSEG-BSCHL(1) = 2.
MOVE WA_BSEG-VENDOR_NO TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-HKONT’ L_FVALUE .
ELSE.
MOVE WA_BSEG-GL_ACCOUNT TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-HKONT’ L_FVALUE .
ENDIF.
* INTERNAL ORDER
IF WA_BSEG-orderid IS NOT INITIAL.
WRITE WA_BSEG-orderid TO L_FVALUE LEFT-JUSTIFIED.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘COBL-AUFNR’ L_FVALUE . ” 訂單號
ENDIF.
* Profit center
IF WA_BSEG-PROFIT_CTR IS NOT INITIAL. ” 利潤中心
CLEAR L_FVALUE.
WRITE WA_BSEG-PROFIT_CTR TO L_FVALUE LEFT-JUSTIFIED.
IF WA_BSEG-BSCHL(1) = 2.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-PRCTR’ L_FVALUE .
ELSE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘COBL-PRCTR’ L_FVALUE .
ENDIF.
ENDIF.
* REASON CODE
IF WA_BSEG-RSTGR IS NOT INITIAL.
WRITE WA_BSEG-RSTGR TO L_FVALUE LEFT-JUSTIFIED.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-RSTGR’ L_FVALUE . ” 原因代碼
ENDIF.
IF WA_BSEG-costcenter IS NOT INITIAL.
WRITE WA_BSEG-costcenter TO L_FVALUE LEFT-JUSTIFIED.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘COBL-KOSTL’ L_FVALUE . ” 成本中心
ENDIF.
IF WA_BSEG-BSCHL <> ‘40′ AND WA_BSEG-BSCHL <> ‘50′.
IF WA_BSEG-PMNTTRMS IS NOT INITIAL.
MOVE WA_BSEG-PMNTTRMS TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-ZTERM’ L_FVALUE . ” 付款條件
ENDIF.
ENDIF.
IF WA_BSEG-alloc_nmbr IS NOT INITIAL.
MOVE WA_BSEG-alloc_nmbr TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-ZUONR’ L_FVALUE . ” 分配編號
ENDIF.
IF WA_BSEG-item_text IS NOT INITIAL.
MOVE WA_BSEG-item_text TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-SGTXT’ L_FVALUE . ” 項目文本
ENDIF.
IF WA_BSEG-ASSET_NO IS NOT INITIAL.
MOVE WA_BSEG-ASSET_NO TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-ANLN1′ L_FVALUE . ” 資產(chǎn)號
MOVE WA_BSEG-SUB_NUMBER TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-ANLN2′ L_FVALUE .
ENDIF.
* CLEAR TBSL.
* SELECT SINGLE * FROM TBSL WHERE BSCHL = WA_BSEG-BSCHL.
* IF TBSL-KOART = ‘K’.
MOVE WA_BSEG-XREF1 TO L_FVALUE.
PERFORM F_FILL_FIELD
TABLES LT_FTPOST
USING L_STYPE L_COUNTER ‘BSEG-XREF1′ L_FVALUE . ” 參考碼,,用于憑證過濾
* ENDIF.
CLEAR WA_BSEG.
ENDLOOP.
CALL FUNCTION ‘POSTING_INTERFACE_DOCUMENT’
EXPORTING
I_TCODE = L_TCODE
IMPORTING
E_SUBRC = L_SUBRC
TABLES
T_BLNTAB = LT_BLNTAB
T_FTPOST = LT_FTPOST
T_FTTAX = LT_FTTAX
EXCEPTIONS
ACCOUNT_MISSING = 1
COMPANY_CODE_MISSING = 2
POSTING_KEY_INVALID = 3
POSTING_KEY_MISSING = 4
RECORD_TYPE_INVALID = 5
TRANSACTION_CODE_INVALID = 6
AMOUNT_FORMAT_ERROR = 7
TOO_MANY_LINE_ITEMS = 8
COMPANY_CODE_INVALID = 9
SCREEN_NOT_FOUND = 10
NO_AUTHORIZATION = 11
OTHERS = 12.
IF SY-SUBRC <> 0.
I_RETURN-TYPE = ‘E’.
ENDIF.
CALL FUNCTION ‘POSTING_INTERFACE_END’. ” 錄屏結(jié)束
* 調(diào)用會話,執(zhí)行錄屏內(nèi)容
SUBMIT RSBDCSUB WITH MAPPE EQ G_GROUPID
WITH VON EQ SY-DATUM
WITH BIS EQ SY-DATUM
WITH Z_VERARB EQ ‘X’
WITH FEHLER EQ ”
EXPORTING LIST TO MEMORY
AND RETURN.
* 等待5S后,,讀取生成的憑證號
WAIT UP TO 5 SECONDS.
SELECT SINGLE BELNR FROM BKPF INTO L_BELNR
WHERE bukrs = I_BKPF-COMP_CODE
AND GJAHR = L_GJAHR
AND GRPID = G_GROUPID.
IF SY-subrc = 0.
I_RETURN-TYPE = ‘S’.
CONCATENATE L_GJAHR ‘_’ I_BKPF-COMP_CODE ‘_’ L_BELNR INTO I_RETURN-BELNR.
* ———————————————————————————–
* MODIFY USER FROM rfc USER
* ———————————————————————————–
UPDATE BKPF SET USNAM = I_BKPF-USERNAME
XREF1_HD = I_BKPF-XREF1_HD
XREF2_HD = I_BKPF-XREF2_HD
WHERE BUKRS = I_BKPF-COMP_CODE
AND BELNR = L_BELNR
AND GJAHR = L_GJAHR.
IF SY-SUBRC = 0 .
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.
* —————————————————————————————
ELSE.
I_RETURN-TYPE = ‘E’.
PERFORM FM_BDC_LOG USING G_GROUPID
CHANGING I_RETURN-MESSAGE.
ENDIF.
*&———————————————————————*
*& Form FM_BDC_LOG
*&———————————————————————*
* 如果SESSION執(zhí)行出錯,,讀取錯誤信息
*———————————————————————-*
* –>P_G_GROUPID text
* <–P_I_RETURN_MESSAGE text
*———————————————————————-*
FORM FM_BDC_LOG USING L_GROUPID TYPE APQI-GROUPID
CHANGING L_MESSAGE TYPE BAPI_MSG.
TABLES:bapiret2.
DATA: I_APQL TYPE TABLE OF APQL WITH HEADER LINE.
DATA: BEGIN OF logtable OCCURS 50, ” plain log information in TemSe
enterdate LIKE btctle-enterdate,
entertime LIKE btctle-entertime,
logmessage(400) TYPE C,
END OF logtable.
DATA: l_logtable LIKE logtable.
SELECT * FROM apql
INTO TABLE i_apql
WHERE groupid = L_GROUPID
AND mandant = sy-mandt.
LOOP AT i_apql.
FREE logtable.
PERFORM read_bdc_log_plain(rsbdc_protocol)
TABLES
logtable
USING
i_apql-temseid i_apql-mandant.
LOOP AT logtable .
l_logtable = logtable.
IF logtable-logmessage+9(20) NE ”.
CLEAR bapiret2.
bapiret2-ID = logtable-logmessage+74(16).
bapiret2-NUMBER = logtable-logmessage+94(3).
bapiret2-message_v1 = logtable-logmessage+100(20).
CALL FUNCTION ‘BAPI_MESSAGE_GETDETAIL’
EXPORTING
ID = bapiret2-ID
NUMBER = bapiret2-NUMBER
textformat = ‘ASC’
message_v1 = bapiret2-message_v1
IMPORTING
MESSAGE = bapiret2-MESSAGE.
CONCATENATE bapiret2-MESSAGE ‘//’ L_MESSAGE INTO L_MESSAGE.
ENDIF.
ENDLOOP.
ENDLOOP.
ENDFORM.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
二、沖銷憑證
問題1:原使用CALL FUNCTION ’NUMBER_RANGE_DEQUEUE’獲取下一個憑證號,,卻發(fā)生跳號現(xiàn)象,。只得人工計算下個憑證號。沖銷憑證BAPI需要參數(shù)-OBJ_KEY(憑證號+公司代碼+會計年度)
SELECT SINGLE STBLA FROM T003 INTO L_STBLA
WHERE BLART = LS_BKPF-BLART. “ 獲取沖銷憑證的憑證類型
SELECT SINGLE NUMKR FROM T003 INTO L_NUMKR
WHERE BLART = L_STBLA. “ 沖銷憑證號碼段
CONCATENATE L_NUMKR ’%’ INTO L_STRING.
SELECT BELNR FROM BKPF INTO TABLE IT_BSEG
WHERE BUKRS = I_COMP_CODE
AND GJAHR = L_GJAHR
AND BLART = L_STBLA
AND BELNR LIKE L_STRING.
if SY-SUBRC = 0.
SORT IT_BSEG BY BELNR DESCENDING.
READ TABLE IT_BSEG INDEX 1.
IT_BSEG-BELNR = IT_BSEG-BELNR + 1.
問題2:1,、se37 . run ‘Z_DOCUMENT_REV_POST’. (This function calls BAPI-
BAPI_ACC_GL_POSTING_REV_POST).
2,、with Parameter
BELNR RE PSTNG_DATE FI USERNAME
2012_H001_2000000271 03
3、created reversal document 2000000281 successfully
4,、But in FBN1(COMPANY:H001), the current number is still 2000000280.
SAP的回復(fù)如下:
You use a custom function module (Z_DOCUMENT_REV_POST) which then
calls the BAPI BAPI_ACC_GL_POSTING_REV_POST. And you submit the document number directly to the BAPI in field REVERSAL-OBJ_KEY. The document is
posted correctly with the document number entered, but the document
number itself has not been drawn from the database.
This is why document number level is not updated, but document is posted correctly.
As we talked via phone, you use the function module
BAPI_ACC_GL_POSTING_POST to post the source document . This function module will draw the document number for OBJ_TYPE = BKPFF automatically, but the reversal BAPI will NOT do this.
We have two options for this issue:
1.Call function module RF_GET_DOCUMENT_NUMBER before calling the
reversal BAPI BAPI_ACC_GL_POSTING_REV_POST to get the next free document number