Make your own free website on Tripod.com
SAP ABAP Programming, Functional and Basis Components Books


SAP BAPI - Create Reservation with authorization checks for Ext. Material Group

This abap program have similiar functionality as transaction code 'MB21'.  It make use of the ABAP BAPI functions 'BAPI_RESERVATION_CREATE'.  It have more AUTHORITY-CHECK than 'MB21'.  The purpose is to avoid changing the SAP standard program.

REPORT ZRES_311.

tables : RESB,
         T001W,
         MARA.

** Internal tables for BAPI call
data: begin of gm_header.
        include structure BAPIRKPFC.
data: end of gm_header.

data: begin of gm_item occurs 0.
        include structure BAPIRESBC.
data: end of gm_item.

data: begin of zreturn occurs 0.
        include structure BAPIRETURN.
data: end of zreturn.

DATA: AUTH_FLG(1).

PARAMETERS X_BDTER LIKE RESB-BDTER DEFAULT SY-DATUM.
PARAMETERS X_WERKS LIKE RESB-WERKS.
PARAMETERS X_BWART LIKE RESB-BWART.
PARAMETERS X_REC01 LIKE RESB-UMLGO.
PARAMETERS X_ISS01 LIKE RESB-UMLGO.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 16(10) TEXT-010.
SELECTION-SCREEN COMMENT 37(10) TEXT-020.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M01 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q01 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M02 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q02 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M03 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q03 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M04 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q04 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M05 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q05 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M06 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q06 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M07 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q07 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M08 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q08 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M09 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q09 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.
SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 05(10) TEXT-040.
PARAMETERS X_M10 LIKE RESB-MATNR.
SELECTION-SCREEN COMMENT 35(01) TEXT-050.
PARAMETERS X_Q10 LIKE RESB-ERFMG.
SELECTION-SCREEN END OF LINE.

PERFORM AUTHORITY_CHECK.
PERFORM GM_DATA.
* CHECK WHETHER TABLE IS EMPTY
IF GM_ITEM[] is initial.
   FORMAT COLOR COL_NEGATIVE.
   WRITE: / 'All Material are EMPTY'.
ELSEIF AUTH_FLG = 'X'.
   FORMAT COLOR COL_NEGATIVE.
   WRITE: / 'Please obtain all your necessary Authorization'.
ELSE.
   PERFORM BAPI_CREATE_311.
ENDIF.

FORM AUTHORITY_CHECK.
   CLEAR AUTH_FLG.
   FORMAT COLOR COL_NEGATIVE.

   AUTHORITY-CHECK OBJECT 'M_MRES_WWA'
   ID 'ACTVT' FIELD '03'
   ID 'WERKS' FIELD X_WERKS.
   IF SY-SUBRC NE 0.
      AUTH_FLG = 'X'.
      WRITE:/ 'You are not authorised for Plant :',  X_WERKS.
   ENDIF.

   AUTHORITY-CHECK OBJECT 'M_MRES_BWA'
   ID 'ACTVT' FIELD '03'
   ID 'BWART' FIELD X_BWART.
   IF SY-SUBRC NE 0.
      AUTH_FLG = 'X'.
      WRITE:/ 'You are not authorised for Movement Type :',  X_BWART.
   ENDIF.

   AUTHORITY-CHECK OBJECT 'M_MSEG_LGO'
   ID 'ACTVT' FIELD '03'
   ID 'BWART' FIELD X_REC01.
   IF SY-SUBRC NE 0.
      AUTH_FLG = 'X'.
      WRITE:/ 'You are not authorised for Storage Location :',  X_REC01.
   ENDIF.

   AUTHORITY-CHECK OBJECT 'M_MSEG_LGO'
   ID 'ACTVT' FIELD '03'
   ID 'BWART' FIELD X_ISS01.
   IF SY-SUBRC NE 0.
      AUTH_FLG = 'X'.
      WRITE:/ 'You are not authorised for Storage Location :',  X_ISS01.
   ENDIF.

   FORMAT COLOR OFF.
ENDFORM.

