*&---------------------------------------------------------------------*

*& Report  ZUFI004
*& Description : Download/Upload Vendor Master
*&---------------------------------------------------------------------*
REPORT ZUFI004 NO STANDARD PAGE HEADING
       LINE-COUNT 65 LINE-SIZE 1000 MESSAGE-ID MG.
 
*****************************************************************
**                      Define tables                          **
*****************************************************************
TABLES: LFA1,LFB1,LFM1, ADRC, WYT3, LFBK, ADR6, LFB5, KNVK.
 
*****************************************************************
**                Define selection criteria                    **
*****************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B00 WITH FRAME TITLE TEXT-S00.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
SELECT-OPTIONS: P_BUKRS FOR LFB1-BUKRS
                NO INTERVALS NO-EXTENSION OBLIGATORY DEFAULT '3331',
                S_EKORG FOR LFM1-EKORG OBLIGATORY DEFAULT '33D2'.
SELECTION-SCREEN END OF BLOCK B1.
*SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME.
*SELECT-OPTIONS: S_KTOKK FOR LFA1-KTOKK,
*                S_LIFNR FOR LFA1-LIFNR.
*SELECTION-SCREEN END OF BLOCK B2.
SELECTION-SCREEN END OF BLOCK B00.
 
SELECTION-SCREEN BEGIN OF BLOCK B02 WITH FRAME TITLE TEXT-S02.
PARAMETERS: P_UPLOAD LIKE RLGRAP-FILENAME DEFAULT TEXT-002.
PARAMETERS: P_UPFILE LIKE RLGRAP-FILENAME DEFAULT TEXT-003.
PARAMETERS: P_GROUP(12)  DEFAULT 'U_VENDOR'.
SELECTION-SCREEN END OF BLOCK B02.
 
*****************************************************************
**                Define internal table                        **
*****************************************************************
CONSTANTS: MX_TCODE LIKE TSTC-TCODE VALUE 'XK02'.
DATA: T_MODE TYPE C VALUE 'N'.
 
DATA: BEGIN OF IT_UPLOADPC OCCURS 0,
*First screen input
      BUKRS LIKE LFB1-BUKRS,          "Company (Company data)
      EKORG LIKE LFM1-EKORG,          "Purch. Org.(Purchasing org. data)
      KTOKK LIKE LFA1-KTOKK,          "Account group(General dada)
      LIFNR LIKE LFA1-LIFNR,          "Vendor
******GERENAL DATA**********************
*Field for screen number of 110
      ANRED LIKE LFA1-ANRED,          "Title
      NAME1 LIKE LFA1-NAME1,          "Vendor's Name1
      NAME2 LIKE LFA1-NAME2,          "Vendor's Name2
      SORT1 LIKE ADDR1_DATA-SORT1,    "Search term
      SORT2 LIKE ADDR1_DATA-SORT2,    "Search term2
      STR_SUPPL1 LIKE ADRC-STR_SUPPL1,                      "Street2
      STR_SUPPL2 LIKE ADRC-STR_SUPPL2,                      "Street3
      STR_SUPPL3 LIKE ADRC-STR_SUPPL3,                      "Street4
      STR_SUPPL4 LIKE ADRC-LOCATION,                        "Street5
 
      PSTLZ LIKE LFA1-PSTLZ,          "Postal code
      ORT01 LIKE LFA1-ORT01,          "City
      LAND1 LIKE LFA1-LAND1,          "Country
      REGIO LIKE LFA1-REGIO,          "Region
      PFACH  LIKE LFA1-PFACH,         "P.O. Box
      PSTL2  LIKE LFA1-PSTL2,         "P.O. Box postal code
 
*address_zf
      ADR_ZF(40) TYPE C,                "Chinese(ZF)
      SUPPL1_ZF LIKE ADRC-STR_SUPPL1,                       "Street2
      SUPPL2_ZF LIKE ADRC-STR_SUPPL2,                       "Street3
      SUPPL3_ZF LIKE ADRC-STR_SUPPL3,                       "Street4
      SUPPL4_ZF LIKE ADRC-LOCATION,                         "Street5
      PSTLZ_ZF  LIKE KNA1-PSTLZ,       "Postal code
      ORT01_ZF  LIKE KNA1-ORT01,       "City
 
