转自:http://blog.chinaunix.net/uid-22490185-id-75722.html
BAPI: BAPI_ACC_GL_POSTING_POST
生成总账会计凭证的源程序
1. BAPI没有记帐码的字段
BAPI一般是给其他系统调用的,所以BAPI的参数字段名和SAP的字段名不同,对于其他系统更容易理解,除了SAP,其他系统还有记帐码一说吗?!
2. BAPI创建成功后不返回SAP凭证号
OBJ_参考交易,OBJ_KEY字段参考关键 这2个参数如果赋值,则return消息中不会有SAP的凭证号信息了。
3.关于extension1参数
当用户的会计凭证信息在BAPI中没有参数支持时,可以借用extension存储。SAP提供增强对这个参数进行操作。
BAPI_ACC_GL_POSTING_POST: User exit ACBAPI01
BAPI_ACC_DOCUMENT_POST: BADI ACC_DOCUMENT~CHANGE
4. Return返回的信息是创建成功,但在SAP系统中找不到凭证
是否忘记Commit work了?!
** 以下是生成总账会计凭证的源码:
REPORT zbapi_gl_posting.
****声明变量和内表
DATA: obj_type LIKE bapiache02-obj_type, "参考过程
obj_key LIKE bapiache02-obj_key, "对象关键字
obj_sys LIKE bapiache02-obj_sys, "源凭证的逻辑系统
gs_dochead LIKE bapiache08, "凭证抬头结构
gt_accountgl LIKE bapiacgl08 OCCURS 0 WITH HEADER LINE, "行项目表
gt_curramount LIKE bapiaccr08 OCCURS 0 WITH HEADER LINE,"货币金额
gt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE, "记账后值返回
gt_extension LIKE bapiextc OCCURS 0 WITH HEADER LINE,"客户退出参数
bapi_retn_info LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA: v_error_flag.
****会计凭证的抬头 对应BKPF表
gs_dochead-comp_code = ‘1100‘. "凭证的公司代码, 必输
gs_dochead-doc_date = sy-datum. "凭证的凭证日期, 必输
gs_dochead-pstng_date = sy-datum. "凭证的记账日期, 必输
gs_dochead-fis_period = sy-datum+4(2). "凭证的会计期间, 必输
gs_dochead-doc_type = ‘SA‘. "凭证的凭证类型, 必输
gs_dochead-username = sy-uname. "凭证的录入用户, 必输
gs_dochead-header_txt = ‘Test using BAPI‘. "凭证的抬头文本
gs_dochead-trans_date = sy-datum. "凭证的换算日期
gs_dochead-fisc_year = sy-datum(4). "凭证的会计年度
gs_dochead-reason_rev = ‘03‘. "凭证的冲销原因
gs_dochead-ref_doc_no = ‘‘. "凭证的参考凭证编号
**** 会计凭证的行项目 对应BSEG表
gt_accountgl-itemno_acc = 1. "凭证行项目编号, 必输
gt_accountgl-gl_account = ‘0010010113‘. "总账科目, 必输
gt_accountgl-comp_code = ‘1100‘. "公司代码, 必输
gt_accountgl-costcenter = ‘‘. "成本中心
gt_accountgl-bus_area = ‘1103‘. "业务范围
gt_accountgl-customer = ‘A10003‘. "客户编号
gt_accountgl-vendor_no = ‘20025‘. "供应商编号
gt_accountgl-alloc_nmbr = ‘分配1‘. "分配
gt_accountgl-item_text = ‘行项目文本1‘. "行项目文本
APPEND gt_accountgl.
gt_accountgl-itemno_acc = 2.
gt_accountgl-gl_account = ‘0055020129‘.
gt_accountgl-comp_code = ‘1100‘.
gt_accountgl-costcenter = ‘00 11010107‘.
gt_accountgl-bus_area = ‘1103‘.
gt_accountgl-customer = ‘A10003‘.
gt_accountgl-vendor_no = ‘20025‘.
gt_accountgl-alloc_nmbr = ‘分配2‘.
gt_accountgl-item_text = ‘行项目文本2‘.
APPEND gt_accountgl.
***** 行项目的货币/金额
gt_curramount-itemno_acc = 1. "行项目编号, 必输
gt_curramount-currency = ‘CNY‘. "行项目凭证货币, 必输
gt_curramount-amt_doccur = ‘100.00‘. "行项目金额, 必输
gt_curramount-exch_rate = 0. "行项目汇率
APPEND gt_curramount.
gt_curramount-itemno_acc = ‘2‘.
gt_curramount-currency = ‘CNY‘.
gt_curramount-amt_doccur = ‘-100.00‘.
gt_curramount-exch_rate = 0.
APPEND gt_curramount.
**** 调用功能函数: 总账凭证的过账
CALL FUNCTION ‘BAPI_ACC_GL_POSTING_POST‘
EXPORTING
documentheader = gs_dochead "凭证抬头
* importing
* obj_type = obj_type
* obj_key = obj_key
* obj_sys = obj_sys
TABLES
accountgl = gt_accountgl "凭证行项目
currencyamount = gt_curramount "凭证货币和金额
return = gt_return "值返回
extension1 = gt_extension "例外出口
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0. "[[[
MESSAGE ‘Problem occured‘ TYPE ‘I‘.
ELSE. "|||
LOOP AT gt_return.
CLEAR bapi_retn_info.
MOVE-CORRESPONDING gt_return TO bapi_retn_info.
IF gt_return-type = ‘A‘ OR gt_return-type = ‘E‘.
v_error_flag = ‘X‘.
ENDIF.
APPEND bapi_retn_info.
ENDLOOP.
IF v_error_flag = ‘X‘.
MESSAGE ‘Problem occured ‘ TYPE ‘I‘.
ROLLBACK WORK.
ELSE.
COMMIT WORK.
MESSAGE ‘DOCUMENT IS CREATED!‘ TYPE ‘I‘.
ENDIF.
ENDIF. "]]]