ABAP 创建及修改采购申请

创建采购申请: BAPI_PR_CREATE

修改采购申请:BAPI_PR_CHANGE

审批采购申请:BAPI_REQUISITION_RELEASE (按行)

取消审批采购申请:BAPI_REQUISITION_RESET_RELEASE(按行)

创建示例代码如下:

  DATA: LT_TEMP TYPE STANDARD TABLE OF TY_DATA,
        LS_TEMP TYPE TY_DATA.
  DATA: LV_FLAG TYPE CHAR01.
  DATA: LS_PRHEADER            TYPE BAPIMEREQHEADER,
        LS_PRHEADERX           TYPE BAPIMEREQHEADERX,
        LT_PRITEM              TYPE TABLE OF BAPIMEREQITEMIMP,
        LS_PRITEM              TYPE BAPIMEREQITEMIMP,
        LT_PRITEMX             TYPE TABLE OF BAPIMEREQITEMX,
        LS_PRITEMX             TYPE BAPIMEREQITEMX,
        LT_PRACCOUNT           TYPE TABLE OF BAPIMEREQACCOUNT,
        LS_PRACCOUNT           TYPE BAPIMEREQACCOUNT,
        LT_PRACCOUNTX          TYPE TABLE OF BAPIMEREQACCOUNTX,
        LS_PRACCOUNTX          TYPE BAPIMEREQACCOUNTX,
        LT_HEADERTEXT          TYPE TABLE OF BAPIMEREQHEADTEXT,
        LS_HEADERTEXT          TYPE BAPIMEREQHEADTEXT,
        LT_EXTENSIONIN         TYPE TABLE OF  BAPIPAREX,
        LS_EXTENSIONIN         TYPE BAPIPAREX,
        LT_REQUISITION_ACCOUNT TYPE TABLE OF BAPIEBKN,
        LS_REQUISITION_ACCOUNT TYPE BAPIEBKN,
        LS_BAPI_TE_MEREQITEM   TYPE BAPI_TE_MEREQITEM,
        LS_BAPI_TE_MEREQITEMX  TYPE BAPI_TE_MEREQITEMX.
  DATA: LT_PRCOMPONENTS        TYPE STANDARD TABLE OF  BAPIMEREQCOMPONENT.
  DATA: LS_PRCOMPONENTS        TYPE BAPIMEREQCOMPONENT.
  DATA: LT_PRCOMPONENTSX       TYPE STANDARD TABLE OF   BAPIMEREQCOMPONENTX.
  DATA: LS_PRCOMPONENTSX       TYPE BAPIMEREQCOMPONENTX.
  DATA: LV_BANFN   TYPE BANFN,
        LV_MESSAGE TYPE STRING,
        LV_BNFPO   TYPE BNFPO,
        LT_RETURN  TYPE TABLE OF BAPIRET2,
        LS_RETURN  TYPE BAPIRET2.
  DATA: LS_COMPONENTS TYPE TY_COMPONENTS.
  DATA: LS_EBKN TYPE TY_EBKN.
  DATA: LV_SERIAL_NO TYPE DZEKKN.

  LT_TEMP = GT_DATA.

  DELETE LT_TEMP WHERE BOX IS INITIAL.
  DELETE LT_TEMP WHERE BANFN IS NOT INITIAL.
  SORT LT_TEMP BY  BSART AFNAM.
  DELETE ADJACENT DUPLICATES FROM LT_TEMP COMPARING BSART AFNAM .


  LOOP AT LT_TEMP INTO LS_TEMP WHERE BOX IS NOT INITIAL.
    CLEAR: LV_BANFN,LV_MESSAGE,LV_BNFPO,LT_RETURN,LS_RETURN,
    LS_PRHEADER,LS_PRHEADERX,LT_PRITEM,LT_PRITEMX,LT_PRACCOUNT,LT_PRACCOUNTX,
    LS_PRITEM,LS_PRITEMX,LS_PRACCOUNT,LS_PRACCOUNTX,LT_PRCOMPONENTS,LS_PRCOMPONENTS,
    LT_PRCOMPONENTSX,LS_PRCOMPONENTSX.

    LS_PRHEADER-PR_TYPE = LS_TEMP-BSART.  "采购申请类型
    LS_PRHEADERX-PR_TYPE = 'X'.           "采购申请类型标识

    LOOP AT GT_DATA INTO GS_DATA WHERE BSART = LS_TEMP-BSART AND AFNAM = LS_TEMP-AFNAM AND BOX IS NOT INITIAL AND BANFN IS INITIAL .
      CLEAR:LS_PRITEM.
      LV_BNFPO = LV_BNFPO + 10.
      GS_DATA-BNFPO = LV_BNFPO.
      LS_PRITEM-PREQ_ITEM  = LV_BNFPO.       "行号
      LS_PRITEM-MATERIAL   = GS_DATA-MATNR.  "物料号
      LS_PRITEM-SHORT_TEXT = GS_DATA-TXZ01.  "短文本
      LS_PRITEM-PLANT      = GS_DATA-WERKS.  "工厂
      LS_PRITEM-PUR_GROUP  = 'WS1'.          "采购组织
      LS_PRITEM-QUANTITY   = GS_DATA-MENGE.  "申请数量
      LS_PRITEM-UNIT       = GS_DATA-MEINS.  "单位
      LS_PRITEM-MATL_GROUP = GS_DATA-MATKL.  "物料组
      LS_PRITEM-DELIV_DATE = GS_DATA-LFDAT.  "交货日期
      LS_PRITEM-PREQ_NAME  = GS_DATA-AFNAM.  "申请人
      LS_PRITEM-PREQ_PRICE = GS_DATA-PREIS.  "评估价格
      LS_PRITEM-CURRENCY   = GS_DATA-WAERS.  "货币
      LS_PRITEM-ACCTASSCAT = GS_DATA-KNTTP.  "科目分配类别
      LS_PRITEM-ITEM_CAT   = GS_DATA-PSTYP.  "项目类别
      LS_PRITEM-TRACKINGNO = GS_DATA-BEDNR.  "需求跟踪号
     "K类别的采购
      IF GS_DATA-KOSTL IS NOT INITIAL AND GS_DATA-BSART = 'Z002'.
        LS_PRACCOUNT-PREQ_ITEM    =  LV_BNFPO. "采购申请行项目
        LS_PRACCOUNT-SERIAL_NO    = '01'.
        LS_PRACCOUNT-QUANTITY     = GS_DATA-MENGE.  "申请数量
        LS_PRACCOUNT-COSTCENTER   = GS_DATA-KOSTL .
        LS_PRACCOUNT-CO_AREA      = GS_DATA-KOKRS .
        LS_PRACCOUNT-GL_ACCOUNT   = GS_DATA-SAKTO.
        APPEND LS_PRACCOUNT TO LT_PRACCOUNT.


        LS_PRACCOUNTX-PREQ_ITEM    =  LV_BNFPO. "采购申请行项目
        LS_PRACCOUNTX-SERIAL_NO     = '01'.
        LS_PRACCOUNTX-QUANTITY     = 'X'. "申请数量
        LS_PRACCOUNTX-COSTCENTER   = 'X'.
        LS_PRACCOUNTX-CO_AREA      = 'X'.
        "LS_PRACCOUNTX-ASSET_NO   = 'X'.
        LS_PRACCOUNTX-GL_ACCOUNT   = 'X'.
        APPEND LS_PRACCOUNTX TO LT_PRACCOUNTX.

      ENDIF.

      IF GS_DATA-BSART = 'Z004'.
        CLEAR:LV_SERIAL_NO.
        LOOP AT GS_DATA-EBKN INTO LS_EBKN.
          LV_SERIAL_NO = LV_SERIAL_NO + 1.
          LS_PRACCOUNT-PREQ_ITEM    =  LV_BNFPO. "采购申请行项目
          LS_PRACCOUNT-SERIAL_NO    = LV_SERIAL_NO.
          LS_PRACCOUNT-QUANTITY     = LS_EBKN-MENGE.  "申请数量
          LS_PRACCOUNT-COSTCENTER   = GS_DATA-KOSTL .
          LS_PRACCOUNT-CO_AREA      = GS_DATA-KOKRS .
          LS_PRACCOUNT-GL_ACCOUNT   = LS_EBKN-SAKTO.
          LS_PRACCOUNT-ASSET_NO     = LS_EBKN-ANLN1 .
          APPEND LS_PRACCOUNT TO LT_PRACCOUNT.


          LS_PRACCOUNTX-PREQ_ITEM    =  LV_BNFPO. "采购申请行项目
          LS_PRACCOUNTX-SERIAL_NO     = LV_SERIAL_NO.
          LS_PRACCOUNTX-QUANTITY     = 'X'. "申请数量
          LS_PRACCOUNTX-COSTCENTER   = 'X'.
          LS_PRACCOUNTX-CO_AREA      = 'X'.
          LS_PRACCOUNTX-ASSET_NO     = 'X'.
          LS_PRACCOUNTX-GL_ACCOUNT   = 'X'.
          APPEND LS_PRACCOUNTX TO LT_PRACCOUNTX.
        ENDLOOP.


      ENDIF.


      LS_PRITEMX-PREQ_ITEM  =  LV_BNFPO.
      LS_PRITEMX-PREQ_ITEMX =  'X'.
      LS_PRITEMX-MATERIAL   =  'X'.
      LS_PRITEMX-SHORT_TEXT =  'X'.
      LS_PRITEMX-PLANT      =  'X'.
      LS_PRITEMX-MATL_GROUP =  'X'.
      LS_PRITEMX-PUR_GROUP  =  'X'.
      LS_PRITEMX-QUANTITY   =  'X'.
      LS_PRITEMX-UNIT    =  'X'.
      LS_PRITEMX-DELIV_DATE =  'X'.
      LS_PRITEMX-PREQ_NAME  =  'X'.
      LS_PRITEMX-PREQ_PRICE =  'X'.
      LS_PRITEMX-CURRENCY   =  'X'.
      LS_PRITEMX-ACCTASSCAT =  'X'.
      LS_PRITEMX-ITEM_CAT   =  'X'.
      LS_PRITEMX-TRACKINGNO =  'X'.


      APPEND LS_PRITEM TO LT_PRITEM.
      APPEND LS_PRITEMX TO LT_PRITEMX.


     "带bom的采购申请
      IF GS_DATA-BSART = 'Z003'.

        LOOP AT GS_DATA-COMPONENTS INTO LS_COMPONENTS.
          CLEAR:LS_PRCOMPONENTS,LS_PRCOMPONENTSX.

          LS_PRCOMPONENTS-PREQ_ITEM       = LV_BNFPO.
          LS_PRCOMPONENTS-ITEM_NO         = LS_COMPONENTS-RSPOS.
          LS_PRCOMPONENTS-MATERIAL        = LS_COMPONENTS-MATNR.
          LS_PRCOMPONENTS-ENTRY_QUANTITY  = LS_COMPONENTS-BDMNG.
          LS_PRCOMPONENTS-ENTRY_UOM       = LS_COMPONENTS-MEINS.
          LS_PRCOMPONENTS-PLANT           = GS_DATA-WERKS.
          LS_PRCOMPONENTS-CHANGE_ID       = 'I'.
          LS_PRCOMPONENTS-ITEM_CAT        = GS_DATA-PSTYP.  "项目类别
          APPEND LS_PRCOMPONENTS TO LT_PRCOMPONENTS.



          LS_PRCOMPONENTSX-PREQ_ITEM       = LV_BNFPO.
          LS_PRCOMPONENTSX-ITEM_NO         = LS_COMPONENTS-RSPOS.
          LS_PRCOMPONENTSX-MATERIAL        = 'X'.
          LS_PRCOMPONENTSX-ENTRY_QUANTITY  = 'X'.
          LS_PRCOMPONENTSX-ENTRY_UOM       = 'X'.
          LS_PRCOMPONENTSX-PLANT           = 'X'.
          LS_PRCOMPONENTSX-CHANGE_ID       = 'X'.
          LS_PRCOMPONENTSX-ITEM_CAT        = 'X'.
          APPEND LS_PRCOMPONENTSX TO LT_PRCOMPONENTSX.
        ENDLOOP.


      ENDIF.


      MODIFY GT_DATA FROM GS_DATA.
    ENDLOOP.


    "调用BAPI创建采购申请
    CALL FUNCTION 'BAPI_PR_CREATE'
      EXPORTING
        PRHEADER      = LS_PRHEADER
        PRHEADERX     = LS_PRHEADERX