*address_zh
      ADR_ZH(40) TYPE C,                "Chinese(ZH)
      SUPPL1_ZH LIKE ADRC-STR_SUPPL1,                       "Street2
      SUPPL2_ZH LIKE ADRC-STR_SUPPL2,                       "Street3
      SUPPL3_ZH LIKE ADRC-STR_SUPPL3,                       "Street4
      SUPPL4_ZH LIKE ADRC-LOCATION,                         "Street5
      PSTLZ_ZH  LIKE KNA1-PSTLZ,       "Postal code
      ORT01_ZH  LIKE KNA1-ORT01,       "City
 
      SPRAS(4),                       "Language
      TELF1 LIKE LFA1-TELF1,          "Tel number
      TELFX LIKE LFA1-TELFX,          "Fax number
      EMAIL(40) TYPE C,               "Eamil
      KUNNR(10),                      "Customer Code
 
      STCEG LIKE LFA1-STCEG,          "VAT reg.no.
      BANKS LIKE LFBK-BANKS,          "Country
      BANKL(15),                      "Bank key
      BANKN LIKE LFBK-BANKN,          "Bank account
      KOINH LIKE LFBK-KOINH,          "Account holder name
      ESRNR LIKE LFA1-ESRNR,          "ISR NUMBER
      CONTACT(40) TYPE C,             "Contact Person
 
******COMPANY CODE DATA*****************
      AKONT LIKE LFB1-AKONT,          "Recon. account
      ZUAWA LIKE LFB1-ZUAWA,          "Sort key
      ZTERM LIKE LFB1-ZTERM,          "Payment terms
      REPRF LIKE LFB1-REPRF,          "Chk double inv.
      ZWELS LIKE LFB1-ZWELS,          "Payment methods
      MAHNA LIKE LFB5-MAHNA,          "dunny procedure
 
******PURCHASING ORG. DATA**************
      WAERS LIKE LFM1-WAERS,          "Order currency
      INCO1 LIKE LFM1-INCO1,          "Incoterms
      INCO2 LIKE LFM1-INCO2,          "Incoterms(part 2)
      KALSK LIKE LFM1-KALSK,          "Schema Group, Vendor
      WEBRE LIKE LFM1-WEBRE,          "GR-based inv. verif.
      VERKF LIKE LFM1-VERKF,          "Salesperson
      TELF2 LIKE LFM1-TELF1,          "Telephone
      EIKTO LIKE LFM1-EIKTO,          "Acct with Vendor
      BRSCH LIKE LFA1-BRSCH,          "Industry
      END OF IT_UPLOADPC.
 
DATA: BEGIN OF ITAB OCCURS 0,
*First screen input
      BUKRS LIKE LFB1-BUKRS,          "Company (Company data)
      EKORG LIKE LFM1-EKORG,          "Purch. Org.(Purchasing org. data)
      KTOKK LIKE LFA1-KTOKK,          "Account group(General dada)
      LIFNR LIKE LFA1-LIFNR,          "Vendor
******GERENAL DATA**********************
*Field for screen number of 110
      ANRED LIKE LFA1-ANRED,          "Title
      NAME1 LIKE LFA1-NAME1,          "Vendor's Name1
      NAME2 LIKE LFA1-NAME2,          "Vendor's Name2
      SORT1 LIKE ADDR1_DATA-SORT1,    "Search term
      SORT2 LIKE ADDR1_DATA-SORT2,    "Search term2
      STR_SUPPL1 LIKE ADRC-STR_SUPPL1,                      "Street2
      STR_SUPPL2 LIKE ADRC-STR_SUPPL2,                      "Street3
      STR_SUPPL3 LIKE ADRC-STR_SUPPL3,                      "Street4
      STR_SUPPL4 LIKE ADRC-LOCATION,                        "Street5
 
      PSTLZ LIKE LFA1-PSTLZ,          "Postal code
      ORT01 LIKE LFA1-ORT01,          "City
      LAND1 LIKE LFA1-LAND1,          "Country
      REGIO LIKE LFA1-REGIO,          "Region
      PFACH  LIKE LFA1-PFACH,         "P.O. Box
      PSTL2  LIKE LFA1-PSTL2,         "P.O. Box postal code
 