FORM GM_DATA.
     REFRESH GM_ITEM.
     CLEAR   GM_HEADER.

     GM_HEADER-RES_DATE   = X_BDTER.
     GM_HEADER-PLANT      = X_WERKS.
     GM_HEADER-MOVE_PLANT = X_WERKS.
     GM_HEADER-MOVE_STLOC = X_REC01.
     GM_HEADER-MOVE_TYPE  = X_BWART.
     GM_HEADER-CREATED_BY = SY-UNAME.
     TRANSLATE GM_HEADER TO UPPER CASE.

     GM_ITEM-REQ_DATE     = X_BDTER.
     GM_ITEM-MOVEMENT     = 'X'.
     GM_ITEM-PLANT        = X_WERKS.
     GM_ITEM-STORE_LOC    = X_ISS01.

     IF X_M01 <> SPACE.
        GM_ITEM-MATERIAL     = X_M01.
        GM_ITEM-QUANTITY     = X_Q01.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M02 <> SPACE.
        GM_ITEM-MATERIAL     = X_M02.
        GM_ITEM-QUANTITY     = X_Q02.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M03 <> SPACE.
        GM_ITEM-MATERIAL     = X_M03.
        GM_ITEM-QUANTITY     = X_Q03.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M04 <> SPACE.
        GM_ITEM-MATERIAL     = X_M04.
        GM_ITEM-QUANTITY     = X_Q04.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M05 <> SPACE.
        GM_ITEM-MATERIAL     = X_M05.
        GM_ITEM-QUANTITY     = X_Q05.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M06 <> SPACE.
        GM_ITEM-MATERIAL     = X_M06.
        GM_ITEM-QUANTITY     = X_Q06.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M07 <> SPACE.
        GM_ITEM-MATERIAL     = X_M07.
        GM_ITEM-QUANTITY     = X_Q07.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M08 <> SPACE.
        GM_ITEM-MATERIAL     = X_M08.
        GM_ITEM-QUANTITY     = X_Q08.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M09 <> SPACE.
        GM_ITEM-MATERIAL     = X_M09.
        GM_ITEM-QUANTITY     = X_Q09.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.
     IF X_M10 <> SPACE.
        GM_ITEM-MATERIAL     = X_M10.
        GM_ITEM-QUANTITY     = X_Q10.
        PERFORM AUTHORITY_MARA.
        TRANSLATE GM_ITEM TO UPPER CASE.
        APPEND GM_ITEM.
     ENDIF.

ENDFORM.

FORM AUTHORITY_MARA.
   FORMAT COLOR COL_NEGATIVE.

   CLEAR MARA.
   SELECT SINGLE * FROM MARA WHERE MATNR = GM_ITEM-MATERIAL.

   AUTHORITY-CHECK OBJECT 'Z:EXTWG'
   ID 'ACTVT' FIELD '03'
   ID 'EXTWG' FIELD MARA-EXTWG.
   IF SY-SUBRC NE 0.
      AUTH_FLG = 'X'.
      WRITE:/ 'You are not authorised for this Parts :',
              MARA-MATNR, MARA-EXTWG.
   ENDIF.

   FORMAT COLOR OFF.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM bapi_create_311                                          *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form bapi_create_311.
  data: RES_NO like BAPIRKPFC-RES_NO,
        TLINES TYPE I.

  REFRESH ZRETURN.
  CLEAR   RES_NO.

call function 'BAPI_RESERVATION_CREATE'
  exporting
    reservation_header       = GM_HEADER
*   NO_COMMIT                =
  IMPORTING
    RESERVATION              = RES_NO
  tables
    reservation_items        = GM_ITEM
    RETURN                   = ZRETURN.

  COMMIT WORK.

  DESCRIBE TABLE ZRETURN LINES TLINES.
  IF TLINES GT 0.
    format color col_negative.
    write : / 'Update Failed.', zreturn-message(200), 255 ' '.
  ELSE.
    format color col_normal.
    WRITE : / RES_NO,
    'Reservation created successfully ', 255 ' '.
  ENDIF.
  FORMAT COLOR OFF.

* Delay
  CALL FUNCTION 'FMCT_WAIT_FOR_UPDATE'
         EXPORTING
              I_SEC = 4.

endform.

INITIALIZATION.
  CLEAR:
         X_M01, X_Q01,
         X_M02, X_Q02,
         X_M03, X_Q03,
         X_M04, X_Q04,
         X_M05, X_Q05.

  CASE SY-UNAME.

     WHEN 'USR01'.
         X_ISS01 = 'AAA'.

     WHEN 'USR02'.
         X_ISS01 = 'BBB'.

     WHEN OTHERS.
         X_ISS01 = 'CCC'.

  ENDCASE.

Back to Previous

Return to SAP Hints and Tips on Configuration and ABAP/4 Programming