Monday, November 5, 2007

BDC Using Session and Call Transaction methods with line items

on methods with line items

*&---------------------------------------------------------------------*
*& Report Z_FI_BDC_GL_ACCOUNT
*&
*&---------------------------------------------------------------------*
REPORT Z_FI_BDC_GL_ACCOUNT NO STANDARD PAGE HEADING
LINE-SIZE 255.

************************************************************************
* Program Name : Z_FI_BDC_LEGACY_DATA *
* Description : *
* *
* 1. Read input file *
* 2. Check the mandatory fields and if any one of them is missing *
* move the record to error file. *
* 3. Fill the bdcdata and call F-02 transaction *
* 4. If any errors format the message and send it for reporting *
* 5. If any errors, it downloads the error file *
* 6. Based on user input, this programs does the follwoing *
* a).Call transaction and in case of errors add to a session *
* B).Create session *
* 7. Display the Audit report *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Request No. Created Date Developer name *
*----------------------------------------------------------------------*
* SRDK901468 19/11/2006 XYZ
*----------------------------------------------------------------------*
* FLAGS *
*----------------------------------------------------------------------*

DATA: F_STOP, " Flag used to stop processing
F_COLOR. " Flag to set Background color

DATA : LV_FLAG TYPE C.

*----------------------------------------------------------------------*
* INTERNAL TABLE DECLARATIONS *
*----------------------------------------------------------------------*

*--- Internal table for input file for ascii data load
DATA: BEGIN OF I_INFILE_ASC OCCURS 0,
STRING(1000) TYPE C,
END OF I_INFILE_ASC.
*--- Internal table to store messages from Call Transaction.
DATA : I_BDCMSGCOLL TYPE STANDARD TABLE OF BDCMSGCOLL WITH HEADER LINE,
*--- Internal table for BDC data
IT_BDCTAB TYPE BDCDATA OCCURS 0 WITH HEADER LINE.
*--- Internal to hold the records in the text file
DATA : BEGIN OF IT_DATA OCCURS 10,
SNO(1),
STRING1(10),
STRING2(17),
STRING3(13),
STRING4(50),
STRING5(10),
STRING6(16),
END OF IT_DATA.
*--- Internal table to hold the Header data
DATA : BEGIN OF IT_HEAD OCCURS 0,
RECTYPE(6) TYPE N, " Record Type ( Header/Line )
BLDAT(10), " Document Date
BLART(2), " Type
BUKRS(4), " Company Code
BUDAT(10), " Posting Date
WAERS(5), " Currency Key
XBLNR(16), " Invoice number
END OF IT_HEAD.
*--- Internal table to hold the Line items
DATA : BEGIN OF IT_LITEM OCCURS 0,
RECTYPE(6) TYPE N, " Record Type ( Header/Line )
NEWBS(2), " Posting Key
NEWKO(17), " G/L Account
WRBTR(13), " Amount
SGTXT(50), " Text
PRCTR(10), " Profit centre
END OF IT_LITEM.

*--- Internal table to hold the error records.
DATA : BEGIN OF IT_ERROR OCCURS 0.
INCLUDE STRUCTURE IT_DATA.
DATA : MESSAGE(80) TYPE C,
END OF IT_ERROR.

*--- Internal to hold the records for file validation
DATA: BEGIN OF IT_DATA_TMP OCCURS 0.
INCLUDE STRUCTURE IT_DATA.
DATA: MESSAGE(80),
END OF IT_DATA_TMP.

*--- Internal table for session names
DATA : BEGIN OF IT_SESSION OCCURS 0,
NAME(12),
END OF IT_SESSION.

*--- Internal table to hold Collecting messages in the SAP System
DATA : IT_MSG LIKE BDCMSGCOLL OCCURS 10 WITH HEADER LINE.

*--- Internal table to hold error records to be downloaded
DATA : BEGIN OF I_FINAL OCCURS 0,
STRING(1000),
END OF I_FINAL.

*--- Internal table for session names
DATA : BEGIN OF I_SESSION OCCURS 0,
NAME(12),
END OF I_SESSION.

*----------------------------------------------------------------------*
* CONSTANTS *
*----------------------------------------------------------------------*
CONSTANTS : C_FILETYPE LIKE RLGRAP-FILETYPE VALUE 'DAT', " Filetype
C_TCODE LIKE TSTC-TCODE VALUE 'F-02', " Transaction
C_MODE(1) VALUE 'A', " Mode
C_UPDATE(1) VALUE 'S', " Update
C_SESS(12) TYPE C VALUE 'GL_SESS'.

*----------------------------------------------------------------------*
* DATA DECLARATIONS *
*----------------------------------------------------------------------*
DATA: LV_RECNO TYPE I, " Record Number
V_TABIX LIKE SY-TABIX, " Table Index
V_MESG(80), " Message,
V_ERROR TYPE I, " No.of Errors
V_SUCCESS TYPE I, " No.of successes
V_BDC_SESS TYPE APQI-GROUPID VALUE 'UPLOAD', " Queue info defn
V_MAX TYPE I VALUE '900', " Max records
V_LINES TYPE I, " No.of records
V_INSERT TYPE I,
V_COUNTER(2) TYPE N, " No. of Sessions
F_OPENSESSION TYPE C. " Flag for Open Ses