*address_zf
      ADR_ZF(40) TYPE C,                "Chinese(ZF)
      SUPPL1_ZF LIKE ADRC-STR_SUPPL1,                       "Street2
      SUPPL2_ZF LIKE ADRC-STR_SUPPL2,                       "Street3
      SUPPL3_ZF LIKE ADRC-STR_SUPPL3,                       "Street4
      SUPPL4_ZF LIKE ADRC-LOCATION,                         "Street5
      PSTLZ_ZF  LIKE KNA1-PSTLZ,       "Postal code
      ORT01_ZF  LIKE KNA1-ORT01,       "City
 
*address_zh
      ADR_ZH(40) TYPE C,                "Chinese(ZH)
      SUPPL1_ZH LIKE ADRC-STR_SUPPL1,                       "Street2
      SUPPL2_ZH LIKE ADRC-STR_SUPPL2,                       "Street3
      SUPPL3_ZH LIKE ADRC-STR_SUPPL3,                       "Street4
      SUPPL4_ZH LIKE ADRC-LOCATION,                         "Street5
      PSTLZ_ZH  LIKE KNA1-PSTLZ,       "Postal code
      ORT01_ZH  LIKE KNA1-ORT01,       "City
 
      SPRAS(4),                       "Language
      TELF1 LIKE LFA1-TELF1,          "Tel number
      TELFX LIKE LFA1-TELFX,          "Fax number
      EMAIL(40) TYPE C,               "Eamil
      KUNNR(10),                      "Customer Code
 
      STCEG LIKE LFA1-STCEG,          "VAT reg.no.
      BANKS LIKE LFBK-BANKS,          "Country
      BANKL(15),                      "Bank key
      BANKN LIKE LFBK-BANKN,          "Bank account
      KOINH LIKE LFBK-KOINH,          "Account holder name
      ESRNR LIKE LFA1-ESRNR,          "ISR NUMBER
      CONTACT(40) TYPE C,
 
******COMPANY CODE DATA*****************
      AKONT LIKE LFB1-AKONT,          "Recon. account
      ZUAWA LIKE LFB1-ZUAWA,          "Sort key
      ZTERM LIKE LFB1-ZTERM,          "Payment terms
      REPRF LIKE LFB1-REPRF,          "Chk double inv.
      ZWELS LIKE LFB1-ZWELS,          "Payment methods
      MAHNA LIKE LFB5-MAHNA,          "dunny procedure
 
******PURCHASING ORG. DATA**************
      WAERS LIKE LFM1-WAERS,          "Order currency
      INCO1 LIKE LFM1-INCO1,          "Incoterms
      INCO2 LIKE LFM1-INCO2,          "Incoterms(part 2)
      KALSK LIKE LFM1-KALSK,          "Schema Group, Vendor
      WEBRE LIKE LFM1-WEBRE,          "GR-based inv. verif.
      VERKF LIKE LFM1-VERKF,          "Salesperson
      TELF2 LIKE LFM1-TELF1,          "Telephone
      EIKTO LIKE LFM1-EIKTO,          "Acct with Vendor
      BRSCH LIKE LFA1-BRSCH,          "Industry
 
      SUBRC LIKE SY-SUBRC,
      MESSAGE(120),
      END OF ITAB.
DATA: I_UPLOAD LIKE ITAB OCCURS 0 WITH HEADER LINE.
 
DATA: BEGIN OF I_TXT OCCURS 0,
     C_01(10)  VALUE 'Company',
     C_02(14)  VALUE 'Purch. Org.',
     C_03(18)  VALUE 'Account group',
     C_04(10)  VALUE 'Vendor',
