目录
- BAPI创建会计凭证和冲销凭证
- 组件 利润中心冲销不可能
- 原因
- 由于一个变量为空导致不可以冲销
- 代码附上
- 创建会计凭证代码
- 冲销会计凭证代码
BAPI创建会计凭证和冲销凭证
在使用冲销会计凭证的BAPI,即BAPI_ACC_DOCUMENT_REV_POST
时,遇到下面的问题:
组件 利润中心冲销不可能
原因
由于一个变量为空导致不可以冲销
最后追查到一个底表,有一个select语句,查询TKA00PCA表,有个字段是空,在程序里面有对这个字段进行判断,如果是空,则无法冲销,所以不能使用BAPI对其进行冲销了!
之后,换了一个工厂的数据,发现可以成功冲销,说明是配置的问题,不是代码的问题,而且由于FICO模块不是很懂,就没有去配置,是直接换了一个工厂进行测试,测试通过。
代码附上
创建会计凭证代码
*&---------------------------------------------------------------------*
*& Report Z_BAPI_ACC_DOCUMENT_LHY
*&---------------------------------------------------------------------*
*& Type:Report
*& Creator:LiuHongyu
*& Create On:2024.2.2
*& Description:创建会计凭证的BAPI:BAPI_ACC_DOCUMENT_POST
*&---------------------------------------------------------------------*
REPORT z_bapi_acc_document_lhy.DATA documentheader TYPE bapiache09.
*DATA CUSTOMERCPD TYPE BAPIACPA09.
*DATA CONTRACTHEADER TYPE BAPIACCAHD.
*DATA OBJ_TYPE TYPE BAPIACHE09-OBJ_TYPE.
*DATA OBJ_KEY TYPE BAPIACHE09-OBJ_KEY.
*DATA OBJ_SYS TYPE BAPIACHE09-OBJ_SYS.DATA accountgl TYPE STANDARD TABLE OF bapiacgl09 WITH HEADER LINE.
*DATA ACCOUNTRECEIVABLE TYPE STANDARD TABLE OF BAPIACAR09.
*DATA ACCOUNTPAYABLE TYPE STANDARD TABLE OF BAPIACAP09.
*DATA ACCOUNTTAX TYPE STANDARD TABLE OF BAPIACTX09.DATA currencyamount TYPE STANDARD TABLE OF bapiaccr09 WITH HEADER LINE.
*DATA CRITERIA TYPE STANDARD TABLE OF BAPIACKEC9.
*DATA VALUEFIELD TYPE STANDARD TABLE OF BAPIACKEV9.
*DATA EXTENSION1 TYPE STANDARD TABLE OF BAPIACEXTC.DATA return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.
*DATA PAYMENTCARD TYPE STANDARD TABLE OF BAPIACPC09.
*DATA CONTRACTITEM TYPE STANDARD TABLE OF BAPIACCAIT.DATA extension2 TYPE STANDARD TABLE OF bapiparex WITH HEADER LINE.
*DATA REALESTATE TYPE STANDARD TABLE OF BAPIACRE09.
*DATA ACCOUNTWT TYPE STANDARD TABLE OF BAPIACWT09.DATA lv_datum TYPE d.DATA ls_zsacc_document TYPE zsacc_document.lv_datum = sy-datum.documentheader-comp_code = 1000. "公司代码documentheader-doc_date = lv_datum. "凭证中的凭证日期documentheader-pstng_date = lv_datum. "凭证的过账日期documentheader-doc_type = 'SA'. "凭证日期documentheader-username = sy-uname. "用户documentheader-header_txt = '产线报销费用凭证测试-lhy'.IF accountgl-costcenter IS INITIAL."accountgl-costcenter = lt_data-kostl."accountgl-costcenter = |{ '17101301' ALPHA = IN }|. "成本中心,这个要有ENDIF.accountgl-itemno_acc = '1'. "会计凭证行项目编号accountgl-material = |{ '5913' ALPHA = IN }|."物料号accountgl-plant = '1000'. "工厂"accountgl-orderid = |{ '1001754' ALPHA = IN }|. "订单号accountgl-gl_account = |{ '1001010000' ALPHA = IN }|. "借方总账科目"accountgl-gl_account = |{ '11001010' ALPHA = IN }|. "总账科目11001010,用这个试试accountgl-value_date = sy-datum. "起息日,一定要有APPEND accountgl.CLEAR: accountgl.accountgl-itemno_acc = '2'. "会计凭证行项目编号accountgl-material = |{ '5913' ALPHA = IN }|."物料号accountgl-plant = '1000'. "工厂"accountgl-orderid = |{ '1001754' ALPHA = IN }|. "订单号accountgl-gl_account = |{ '1001010001' ALPHA = IN }|. "贷方科目"accountgl-costcenter = |{ '17101301' ALPHA = IN }|. "成本中心,这个要有"accountgl-gl_account = '10010000' . "总账科目accountgl-value_date = sy-datum."起息日,一定要有APPEND accountgl.CLEAR: accountgl.currencyamount-itemno_acc = '1'.currencyamount-curr_type = '00'."评估视图currencyamount-currency = 'CNY'. "申请币别currencyamount-amt_doccur = 9999. "金额APPEND currencyamount.CLEAR:currencyamount.currencyamount-itemno_acc = '2'.currencyamount-curr_type = '00'."评估视图currencyamount-currency = 'CNY'. "申请币别currencyamount-amt_doccur = -9999. "金额APPEND currencyamount.CLEAR:currencyamount.
"--------------借贷方过账码增强相关的信息------------------------------CLEAR ls_zsacc_document.ls_zsacc_document-posnr = 1. "凭证行项目ls_zsacc_document-bschl = '40'."记账码CLEAR extension2.APPEND extension2.CLEAR ls_zsacc_document.ls_zsacc_document-posnr = 2. "贷方行项目ls_zsacc_document-bschl = '50'. "记账码CLEAR extension2.extension2-structure = 'ZSACC_DOCUMENT'.extension2-valuepart1 = ls_zsacc_document.APPEND extension2.CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'EXPORTINGdocumentheader = documentheader
* CUSTOMERCPD = CUSTOMERCPD
* CONTRACTHEADER = CONTRACTHEADER
* IMPORTING
* OBJ_TYPE = OBJ_TYPE
* OBJ_KEY = OBJ_KEY
* OBJ_SYS = OBJ_SYSTABLESaccountgl = accountgl
* ACCOUNTRECEIVABLE = ACCOUNTRECEIVABLE
* ACCOUNTPAYABLE = ACCOUNTPAYABLE
* ACCOUNTTAX = ACCOUNTTAXcurrencyamount = currencyamount
* CRITERIA = CRITERIA
* VALUEFIELD = VALUEFIELD
* EXTENSION1 = EXTENSION1return = return
* PAYMENTCARD = PAYMENTCARD
* CONTRACTITEM = CONTRACTITEMextension2 = extension2
* REALESTATE = REALESTATE
* ACCOUNTWT = ACCOUNTWT.IF line_exists( return[ type = 'E'] ). "如果有E的错误行cl_demo_output=>write( '错误消息:' ).cl_demo_output=>write( return ).cl_demo_output=>display( ).ELSE."DATA wait TYPE bapita-wait.DATA return_transaction TYPE bapiret2.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'IMPORTINGreturn = return_transaction.cl_demo_output=>display( return ).ENDIF.
冲销会计凭证代码
*&---------------------------------------------------------------------*
*& Report Z_BAPI_ACC_CANCEL_LHY
*&---------------------------------------------------------------------*
*& Type:Report
*& Creator:LiuHongyu
*& Create On:2024.2.2
*& Description:冲销会计凭证的BAPI:BAPI_ACC_DOCUMENT_REV_POST
*&---------------------------------------------------------------------*
REPORT z_bapi_acc_cancel_lhy.DATA reversal TYPE bapiacrev.DATA bus_act TYPE bapiache09-bus_act.
* DATA OBJ_TYPE TYPE BAPIACREV-OBJ_TYPE.DATA obj_key TYPE bapiacrev-obj_key.
* DATA OBJ_SYS TYPE BAPIACREV-OBJ_SYS.DATA lt_return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.DATA i_return TYPE bapiret2.DATA ls_bkpf TYPE bkpf.DATA l_belnr TYPE bkpf-belnr.DATA : l_message TYPE string."l_gjahr = |{ '100000217' ALPHA = IN }|.l_belnr = |{ '100000022' ALPHA = IN }|.SELECT SINGLE * FROM bkpf INTO ls_bkpf WHERE bukrs = '1000'AND belnr = l_belnrAND gjahr = '2024'AND xreversal = ''.IF sy-subrc <> 0.MESSAGE '表:BKPF(会计核算凭证标题)没有数据或该凭证已冲销!请核查数据!' TYPE 'E'.ENDIF."bapi参数赋值reversal-obj_type = ls_bkpf-awtyp. "参考过程:BKPFFreversal-obj_key_r = ls_bkpf-awkey. "取消: 对象码"reversal-obj_key = ls_bkpf-awkey. "对象键值"reversal-pstng_date = ls_bkpf-budat. "凭证中的过账日期"reversal-fis_period = ls_bkpf-monat. "会计期间reversal-comp_code = ls_bkpf-bukrs. "公司代码:1710"reversal-reason_rev = '02'. "原因代码
* reversal-ac_doc_no = ls_bkpf-belnr. "会计凭证号码
* 取得系统 LOGICAL SYSTEMCALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'IMPORTINGown_logical_system = reversal-obj_sys."调用bapi函数,冲销会计凭证CALL FUNCTION 'BAPI_ACC_DOCUMENT_REV_POST'EXPORTINGreversal = reversalbus_act = ls_bkpf-glvorIMPORTING
* OBJ_TYPE = OBJ_TYPEobj_key = obj_key
* OBJ_SYS = OBJ_SYSTABLESreturn = lt_return.
* IF sy-subrc NE 0. "如果sy-subrc 不等于0
* cl_demo_output=>display( lt_return ).
* ENDIF.
* IF sy-subrc EQ 0. "如果sy-subrc 等于0
* cl_demo_output=>display( lt_return ).
* ENDIF.READ TABLE lt_return WITH KEY type = 'E'.IF sy-subrc NE 0.
* 提交凭证过账CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'IMPORTINGreturn = i_return.IF i_return IS INITIAL.l_message = |会计凭证冲销成功!会计凭证号:{ obj_key(10) }|.MESSAGE l_message TYPE 'E'.COMMIT WORK AND WAIT."<fs_alv>-icon = '@08@'.ELSE."<fs_alv>-icon = '@0A@'.l_message = '过账失败,请用事务代码ST22查看详细信息'.ENDIF.ELSE.CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'."<fs_alv>-icon = '@0A@'.LOOP AT lt_return WHERE type = 'A' OR type = 'E'.l_message = l_message && lt_return-message.ENDLOOP.MESSAGE l_message TYPE 'E'.ENDIF.