DATA: V_EMPTY TYPE I, " No. of empty records
V_TOTAL TYPE I, " Total no. of records with data
V_SESSION TYPE APQI-GROUPID, " Session name
V_DEL TYPE X VALUE '09'. " To seperate fields in case of DAT

DATA: IT_INTERN TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.
DATA: V_SFILE(255), " File path
V_FILEEXT(3). " File Extension
*----------------------------------------------------------------------*
* SELECTION SCREEN. *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK SCR1 WITH FRAME TITLE TEXT-001.
PARAMETER : P_FILE TYPE RLGRAP-FILENAME OBLIGATORY DEFAULT
'C:/GL_Account.TXT',
P_ERR TYPE RLGRAP-FILENAME OBLIGATORY DEFAULT
'C:/GL_Account_ERR.TXT'.

PARAMETER: P_FTYPE LIKE RLGRAP-FILETYPE DEFAULT 'DAT'.
SELECTION-SCREEN END OF BLOCK SCR1.

SELECTION-SCREEN BEGIN OF BLOCK MODE WITH FRAME TITLE TEXT-002.
PARAMETER : P_CT RADIOBUTTON GROUP BDC, " Call Tran &
P_BDC RADIOBUTTON GROUP BDC DEFAULT 'X'. " Create BDC sess
PARAMETER : P_SESS1 TYPE APQI-GROUPID OBLIGATORY DEFAULT C_SESS.
" Session name
PARAMETER : P_MAX(4) OBLIGATORY DEFAULT '100', " no.of recs in a session
P_TEST AS CHECKBOX DEFAULT 'X'. " Test mode
PARAMETERS: P_HEADER TYPE I DEFAULT 0. " Header Lines
PARAMETERS: P_BEGCOL TYPE I DEFAULT 1 NO-DISPLAY,
P_BEGROW TYPE I DEFAULT 1 NO-DISPLAY,
P_ENDCOL TYPE I DEFAULT 100 NO-DISPLAY,
P_ENDROW TYPE I DEFAULT 32000 NO-DISPLAY.
SELECTION-SCREEN END OF BLOCK MODE.

*---------------------------------------------------------------------*
* AT SELECTION-SCREEN *
*---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
*--- Validating file
PERFORM VALIDATE_FILE USING P_FILE.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ERR.
*--- Validating file
PERFORM VALIDATE_FILE USING P_ERR.

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

*--- Perform to store the data to an internal table
PERFORM READ_INPUT_FILE_DATA.
*--- Perform to split the file into re types
PERFORM SPLIT_FILE_RE_TYPES.

*--- Perform to get values from the text file
* PERFORM FILE_VALIDATION.
* Process data in non test mode only
IF P_TEST = SPACE.
IF NOT IT_DATA[] IS INITIAL.
PERFORM PROCESS_DATA.
ELSE.
MESSAGE S003(Z_MSGCLASS).
F_STOP = 'X'.
STOP.
ENDIF.
ENDIF.
*----------------------------------------------------------------------*
* END OF SELECTION. *
*----------------------------------------------------------------------*
END-OF-SELECTION.

IF F_STOP = SPACE.
*--- Perform to display the selection screen of the report
PERFORM DISPLAY_SEL_SCR_REPORT.
*--- Perform to display the session report
PERFORM SESSION_REPORT.
*--- Perform to display the no.of records,success and error records
PERFORM DISPLAY_SUMMARY_REPORT.
*--- Downloading error records.
PERFORM DOWNLOAD_ERROR_FILES.
*---- Perform to display the error records
PERFORM DISPLAY_ERROR_RECORDS.
ENDIF.
*----------------------------------------------------------------------*
* Form : validate_input_file
*----------------------------------------------------------------------*
* Description : To provide F4 help for file if read from PC
*----------------------------------------------------------------------*
FORM VALIDATE_FILE USING F_FILE TYPE RLGRAP-FILENAME.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
CHANGING
FILE_NAME = F_FILE
EXCEPTIONS
MASK_TOO_LONG = 1
OTHERS = 2.

IF SY-SUBRC <> 0.
MESSAGE I010(Z_MSGCLASS). " 'Error in getting filename'.
ENDIF.

ENDFORM. " validate_input_file
*----------------------------------------------------------------------*
* Form : Read_inputfile
*----------------------------------------------------------------------*
* Description : To upload the data into internal table
*----------------------------------------------------------------------*
FORM READ_INPUTFILE TABLES F_INFILE
USING F_FILE TYPE RLGRAP-FILENAME
F_FILTYP TYPE RLGRAP-FILETYPE.
DATA : LV_DEL TYPE CHAR01,
LV_FILE TYPE STRING.
CLEAR F_INFILE.
REFRESH F_INFILE.
LV_FILE = F_FILE.

*--- Function module to upload the input file
IF F_FILTYP = 'DAT'.
LV_DEL = ''.
ENDIF.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILE
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = LV_DEL
TABLES
DATA_TAB = F_INFILE
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17 .

IF SY-SUBRC <> 0.
MESSAGE I011(Z_MSGCLASS). " 'Error in uploading the file'.
F_STOP = 'X'.
STOP.
ENDIF.

ENDFORM. "Read_inputfile
*&---------------------------------------------------------------------*
*& Form FORMAT_ASC_DATA
*&---------------------------------------------------------------------*
* To format the ASCII data
*----------------------------------------------------------------------*
FORM FORMAT_ASC_DATA.

LOOP AT I_INFILE_ASC.

