一個較為全面的ALV應用程式

mmccking發表於2007-09-10

1.ALV輸出資訊,

2.設定使用者自定義GUI,

3.事件觸發。

[@more@]

*----------------------------------------------------------------------*
* アドオン機能名稱 : 購買発注數量一括更新
* プログラムID : ZTEST_ALV_RAY
* プログラム名稱 : ZTEST_ALV_RAY
* 作成者 : ZHANGRAY
*----------------------------------------------------------------------*
* <処理概要>    
* 1.選択畫面入力値のチェック
* 2.選択畫面入力値を元に、購買発注データを取得
* 3.購買発注データが存在する場合、購買発注伝票一覧及び更新畫面を表示
* 4.購買発注伝票一覧及び更新畫面で更新及び照會の判定
* 5.更新ボタン押下時、汎用モジュール:BAPI_PO_CHANGEを用いて購買伝票
* 更新処理、ダブルクリック時、購買伝票照會畫面に遷移
*----------------------------------------------------------------------*
* <更新記録>  
* 更新日 No.  更新者 更新內容
* ---------- ---- ------------ --------------------------------------
*
*----------------------------------------------------------------------*
REPORT ZTEST_ALV_RAY NO STANDARD PAGE HEADING
MESSAGE-ID ZMSG.

*----------------------------------------------------------------------*
* INCLUDE
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TABLE
*----------------------------------------------------------------------*
TABLES:
EKKO, "購買伝票ヘッダ
EKPO. "購買伝票明細

*----------------------------------------------------------------------*
* TYPE-POOLS
*----------------------------------------------------------------------*
TYPE-POOLS:
SLIS.

*----------------------------------------------------------------------*
* TYPE
*----------------------------------------------------------------------*
TYPES:
BEGIN OF TYP_OUTPUT,
CHKBX TYPE C, "チェックボックス
EBELN LIKE EKKO-EBELN, "購買伝票番號
EBELP LIKE EKPO-EBELP, "購買伝票の明細番號
MATNR LIKE EKPO-MATNR, "品目コード
MAKTX LIKE MAKT-MAKTX, "品目テキスト
MENGE LIKE EKPO-MENGE, "購買発注數量
MEINS LIKE EKPO-MEINS, "発注単位
LIFNR LIKE EKKO-LIFNR, "仕入先
NAME1 LIKE ADRC-NAME1, "名稱1
WERKS LIKE EKPO-WERKS, "プラント
END OF TYP_OUTPUT.

* 購買発注データ取得用
TYPES:
BEGIN OF TYP_DATA,
EBELN TYPE EKKO-EBELN, "購買伝票
LIFNR TYPE EKKO-LIFNR, "仕入先
EBELP TYPE EKPO-EBELP, "明細
MATNR TYPE EKPO-MATNR, "品目コード
MENGE TYPE EKPO-MENGE, "購買発注數量
MEINS TYPE EKPO-MEINS, "発注単位
WERKS TYPE EKPO-WERKS, "プラント
END OF TYP_DATA.

* 品目テキスト
TYPES:
BEGIN OF TYP_MAKTX,
MATNR TYPE MAKT-MATNR,
MAKTX TYPE MAKT-MAKTX,
END OF TYP_MAKTX.

* アドレス明細
TYPES:
BEGIN OF TYP_NAME1,
LIFNR TYPE LFA1-LIFNR,
NAME1 TYPE ADRC-NAME1,
END OF TYP_NAME1.

*----------------------------------------------------------------------*
* STRUCTURE
*----------------------------------------------------------------------*
DATA:
TH_LAYOUT TYPE SLIS_LAYOUT_ALV,
TH_DATA TYPE TYP_DATA,
TH_MAKTX TYPE TYP_MAKTX,
TH_NAME1 TYPE TYP_NAME1,
TH_OUTPUT TYPE TYP_OUTPUT.

* ALV用
DATA:
TH_FIELDCAT TYPE SLIS_FIELDCAT_ALV.

* ALV參照用
DATA:
BEGIN OF TH_ALV_FIELDCAT,
EBELN LIKE EKKO-EBELN, "購買伝票番號
EBELP LIKE EKPO-EBELP, "購買伝票の明細番號
MATNR LIKE EKPO-MATNR, "品目コード
MAKTX LIKE MAKT-MAKTX, "品目テキスト
MENGE LIKE EKPO-MENGE, "購買発注數量
MEINS LIKE EKPO-MEINS, "発注単位
LIFNR LIKE EKKO-LIFNR, "仕入先
NAME1 LIKE ADRC-NAME1, "名稱1
WERKS LIKE EKPO-WERKS, "プラント
END OF TH_ALV_FIELDCAT.

