1. 程式人生 > >根據銷售訂單建立交貨單並自動揀配、過賬發貨程式

根據銷售訂單建立交貨單並自動揀配、過賬發貨程式

Code listing for: ZSD020
Description: 根據銷售訂單建立交貨單並過賬發貨
*&---------------------------------------------------------------------*
* 程式名稱: 根據銷售訂單建立交貨單並過賬發貨
* 程式名  :ZSD020
* 開發日期:2013-08-26
* 建立者  :
* 申請者  :
*----------------------------------------------------------------------*
* 概 要  :
*&---------------------------------------------------------------------*
REPORT ZSD020. ***************************************************************** *TYPE DECLARATION ***************************************************************** TYPES:BEGIN OF TY_TAB_UPLOAD, "上傳檔案接收表 SHIP_POINT TYPE BAPIDLVCREATEHEADER-SHIP_POINT, "裝運點 VBELN(10) TYPE C, "訂單號 REF_ITEM TYPE RFPOS_VL, "專案 DLV_QTY TYPE LFIMG, "交貨數量 MESSAGE TYPE STRING, END OF TY_TAB_UPLOAD. TYPES:BEGIN OF TY_VBAP, VRKME TYPE VBAP-VRKME, END OF TY_VBAP. *****************************************************************
*TYPE DECLARATION ***************************************************************** TABLES: VBAP. ************************************************************************ * INTERNAL TABLE DECLARATION ************************************************************************ DATA: IT_TAB_UPLOAD TYPE STANDARD TABLE OF TY_TAB_UPLOAD, "上傳檔案接收表 IT_INTERN LIKE STANDARD TABLE OF ALSMEX_TABLINE, "EXCEL檔案接收表 SALES_ITEMS LIKE STANDARD TABLE OF BAPIDLVREFTOSALESORDER, "BAPI輸入表: IT_RETURN LIKE STANDARD TABLE OF BAPIRET2, "BAPI返回引數 IT_SERIAL_NUMBERS LIKE STANDARD TABLE OF BAPIDLVSERIALNUMBER, "BAPI返回引數 IT_EXTENSION_IN LIKE STANDARD TABLE OF BAPIPAREX, "BAPI返回引數 IT_DELIVERIES LIKE STANDARD TABLE OF BAPISHPDELIVNUMB, "BAPI返回引數 IT_CREATED_ITEMS LIKE STANDARD TABLE OF BAPIDLVITEMCREATED, "BAPI返回引數 IT_EXTENSION_OUT LIKE STANDARD TABLE OF BAPIPAREX. "BAPI返回引數 ************************************************************************
* WORKAREA DECLARATION ************************************************************************ DATA: WA_VBAP TYPE TY_VBAP, WA_TAB_UPLOAD TYPE TY_TAB_UPLOAD, "上傳檔案接收表 WA_SALES_ITEMS TYPE BAPIDLVREFTOSALESORDER, "BAPI輸入表: G_SHIP_POINT TYPE BAPIDLVCREATEHEADER-SHIP_POINT, "裝運點 WA_INTERN LIKE ALSMEX_TABLINE, "EXCEL引數表 G_DELIVERY TYPE BAPISHPDELIVNUMB-DELIV_NUMB, "BAPI返回引數 G_NUM_DELIVERIES TYPE BAPIDLVCREATEHEADER-NUM_DELIVERIES, "BAPI返回引數 WA_CREATED_ITEMS LIKE BAPIDLVITEMCREATED, WA_RETURN LIKE BAPIRET2. ************************************************************************ * SELECTION SCREEN DECLARATION ************************************************************************ SELECTION-SCREEN BEGIN OF BLOCK BLK. PARAMETERS: P_FIN TYPE RLGRAP-FILENAME OBLIGATORY. "檔名 SELECTION-SCREEN END OF BLOCK BLK. ************************************************************************ * AT SELECTION SCREEN ************************************************************************ AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FIN. PERFORM FRM_F4_HELP CHANGING P_FIN. "檔案瀏覽 ************************************************************************ * EVENT START OF SELECTION ************************************************************************ START-OF-SELECTION. PERFORM FM_UPLOAD_DATA. "上傳檔案 *&---------------------------------------------------------------------* * FORM ADD_ZERO *&---------------------------------------------------------------------* * 增加前導零 *&---------------------------------------------------------------------* DEFINE ADD_ZERO. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING INPUT = &1 IMPORTING OUTPUT = &1. END-OF-DEFINITION. *&---------------------------------------------------------------------* * FORM DELIVERY *&---------------------------------------------------------------------* * BAPI發貨過賬 * 輸入引數:交貨單號 P_VBELN *&---------------------------------------------------------------------* FORM  DELIVERY USING P_VBELN TYPE VBELN.   DATA:G_LIKP LIKE  LIKP.   DATA:G_LIPS LIKE  LIPS.   DATA GV_14(14TYPE C.   DATABEGIN OF I_VBKOK OCCURS 11.           INCLUDE STRUCTURE VBKOK.   DATAEND OF I_VBKOK.   DATABEGIN OF I_VBPOK_TAB OCCURS 12.           INCLUDE STRUCTURE VBPOK.   DATAEND OF I_VBPOK_TAB.   DATA:EF_ERROR_ANY TYPE XFELD.   SELECT SINGLE INTO G_LIKP FROM LIKP       WHERE VBELN P_VBELN.   I_VBKOK-VBELN_VL        P_VBELN.        "DELIVERY NUMBER   I_VBKOK-WABUC           'X'.            "AUTOMATIC PGI   I_VBKOK-WADAT_IST       G_LIKP-BLDAT.   APPEND I_VBKOK.   SELECT INTO G_LIPS FROM LIPS     WHERE VBELN P_VBELN.     I_VBPOK_TAB-VBELN_VL    P_VBELN.      "DELIVERY NUMBER     I_VBPOK_TAB-POSNR_VL    G_LIPS-POSNR.     I_VBPOK_TAB-VBELN       P_VBELN.     I_VBPOK_TAB-POSNN       G_LIPS-POSNR.     I_VBPOK_TAB-MATNR       G_LIPS-MATNR.     GV_14                   G_LIPS-LFIMG.     CONDENSE GV_14.     I_VBPOK_TAB-PIKMG       GV_14 .     APPEND I_VBPOK_TAB.   ENDSELECT.   CALL FUNCTION 'WS_DELIVERY_UPDATE'     EXPORTING       VBKOK_WA       I_VBKOK       COMMIT         'X'       DELIVERY       P_VBELN       UPDATE_PICKING 'X'      IMPORTING      EF_ERROR_ANY_0  EF_ERROR_ANY     TABLES       VBPOK_TAB      I_VBPOK_TAB     EXCEPTIONS       ERROR_MESSAGE  1       OTHERS         2.   IF EF_ERROR_ANY 'X'.     WRITE:/ P_VBELN ' 發貨過賬失敗!可能需要手動發貨過賬'.   ELSE.     WRITE:/ P_VBELN ' 發貨過賬成功!'.   ENDIF. ENDFORM.                    "DELIVERY *&---------------------------------------------------------------------* * FORM FM_UPLOAD_DATA *&---------------------------------------------------------------------* * 上傳檔案 *&---------------------------------------------------------------------* FORM FM_UPLOAD_DATA. CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING FILENAME = P_FIN I_BEGIN_COL = 1 I_BEGIN_ROW = 1 I_END_COL = 255 I_END_ROW = 65536 TABLES INTERN = IT_INTERN EXCEPTIONS INCONSISTENT_PARAMETERS = 1 UPLOAD_OLE = 2 OTHERS = 3. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. DELETE IT_INTERN WHERE ROW = 1. "刪除欄位名行 LOOP AT IT_INTERN INTO WA_INTERN. "將EXCEL資訊儲存至內表IT_TAB_UPLOAD CASE WA_INTERN-COL. WHEN 1. WA_TAB_UPLOAD-SHIP_POINT = WA_INTERN-VALUE. WHEN 2. WA_TAB_UPLOAD-VBELN = WA_INTERN-VALUE. ADD_ZERO WA_TAB_UPLOAD-VBELN. WHEN 3. WA_TAB_UPLOAD-REF_ITEM = WA_INTERN-VALUE. WHEN 4. WA_TAB_UPLOAD-DLV_QTY = WA_INTERN-VALUE. APPEND WA_TAB_UPLOAD TO IT_TAB_UPLOAD. ENDCASE. ENDLOOP. LOOP AT IT_TAB_UPLOAD INTO WA_TAB_UPLOAD. "填充BAPI引數表:SALES_ITEMS G_SHIP_POINT = WA_TAB_UPLOAD-SHIP_POINT. WA_SALES_ITEMS-REF_DOC = WA_TAB_UPLOAD-VBELN. WA_SALES_ITEMS-REF_ITEM = WA_TAB_UPLOAD-REF_ITEM. WA_SALES_ITEMS-DLV_QTY = WA_TAB_UPLOAD-DLV_QTY. SELECT SINGLE VRKME FROM VBAP INTO CORRESPONDING FIELDS OF WA_VBAP "數量單位取VBAP中單位 WHERE VBELN = WA_TAB_UPLOAD-VBELN AND POSNR = WA_TAB_UPLOAD-REF_ITEM. WA_SALES_ITEMS-SALES_UNIT = WA_VBAP-VRKME. APPEND WA_SALES_ITEMS TO SALES_ITEMS. ENDLOOP. PERFORM MANAGE_DATA. "建立交貨單,發貨過賬 ENDFORM. "FM_UPLOAD_DATA *&---------------------------------------------------------------------* * FORM MANAGE_DATA *&---------------------------------------------------------------------* * BAPI處理建立交貨單 *&---------------------------------------------------------------------* FORM MANAGE_DATA. CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_SLS' EXPORTING SHIP_POINT = G_SHIP_POINT * DUE_DATE = * DEBUG_FLG = * NO_DEQUEUE = ' ' IMPORTING DELIVERY = G_DELIVERY NUM_DELIVERIES = G_NUM_DELIVERIES TABLES SALES_ORDER_ITEMS = SALES_ITEMS SERIAL_NUMBERS = IT_SERIAL_NUMBERS EXTENSION_IN = IT_EXTENSION_IN DELIVERIES = IT_DELIVERIES CREATED_ITEMS = IT_CREATED_ITEMS EXTENSION_OUT = IT_EXTENSION_OUT RETURN = IT_RETURN . CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING WAIT = 'X'. LOOP AT IT_RETURN INTO WA_RETURN. IF WA_RETURN-ROW = 0. ELSE. READ TABLE IT_TAB_UPLOAD INTO WA_TAB_UPLOAD INDEX WA_RETURN-ROW. CONCATENATE WA_TAB_UPLOAD-MESSAGE WA_RETURN-MESSAGE INTO WA_TAB_UPLOAD-MESSAGE. MODIFY IT_TAB_UPLOAD FROM WA_TAB_UPLOAD INDEX WA_RETURN-ROW. ENDIF. ENDLOOP. LOOP AT IT_TAB_UPLOAD INTO WA_TAB_UPLOAD. IF WA_TAB_UPLOAD-MESSAGE IS NOT INITIAL. WRITE: / ' 單據號:', WA_TAB_UPLOAD-VBELN , ' 專案號:' , 
      WA_TAB_UPLOAD-REF_ITEM , '錯誤資訊:', WA_TAB_UPLOAD-MESSAGE.
    ENDIF.
  ENDLOOP.

  IF IT_CREATED_ITEMS IS NOT INITIAL.


    WRITE: / , /  '==================================================================================================================================================================='.
    READ TABLE IT_CREATED_ITEMS INTO WA_CREATED_ITEMS INDEX 1.       "自動揀配過賬

    WRITE: / , /  ' 已建立交貨單:' , WA_CREATED_ITEMS-DELIV_NUMB.