IT_DATA-SNO = I_INFILE_ASC-STRING+0(6).
IT_DATA-STRING1 = I_INFILE_ASC-STRING+6(10).
IT_DATA-STRING2 = I_INFILE_ASC-STRING+16(17).
IT_DATA-STRING3 = I_INFILE_ASC-STRING+33(13).
IT_DATA-STRING4 = I_INFILE_ASC-STRING+46(50).
IT_DATA-STRING5 = I_INFILE_ASC-STRING+96(10).
IT_DATA-STRING6 = I_INFILE_ASC-STRING+106(16).

ENDLOOP.

ENDFORM. " FORMAT_ASC_DATA
*---------------------------------------------------------------------*
* Form FILE_VALIDATION
*---------------------------------------------------------------------*
* To validate data
*---------------------------------------------------------------------*
FORM FILE_VALIDATION.

DATA: LV_TABIX LIKE SY-TABIX,
LV_ERROR TYPE C,
LV_RECTYPE TYPE I,
LV_PSTKEY(2),
LV_CRAMT TYPE I,
LV_DRAMT TYPE I.

LOOP AT IT_HEAD.
CLEAR LV_ERROR.
CLEAR IT_DATA_TMP.
CLEAR IT_DATA_TMP[].

LV_RECTYPE = IT_HEAD-RECTYPE.
* ----- check for the mandatory fields
IF IT_HEAD-BLDAT IS INITIAL
OR IT_HEAD-BLART IS INITIAL
OR IT_HEAD-BUKRS IS INITIAL
OR IT_HEAD-BUDAT IS INITIAL
OR IT_HEAD-WAERS IS INITIAL.

V_MESG = 'Mandatory fields missing'.
LV_ERROR = 'X'.

ENDIF.
IF LV_ERROR = SPACE.
LOOP AT IT_LITEM WHERE RECTYPE = IT_HEAD-RECTYPE.
IF LV_FLAG = SPACE.
LV_PSTKEY = IT_LITEM-NEWBS.
LV_FLAG = 'X'.
ENDIF.
IF LV_PSTKEY = IT_LITEM-NEWBS.
LV_CRAMT = LV_CRAMT + IT_LITEM-WRBTR.
ELSE.
LV_DRAMT = LV_DRAMT + IT_LITEM-WRBTR.
ENDIF.
*--- Checking the mandatory fields in line items
IF IT_LITEM-NEWBS IS INITIAL
OR IT_LITEM-NEWKO IS INITIAL
OR IT_LITEM-WRBTR IS INITIAL.
V_MESG = 'Mandatory fields missing'.
LV_ERROR = 'X'.
EXIT.
ENDIF.
AT LAST.
*--- Comparing the total DR amount and total CR amount
IF LV_CRAMT <> LV_DRAMT.
LV_ERROR = 'X'.
V_MESG = 'Difference in CR/DR amount'.
ENDIF.
ENDAT.

ENDLOOP.
ENDIF.
IF LV_ERROR = 'X'.

PERFORM FORMAT_ERROR_DATA USING LV_RECTYPE.
*--- Perform to store the error record
PERFORM ERROR_PROCESSING.

DELETE IT_HEAD WHERE RECTYPE = LV_RECTYPE.
DELETE IT_LITEM WHERE RECTYPE = LV_RECTYPE.
CLEAR IT_HEAD.
CLEAR IT_LITEM.
ENDIF.

ENDLOOP.
CLEAR V_MESG.

ENDFORM. " FILE_VALIDATION
*&---------------------------------------------------------------------*
*& Form error_processing
*&---------------------------------------------------------------------*
* To process the error records
*----------------------------------------------------------------------*
FORM ERROR_PROCESSING.
V_ERROR = V_ERROR + 1.
IT_ERROR-MESSAGE = V_MESG.
APPEND LINES OF IT_DATA_TMP TO IT_ERROR.
ENDFORM. " error_processing
*---------------------------------------------------------------------*
* Form : process_data *
*---------------------------------------------------------------------*
* --- Populating the BDC Data *
*---------------------------------------------------------------------*
FORM PROCESS_DATA .

DATA: LV_RECTYPE TYPE I.

LOOP AT IT_HEAD.

LV_RECTYPE = IT_HEAD-RECTYPE.

CLEAR :IT_BDCTAB,
I_BDCMSGCOLL.
REFRESH:IT_BDCTAB,
I_BDCMSGCOLL.
*--- To fill the data from the text file
PERFORM FILL_HEADER_DATA.
*--- Perform to populate bdc data
PERFORM FILL_LINE_ITEM_DATA.


IF P_CT EQ 'X'.
*--- Posts data using call transaction method.
PERFORM CALL_TRANSACTION USING LV_RECTYPE.
ELSE.
*--- Posts data using session method.
PERFORM CALL_SESSION USING C_TCODE P_MAX P_SESS1.
ENDIF.

ENDLOOP.
IF F_OPENSESSION = 'X'.
PERFORM CLOSE_SESSION.
ENDIF.

ENDFORM. " PROCESS_DATA
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING F_PROGRAM F_DYNPRO.
CLEAR IT_BDCTAB.
IT_BDCTAB-PROGRAM = F_PROGRAM.
IT_BDCTAB-DYNPRO = F_DYNPRO.
IT_BDCTAB-DYNBEGIN = 'X'.
APPEND IT_BDCTAB.
ENDFORM.

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
IF FVAL <> SPACE.
CLEAR IT_BDCTAB.
IT_BDCTAB-FNAM = FNAM.
IT_BDCTAB-FVAL = FVAL.
APPEND IT_BDCTAB.
ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
* Form : call_transaction *
*---------------------------------------------------------------------*
*--- Posts data using call transaction method. *
*---------------------------------------------------------------------*
FORM CALL_TRANSACTION USING F_RECTYPE.