*----------------------------------------------------------------------*
* INTERNAL TABLE
*----------------------------------------------------------------------*
DATA:
TD_DATA TYPE TABLE OF TYP_DATA, "出力用內部テーブル
TD_OUTPUT TYPE STANDARD TABLE OF TYP_OUTPUT,
TD_MAKTX TYPE TABLE OF TYP_MAKTX, "テキスト內部テーブル
TD_NAME1 TYPE TABLE OF TYP_NAME1. "明細內部テーブル

* ALV用
DATA:
TD_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
TD_EVENTS TYPE SLIS_T_EVENT,
TD_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER.

*----------------------------------------------------------------------*
* DATA
*----------------------------------------------------------------------*
DATA:
W_DATE TYPE SY-DATUM, "開始日付
W_TIME TYPE SY-UZEIT, "開始日時刻
W_EKOTX LIKE T024E-EKOTX. "購買組織テキスト

* ALV用
DATA:
W_REPID TYPE SY-REPID.

*----------------------------------------------------------------------*
* COUNTER
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* INDEX
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* FLAG
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* TABLE CONTROL
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* CONSTANTS
*----------------------------------------------------------------------*
CONSTANTS:
CNS_STATUS_SET TYPE SLIS_FORMNAME VALUE 'FRM_PF_STATUS_SET',
CNS_USER_COMMAND TYPE SLIS_FORMNAME VALUE 'FRM_USER_COMMAND',
CNS_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'FRM_TOP_OF_PAGE',
CNS_ALV_FIELDCAT TYPE SLIS_FORMNAME VALUE 'TH_ALV_FIELDCAT'.

*----------------------------------------------------------------------*
* SELECT-OPTIONS
*----------------------------------------------------------------------*
PARAMETERS:
P_EKORG LIKE EKKO-EKORG OBLIGATORY. "購買組織
*
SELECT-OPTIONS:
S_BEDAT FOR EKKO-BEDAT OBLIGATORY, "購買伝票日付
S_EKGRP FOR EKKO-EKGRP,
S_LIFNR FOR EKKO-LIFNR,
S_EBELN FOR EKKO-EBELN,
S_MATNR FOR EKPO-MATNR,
S_WERKS FOR EKPO-WERKS.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.

* 初期化
PERFORM FRM_INIT_PROC.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.

* 購買組織の存在チェック
PERFORM FRM_CHECK_EKORG.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.

* 開始日付・時刻の取得
GET TIME.
W_DATE = SY-DATUM.
W_TIME = SY-UZEIT.

* データの抽出
PERFORM FRM_GET_DATA.

* 品目テキストの取得
PERFORM FRM_GET_MAKTX.

* 仕入先の名稱の取得
PERFORM FRM_GET_NAME1.

* 購買発注データを取得・編集した
PERFORM FRM_UNITE_DATA.

* 購買発注データをソート処理
SORT TD_OUTPUT ASCENDING BY EBELN "購買伝票番號
EBELP. "購買明細番號

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.

* ALVの初期化
PERFORM FRM_SET_ALV.

* ALVの出力
PERFORM FRM_OUTPUT_ALV.

*----------------------------------------------------------------------*
* TOP-OF-PAGE
*----------------------------------------------------------------------*
TOP-OF-PAGE.

*&---------------------------------------------------------------------*
*& Form FRM_INIT_SCREEN
*&---------------------------------------------------------------------*
* 初期化
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_INIT_PROC.

* 初期化
CLEAR:
W_EKOTX,
W_DATE,
W_TIME,
W_EKOTX,
W_REPID,
TH_DATA,
TH_MAKTX,
TH_NAME1,
TH_FIELDCAT.
*
REFRESH:
TD_DATA,
TD_MAKTX,
TD_NAME1,
TD_FIELDCAT,
TD_EVENTS,
TD_TOP_OF_PAGE.

ENDFORM. " FRM_INIT_PROC
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* 出力データの抽出
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_GET_DATA .

