Upload File Text to The Internal Table and Write to File Log
I. Description.
Trong file text chứa thông tin của một các chứng từ kế toán. Mục tiêu là Insert, Update, Delete những dòng chứng từ kế toán đó vào trong bảng ZBKEG đã tạo sẵn. Để biết dòng nào được Insert, Update, Delete thì ta căn cứ vào số hiệu đầu mỗi dòng. Nếu là
1 - Insert
2- Update
3- Delete
Khi thực hiện xử lý dữ liệu giữa file text và table- ZBKEG. Nếu thực hiện việc xử lý thành công hay thất bại thì in ra file Log kèm thông tin status việc xử lý đính kèm.
INPUT
TYPE-POOLS: TRUXS.
TABLES ZBKEG.
DATA: TT_ZBKEG TYPE STANDARD TABLE OF ZBKEG INITIAL SIZE 0,
WA_ZBKEG TYPE ZBKEG.
TYPES:
BEGIN OF ZTY_INPUT,
TYP TYPE C,
BUKRS TYPE ZBKEG-BUKRS, "Company code
BELNR TYPE ZBKEG-BELNR, "Document number
GJAHR TYPE ZBKEG-GJAHR, "Fiscal Year
BUZEI TYPE ZBKEG-BUZEI, "Line Item
BLDAT TYPE ZBKEG-BLDAT, "Document Date
BUDAT TYPE ZBKEG-BUDAT, "Posting date
MONAT TYPE ZBKEG-MONAT, "Posting Period
SHKZG TYPE ZBKEG-SHKZG, "Credit/ Debit
SAKNR TYPE ZBKEG-SAKNR, "G/L account
WAERK TYPE ZBKEG-WAERK, "Currency
SGTXT TYPE ZBKEG-SGTXT, "Text
END OF ZTY_INPUT,
ZTT_INPUT TYPE STANDARD TABLE OF ZTY_INPUT.
DATA: TT_INPUT TYPE ZTT_INPUT,
WA_INPUT TYPE ZTY_INPUT.
TYPES:
BEGIN OF ZTY_OUTPUT,
BUKRS TYPE ZBKEG-BUKRS, "Company code
BELNR TYPE ZBKEG-BELNR, "Document number
GJAHR TYPE ZBKEG-GJAHR, "Fiscal Year
BUZEI TYPE ZBKEG-BUZEI, "Line Item
BLDAT TYPE ZBKEG-BLDAT, "Document Date
BUDAT TYPE ZBKEG-BUDAT, "Posting date
MONAT TYPE ZBKEG-MONAT, "Posting Period
SHKZG TYPE ZBKEG-SHKZG, "Credit/ Debit
SAKNR TYPE ZBKEG-SAKNR, "G/L account
WAERK TYPE ZBKEG-WAERK, "Currency
SGTXT TYPE ZBKEG-SGTXT, "Text
MESSG TYPE CHAR50, "Message
END OF ZTY_OUTPUT,
ZTT_OUTPUT TYPE STANDARD TABLE OF ZTY_OUTPUT.
DATA: TT_OUTPUT TYPE ZTT_OUTPUT,
WA_OUTPUT TYPE ZTY_OUTPUT.
DATA: V_INPUT TYPE STRING,
V_OUTPUT TYPE STRING.
DATA: GT_TEXT TYPE TRUXS_T_TEXT_DATA.
SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-001.
PARAMETERS: P_INPUT TYPE IBIPPARMS-PATH OBLIGATORY,
P_OUTPUT TYPE IBIPPARMS-PATH OBLIGATORY.
SELECTION-SCREEN END OF BLOCK DATA.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INPUT.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_INPUT.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_OUTPUT.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_OUTPUT.
START-OF-SELECTION.
PERFORM READ_FILE.
PERFORM GUI_UPLOAD.
PERFORM PROCESS.
PERFORM GUI_DOWNLOAD.
FORM READ_FILE.
V_INPUT = P_INPUT.
V_OUTPUT = P_OUTPUT.
ENDFORM.
FORM GUI_UPLOAD.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = V_INPUT
FILETYPE = 'ASC'
* HAS_FIELD_SEPARATOR = 'X'
TABLES
DATA_TAB = TT_INPUT.
ENDFORM.
FORM PROCESS.
IF TT_INPUT[] IS INITIAL.
MESSAGE S008(ZSAC) DISPLAY LIKE 'E'.
ENDIF.
CLEAR: TT_OUTPUT, WA_OUTPUT.
LOOP AT TT_INPUT INTO WA_INPUT.
MOVE-CORRESPONDING WA_INPUT TO WA_ZBKEG.
MOVE-CORRESPONDING WA_INPUT TO WA_OUTPUT.
IF WA_INPUT-TYP = '1' .
SELECT * INTO TABLE TT_ZBKEG
FROM ZBKEG.
READ TABLE TT_ZBKEG INTO WA_ZBKEG WITH KEY BUKRS = WA_INPUT-BUKRS
BELNR = WA_INPUT-BELNR
GJAHR = WA_INPUT-GJAHR
BUZEI = WA_INPUT-BUZEI.
IF SY-SUBRC <> 0.
INSERT ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0 .
WA_OUTPUT-MESSG = TEXT-002.
ELSE.
WA_OUTPUT-MESSG = TEXT-005.
ENDIF.
ELSE.
UPDATE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0 .
WA_OUTPUT-MESSG = TEXT-003.
ELSE.
WA_OUTPUT-MESSG = TEXT-006.
ENDIF.
ENDIF.
ELSEIF WA_INPUT-TYP = '2'.
UPDATE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0.
WA_OUTPUT-MESSG = TEXT-003.
ELSE.
WA_OUTPUT-MESSG = TEXT-006.
ENDIF.
ELSEIF WA_INPUT-TYP = '3'.
DELETE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0.
WA_OUTPUT-MESSG = TEXT-004.
ELSE.
WA_OUTPUT-MESSG = TEXT-007.
ENDIF.
ENDIF.
APPEND WA_OUTPUT TO TT_OUTPUT.
ENDLOOP.
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
EXPORTING
I_FIELD_SEPERATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB " Class lay tab
TABLES
I_TAB_SAP_DATA = TT_OUTPUT
CHANGING
I_TAB_CONVERTED_DATA = GT_TEXT
EXCEPTIONS
CONVERSION_FAILED = 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.
ENDFORM.
FORM GUI_DOWNLOAD.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
EXPORTING
FILENAME = V_OUTPUT
CODEPAGE = '4110'
* FILETYPE = 'DAT' " ASC, BIN , DAT
* WRITE_FIELD_SEPARATOR = ABAP_ON
CHANGING
DATA_TAB = GT_TEXT
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
NOT_SUPPORTED_BY_GUI = 22
ERROR_NO_GUI = 23
OTHERS = 24.
ENDFORM.
TABLES ZBKEG.
DATA: TT_ZBKEG TYPE STANDARD TABLE OF ZBKEG INITIAL SIZE 0,
WA_ZBKEG TYPE ZBKEG.
TYPES:
BEGIN OF ZTY_INPUT,
TYP TYPE C,
BUKRS TYPE ZBKEG-BUKRS, "Company code
BELNR TYPE ZBKEG-BELNR, "Document number
GJAHR TYPE ZBKEG-GJAHR, "Fiscal Year
BUZEI TYPE ZBKEG-BUZEI, "Line Item
BLDAT TYPE ZBKEG-BLDAT, "Document Date
BUDAT TYPE ZBKEG-BUDAT, "Posting date
MONAT TYPE ZBKEG-MONAT, "Posting Period
SHKZG TYPE ZBKEG-SHKZG, "Credit/ Debit
SAKNR TYPE ZBKEG-SAKNR, "G/L account
WAERK TYPE ZBKEG-WAERK, "Currency
SGTXT TYPE ZBKEG-SGTXT, "Text
END OF ZTY_INPUT,
ZTT_INPUT TYPE STANDARD TABLE OF ZTY_INPUT.
DATA: TT_INPUT TYPE ZTT_INPUT,
WA_INPUT TYPE ZTY_INPUT.
TYPES:
BEGIN OF ZTY_OUTPUT,
BUKRS TYPE ZBKEG-BUKRS, "Company code
BELNR TYPE ZBKEG-BELNR, "Document number
GJAHR TYPE ZBKEG-GJAHR, "Fiscal Year
BUZEI TYPE ZBKEG-BUZEI, "Line Item
BLDAT TYPE ZBKEG-BLDAT, "Document Date
BUDAT TYPE ZBKEG-BUDAT, "Posting date
MONAT TYPE ZBKEG-MONAT, "Posting Period
SHKZG TYPE ZBKEG-SHKZG, "Credit/ Debit
SAKNR TYPE ZBKEG-SAKNR, "G/L account
WAERK TYPE ZBKEG-WAERK, "Currency
SGTXT TYPE ZBKEG-SGTXT, "Text
MESSG TYPE CHAR50, "Message
END OF ZTY_OUTPUT,
ZTT_OUTPUT TYPE STANDARD TABLE OF ZTY_OUTPUT.
DATA: TT_OUTPUT TYPE ZTT_OUTPUT,
WA_OUTPUT TYPE ZTY_OUTPUT.
DATA: V_INPUT TYPE STRING,
V_OUTPUT TYPE STRING.
DATA: GT_TEXT TYPE TRUXS_T_TEXT_DATA.
SELECTION-SCREEN BEGIN OF BLOCK DATA WITH FRAME TITLE TEXT-001.
PARAMETERS: P_INPUT TYPE IBIPPARMS-PATH OBLIGATORY,
P_OUTPUT TYPE IBIPPARMS-PATH OBLIGATORY.
SELECTION-SCREEN END OF BLOCK DATA.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INPUT.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_INPUT.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_OUTPUT.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_OUTPUT.
START-OF-SELECTION.
PERFORM READ_FILE.
PERFORM GUI_UPLOAD.
PERFORM PROCESS.
PERFORM GUI_DOWNLOAD.
FORM READ_FILE.
V_INPUT = P_INPUT.
V_OUTPUT = P_OUTPUT.
ENDFORM.
FORM GUI_UPLOAD.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = V_INPUT
FILETYPE = 'ASC'
* HAS_FIELD_SEPARATOR = 'X'
TABLES
DATA_TAB = TT_INPUT.
ENDFORM.
FORM PROCESS.
IF TT_INPUT[] IS INITIAL.
MESSAGE S008(ZSAC) DISPLAY LIKE 'E'.
ENDIF.
CLEAR: TT_OUTPUT, WA_OUTPUT.
LOOP AT TT_INPUT INTO WA_INPUT.
MOVE-CORRESPONDING WA_INPUT TO WA_ZBKEG.
MOVE-CORRESPONDING WA_INPUT TO WA_OUTPUT.
IF WA_INPUT-TYP = '1' .
SELECT * INTO TABLE TT_ZBKEG
FROM ZBKEG.
READ TABLE TT_ZBKEG INTO WA_ZBKEG WITH KEY BUKRS = WA_INPUT-BUKRS
BELNR = WA_INPUT-BELNR
GJAHR = WA_INPUT-GJAHR
BUZEI = WA_INPUT-BUZEI.
IF SY-SUBRC <> 0.
INSERT ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0 .
WA_OUTPUT-MESSG = TEXT-002.
ELSE.
WA_OUTPUT-MESSG = TEXT-005.
ENDIF.
ELSE.
UPDATE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0 .
WA_OUTPUT-MESSG = TEXT-003.
ELSE.
WA_OUTPUT-MESSG = TEXT-006.
ENDIF.
ENDIF.
ELSEIF WA_INPUT-TYP = '2'.
UPDATE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0.
WA_OUTPUT-MESSG = TEXT-003.
ELSE.
WA_OUTPUT-MESSG = TEXT-006.
ENDIF.
ELSEIF WA_INPUT-TYP = '3'.
DELETE ZBKEG FROM WA_ZBKEG.
IF SY-SUBRC <> 0.
WA_OUTPUT-MESSG = TEXT-004.
ELSE.
WA_OUTPUT-MESSG = TEXT-007.
ENDIF.
ENDIF.
APPEND WA_OUTPUT TO TT_OUTPUT.
ENDLOOP.
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
EXPORTING
I_FIELD_SEPERATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB " Class lay tab
TABLES
I_TAB_SAP_DATA = TT_OUTPUT
CHANGING
I_TAB_CONVERTED_DATA = GT_TEXT
EXCEPTIONS
CONVERSION_FAILED = 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.
ENDFORM.
FORM GUI_DOWNLOAD.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
EXPORTING
FILENAME = V_OUTPUT
CODEPAGE = '4110'
* FILETYPE = 'DAT' " ASC, BIN , DAT
* WRITE_FIELD_SEPARATOR = ABAP_ON
CHANGING
DATA_TAB = GT_TEXT
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
NOT_SUPPORTED_BY_GUI = 22
ERROR_NO_GUI = 23
OTHERS = 24.
ENDFORM.
Comments
Post a Comment