*Field for screen number of 110
     C_05(15)  VALUE 'Title',
     C_06(40)  VALUE 'Vendor Name1',
     C_07(40)  VALUE 'Vendor Name2',
 
     C_08(20)  VALUE 'Search term1',
     C_09(20)  VALUE 'Search term2',
     C_10(40)  VALUE 'Street2',
     C_11(40)  VALUE 'Street3',
     C_12(40)  VALUE 'Street4',
     C_13(40)  VALUE 'Street5',
     C_14(15) VALUE 'Postal code',
     C_15(6)  VALUE 'City',
     C_16(10)  VALUE 'Country',
     C_17(8)  VALUE 'Region',
     C_18(20) VALUE 'PO Box',
     C_19(20) VALUE 'PO Box Postal Code',
     C_20(20) VALUE 'Chinese(ZF)',
     C_21(40) VALUE 'Street2',
     C_22(40) VALUE 'Street3',
     C_23(40) VALUE 'Street4',
     C_24(40) VALUE 'Street5',
     C_25(15) VALUE 'Postal code',
     C_26(6)  VALUE 'City',
     C_27(20) VALUE 'Chinese(ZH)',
     C_28(40) VALUE 'Street2',
     C_29(40) VALUE 'Street3',
     C_30(40) VALUE 'Street4',
     C_31(40) VALUE 'Street5',
     C_32(15) VALUE 'Postal code',
     C_33(6)  VALUE 'City',
 
     C_34(10) VALUE 'Language',
     C_35(18) VALUE 'Tel number',
     C_36(18) VALUE 'Fax number',
     C_37(20) VALUE 'Eamil',
     C_38(20) VALUE 'Customer Code',
 
     C_39(24) VALUE 'VAT reg.no.',
     C_40(10) VALUE 'Country',
     C_41(10) VALUE 'Bank key',
     C_42(40) VALUE 'Bank account',
     C_43(27) VALUE 'Account holder name',
     C_44(20) VALUE 'ISR Number',
     C_45(20) VALUE 'Contact Person',
 
*Field for screen number of 210
     C_46(15) VALUE 'Recon. account',
     C_47(10) VALUE 'Sort key',
 
*Field for screen number of 215
     C_48(15) VALUE 'Payment terms',
     C_49(18) VALUE 'Chk double inv.',
     C_50(20) VALUE 'Payment methods',
     C_51(15) VALUE 'Dunning Pro',
 
*Field for screen number of 310
     C_52(10) VALUE 'Currency',
     C_53(12) VALUE 'Incoterms',
     C_54(24) VALUE 'Incoterms(Part 2)',
     C_55(30) VALUE 'Schema Group, Vendor',
     C_56(25) VALUE 'GR-based inv. verif.',
     C_57(40) VALUE 'Sales Person',
     C_58(20) VALUE 'Telephone',
     C_59(20) VALUE 'Acct with Vendor',
     C_60(12) VALUE 'Industry',
     C_61(15) VALUE 'Invoicing Party',
     C_62(30) VALUE 'Company Level deletion',
     C_63(30) VALUE 'Purchasing Level deletion',
     C_64(30) VALUE 'Central deletion block',
     END OF I_TXT.
 
* BDC data
DATA BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
 
* Messages of call transaction
DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
*****************************************************************
AT SELECTION-SCREEN.
*****************************************************************
 
  IF P_GROUP = ''.
    MESSAGE E000 WITH 'Batch input session name cannot empty !!!'.
  ENDIF.
 
 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_UPLOAD. "For dropdown filename
  PERFORM F4_FILENAME USING P_UPLOAD.
*****************************************************************
START-OF-SELECTION.
*****************************************************************
 
  PERFORM SUB_UPLOAD_FILE TABLES IT_UPLOADPC
                            USING P_UPLOAD.
  MOVE: IT_UPLOADPC[] TO ITAB[].
 
** Delete Head line
*    DELETE ITAB INDEX 1. "Delete Head line
 
  PERFORM SUB_CHANGE_DATA.
 
  IF I_UPLOAD[] IS INITIAL.
    MESSAGE S208 WITH 'no data match your selected criteria.'.
  ELSE.
    PERFORM DBC_PROCESS_ONLINE.
    PERFORM SUB_UPLOAD_RESULT.
  ENDIF.
 
*****************************************************************
*&---------------------------------------------------------------------*
*&      Form  sub_change_data
*&---------------------------------------------------------------------*
FORM SUB_CHANGE_DATA.
  DELETE ITAB WHERE NAME1 = ''.
  LOOP AT ITAB.
*Bank key
*    IF ITAB-BANKL NE ''.
*      PERFORM DATA_ALPHA_CONVERSION USING ITAB-BANKL.
*    ENDIF.
*Sort key
    PERFORM DATA_ALPHA_CONVERSION USING ITAB-ZUAWA.