CLEAR: V_LINES,
V_MESG,
IT_DATA_TMP,
IT_DATA_TMP[].
*----- call transaction method to upload data
CALL TRANSACTION C_TCODE USING IT_BDCTAB
MODE C_MODE
UPDATE C_UPDATE
MESSAGES INTO I_BDCMSGCOLL.

IF SY-SUBRC <> 0.
DESCRIBE TABLE I_BDCMSGCOLL LINES V_LINES.
READ TABLE I_BDCMSGCOLL INDEX V_LINES TRANSPORTING ALL FIELDS.
*--- Formats message returned by Call transaction.
PERFORM FORMAT_MESSAGE.
PERFORM FORMAT_ERROR_DATA USING F_RECTYPE.
*--- Perform to process error processing
PERFORM ERROR_PROCESSING.
PERFORM CALL_SESSION USING C_TCODE P_MAX P_SESS1.
ELSE. "If call transaction is successful.
* V_SUCCESS = V_SUCCESS + 1.
V_SUCCESS = IT_HEAD-RECTYPE.
ENDIF.

ENDFORM. " call_transaction
*&---------------------------------------------------------------------*
*& Form format_message
*&---------------------------------------------------------------------*
*-------Reading messages
*----------------------------------------------------------------------*
FORM FORMAT_MESSAGE.
CLEAR V_MESG.
*--- Function module to get the message.
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = I_BDCMSGCOLL-MSGID
LANG = SY-LANGU
NO = I_BDCMSGCOLL-MSGNR
V1 = I_BDCMSGCOLL-MSGV1
V2 = I_BDCMSGCOLL-MSGV2
V3 = I_BDCMSGCOLL-MSGV3
V4 = I_BDCMSGCOLL-MSGV4
IMPORTING
MSG = V_MESG.

ENDFORM. " format_message
*----------------------------------------------------------------------*
* Form : call_session
*----------------------------------------------------------------------*
* Description : To call the session either for FSP0 or FSP2
*----------------------------------------------------------------------*
FORM CALL_SESSION USING FP_TCODE TYPE ANY
F_MAX TYPE ANY
F_SESS1 TYPE ANY.
* To open the session
PERFORM OPEN_SESSION USING F_MAX F_SESS1.
* Perform Insert BDC Session
PERFORM BDC_INSERT USING FP_TCODE.
* To close the session
IF V_INSERT = F_MAX .
PERFORM CLOSE_SESSION.
ENDIF.
ENDFORM. " call_session
*----------------------------------------------------------------------*
* Form : open_session
*----------------------------------------------------------------------*
* Description : Form to open BDC open group.
*----------------------------------------------------------------------*
FORM OPEN_SESSION USING F_MAX TYPE ANY
F_SESS1 TYPE ANY.

IF V_INSERT = F_MAX OR F_OPENSESSION = SPACE.
V_COUNTER = V_COUNTER + 1.
CLEAR V_SESSION.
CONCATENATE F_SESS1 '_' V_COUNTER INTO V_SESSION.
* Opening the BDC Session
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = V_SESSION
KEEP = 'X'
USER = SY-UNAME
EXCEPTIONS
CLIENT_INVALID = 1
DESTINATION_INVALID = 2
GROUP_INVALID = 3
GROUP_IS_LOCKED = 4
HOLDDATE_INVALID = 5
INTERNAL_ERROR = 6
QUEUE_ERROR = 7
RUNNING = 8
SYSTEM_LOCK_ERROR = 9
USER_INVALID = 10
OTHERS = 11.
IF SY-SUBRC <> 0.
MESSAGE I007(Z_MSGCLASS). " 'Error in Opening the Session'.
EXIT.
ELSE.
CLEAR V_INSERT.
F_OPENSESSION = 'X'.
ENDIF.
ENDIF.
ENDFORM. " open_session
*----------------------------------------------------------------------*
* Form : bdc_insert
*----------------------------------------------------------------------*
* Description : Form to Insert data.
*----------------------------------------------------------------------*
FORM BDC_INSERT USING FP_TCODE TYPE ANY.
* Inserting BDC
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = FP_TCODE
TABLES
DYNPROTAB = IT_BDCTAB
EXCEPTIONS
INTERNAL_ERROR = 1
NOT_OPEN = 2
QUEUE_ERROR = 3
TCODE_INVALID = 4
PRINTING_INVALID = 5
POSTING_INVALID = 6
OTHERS = 7.
IF SY-SUBRC <> 0.
MESSAGE I008(Z_MSGCLASS). " 'Error in inserting the record'.
ELSE.
V_INSERT = V_INSERT + 1.
ENDIF.
ENDFORM. " bdc_insert
*----------------------------------------------------------------------*
* Form : close_session
*----------------------------------------------------------------------*
* Description : To close the session
*----------------------------------------------------------------------*
FORM CLOSE_SESSION.
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 2
OTHERS = 3.