SELECT EKKO~EBELN "購買伝票
EKKO~LIFNR "仕入先
EKPO~EBELP "明細
EKPO~MATNR "品目コード
EKPO~MENGE "購買発注數量
EKPO~MEINS "発注単位
EKPO~WERKS "Plant
INTO TABLE TD_DATA
FROM EKKO
INNER JOIN EKPO
ON EKKO~EBELN = EKPO~EBELN
INNER JOIN EKET
ON EKKO~EBELN = EKET~EBELN
AND EKPO~EBELP = EKET~EBELP
WHERE EKKO~EKORG = P_EKORG
AND EKPO~LOEKZ = SPACE
AND EKET~WEMNG = 0
* AND EKKO~ERNAM = SY-UNAME
AND EKPO~MATNR <> SPACE
AND EKKO~LIFNR IN S_LIFNR
AND EKKO~BEDAT IN S_BEDAT
AND EKKO~EBELN IN S_EBELN
AND EKPO~MATNR IN S_MATNR
AND EKPO~WERKS IN S_WERKS.

* リターンコード判斷
* 検索失敗時
IF SY-SUBRC <> 0.
*- エラーメッセージ出力
MESSAGE S002 DISPLAY LIKE 'E'. "対象データが存在しません
*- 初期畫面に返る
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM. " FRM_GET_DATA
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_EKORG
*&---------------------------------------------------------------------*
* 購買組織の存在チェック
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_CHECK_EKORG .

SELECT SINGLE EKOTX
FROM T024E
INTO W_EKOTX
WHERE EKORG = P_EKORG.

* リターンコード判斷
* 検索失敗時
IF SY-SUBRC <> 0.
* エラーメッセージ出力
MESSAGE E001 WITH P_EKORG. "購買組織 &1 は存在しません
ENDIF.

ENDFORM. " FRM_CHECK_EKORG
*&---------------------------------------------------------------------*
*& Form FRM_GET_MAKTX
*&---------------------------------------------------------------------*
* 品目テキストの取得
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_GET_MAKTX.

SELECT MATNR "品目コード
MAKTX "品目テキスト
FROM MAKT
INTO TABLE TD_MAKTX
FOR ALL ENTRIES IN TD_DATA
WHERE MATNR = TD_DATA-MATNR "品目コード
AND SPRAS = SY-LANGU.

* リターンコード判斷
* 検索失敗時
IF SY-SUBRC <> 0.
REFRESH TD_MAKTX.
ENDIF.

ENDFORM. " FRM_GET_MAKTX
*&---------------------------------------------------------------------*
*& Form FRM_GET_NAME1
*&---------------------------------------------------------------------*
* 仕入先の名稱の取得
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_GET_NAME1.

SELECT LFA1~LIFNR
ADRC~NAME1
INTO TABLE TD_NAME1
FROM LFA1
INNER JOIN ADRC
ON LFA1~ADRNR = ADRC~ADDRNUMBER
FOR ALL ENTRIES IN TD_DATA
WHERE LFA1~LIFNR = TD_DATA-LIFNR.

* リターンコード判斷
* 検索失敗時
IF SY-SUBRC <> 0.
REFRESH TD_NAME1.
ENDIF.

ENDFORM. " FRM_GET_NAME1

*&---------------------------------------------------------------------*
*& Form FRM_SET_ALV
*&---------------------------------------------------------------------*
* ALVの初期化
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_SET_ALV.

W_REPID = SY-REPID.

* 出力のセット
PERFORM FRM_SET_LAYOUT.

* 事件の取得
PERFORM FRM_EVENTTAB_BUILD USING TD_EVENTS.

* ALV畫面ヘッダ內容のセット
PERFORM FRM_COMMENT_BUILD USING TD_TOP_OF_PAGE.

ENDFORM. " FRM_SET_ALV
*&---------------------------------------------------------------------*
*& Form FRM_EVENTTAB_BUILD
*&---------------------------------------------------------------------*
* 事件の取得
*----------------------------------------------------------------------*
* --&gtP_GT_EVENTS イベント內表(USINGパラメータ)
*----------------------------------------------------------------------*
FORM FRM_EVENTTAB_BUILD USING P_T_EVENTS TYPE SLIS_T_EVENT.

DATA: LTH_EVENT TYPE SLIS_ALV_EVENT.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = P_T_EVENTS
EXCEPTIONS
LIST_TYPE_WRONG = 1
OTHERS = 2.

* リターンコード判定
*- 抽出失敗時
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

