본문 바로가기
MM/ETC

[MM] Create PO BAPI

by clode 2023. 3. 2.
728x90
반응형

BAPI_PO_CREATE1

  CONSTANTS : LC_TYPE  TYPE ESART VALUE 'NB'.
  DATA : LT_POITEM        LIKE TABLE OF BAPIMEPOITEM,
         LT_POITEMX       LIKE TABLE OF BAPIMEPOITEMX,
         LT_POACCOUNT     LIKE TABLE OF BAPIMEPOACCOUNT  WITH HEADER LINE,
         LT_POACCOUNTX    LIKE TABLE OF BAPIMEPOACCOUNTX WITH HEADER LINE,
         LT_RETURN        LIKE TABLE OF BAPIRET2         WITH HEADER LINE,
         LV_PURCHASEORDER LIKE BAPIMEPOHEADER-PO_NUMBER,
         LS_EXPHEADER     LIKE BAPIMEPOHEADER.
  DATA : LV_EBELP TYPE EBELP.

* 이전 처리 메세지 CLEAR
  CLEAR: GS_0100.
  MODIFY GT_0100 FROM GS_0100 TRANSPORTING ICON MSG
                              WHERE ICON IS NOT INITIAL.


  LOOP AT GT_0100 INTO DATA(LS_0100) WHERE CHECK EQ GC_X.
    "header, item 1:1 관계인 상황
*--------------------------------------------------------------------*
* HEADER
*--------------------------------------------------------------------*
*    IF ls_poheader IS INITIAL.
    DATA(LV_LIFNR)          = |{ LS_0100-LIFNR ALPHA = IN }|.

    DATA(LS_POHEADER) = VALUE BAPIMEPOHEADER(
                              DOC_TYPE    = 
                              VENDOR      = 
                              COMP_CODE   = 
                              PURCH_ORG   = 
                              PUR_GROUP   = 
                              DOC_DATE    = 
                              CREATED_BY  = 
                              CREAT_DATE  = 
                              CURRENCY    = 
                              PMNTTRMS    =  ).

    DATA(LS_POHEADERX) = VALUE BAPIMEPOHEADERX(
                               DOC_TYPE   = GC_X
                               VENDOR     = GC_X
                               COMP_CODE  = GC_X
                               PURCH_ORG  = GC_X
                               PUR_GROUP  = GC_X
                               DOC_DATE   = GC_X
                               CREATED_BY = GC_X
                               CREAT_DATE = GC_X
                               CURRENCY   = GC_X
                               PMNTTRMS   = GC_X ).

*    ENDIF.

*--------------------------------------------------------------------*
* POITEM
*--------------------------------------------------------------------*
    ADD 10 TO LV_EBELP.

    LT_POITEM = VALUE #( BASE LT_POITEM
                  ( PO_ITEM       = LV_EBELP          " 아이템번호
*                    material      =     " 자재코드
*                    short_text    =     " 자재명
                    QUANTITY      =     " 수량
*                    po_unit       = 
                    NET_PRICE     =      " 원가

                    PLANT         = 
                    TAX_CODE      =      " 세금코드
                    ACCTASSCAT    = 
*                    stge_loc      = 
*                    item_cat      = 

                    PREQ_NO      = 
                    PREQ_ITEM    = 
                    MATL_GROUP   = 
                    INFO_REC     = ) ).

    LT_POITEMX = VALUE #( BASE LT_POITEMX
                   ( PO_ITEM       = LV_EBELP
*                     material      = gc_x
*                     short_text    = gc_x
                     QUANTITY      = GC_X
*                     po_unit       = gc_x
                     NET_PRICE     = GC_X

                     PLANT         = GC_X
                     TAX_CODE      = GC_X
                     ACCTASSCAT    = GC_X
*                     stge_loc     = gc_x
*                     item_cat     = gc_x

                     PREQ_NO       = GC_X
                     PREQ_ITEM     = GC_X
                     MATL_GROUP    = GC_X
                     INFO_REC      = GC_X ) ).


*--------------------------------------------------------------------*
* POACCOUNT
*--------------------------------------------------------------------*
*        DATA(lv_kostl)            = |{ kostl ALPHA = IN }|.
*        DATA(lv_sakto)            = |{ sakto ALPHA = IN }|.
*
*        lt_poaccount-po_item      = lv_ebelp.
*        lt_poaccount-costcenter   = lv_kostl.
*        lt_poaccount-gl_account   = lv_sakto.
*
*        APPEND lt_poaccount.
*
*        lt_poaccountx-po_item     = lv_ebelp.
*        lt_poaccountx-costcenter  = abap_true.
*        lt_poaccountx-gl_account  = abap_true.
*
*        APPEND lt_poaccountx.

*--------------------------------------------------------------------*
* BAPI Execute
*--------------------------------------------------------------------*
*    AT END OF banfn.
    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        POHEADER         = LS_POHEADER               " Header Data
        POHEADERX        = LS_POHEADERX              " Header Data (Change Parameter)
      IMPORTING
        EXPPURCHASEORDER = LV_PURCHASEORDER          " Purchasing Document Number
        EXPHEADER        = LS_EXPHEADER              " Purchasing Document Number
      TABLES
        RETURN           = LT_RETURN                 " Return Parameter
        POITEM           = LT_POITEM                 " Item Data
        POITEMX          = LT_POITEMX                " Item Data (Change Parameter)
        POACCOUNT        = LT_POACCOUNT              " Account Assignment Fields
        POACCOUNTX       = LT_POACCOUNTX.            " Account Assignment Fields (Change Parameter)


    READ TABLE LT_RETURN INTO DATA(LS_RETURN) WITH KEY TYPE = GC_E.
    IF SY-SUBRC EQ 0.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      LS_0100 = VALUE #( BASE LS_0100
                          ICON = ICON_LED_RED
                          MSG = LT_RETURN-MESSAGE ).

      "에러메시지 update
      GV_ERROR = GC_X.
      DATA(LV_MSG70) = |{ LT_RETURN-MESSAGE } { TEXT }|. 

    ELSE.
      READ TABLE LT_RETURN INTO LS_RETURN WITH KEY TYPE = GC_S.
      IF SY-SUBRC EQ 0 AND LV_PURCHASEORDER IS NOT INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

        LS_0100 = VALUE #( BASE LS_0100
                            ICON = ICON_LED_GREEN
                            MSG = TEXT-M02 && LV_PURCHASEORDER ).

      ENDIF.
    ENDIF.

    IF SY-SUBRC EQ 0.
      COMMIT WORK.
    ENDIF.

    MODIFY GT_0100 FROM LS_0100 TRANSPORTING ICON MSG.

    CLEAR : LS_POHEADER, LS_POHEADERX, LT_POITEM[], LT_POITEM, LT_POITEMX[], LT_POITEMX,
            LV_PURCHASEORDER, LV_EBELP, LS_0100.
*    ENDAT.

  ENDLOOP.

  WAIT UP TO GC_SECONDS SECONDS.

728x90
반응형

'MM > ETC' 카테고리의 다른 글

[MM] 월별 대비 실적 조회  (0) 2023.03.02
[MM] Release PO BAPI  (0) 2023.03.02
User-exit  (0) 2023.02.27
Batch Job  (0) 2023.02.24
CTS  (0) 2023.02.21

댓글