IF SY-SUBRC <> 0.
MESSAGE I009(Z_MSGCLASS). " 'Session cannot be closed'.
ELSE.
I_SESSION-NAME = V_SESSION.
APPEND I_SESSION.
CLEAR I_SESSION.
CLEAR F_OPENSESSION.
ENDIF.
ENDFORM. " close_session
*---------------------------------------------------------------------*
* Form DOWNLOAD_ERROR_FILES
*---------------------------------------------------------------------*
*--- Downloading the error records to erro file
*----------------------------------------------------------------------*
FORM DOWNLOAD_ERROR_FILES.

DATA : LV_DEL TYPE CHAR01,
LV_FILE TYPE STRING.

IF NOT IT_ERROR[] IS INITIAL.

IF P_FTYPE = 'DAT'.
LOOP AT IT_ERROR.

CONCATENATE IT_ERROR-SNO
IT_ERROR-STRING1
IT_ERROR-STRING2
IT_ERROR-STRING3
IT_ERROR-STRING4
IT_ERROR-STRING5
IT_ERROR-STRING6
INTO I_FINAL-STRING SEPARATED BY V_DEL.
APPEND I_FINAL.
CLEAR I_FINAL.
ENDLOOP.
CLEAR I_FINAL.
ELSEIF P_FTYPE = 'ASC'.
LOOP AT IT_ERROR.

I_FINAL-STRING+0(6) = IT_ERROR-SNO.
I_FINAL-STRING+6(10) = IT_ERROR-STRING1.
I_FINAL-STRING+16(17) = IT_ERROR-STRING2.
I_FINAL-STRING+33(13) = IT_ERROR-STRING3.
I_FINAL-STRING+46(50) = IT_ERROR-STRING4.
I_FINAL-STRING+96(10) = IT_ERROR-STRING5.
I_FINAL-STRING+106(16) = IT_ERROR-STRING6.
APPEND I_FINAL.
CLEAR I_FINAL.
ENDLOOP.
ENDIF. "IF P_FTYPE = 'DAT'.

LV_FILE = P_ERR.

IF P_FTYPE = 'DAT'.
LV_DEL = ''.
ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
FILENAME = LV_FILE
FILETYPE = 'ASC'
* APPEND = ' '
WRITE_FIELD_SEPARATOR = LV_DEL
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = I_FINAL
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
OTHERS = 22.
IF SY-SUBRC <> 0.
MESSAGE I012(Z_MSGCLASS). " Error in downloading the file.
ENDIF.

ENDIF. "IF NOT I_FILE_ERROR[] IS INITIAL.

ENDFORM. " DOWNLOAD_ERROR_FILES
*&---------------------------------------------------------------------*
*& Form READ_INPUT_DATA
*&---------------------------------------------------------------------*
* To Read the input file data
*----------------------------------------------------------------------*
FORM READ_INPUT_FILE_DATA.

*--- Splitting the filename and its extension
PERFORM SPLIT_FILENAME_EXT.

CASE V_FILEEXT.

WHEN 'XLS'.
*--- Perform to convert the Excel data into an internal table
PERFORM CONVERT_XLS_ITAB.
WHEN 'TXT'.
IF P_FTYPE = 'ASC'.
*--- Uploading the data from server
PERFORM READ_INPUTFILE TABLES I_INFILE_ASC
USING P_FILE P_FTYPE.
*--- Perform Formating ASC type data
PERFORM FORMAT_ASC_DATA.
ELSEIF P_FTYPE = 'DAT'.
*--- Uploading the data from server
PERFORM READ_INPUTFILE TABLES IT_DATA
USING P_FILE P_FTYPE.
ENDIF.
WHEN OTHERS.
MESSAGE S000(Z_MSGCLASS).
F_STOP = 'X'.
STOP.
ENDCASE.
IF NOT IT_DATA[] IS INITIAL.
*--- Perform to delete Header lines
PERFORM DELETE_HEADER_EMPTY_RECS.
ENDIF.
ENDFORM. " READ_INPUT_FILE_DATA
*&---------------------------------------------------------------------*
*& Form SPLIT_FILENAME_EXT
*&---------------------------------------------------------------------*
* To split the filename and the extension
*----------------------------------------------------------------------*
FORM SPLIT_FILENAME_EXT.

MOVE P_FILE TO V_SFILE.
*--- To get the extensions of the file
CALL FUNCTION 'CV120_SPLIT_FILE'
EXPORTING
PF_FILE = V_SFILE
IMPORTING
* PFX_FILE =
PFX_EXTENSION = V_FILEEXT
* PFX_DOTEXTENSION =
.
*--- Checking the case of file extension
IF V_FILEEXT NA SY-ABCDE.
TRANSLATE V_FILEEXT TO UPPER CASE.
ENDIF.

ENDFORM. " SPLIT_FILENAME_EXT
*&---------------------------------------------------------------------*
*& Form MOVE_DATA
*&---------------------------------------------------------------------*
* To move the input file data to an internal table
*----------------------------------------------------------------------*
FORM MOVE_DATA.

DATA : LV_INDEX TYPE I.
FIELD-SYMBOLS .
*--- Sorting the internal table
SORT IT_INTERN BY ROW COL.
CLEAR IT_INTERN.

LOOP AT IT_INTERN.
MOVE IT_INTERN-COL TO LV_INDEX.
*--- Assigning the each record to an internal table row
ASSIGN COMPONENT LV_INDEX OF STRUCTURE IT_DATA TO .
IF SY-SUBRC = 0.
*--- Asigning the field value to a field symbol
condense IT_INTERN-VALUE.
MOVE IT_INTERN-VALUE TO .
ENDIF.
AT END OF ROW.
APPEND IT_DATA.
CLEAR IT_DATA.
ENDAT.
ENDLOOP.