READ TABLE P_T_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
INTO LTH_EVENT.
IF SY-SUBRC = 0.
MOVE CNS_TOP_OF_PAGE TO LTH_EVENT-FORM.
APPEND LTH_EVENT TO P_T_EVENTS.
ENDIF.

ENDFORM. " FRM_EVENTTAB_BUILD
*&---------------------------------------------------------------------*
*& Form FRM_SET_ALV_FIELDCAT
*&---------------------------------------------------------------------*
* SET ALV FIELDCAT
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_SET_ALV_FIELDCAT.

* ALV FIELDCATの設定
LOOP AT TD_FIELDCAT INTO TH_FIELDCAT.

*- 購買數量を入力可能に変更
CASE TH_FIELDCAT-FIELDNAME.
*-- 購買伝票
WHEN 'EBELN'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F01 10.
*-- 明細
WHEN 'EBELP'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F02 5.
*-- 品目コード
WHEN 'MATNR'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F03 18.
*-- 品目テキスト
WHEN 'MAKTX'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F04 40.
*-- 購買発注數量
WHEN 'MENGE'.
TH_FIELDCAT-EDIT = 'X'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F05 17.
*-- 発注単位
WHEN 'MEINS'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F06 8.
*-- 仕入先
WHEN 'LIFNR'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F07 10.
*-- 名稱
WHEN 'NAME1'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F08 40.
*-- プラント
WHEN 'WERKS'.
PERFORM FRM_MODIFY_FIELDCAT USING TEXT-F09 4.
ENDCASE.

ENDLOOP.

ENDFORM. " FRM_SET_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_OUTPUT_ALV
*&---------------------------------------------------------------------*
* ALVの出力
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_OUTPUT_ALV.