*Dunn.Procedure
    IF ITAB-MAHNA NE ''.
      PERFORM DATA_ALPHA_CONVERSION USING ITAB-MAHNA.
    ENDIF.
*Schema Group, Vendor
    IF ITAB-KALSK NE ''.
      PERFORM DATA_ALPHA_CONVERSION USING ITAB-KALSK.
    ENDIF.
    IF ITAB-LIFNR NE ''.
      PERFORM DATA_ALPHA_CONVERSION USING ITAB-LIFNR.
    ENDIF.
    MODIFY ITAB.
    MOVE-CORRESPONDING ITAB TO I_UPLOAD.
    I_UPLOAD-SUBRC = 0.
    I_UPLOAD-MESSAGE = ''.
    APPEND I_UPLOAD.
    CLEAR I_UPLOAD.
  ENDLOOP.
  IF S_EKORG NE ''.
    DELETE I_UPLOAD WHERE NOT ( BUKRS IN P_BUKRS AND EKORG IN S_EKORG ).
  ELSE.
    DELETE I_UPLOAD WHERE NOT ( BUKRS IN P_BUKRS ).
  ENDIF.
ENDFORM.                    "SUB_CHANGE_DATA
 
*&---------------------------------------------------------------------*
*&      Form  sub_upload_file
*&---------------------------------------------------------------------*
FORM SUB_UPLOAD_FILE TABLES IT_DATA USING P_FILE.
  CALL FUNCTION 'WS_UPLOAD'
    EXPORTING
      CODEPAGE                = ' '
      FILENAME                = P_UPLOAD
      FILETYPE                = 'DAT'
    TABLES
      DATA_TAB                = IT_DATA
    EXCEPTIONS
      CONVERSION_ERROR        = 1
      FILE_OPEN_ERROR         = 2
      FILE_READ_ERROR         = 3
      INVALID_TYPE            = 4
      NO_BATCH                = 5
      UNKNOWN_ERROR           = 6
      INVALID_TABLE_WIDTH     = 7
      GUI_REFUSE_FILETRANSFER = 8
      CUSTOMER_ERROR          = 9
      OTHERS                  = 10.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    " sub_upload_file
 
*&---------------------------------------------------------------------*
*&      Form  bdc_process_online
*&---------------------------------------------------------------------*
FORM DBC_PROCESS_ONLINE.
  FORMAT COLOR COL_HEADING.
  WRITE: / 'ONLINE Upload Data :'.
  ULINE /.
  SKIP.
  FORMAT COLOR OFF.
 
  LOOP AT I_UPLOAD.
    PERFORM BDC_SAVE_DATA.
    PERFORM DBC_PROCESS.
  ENDLOOP.
ENDFORM.                    "DBC_PROCESS_ONLINE
 
*&---------------------------------------------------------------------*
*&      Form  BDC_SAVE_DATA
*&---------------------------------------------------------------------*
FORM BDC_SAVE_DATA.
  DATA: TCKALSK(2) TYPE C,
        TK_LIFNR(10) TYPE C,
        TK_NAME1(40) TYPE C,
        TK_NAME2(40) TYPE C.
 
 
  REFRESH: BDCDATA,MESSTAB.
  CLEAR:   BDCDATA,MESSTAB.
*initial screen
 