*       TESTRUN       = TESTRUN
      IMPORTING
        NUMBER        = LV_BANFN
      TABLES
        RETURN        = LT_RETURN
        PRITEM        = LT_PRITEM
        PRITEMX       = LT_PRITEMX
        PRACCOUNT     = LT_PRACCOUNT
        PRACCOUNTX    = LT_PRACCOUNTX
        PRCOMPONENTS  = LT_PRCOMPONENTS
        PRCOMPONENTSX = LT_PRCOMPONENTSX
        PRHEADERTEXT  = LT_HEADERTEXT
        EXTENSIONIN   = LT_EXTENSIONIN.
    LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE CA 'AEX'.
      LV_MESSAGE = LV_MESSAGE  && LS_RETURN-MESSAGE.
    ENDLOOP.

    IF LV_MESSAGE IS NOT INITIAL .
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      LOOP AT GT_DATA INTO GS_DATA WHERE BSART = LS_TEMP-BSART AND AFNAM = LS_TEMP-AFNAM AND BOX IS NOT INITIAL AND BANFN IS INITIAL ..
        GS_DATA-MSG = LV_MESSAGE.
        GS_DATA-ICON  = ICON_LED_RED.
        MODIFY GT_DATA FROM GS_DATA.
      ENDLOOP.

    ELSE.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
      LOOP AT GT_DATA INTO GS_DATA WHERE BSART = LS_TEMP-BSART AND AFNAM = LS_TEMP-AFNAM AND BOX IS NOT INITIAL AND BANFN IS INITIAL .
        CLEAR:GS_DATA-EDIT,GS_DATA-MSG.
        GS_DATA-BANFN = LV_BANFN.
        GS_DATA-ICON  = ICON_LED_GREEN.
        MODIFY GT_DATA FROM GS_DATA.
      ENDLOOP.
    ENDIF.
  ENDLOOP.


你可能感兴趣的:(ABAP,MM,蓝桥杯,职场和发展)