메뉴 건너뛰기

SAP 한국 커뮤니티

BAPI_PO_CREATE1 구매오더 생성시에 사용하세요

노름마치 2007.12.11 06:39 조회 수 : 9778 추천:2

*&---------------------------------------------------------------------*
*& BAPI를 이용하여 STO 생성시 필요한 변수, 내부테이블
*&---------------------------------------------------------------------*
DATA : gs_poheader     LIKE  bapimepoheader.
DATA : gs_poheaderx    LIKE  bapimepoheaderx.
DATA : gs_expheader    LIKE  bapimepoheader.


DATA : gt_poitem       LIKE  bapimepoitem     OCCURS 0 WITH HEADER LINE.
DATA : gt_poitemx      LIKE  bapimepoitemx    OCCURS 0 WITH HEADER LINE.
DATA : gt_poschedule   LIKE  bapimeposchedule OCCURS 0 WITH HEADER LINE.
DATA : gt_poschedulex  LIKE  bapimeposchedulx OCCURS 0 WITH HEADER LINE.


DATA : gt_return       LIKE  bapiret2         OCCURS 0 WITH HEADER LINE.


DATA : gi_werks   LIKE  mard-werks VALUE '2000'," 출고 플랜트(공급 플랜트)
       gr_werks   LIKE  mard-werks,             " 입고 플랜트, 2010 ~ 2089
       gr_eeind   LIKE  sy-datum,               " 입고 요청일


       g_bsart    LIKE  ekko-bsart VALUE 'UB',  " 구매문서유형(UB, 재고이동)
       g_bukrs    LIKE  ekko-bukrs VALUE ' '


       g_ekorg    LIKE  ekko-ekorg VALUE ' ',


       g_ekgrp    LIKE  ekko-ekgrp VALUE '004'.


*&---------------------------------------------------------------------*
*&      Form  create_stock_transfer_order
*&---------------------------------------------------------------------*
FORM create_stock_transfer_order .


*" 구매품목 및 SCHEDULE LINE
  DATA : l_ebelp    TYPE ebelp.       " 구매문서 품목번호
  DATA : l_etenr    TYPE etenr.       " 일정라인
  DATA : l_zlet0323 LIKE po_zlet0323. " 출고플랜트별 발주생성을 위해
  DATA : l_plndt    TYPE sy-datum.    " 납품예정일 다시계산


  CLEAR : gs_poheader,      gs_poheaderx, gs_expheader,
          gt_poitem[],      gt_poitemx[], gt_poschedule[],
          gt_poschedulex[], gt_return[].
  CLEAR : l_ebelp, l_etenr.



*" 출고플랜트와 입고 플랜트가 다른면 오더를 신규로 생성한다.......
  SORT po_zlet0323 BY werksumwrk matnr.


  LOOP AT po_zlet0323.
    l_zlet0323 = po_zlet0323.


*"  출고 또는 입고플랜트가 다르면 개별발주로 생성한다....
*"  헤더 구성.................................................................
    AT NEW werksumwrk.
      gs_poheader-comp_code  =  g_bukrs.  " 회사코드
      gs_poheader-doc_type   =  g_bsart.  " 구매유형
      gs_poheader-purch_org  =  g_ekorg.  " 구매조직
      gs_poheader-pur_group  =  g_ekgrp.  " 구매그룹
      gs_poheader-suppl_plnt =  l_zlet0323-werks.       " 출고플랜트
      GET PARAMETER ID 'ZUSER' FIELD gs_poheader-ref_1. " 서브로그온 아이디


      gs_poheaderx-comp_code      =  'X'.
      gs_poheaderx-doc_type       =  'X'.
      gs_poheaderx-purch_org      =  'X'.
      gs_poheaderx-pur_group      =  'X'.
      gs_poheaderx-suppl_plnt     =  'X'.
    ENDAT.



*"  아이템 구성.............................................................
    ADD 10 TO l_ebelp.


    gt_poitem-po_item        =  l_ebelp.             " 구매항번
    gt_poitem-material       =  l_zlet0323-matnr.    " 자재코드
    gt_poitem-plant          =  l_zlet0323-umwrk.    " 입고확정플랜트
    gt_poitem-stge_loc       =  g_lgort.             " 입고저장위치
    gt_poitem-quantity       =  l_zlet0323-cnfqt.    " 발주수량


    gt_poitemx-po_item       =  l_ebelp.             " 구매항번
    gt_poitemx-po_itemx      =  'X'.
    gt_poitemx-material      =  'X'.
    gt_poitemx-plant         =  'X'.
    gt_poitemx-stge_loc      =  'X'.
    gt_poitemx-quantity      =  'X'.


    ADD 1 TO l_etenr.
    gt_poschedule-po_item         =  l_ebelp.  " 구매항번
    gt_poschedule-sched_line      =  l_etenr.  " 납품항번
    gt_poschedulex-po_item        =  l_ebelp.  " 구매항번
    gt_poschedulex-sched_line     =  l_etenr.  " 납품항번
    gt_poschedulex-delivery_date  =  'X'.


    IF l_zlet0323-plndt =< sy-datum.
      CLEAR l_plndt.
      l_plndt = sy-datum + 1.
      gt_poschedule-delivery_date =  l_plndt.           " 납품예정일
    ELSE.
      gt_poschedule-delivery_date =  l_zlet0323-plndt.  " 납품예정일
    ENDIF.


    APPEND : gt_poitem,     gt_poitemx,
             gt_poschedule, gt_poschedulex.



    AT END OF werksumwrk.
      CLEAR : gs_expheader, gt_return[].
      CALL FUNCTION 'BAPI_PO_CREATE1'
        EXPORTING
          poheader    = gs_poheader
          poheaderx   = gs_poheaderx
        IMPORTING
          expheader   = gs_expheader
        TABLES
          return      = gt_return[]
          poitem      = gt_poitem[]
          poitemx     = gt_poitemx[]
          poschedule  = gt_poschedule[]
          poschedulex = gt_poschedulex[].


      IF NOT gs_expheader-po_number IS INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ENDIF.


      READ TABLE gt_return INDEX 1.


      po_zlet0323-ebeln  = gs_expheader-po_number.
      po_zlet0323-msglin = gt_return-message(100).
      IF gs_expheader-po_number IS INITIAL.
        po_zlet0323-msgty = 'E'.
      ELSE.
        po_zlet0323-msgty = 'S'.
      ENDIF.


      po_zlet0323-crtdt = sy-datum. " 생성일자
      po_zlet0323-crttm = sy-uzeit. " 생성시간
      GET PARAMETER ID 'ZUSER' FIELD po_zlet0323-crtnm. " 생성자


      MODIFY po_zlet0323 TRANSPORTING ebeln
                                      msgty
                                      msglin
                                      crtdt
                                      crttm
                                      crtnm
                                WHERE werks  = l_zlet0323-werks
                                  AND umwrk  = l_zlet0323-umwrk.


      CLEAR : gs_poheader, gs_poheaderx, gs_expheader,
              gt_poitem[], gt_poitemx[], gt_poschedule[],
              gt_poschedulex[], gt_return[].


      CLEAR : l_ebelp, l_etenr.
    ENDAT.
  ENDLOOP.