*Generate Data & address
  WRITE I_UPLOAD-LIFNR TO TK_LIFNR.
  PERFORM DATA_ALPHA_CONVERSION USING TK_LIFNR.
  SELECT SINGLE * FROM LFA1 WHERE LIFNR = TK_LIFNR.
  IF SY-SUBRC EQ 0.
    PERFORM BDC_DYNPRO      USING 'SAPMF02K' '0101'.
    PERFORM BDC_FIELD       USING 'BDC_CURSOR'
                                  'USE_ZAV'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '/00'.
    PERFORM BDC_FIELD       USING 'RF02K-LIFNR' I_UPLOAD-LIFNR.
    PERFORM BDC_FIELD       USING 'RF02K-BUKRS' I_UPLOAD-BUKRS.
    PERFORM BDC_FIELD       USING 'RF02K-EKORG' I_UPLOAD-EKORG.
    PERFORM BDC_FIELD       USING 'RF02K-D0110' 'X'.
    PERFORM BDC_FIELD       USING 'USE_ZAV' 'X'.
    PERFORM BDC_DYNPRO      USING 'SAPMF02K' '0111'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '=UPDA'.
    PERFORM BDC_FIELD       USING 'BDC_CURSOR'
                                  'ADDR1_DATA-STR_SUPPL1'.
    PERFORM BDC_FIELD       USING 'SZA1_D0100-TITLE_MEDI' I_UPLOAD-ANRED.
    PERFORM BDC_FIELD       USING 'ADDR1_DATA-NAME1' I_UPLOAD-NAME1.
    PERFORM BDC_FIELD       USING 'ADDR1_DATA-SORT1' I_UPLOAD-SORT1.
    PERFORM BDC_FIELD       USING 'ADDR1_DATA-STR_SUPPL1' I_UPLOAD-STR_SUPPL1.
    PERFORM BDC_FIELD       USING 'ADDR1_DATA-COUNTRY' I_UPLOAD-LAND1.
    PERFORM BDC_FIELD       USING 'ADDR1_DATA-LANGU' I_UPLOAD-SPRAS.
    PERFORM BDC_FIELD       USING 'SZA1_D0100-TEL_NUMBER' I_UPLOAD-TELF1.
*    PERFORM BDC_TRANSACTION USING 'XK02'.
  ENDIF.
 
ENDFORM.                    " BDC_SAVE_DATA
 
*&---------------------------------------------------------------------*
*&      Form  bdc_process
*&---------------------------------------------------------------------*
FORM DBC_PROCESS.
  CALL TRANSACTION MX_TCODE USING BDCDATA
                            UPDATE 'S'
*                            MODE 'A'
                            MODE T_MODE
                            MESSAGES INTO MESSTAB.
 
  READ TABLE MESSTAB WITH KEY MSGTYP = 'S'
                              MSGID  = 'F2'
                              MSGNR  = '175'.
  IF SY-SUBRC = 0.
    I_UPLOAD-SUBRC = 0.
    I_UPLOAD-LIFNR = MESSTAB-MSGV1+0(10).
  ELSE.
    ITAB-SUBRC = 4.
    READ TABLE MESSTAB WITH KEY MSGTYP = 'E'.
    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        MSGID               = MESSTAB-MSGID
        MSGNR               = MESSTAB-MSGNR
        MSGV1               = MESSTAB-MSGV1
        MSGV2               = MESSTAB-MSGV2
        MSGV3               = MESSTAB-MSGV3
        MSGV4               = MESSTAB-MSGV4
      IMPORTING
        MESSAGE_TEXT_OUTPUT = I_UPLOAD-MESSAGE.
  ENDIF.
  MODIFY I_UPLOAD.
  CLEAR I_UPLOAD.
ENDFORM.                    "DBC_PROCESS
 
*&---------------------------------------------------------------------*
*&      Form  SUB_UPLOAD_RESULT
*&---------------------------------------------------------------------*
FORM SUB_UPLOAD_RESULT.
  DATA: BEGIN OF ITAB_OUT OCCURS 0,
         ERROR(1) TYPE C,
         KTOKK LIKE LFA1-KTOKK,       "Account grp
         LIFNR LIKE LFA1-LIFNR,       "Vendor Code
         BUKRS LIKE KNB1-BUKRS,       "Company
         EKORG LIKE LFM1-EKORG,       "Purch. Org.
         NAME1 LIKE LFA1-NAME1,       "Company name1
         SORT1 LIKE ADDR1_DATA-SORT1, "Sort Term 1
         MESSAGE(50),
        END OF ITAB_OUT.
 
  DATA: BEGIN OF OUT_TXT OCCURS 0,
          C1(10)  VALUE 'Error',
          C2(15)  VALUE 'Account grp',
          C3(20)  VALUE 'Vendor Code',
          C4(10)  VALUE 'Company',
          C5(20)  VALUE 'Purchase Org.',
          C8(15)  VALUE 'Company Name',
          C9(20)  VALUE 'Sort Term',
          C10(50) VALUE 'Message',
        END OF OUT_TXT.
 
  LOOP AT I_UPLOAD.
    MOVE-CORRESPONDING I_UPLOAD TO ITAB_OUT.
    IF I_UPLOAD-SUBRC = 4.
      MOVE 'X' TO ITAB_OUT-ERROR.
    ENDIF.
    APPEND ITAB_OUT.
    CLEAR ITAB_OUT.
  ENDLOOP.
 
  APPEND OUT_TXT.
  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      FILENAME                = P_UPFILE
      FILETYPE                = 'DAT'
    TABLES
      DATA_TAB                = OUT_TXT
    EXCEPTIONS
      INVALID_FILESIZE        = 1
      INVALID_TABLE_WIDTH     = 2
      INVALID_TYPE            = 3
      NO_BATCH                = 4
      UNKNOWN_ERROR           = 5
      GUI_REFUSE_FILETRANSFER = 6
      CUSTOMER_ERROR          = 7
      OTHERS                  = 8.
 
  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      FILENAME                = P_UPFILE
      FILETYPE                = 'DAT'
      MODE                    = 'A'
    TABLES
      DATA_TAB                = ITAB_OUT
    EXCEPTIONS
      INVALID_FILESIZE        = 1
      INVALID_TABLE_WIDTH     = 2
      INVALID_TYPE            = 3
      NO_BATCH                = 4
      UNKNOWN_ERROR           = 5
      GUI_REFUSE_FILETRANSFER = 6
      CUSTOMER_ERROR          = 7
      OTHERS                  = 8.
ENDFORM.                    "SUB_UPLOAD_RESULT
 
*----------------------------------------------------------------------*
*     form insert_prog                                                 *
*----------------------------------------------------------------------*
FORM INSERT_PROG.
  CALL FUNCTION 'BDC_INSERT'
    EXPORTING
      TCODE     = MX_TCODE
    TABLES
      DYNPROTAB = BDCDATA.
ENDFORM.                    "INSERT_PROG
 
*----------------------------------------------------------------------*
*      Form open_session                                               *
*----------------------------------------------------------------------*
FORM OPEN_BDC_SESSION.
  CALL FUNCTION 'BDC_OPEN_GROUP'
    EXPORTING
      CLIENT              = SY-MANDT
      GROUP               = P_GROUP
      KEEP                = 'X'
      USER                = SY-UNAME
    EXCEPTIONS
      CLIENT_INVALID      = 01
      DESTINATION_INVALID = 02
      GROUP_INVALID       = 03
      HOLDDATE_INVALID    = 04
      INTERNAL_ERROR      = 05
      QUEUE_ERROR         = 06
      RUNNING             = 07
      USER_INVALID        = 08.
 
  IF SY-SUBRC <> 0.
    WRITE : / 'Open BDC Error.'.
  ENDIF.
ENDFORM.                    "OPEN_BDC_SESSION
 
*----------------------------------------------------------------------*
*     form close bdc ssession                                          *
*----------------------------------------------------------------------*
FORM CLOSE_BDC_SESSION.
  CALL FUNCTION 'BDC_CLOSE_GROUP'
    EXCEPTIONS
      NOT_OPEN    = 01
      QUEUE_ERROR = 02.
ENDFORM.                    "CLOSE_BDC_SESSION
 
*&---------------------------------------------------------------------*
*&      Form  data_alpha_conversion
*&---------------------------------------------------------------------*
FORM DATA_ALPHA_CONVERSION  USING  P_ALPHA.
 
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      INPUT  = P_ALPHA
    IMPORTING
      OUTPUT = P_ALPHA.
ENDFORM.                    " data_alpha_conversion
 
*----------------------------------------------------------------------*
* Start new screen                                                     *
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.                    "BDC_DYNPRO
 
*----------------------------------------------------------------------*
* Insert field                                                         *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
  CLEAR BDCDATA.
  BDCDATA-FNAM = FNAM.
  BDCDATA-FVAL = FVAL.
  APPEND BDCDATA.
ENDFORM.                    "BDC_FIELD
 
*&---------------------------------------------------------------------*
*&      Form  f4_filename
*&---------------------------------------------------------------------*
FORM F4_FILENAME USING    P_FILE.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME  = SYST-CPROG
      DYNPRO_NUMBER = SYST-DYNNR
      FIELD_NAME    = 'P_FILE'
    IMPORTING
      FILE_NAME     = P_FILE.
ENDFORM.                    " f4_filename