ABAP FB08 凭证冲销(BAPI_ACC_DOCUMENT_REV_POST)

*&---------------------------------------------------------------------*
*& Form FRM_WRITE_OFF
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_write_off  .
   LOOP  AT lt_data  WHERE box  'X' .
     CASE lt_data - icon .
       WHEN icon_led_green .
        lt_data -zmsg  lt_data -zmsg &&  '该凭证已冲销!!' .
        lt_data - icon  icon_led_yellow .
         MODIFY lt_data .
         PERFORM frm_refresh_display .
         CLEAR lt_data .
         CONTINUE .
       WHEN icon_led_yellow .
         CONTINUE .
       WHEN  OTHERS .
         CLEAR  lt_data -zmsg ,lt_data - icon .
     ENDCASE .

*    调用bapi
     PERFORM bapi_acc_document_rev_post  CHANGING lt_data .
     MODIFY lt_data .
     PERFORM frm_refresh_display .
     CLEAR lt_data .
   ENDLOOP .
ENDFORM .
*&---------------------------------------------------------------------*
*& Form BAPI_ACC_DOCUMENT_REV_POST
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*      <--P_LT_DATA  text
*&---------------------------------------------------------------------*
FORM bapi_acc_document_rev_post  CHANGING p_data TYPE ty_data.
  DATA l_bkpf    TYPE bkpf.
  DATA l_reversal TYPE bapiacrev,
         l_bus_act  TYPE bapiache09-bus_act,
         l_obj_key  TYPE bapiacrev-obj_key,
         gt_return  TYPE TABLE OF bapiret2 WITH HEADER LINE,
         i_return   TYPE bapiret2.

*  获取数据
  SELECT SINGLE FROM bkpf INTO l_bkpf WHERE bukrs p_data-bukrs
                                        AND   belnr p_data-belnr
                                        AND   gjahr p_data-gjahr
                                        AND   xreversal ''.
  IF sy-subrc <> 0.
    p_data-icon icon_led_red.
    p_data-zmsg '表:BKPF(会计核算凭证标题)没有数据或该凭证已冲销!请核查数据!'.
    EXIT.
  ENDIF.

*  bapi 参数赋值
  l_reversal-obj_type     l_bkpf-awtyp.
*  l_reversal-obj_key      = l_bkpf-awkey.
  l_reversal-obj_key_r    l_bkpf-awkey.
  l_reversal-pstng_date   l_bkpf-budat.
  l_reversal-fis_period   l_bkpf-monat.
  l_reversal-comp_code    l_bkpf-bukrs.
*  l_reversal-ac_doc_no    = l_bkpf-belnr.
  l_reversal-reason_rev   '03'.
*   取得系统 LOGICAL SYSTEM
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system l_reversal-obj_sys.
*   调用 BAPI 函数,冲销会计凭证
  CALL FUNCTION 'BAPI_ACC_DOCUMENT_REV_POST'
    EXPORTING
      reversal l_reversal
      bus_act  l_bkpf-glvor
    IMPORTING
      obj_key  l_obj_key
    TABLES
      return   gt_return.

  READ TABLE gt_return WITH KEY type 'E'.
  IF sy-subrc NE 0.
*     提交凭证过账
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   'X'
      IMPORTING
        return i_return.
    IF i_return IS INITIAL.
      p_data-zmsg '会计凭证过账成功!会计凭证号:' && l_obj_key(10.

      PERFORM frm_ztfi020_lock.
      UPDATE ztfi020 SET zcxbs 'X' WHERE  belnr p_data-belnr
                                        AND bukrs p_data-bukrs
                                        AND gjahr p_data-gjahr
                                        AND monat p_data-monat.
      IF sy-subrc 0.
        COMMIT WORK.
        p_data-icon icon_led_green.
      ELSE.
        ROLLBACK WORK.
        p_data-icon icon_led_yellow.
        p_data-zmsg p_data-zmsg && '数据库ZTFI020(报废成本分摊表)更新失败,请核查!'.
      ENDIF.
      PERFORM frm_ztfi020_unlock.
    ELSE.
      p_data-icon icon_led_red.
      p_data-zmsg '过账失败,请用事务代码ST22查看详细信息'.
    ENDIF.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_data-icon icon_led_red.
    LOOP AT gt_return WHERE type 'A' OR type 'E'.
      p_data-zmsg p_data-zmsg && gt_return-message.
    ENDLOOP.
  ENDIF.
ENDFORM.

你可能感兴趣的:(ABAP,BAPI,FB08)