ENDFORM. " MOVE_DATA
*&---------------------------------------------------------------------*
*& Form CONVER_XLS_ITAB
*&---------------------------------------------------------------------*
* To convert the excel data into an internal table
*----------------------------------------------------------------------*
FORM CONVERT_XLS_ITAB.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = P_BEGCOL
I_BEGIN_ROW = P_BEGROW
I_END_COL = P_ENDCOL
I_END_ROW = P_ENDROW
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.

IF NOT IT_INTERN[] IS INITIAL.
*--- Perform to move the data into an internal data
PERFORM MOVE_DATA.
ELSE.
MESSAGE I003(Z_MSGCLASS).
F_STOP = 'X'.
STOP.
ENDIF.

ENDFORM. " CONVERT_XLS_ITAB
*&---------------------------------------------------------------------*
*& Form DELETE_HEADER_EMPTY_RECS
*&---------------------------------------------------------------------*
* To delete the Header and empty records
*----------------------------------------------------------------------*
FORM DELETE_HEADER_EMPTY_RECS.

DATA: LV_TABIX LIKE SY-TABIX.

IF NOT P_HEADER IS INITIAL.
LOOP AT IT_DATA.
IF P_HEADER > 0 AND NOT IT_DATA IS INITIAL.
DELETE IT_DATA FROM 1 TO P_HEADER.
EXIT.
ENDIF.
ENDLOOP.
ENDIF.

CLEAR IT_DATA.
*--- To delete the empty lines from internal table
LOOP AT IT_DATA.
LV_TABIX = SY-TABIX.
IF IT_DATA IS INITIAL.
V_EMPTY = V_EMPTY + 1.
DELETE IT_DATA INDEX LV_TABIX..
ENDIF.
ENDLOOP.

CLEAR IT_DATA.
*--- Total no of recs in file
DESCRIBE TABLE IT_DATA LINES V_TOTAL.
IF V_TOTAL = 0.
MESSAGE I013(Z_MSGCLASS). " No records in the file
F_STOP = 'X'.
STOP.
ENDIF.

ENDFORM. " DELETE_HEADER_EMPTY_RECS
*&---------------------------------------------------------------------*
*& Form DISPLAY_SELECTION_SCR
*&---------------------------------------------------------------------*
* To display the selection screen input
*----------------------------------------------------------------------*
FORM DISPLAY_SEL_SCR_REPORT.

WRITE :/1(132) SY-ULINE.
FORMAT COLOR COL_HEADING INTENSIFIED ON.
WRITE :/1(1) SY-VLINE,
65(65) 'SELECTION SCREEN DATA'(004),
132(1) SY-VLINE.
WRITE :/1(132) SY-ULINE.
FORMAT INTENSIFIED OFF.

*--- Input file name
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Input File name'(005),
27(2) ' : ',
30(101) P_FILE,
132(1) SY-VLINE.

IF NOT IT_ERROR[] IS INITIAL.
*--- Setting the Error file name with .CSV
PERFORM COLOR_FILL.
IF V_FILEEXT = 'XLS'.
IF NOT P_ERR CS '.XLS'.
IF P_ERR CA '.'.
P_ERR = P_ERR(SY-FDPOS).
CONCATENATE P_ERR '.CSV' INTO P_ERR.
ENDIF.
ENDIF.
P_FTYPE = 'DAT'.
V_DEL = '2C'. " Hexadecimal value for ','
ENDIF.
*--- Error File name
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Error File name'(006),
27(2) ' : ',
30(101) P_ERR,
132(1) SY-VLINE.
ENDIF.

*--- File type
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'File Type'(007),
27(2) ' : '.
IF V_FILEEXT = 'XLS'.
WRITE :30 'XLS'(008),
132(1) SY-VLINE.
ELSE.
WRITE :30 P_FTYPE,
132(1) SY-VLINE.
ENDIF.
*--- BDC methods
PERFORM COLOR_FILL.
IF P_CT = 'X'.
WRITE :/1(1) SY-VLINE,
2(24) 'Call Transaction'(009),
27(2) ' : ',
30 P_CT,
132(1) SY-VLINE.
ELSE.
WRITE :/1(1) SY-VLINE,
2(24) 'Create Session'(010),
27(2) ' : ',
30 P_BDC,
132(1) SY-VLINE.
ENDIF.
*--- Test Mode
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Test Mode'(011),
27(2) ' : ',
30 P_TEST,
132(1) SY-VLINE.
*--- Header lines
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Header Lines'(023),
27(2) ' : ',
30 P_HEADER LEFT-JUSTIFIED,
132(1) SY-VLINE.
WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_SELECTION_SCR
*&---------------------------------------------------------------------*
*& Form DISPLAY_SUCCESS_ERROR_DATA
*&---------------------------------------------------------------------*
* To display the no.of records,success and error records
*----------------------------------------------------------------------*
FORM DISPLAY_SUMMARY_REPORT.
SKIP 3.
FORMAT COLOR COL_HEADING INTENSIFIED ON.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
65 'SUMMARY REPORT'(013),
132(1) SY-VLINE.
FORMAT INTENSIFIED OFF.
PERFORM COLOR_FILL.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
2(24) 'Processed Records'(014),
27(2) ' : ',
30 LV_RECNO,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Successfully Uploaded'(015),
27(2) ' : ',
30 V_SUCCESS,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Error Records'(016),
27(2) ' : ',
30 V_ERROR,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Empty Records'(017),
27(2) ' : ',
30 V_EMPTY,
132(1) SY-VLINE.
IF P_CT IS INITIAL.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(23) 'No.of Records Uploaded'(026),
25(2) ' : ',
30 V_INSERT,
132(1) SY-VLINE.
ENDIF.
WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_SUCCESS_ERROR_DATA
*----------------------------------------------------------------------*
* Form : session_report
*----------------------------------------------------------------------*
* Description : To display the list of sessions created
*----------------------------------------------------------------------*
FORM SESSION_REPORT .
IF NOT I_SESSION[] IS INITIAL.
FORMAT COLOR COL_HEADING.
WRITE :/01(132) SY-ULINE.
WRITE :/01(01) SY-VLINE,
65 'Sessions created'(017) CENTERED,
132(01) SY-VLINE.
WRITE :/01(132) SY-ULINE.
FORMAT INTENSIFIED OFF.

FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
WRITE :/01(132) SY-ULINE.
LOOP AT I_SESSION.
WRITE : /1 SY-VLINE.
AT FIRST.
WRITE : 5 'Session names'(018).
ENDAT.
WRITE : 27 ':', 30 I_SESSION-NAME,
132 SY-VLINE.
ENDLOOP.
WRITE : /01(132) SY-ULINE.
ENDIF.
ENDFORM. " session_report
*&---------------------------------------------------------------------*
*& Form DISPLAY_ERROR_RECORDS
*&---------------------------------------------------------------------*
* To display the error records
*----------------------------------------------------------------------*
FORM DISPLAY_ERROR_RECORDS.

IF V_ERROR > 0.
* ---- Perform to display the error header
PERFORM DISPLAY_ERROR_HEADER.
* ---- Perform to display the error records
PERFORM DISPLAY_ERROR_DETAIL.
ENDIF.

ENDFORM. " DISPLAY_ERROR_RECORDS
*&---------------------------------------------------------------------*
*& Form DISPLAY_ERROR_HEADER
*&---------------------------------------------------------------------*
* To display the error header
*----------------------------------------------------------------------*
FORM DISPLAY_ERROR_HEADER.

FORMAT COLOR COL_HEADING INTENSIFIED ON.
SKIP.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
65 'ERROR RECORDS'(019),
132(1) SY-VLINE.
FORMAT INTENSIFIED OFF.
FORMAT COLOR COL_HEADING.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
2(4) 'S.No',
7(1) SY-VLINE,
8(13) 'Document Date'(020),
22(1) SY-VLINE,
23(20) 'Company Code'(021),
44(1) SY-VLINE,
45(80) 'Error Message'(022),
132(1) SY-VLINE.

WRITE :/1(132) SY-ULINE.
FORMAT INTENSIFIED OFF.

ENDFORM. " DISPLAY_ERROR_HEADER
*&---------------------------------------------------------------------*
*& Form DISPLAY_ERROR_DETAIL
*&---------------------------------------------------------------------*
* To display the error records
*----------------------------------------------------------------------*
FORM DISPLAY_ERROR_DETAIL.

LOOP AT IT_ERROR.
PERFORM COLOR_FILL.
WRITE : /1(1) SY-VLINE,
2(4) IT_ERROR-SNO,
7(1) SY-VLINE,
8(13) IT_ERROR-STRING1,
22(1) SY-VLINE,
23(20) IT_ERROR-STRING3,
44(1) SY-VLINE,
45(80) IT_ERROR-MESSAGE,
132(1) SY-VLINE.
ENDLOOP.
WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_ERROR_DETAIL
*&---------------------------------------------------------------------*
*& Form COLOR_FILL
*&---------------------------------------------------------------------*
*--- To display the background color
*----------------------------------------------------------------------*
FORM COLOR_FILL.

IF F_COLOR = SPACE.
FORMAT COLOR 2.
F_COLOR = 'X'.
ELSE.
FORMAT COLOR 4.
CLEAR F_COLOR.
ENDIF.

ENDFORM. " COLOR_FILL
*&---------------------------------------------------------------------*
*& Form SPLIT_FILE_RE_TYPES
*&---------------------------------------------------------------------*
* To Split the Header and Line item data
*----------------------------------------------------------------------*
FORM SPLIT_FILE_RE_TYPES .

LOOP AT IT_DATA.

IF IT_DATA-SNO = 1.

LV_RECNO = LV_RECNO + 1.
IT_HEAD-RECTYPE = LV_RECNO.
IT_HEAD-BLDAT = IT_DATA-STRING1.
IT_HEAD-BLART = IT_DATA-STRING2.
IT_HEAD-BUKRS = IT_DATA-STRING3.
IT_HEAD-BUDAT = IT_DATA-STRING4.
IT_HEAD-WAERS = IT_DATA-STRING5.
IT_HEAD-XBLNR = IT_DATA-STRING6.
APPEND IT_HEAD.
CLEAR IT_HEAD.

ELSEIF IT_DATA-SNO = 2.