*    LOOP AT IT_CREATED_ITEMS INTO WA_CREATED_ITEMS.
*      WRITE: / , /' 單據號:' , WA_CREATED_ITEMS-REF_DOC ,
*                  ' 專案號:' , WA_CREATED_ITEMS-REF_ITEM ,
*                  ' 發貨單專案號:' , WA_CREATED_ITEMS-DELIV_ITEM ,
*                  ' 物料號:' , WA_CREATED_ITEMS-MATERIAL ,
*                  ' 發貨數量:' , WA_CREATED_ITEMS-DLV_QTY ,
*                  ' 發貨單位:' , WA_CREATED_ITEMS-SALES_UNIT.
*    ENDLOOP.
    PERFORM DELIVERY USING WA_CREATED_ITEMS-DELIV_NUMB.
  ELSE.
    WRITE: / , / ' 發貨單未能建立!'.
  ENDIF.


ENDFORM.                    "MANAGE_DATA


*&---------------------------------------------------------------------*
*  FORM  FRM_F4_HELP
*&---------------------------------------------------------------------*
*  F4檔案瀏覽幫助
*&---------------------------------------------------------------------*

FORM FRM_F4_HELP  CHANGING O_FNAME TYPE RLGRAP-FILENAME.
  DATA: L_FILETAB TYPE FILETABLE,
        L_WAFTAB LIKE LINE OF L_FILETAB,
        L_RC TYPE I.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = '開啟檔案'
      INITIAL_DIRECTORY       = 'C:/'
    CHANGING
      FILE_TABLE              = L_FILETAB
      RC                      = L_RC
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      NOT_SUPPORTED_BY_GUI    = 4
      OTHERS                  = 5.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    EXIT.
  ELSE.
    READ TABLE L_FILETAB INTO L_WAFTAB INDEX 1.
    O_FNAME = L_WAFTAB-FILENAME.
    CLEAR: L_FILETAB,
           L_WAFTAB.
  ENDIF.

ENDFORM.                    "FRM_F4_HELP