* ALVグリッド表示前処理   
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-REPID "プログラムID
I_INTERNAL_TABNAME = CNS_ALV_FIELDCAT "ALV表示用
I_INCLNAME = SY-REPID "プログラムID
CHANGING
*- 專案テキストのある專案カタログ
CT_FIELDCAT = TD_FIELDCAT[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.

* ALV FIELDCATの設定
PERFORM FRM_SET_ALV_FIELDCAT.

* ALVグリッドデータ出力
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = W_REPID
I_CALLBACK_PF_STATUS_SET = CNS_STATUS_SET
I_CALLBACK_USER_COMMAND = CNS_USER_COMMAND
IS_LAYOUT = TH_LAYOUT
IT_FIELDCAT = TD_FIELDCAT[]
IT_EVENTS = TD_EVENTS[]
TABLES
T_OUTTAB = TD_OUTPUT
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
* リターンコード判斷
* ALV出力失敗時
IF SY-SUBRC <> 0.
MESSAGE S007 DISPLAY LIKE 'E'.
ENDIF.

ENDFORM. " FRM_OUTPUT_ALV
*&---------------------------------------------------------------------*
*& Form FRM_COMMENT_BUILD
*&---------------------------------------------------------------------*
* ALV畫面ヘッダ內容のセット
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_COMMENT_BUILD USING LT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER.

DATA:
LTH_LINE TYPE SLIS_LISTHEADER,
LW_DATE(10) TYPE C,
LW_TIME(10) TYPE C.

* 購買発注數量一括更新
CLEAR LTH_LINE.
LTH_LINE-TYP = 'H'.
LTH_LINE-INFO = '購買発注數量一括更新'.
APPEND LTH_LINE TO LT_LIST_TOP_OF_PAGE.

* 購買組織
CLEAR LTH_LINE.
LTH_LINE-TYP = 'S'.
LTH_LINE-KEY = '購買組織'.
LTH_LINE-INFO = P_EKORG.
APPEND LTH_LINE TO LT_LIST_TOP_OF_PAGE.

* 購買組織テキスト
CLEAR LTH_LINE.
LTH_LINE-TYP = 'S'.
LTH_LINE-INFO = W_EKOTX.
APPEND LTH_LINE TO LT_LIST_TOP_OF_PAGE.

* 作成日
CLEAR LTH_LINE.
WRITE W_DATE TO LW_DATE DD/MM/YYYY.
LTH_LINE-TYP = 'S'.
LTH_LINE-KEY = '作成日'.
LTH_LINE-INFO = LW_DATE.
APPEND LTH_LINE TO LT_LIST_TOP_OF_PAGE.

* 作成時刻
CLEAR LTH_LINE.
WRITE W_TIME TO LW_TIME USING EDIT MASK '__:__:__'.
LTH_LINE-TYP = 'S'.
LTH_LINE-KEY = '作成時刻'.
LTH_LINE-INFO = LW_TIME.
APPEND LTH_LINE TO LT_LIST_TOP_OF_PAGE.

ENDFORM. "FRM_COMMENT_BUILD
*&---------------------------------------------------------------------*
*& Form FRM_TOP_OF_PAGE
*&---------------------------------------------------------------------*
* ALV畫面ヘッダ機能設定
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_TOP_OF_PAGE.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = TD_TOP_OF_PAGE.

ENDFORM. " FRM_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& Form FRM_PF_STAUS_SET
*&---------------------------------------------------------------------*
* PF-STATUSの設定
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_PF_STATUS_SET USING RT_EXTAB TYPE SLIS_T_EXTAB.

SET PF-STATUS 'UPDATA'.

ENDFORM. " FRM_PF_STATUS_SET
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
* 出力のセット
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .

* 選択ボタン
TH_LAYOUT-BOX_FIELDNAME = 'CHKBX'.
* 列の最適
TH_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
* ゼブラ
TH_LAYOUT-ZEBRA = 'X'.

ENDFORM. " FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& Form FRM_UNITE_DATA
*&---------------------------------------------------------------------*
* 購買発注データを取得・編集した
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_UNITE_DATA .

LOOP AT TD_DATA INTO TH_DATA.

TH_OUTPUT-EBELN = TH_DATA-EBELN.
TH_OUTPUT-EBELP = TH_DATA-EBELP.
TH_OUTPUT-MATNR = TH_DATA-MATNR.
TH_OUTPUT-WERKS = TH_DATA-WERKS.
TH_OUTPUT-MENGE = TH_DATA-MENGE.
TH_OUTPUT-MEINS = TH_DATA-MEINS.
TH_OUTPUT-LIFNR = TH_DATA-LIFNR.

READ TABLE TD_MAKTX WITH KEY MATNR = TH_DATA-MATNR
INTO TH_MAKTX.
*- 読込成功時
IF SY-SUBRC = 0.
TH_OUTPUT-MAKTX = TH_MAKTX-MAKTX. "品目テキストの編集
ENDIF.

READ TABLE TD_NAME1 WITH KEY LIFNR = TH_DATA-LIFNR
INTO TH_NAME1.
*- 読込成功時
IF SY-SUBRC = 0.
TH_OUTPUT-NAME1 = TH_NAME1-NAME1. "名稱1の編集
ENDIF.

*- 購買発注データ內部テーブル更新
APPEND TH_OUTPUT TO TD_OUTPUT.

ENDLOOP.

ENDFORM. " FRM_UNITE_DATA
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
* 購買伝票照會畫面(ME23N)に遷移機能設定
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING UCOMM LIKE SY-UCOMM
SELFIELD TYPE SLIS_SELFIELD.

DATA:
LW_GRID TYPE REF TO CL_GUI_ALV_GRID, "ALVオブジェクト
LW_VALID(1) TYPE C.

READ TABLE TD_DATA INTO TH_DATA
INDEX SELFIELD-TABINDEX.
CHECK SY-SUBRC = 0.

CASE UCOMM.
*- 購買伝票番號のR/3購買伝票照會畫面(ME23N)へ遷移
WHEN '&IC1'.
SET PARAMETER ID 'BES' FIELD TH_DATA-EBELN.
CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.

*- 數量更新【機能コード】押下時、購買伝票更新処理を行う
WHEN '&UPDATE'.
*-- 購買伝票更新
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = LW_GRID. "ALVオブジェクト

CALL METHOD LW_GRID->CHECK_CHANGED_DATA
IMPORTING
E_VALID = LW_VALID. "チェック

*-- 數量更新
PERFORM FRM_UPDATE_BAPI.
ENDCASE.

ENDFORM. " FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form FRM_UPDATE_BAPI
*&---------------------------------------------------------------------*
* 購買伝票更新
*----------------------------------------------------------------------*
* --&gt なし
* *----------------------------------------------------------------------*
FORM FRM_UPDATE_BAPI.

DATA:
* 選択件數
LCTR_BAPI TYPE I,
* 明細データ
LTD_ITEM TYPE STANDARD TABLE OF BAPIMEPOITEM,
LTH_ITEM TYPE BAPIMEPOITEM,
* 明細データ (変更ツールバー)
LTD_ITEMX TYPE STANDARD TABLE OF BAPIMEPOITEMX,
LTH_ITEMX TYPE BAPIMEPOITEMX,
* リターンパラメータ
LTD_RETURN TYPE STANDARD TABLE OF BAPIRET2,
LTH_RETURN TYPE BAPIRET2,
* 更新テーブル
LTD_BAPI_TEB TYPE STANDARD TABLE OF TYP_OUTPUT,
LTH_BAPI_TEB TYPE TYP_OUTPUT.

* 內部テーブルにデータの複製
LTD_BAPI_TEB = TD_OUTPUT.

* 選択されなっかたデータを削除する
DELETE LTD_BAPI_TEB WHERE CHKBX = SPACE.

* 選択件數の取得
DESCRIBE TABLE LTD_BAPI_TEB LINES LCTR_BAPI.

* 選択件數の判定
* 選択件數>100件の場合
IF LCTR_BAPI > 100.
*- エラーメッセージ出力
MESSAGE E005.
"1回の更新件數の上限は100件です
* 選択件數=0件の場合
ELSEIF LCTR_BAPI = 0.
*- エラーメッセージ出力
MESSAGE E006.
"更新プロジェクトを選んでいません
ENDIF.

LOOP AT LTD_BAPI_TEB INTO LTH_BAPI_TEB.

REFRESH:
LTD_ITEM,
LTD_ITEMX,
LTD_RETURN.
CLEAR:
LTH_ITEM,
LTH_ITEMX,
LTH_RETURN.

* 購買伝票の明細番號
LTH_ITEM-PO_ITEM = LTH_BAPI_TEB-EBELP.
* 購買発注數量
LTH_ITEM-QUANTITY = LTH_BAPI_TEB-MENGE.
APPEND LTH_ITEM TO LTD_ITEM.

* 購買伝票の明細番號
LTH_ITEMX-PO_ITEM = LTH_BAPI_TEB-EBELP.
* 購買発注數量(変更)
LTH_ITEMX-QUANTITY = 'X'.
APPEND LTH_ITEMX TO LTD_ITEMX.

* 購買発注入力のBAPIを呼び出す(処理開始)
CALL FUNCTION 'BAPI_PO_CHANGE'
EXPORTING
PURCHASEORDER = LTH_BAPI_TEB-EBELN "購買伝票番號
TABLES
POITEM = LTD_ITEM "明細データ
POITEMX = LTD_ITEMX "明細データ (変更ツールバー)
RETURN = LTD_RETURN. "リターンパラメータ

* リターンメッセージの処理
LOOP AT LTD_RETURN INTO LTH_RETURN WHERE TYPE = 'I'
OR TYPE = 'W'
OR TYPE = 'E'
OR TYPE = 'A'.
ENDLOOP.

* BAPIを実行後、エラーの判斷
IF SY-SUBRC <> 0.
*-- BAPI で ROLLBACK 実行
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
MESSAGE E004 WITH TH_DATA-EBELN
TH_DATA-EBELP.
"購買伝票番號 &1 明細番號 &2 の數量更新が異常終了しました

*-- 処理終了
EXIT.
ELSE.
*-- BAPI で COMMIT 実行
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
MESSAGE S003.
"購買伝票明細の數量更新が正常終了しました
ENDIF.
ENDLOOP.

ENDFORM. " FRM_UPDATE_BAPI
*&---------------------------------------------------------------------*
*& Form FRM_MODIFY_FIELDCAT
*&---------------------------------------------------------------------*
* カタログテーブルの作成
*----------------------------------------------------------------------*
* --&gt P_TEXT P_TEXTの説明(USINGパラメータ)
* --&gt P_LENTH P_LENTHの説明(USINGパラメータ)
*----------------------------------------------------------------------*
FORM FRM_MODIFY_FIELDCAT USING P_TEXT TYPE ANY
P_LENTH TYPE ANY.

* 一覧ヘッダテキスト
TH_FIELDCAT-SELTEXT_M = P_TEXT.
* 出力長
TH_FIELDCAT-OUTPUTLEN = P_LENTH.
TH_FIELDCAT-REPTEXT_DDIC = P_TEXT.
MODIFY TD_FIELDCAT FROM TH_FIELDCAT.

ENDFORM. " FRM_MODIFY_FIELDCAT

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-968809/,如需轉載,請註明出處,否則將追究法律責任。

相關文章