IT_LITEM-RECTYPE = LV_RECNO.
IT_LITEM-NEWBS = IT_DATA-STRING1.
IT_LITEM-NEWKO = IT_DATA-STRING2.
IT_LITEM-WRBTR = IT_DATA-STRING3.
IT_LITEM-SGTXT = IT_DATA-STRING4.
IT_LITEM-PRCTR = IT_DATA-STRING5.
APPEND IT_LITEM.
CLEAR IT_LITEM.

ENDIF.

ENDLOOP.

ENDFORM. " SPLIT_FILE_RE_TYPES
*&---------------------------------------------------------------------*
*& Form FILL_HEADER_DATA
*&---------------------------------------------------------------------*
* To fill the Header data
*----------------------------------------------------------------------*
FORM FILL_HEADER_DATA .

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0100'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'BKPF-BLDAT' IT_HEAD-BLDAT.
PERFORM BDC_FIELD USING 'BKPF-BLART' IT_HEAD-BLART.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' IT_HEAD-BUKRS.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' IT_HEAD-BUDAT.
PERFORM BDC_FIELD USING 'BKPF-WAERS' IT_HEAD-WAERS.
PERFORM BDC_FIELD USING 'BKPF-XBLNR' IT_HEAD-XBLNR.
CLEAR LV_FLAG.


ENDFORM. " FILL_HEADER_DATA
*&---------------------------------------------------------------------*
*& Form FILL_LINE_ITEM_DATA
*&---------------------------------------------------------------------*
* To fill the line item data
*----------------------------------------------------------------------*
FORM FILL_LINE_ITEM_DATA .

DATA : LV_AMT TYPE P decimals 2,
LV_CNT TYPE I,
LV_TOTAMT TYPE P decimals 2,
LV_PSTKEY(2),
lv_subscr(1).

CLEAR LV_PSTKEY.
clear lv_flag.
CLEAR: LV_AMT , LV_TOTAMT.

LOOP AT IT_LITEM WHERE RECTYPE = IT_HEAD-RECTYPE.

IF LV_FLAG = SPACE.
LV_PSTKEY = IT_LITEM-NEWBS.
ENDIF.

IF LV_PSTKEY = IT_LITEM-NEWBS.
LV_AMT = LV_AMT + IT_LITEM-WRBTR.
ELSE.
LV_TOTAMT = LV_TOTAMT + IT_LITEM-WRBTR.
ENDIF.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' IT_LITEM-NEWBS.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' IT_LITEM-NEWKO.

IF LV_FLAG = 'X'.
PERFORM FILL_SCREEN_0002.
ELSE.
LV_FLAG = 'X'.
ENDIF.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.
PERFORM BDC_FIELD USING 'BSEG-WRBTR' IT_LITEM-WRBTR.
PERFORM BDC_FIELD USING 'BSEG-SGTXT' IT_LITEM-SGTXT.

IF LV_TOTAMT = LV_AMT.
PERFORM FILL_SCREEN_0002.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BS'.
PERFORM BDC_FIELD USING 'BSEG-WRBTR' IT_LITEM-WRBTR.
PERFORM BDC_FIELD USING 'BSEG-SGTXT' IT_LITEM-SGTXT.
PERFORM FILL_SCREEN_0002.
PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWBS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BU'.
EXIT.
ENDIF.

ENDLOOP.

ENDFORM. " FILL_LINE_ITEM_DATA
*&---------------------------------------------------------------------*
*& Form FILL_SCREEN_0002
*&---------------------------------------------------------------------*
* To fill screen 0002
*----------------------------------------------------------------------*
FORM FILL_SCREEN_0002.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'COBL-KOSTL'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=ENTE'.
PERFORM BDC_FIELD USING 'COBL-PRCTR' IT_LITEM-PRCTR.

ENDFORM. " FILL_SCREEN_0002.
*&---------------------------------------------------------------------*
*& Form FORMAT_ERROR_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FORMAT_ERROR_DATA USING F_RECTYPE.

READ TABLE IT_HEAD WITH KEY RECTYPE = F_RECTYPE.
IF SY-SUBRC = 0.
IT_DATA_TMP-SNO = 1.
IT_DATA_TMP-STRING1 = IT_HEAD-BLDAT.
IT_DATA_TMP-STRING2 = IT_HEAD-BLART.
IT_DATA_TMP-STRING3 = IT_HEAD-BUKRS.
IT_DATA_TMP-STRING4 = IT_HEAD-BUDAT.
IT_DATA_TMP-STRING5 = IT_HEAD-WAERS.
IT_DATA_TMP-STRING6 = IT_HEAD-XBLNR.
IT_DATA_TMP-MESSAGE = V_MESG.
APPEND IT_DATA_TMP.
CLEAR IT_DATA_TMP.
ENDIF.
LOOP AT IT_LITEM WHERE RECTYPE = F_RECTYPE.
IT_DATA_TMP-SNO = 2.
IT_DATA_TMP-STRING1 = IT_LITEM-NEWBS.
IT_DATA_TMP-STRING2 = IT_LITEM-NEWKO.
IT_DATA_TMP-STRING3 = IT_LITEM-WRBTR.
IT_DATA_TMP-STRING4 = IT_LITEM-SGTXT.
IT_DATA_TMP-STRING5 = IT_LITEM-PRCTR.
IT_DATA_TMP-STRING6 = SPACE.
IT_DATA_TMP-MESSAGE = V_MESG.
APPEND IT_DATA_TMP.
CLEAR IT_DATA_TMP.
ENDLOOP.

ENDFORM. " FORMAT_ERROR_DATA

No comments: