Monday, October 26, 2015

How to download and upload an Idoc

1. Downloading of IDOC using IDOC_READ_COMPLETELY Function

Using this FM : IDOC_READ_COMPLETELY you can get the data records and status records as well, just give the IDOC no: ,that's enough.


REPORT  ZVXER_IDOCDOWNLOAD
  MESSAGE-ID zv
  LINE-SIZE 1023.

*&*&********************************************************************
*&*&*  TABLES
*&*&********************************************************************

TABLES:     edidc,
            edoc_stli.

TYPE-POOLSslis,
            ABAP.

*&*&********************************************************************
*&*&     INCLUDES
*$*$    (FOR INCLUDES WITH DECLARATIVE PURPOSES ONLY
*$*$     I.E. BDC, ALV, ETC.)
*&*&********************************************************************

INCLUDE idocgdt2.

*$*$********************************************************************
*$*$    GLOBAL CONSTANTS
*$*$********************************************************************

CONSTANTSc_x(1)        TYPE c          VALUE 'X',
           c_1(1)        TYPE c          VALUE '1',
           c_2(1)        TYPE c          VALUE '2',
           c_dot(1)      TYPE c          VALUE '.',
           c_dots(3)     TYPE c          VALUE '...',
           c_asc(10)     TYPE c          VALUE 'ASC',
           c_4310     TYPE ABAP_ENCODING VALUE '4310',
           c_download(20TYPE c         VALUE 'C:\Temp\Download',
           c_a(1)        TYPE c          VALUE '&',
           c_amp(5)      TYPE c          VALUE '&',
           c_gt(1)       TYPE c          VALUE '>',
           c_lt(1)       TYPE c          VALUE '<',
           c_htmgt(4)    TYPE c          VALUE '&gt;',
           c_htmlt(4)    TYPE c          VALUE '&lt;',
           c_text(20)    TYPE c          VALUE 'Downloading...'.

CONSTANTSc_tknumexti1(11TYPE c       VALUE 'TKNUM|EXTI1',
           c_belnr(5)    TYPE c          VALUE 'BELNR',
           c_vbeln(5)    TYPE c          VALUE 'VBELN',
           c_vtrnr(5)    TYPE c          VALUE 'VTRNR',
           c_matnr(5)    TYPE c          VALUE 'MATNR',
           c_bktxt(5)    TYPE c          VALUE 'BKTXT',
           c_aufnr(5)    TYPE c          VALUE 'AUFNR',
           c_tabnam      TYPE edisegdef  VALUE 'TABNAM',
           c_mandt       TYPE edisegdef  VALUE 'MANDT',
           c_docnum      TYPE edisegdef  VALUE 'DOCNUM',
           c_docrel      TYPE edisegdef  VALUE 'DOCREL',
           c_status      TYPE edisegdef  VALUE 'STATUS',
           c_direct      TYPE edisegdef  VALUE 'DIRECT',
           c_outmod      TYPE edisegdef  VALUE 'OUTMOD',
           c_exprss      TYPE edisegdef  VALUE 'EXPRSS',
           c_test        TYPE edisegdef  VALUE 'TEST',
           c_idoctyp     TYPE edisegdef  VALUE 'IDOCTYP',
           c_cimtyp      TYPE edisegdef  VALUE 'CIMTYP',
           c_mestyp      TYPE edisegdef  VALUE 'MESTYP',
           c_mescod      TYPE edisegdef  VALUE 'MESCOD',
           c_mesfct      TYPE edisegdef  VALUE 'MESFCT',
           c_std         TYPE edisegdef  VALUE 'STD',
           c_stdvrs      TYPE edisegdef  VALUE 'STDVRS',
           c_stdmes      TYPE edisegdef  VALUE 'STDMES',
           c_sndpor      TYPE edisegdef  VALUE 'SNDPOR',
           c_sndprt      TYPE edisegdef  VALUE 'SNDPRT',
           c_sndpfc      TYPE edisegdef  VALUE 'SNDPFC',
           c_sndprn      TYPE edisegdef  VALUE 'SNDPRN',
           c_sndsad      TYPE edisegdef  VALUE 'SNDSAD',
           c_sndlad      TYPE edisegdef  VALUE 'SNDLAD',
           c_rcvpor      TYPE edisegdef  VALUE 'RCVPOR',
           c_rcvprt      TYPE edisegdef  VALUE 'RCVPRT',
           c_rcvpfc      TYPE edisegdef  VALUE 'RCVPFC',
           c_rcvprn      TYPE edisegdef  VALUE 'RCVPRN',
           c_rcvsad      TYPE edisegdef  VALUE 'RCVSAD',
           c_rcvlad      TYPE edisegdef  VALUE 'RCVLAD',
           c_credat      TYPE edisegdef  VALUE 'CREDAT',
           c_cretim      TYPE edisegdef  VALUE 'CRETIM',
           c_refint      TYPE edisegdef  VALUE 'REFINT',
           c_refgrp      TYPE edisegdef  VALUE 'REFGRP',
           c_refmes      TYPE edisegdef  VALUE 'REFMES',
           c_arckey      TYPE edisegdef  VALUE 'ARCKEY',
           c_serial      TYPE edisegdef  VALUE 'SERIAL',
           c_i(1)        TYPE c          VALUE 'I',
           c_eq(2)       TYPE c          VALUE 'EQ',
           c_out(4)      TYPE c          VALUE '-out',
           c_in(3)       TYPE c          VALUE '-in',
           c_dash(1)     TYPE c          VALUE '-',
           c_e1edk01(7)  TYPE c          VALUE 'E1EDK01',
           c_e1edt20(7)  TYPE c          VALUE 'E1EDT20',
           c_e1edl20(7)  TYPE c          VALUE 'E1EDL20',
           c_e1edk09(7)  TYPE c          VALUE 'E1EDK09',
           c_e1maram(7)  TYPE c          VALUE 'E1MARAM',
           c_z1qchgh(7)  TYPE c          VALUE 'Z1QCHGH',
           c_z1prodh(7)  TYPE c          VALUE 'Z1PRODH',
           c_e1mbxyh(7)  TYPE c          VALUE 'E1MBXYH',
           c_zasncomm(9TYPE c          VALUE 'ZASN_COMM',
           c_delimite(1TYPE c          VALUE '|',
           c_edidc40(8)  TYPE c          VALUE 'EDI_DC40',
           c_0(1)        TYPE c          VALUE '0',
           c_9(1)        TYPE c          VALUE '9',
           c_lowa(1)     TYPE c          VALUE 'a',
           c_lowz(1)     TYPE c          VALUE 'z',
           c_capA(1)     TYPE c          VALUE 'A',
           c_capZ(1)     TYPE c          VALUE 'Z',
           c_txtext(4)   TYPE c          VALUE '.txt',
           c_xmltxt(4)   TYPE c          VALUE '.xml',
           c_rseidoc2    TYPE rsvar-report VALUE 'RSEIDOC2',
           c_lesst(1)    TYPE c          VALUE '<',
           c_moret(1)    TYPE c          VALUE '>',
           c_lesslash(2TYPE c          VALUE '</',
           c_xml28(39)   TYPE c          VALUE '<?xml version="1.0" encoding="UTF-8" ?>',
           c_xml29(19)   TYPE c          VALUE '   <IDOC BEGIN="1">',
           c_xml33(10)   TYPE c          VALUE '   </IDOC>',
           c_xml34(22)   TYPE c          VALUE '<EDI_DC40 SEGMENT="1">',
           c_xml35(11)   TYPE c          VALUE '</EDI_DC40>',
           c_xml39(13)   TYPE c          VALUE ' SEGMENT="1">'.

*$*$********************************************************************
*$*$    Global Variables
*$*$********************************************************************

TYPES:   BEGIN OF y_filecontent,
           e_xline(1100TYPE c,
         END OF y_filecontent.

TYPES:   BEGIN OF y_tab_docnum,
            docnum TYPE edidc-docnum,
         END OF y_tab_docnum.

DATA:    w_edidc        TYPE edidc,
         w_filecontent  TYPE y_filecontent,
         w_docnum       TYPE y_tab_docnum,
         w_text(40)     TYPE c.

DATA:    is_edidd   TYPE STANDARD TABLE OF edidd.

DATA:    ts_filecontent TYPE STANDARD TABLE OF y_filecontent,
         ts_docnum      TYPE STANDARD TABLE OF y_tab_docnum.

FIELD-SYMBOLS<fs_edidd> TYPE edidd.

*$*$********************************************************************
*$*$    PARAMETERS & SELECT-OPTIONS
*$*$   NAMING CONVENTION: "P_NAME" & "S_NAME"
*$*$********************************************************************

SELECTION-SCREEN BEGIN OF BLOCK ss01 WITH FRAME TITLE text-b30.

SELECT-OPTIONS:    s_cretim  FOR edidc-cretim
                           DEFAULT intim_from_initial_value
                                TO intim_to_initial_value,
                   s_credat  FOR edidc-credat
                           DEFAULT sy-datum
                                TO sy-datum.

SELECTION-SCREEN SKIP.

SELECT-OPTIONS:    s_direct  FOR edidc-direct,
                   s_docnum  FOR edidc-docnum,
                   s_status  FOR edidc-status.

SELECTION-SCREEN SKIP.

SELECT-OPTIONS:    s_idoctp  FOR edidc-idoctp,
                   s_cimtyp  FOR edidc-cimtyp,
                   s_mestyp  FOR edidc-mestyp.

SELECTION-SCREEN SKIP.

SELECT-OPTIONS:    s_sndpor  FOR edoc_stli-sndpor,
                   s_sndprt  FOR edoc_stli-sndprt,
                   s_sndprn  FOR edoc_stli-sndprn.

SELECTION-SCREEN SKIP.

SELECT-OPTIONS:    s_rcvpor  FOR edoc_stli-rcvpor,
                   s_rcvprt  FOR edoc_stli-rcvprt,
                   s_rcvprn  FOR edoc_stli-rcvprn.

SELECTION-SCREEN END OF BLOCK ss01.

* Block1 - Format (plain / tagged) -------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK ss02 WITH FRAME TITLE text-b10.

PARAMETER:         p_idocpl  RADIOBUTTON GROUP gtyp,
                   p_idoctg  RADIOBUTTON GROUP gtyp.

SELECTION-SCREEN END OF BLOCK ss02.

* Block2 - Display / Download ------------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK ss03 WITH FRAME TITLE text-b20.

PARAMETER:         p_dspscn  RADIOBUTTON GROUP gdsp,
                   p_dspdl1  RADIOBUTTON GROUP gdsp,
                   p_dspdln  RADIOBUTTON GROUP gdsp,
                   p_file    TYPE localfile DEFAULT c_download.

SELECTION-SCREEN END OF BLOCK ss03.

*&*&********************************************************************
*&*&*  INITIALIZATION
*&*&********************************************************************

INITIALIZATION.

  MOVE c_x TO p_idocpl.

  MOVE c_x TO p_dspscn.

*&*&********************************************************************
*&*&*  AT SELECTION SCREEN
*&*&********************************************************************

AT SELECTION-SCREEN.

*&*&********************************************************************
*&*&*  START OF SELECTION
*&*&********************************************************************

START-OF-SELECTION.

  PERFORM f_generate_data.

*&*&********************************************************************
*&*&* AT LINE-SELECTION.
*&*&********************************************************************

AT LINE-SELECTION.

  DATAis_rsparams  TYPE STANDARD TABLE OF rsparams .

  DATAe_lit_rsparams TYPE rsparams.

  CLEARe_lit_rsparams.

  FREE:  is_rsparams.

  CALL FUNCTION 'RS_REFRESH_FROM_SELECTOPTIONS'
      EXPORTING
           curr_report     =  c_rseidoc2
*      IMPORTING
*           SP              =
      TABLES
           selection_table is_rsparams
      EXCEPTIONS
          not_found       1
          no_report       2
          OTHERS          3
             .
  IF sy-subrc NE 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

  MOVEc_i      TO  e_lit_rsparams-sign,
        c_eq     TO  e_lit_rsparams-option,
        w_docnum TO  e_lit_rsparams-low.

  MODIFY is_rsparams
    FROM e_lit_rsparams
    TRANSPORTING sign option low
   WHERE selname EQ c_docnum.

  SUBMIT rseidoc2 WITH SELECTION-TABLE is_rsparams
    AND RETURN.

*-----------------------------------------------------------------------
*  Display IDoc in plain format
*-----------------------------------------------------------------------

FORM f_generate_data.

  DATA:  is_fieldname   TYPE STANDARD TABLE OF fieldname.

  DATA:  w_xvalue       TYPE char200,
         w_caddfile     TYPE localfile,
         w_xtmpdoc      TYPE char20,
         w_xfields      TYPE char200.

  FIELD-SYMBOLS<fs_fieldname> TYPE fieldname.

  CLEARw_xtmpdoc,
         w_xvalue.

  FREE is_fieldname.

  IF  p_dspdl1 EQ c_x )
  OR  p_dspdln EQ c_x )
  AND p_file IS INITIAL .

    MESSAGE e000(0hWITH text-e01.

  ENDIF.

* select all IDocs according EDIDC selection criteria

  FREE ts_docnum.

  SELECT docnum
    FROM edidc
    INTO TABLE ts_docnum
   WHERE cretim IN s_cretim
     AND credat IN s_credat
     AND direct IN s_direct
     AND docnum IN s_docnum
     AND status IN s_status
     AND idoctp IN s_idoctp
     AND cimtyp IN s_cimtyp
     AND mestyp IN s_mestyp
     AND rcvpor IN s_rcvpor
     AND rcvprt IN s_rcvprt
     AND rcvprn IN s_rcvprn
     AND sndpor IN s_sndpor
     AND sndprt IN s_sndprt
     AND sndprn IN s_sndprn
   ORDER BY docnum.

* Message if not data are found

  IF sy-subrc NE 0.

    MESSAGE i000(0hWITH text-i01.

    EXIT.

  ENDIF.

  CLEAR w_docnum.

  LOOP AT  ts_docnum
    INTO w_docnum.

    FREE :   is_edidd.

    CLEAR w_edidc.

    CALL FUNCTION 'IDOC_READ_COMPLETELY'
      EXPORTING
         document_number                w_docnum-docnum
      IMPORTING
         idoc_control                   w_edidc
*        NUMBER_OF_DATA_RECORDS         =
*        NUMBER_OF_STATUS_RECORDS       =
      TABLES
*       INT_EDIDS                      =
         int_edidd                      is_edidd
      EXCEPTIONS
         document_not_exist             1
         document_number_invalid        2
         OTHERS                         3.

    IF sy-subrc NE 0.

      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ENDIF.

*   determine file name appendix

    CLEAR w_caddfile.

*   only for EACH file download.
    IF p_dspdln EQ c_x.

      CONCATENATE w_caddfile c_dash sy-sysid sy-mandt INTO w_caddfile.

      MOVE w_docnum TO w_xtmpdoc.

      SHIFT w_xtmpdoc LEFT DELETING LEADING c_0.

      CONCATENATE w_caddfile c_dash w_xtmpdoc INTO w_caddfile.

      IF w_edidc-direct EQ c_1.

        CONCATENATE w_caddfile c_out  INTO w_caddfile.

      ELSE.

        CONCATENATE w_caddfile c_in  INTO w_caddfile.

      ENDIF.

      IF w_edidc-cimtyp IS INITIAL.

        CONCATENATE w_caddfile c_dash w_edidc-mestyp c_dot w_edidc-idoctp                      INTO w_caddfile.

      ELSE.

        CONCATENATE w_caddfile c_dash w_edidc-mestyp c_dot w_edidc-idoctp c_dot w_edidc-cimtyp INTO w_caddfile.

      ENDIF.

      IF w_edidc-direct EQ c_1.

        CONCATENATE w_caddfile c_dash w_edidc-rcvprn INTO w_caddfile.

      ELSE.

        CONCATENATE w_caddfile c_dash w_edidc-sndprn INTO w_caddfile.

      ENDIF.

*     determine first IDOC line and to be considered field names

      UNASSIGN <fs_edidd>.

      LOOP AT is_edidd
        ASSIGNING <fs_edidd>.

        EXIT.

      ENDLOOP.

      CLEAR w_xfields.

*     Verification can be done via SAP table IDOCSYN
      CASE <fs_edidd>-segnam.

        WHEN c_e1edk01.
*         for IDoc type ORDERS01, ..., ORDERS05, INVOIC01, ORDERS02

          MOVE c_belnr TO w_xfields.

        WHEN c_e1edt20.
*         for IDoc type SHPMNT02, ..., SHPMNT06

          MOVE c_tknumexti1 TO w_xfields.

        WHEN c_e1edl20.
*         for IDoc type DELVRY01, ..., DELVRY07

          MOVE c_vbeln TO w_xfields.

        WHEN  c_e1edk09.
*         for IDoc type DELFOR01, DELFOR02

          MOVE c_vtrnr TO w_xfields.

        WHEN c_e1maram.
*         for IDoc type MATMAS01, ..., MATMAS05

          MOVE c_matnr TO w_xfields.

        WHEN c_z1qchgh.
*         for IDoc type ZQMCHG01

          MOVE c_bktxt TO w_xfields.

        WHEN c_z1prodh.
*         for IDoc type ZMPROD01

          MOVE c_aufnr TO w_xfields.

        WHEN c_e1mbxyh.
*         for IDoc type WMMBID01, WMMBID02, ZINVSNAP, ZINVSP

          MOVE c_bktxt TO  w_xfields.

        WHEN c_zasncomm.
*         for IDoc type ZSHASN (no information in 1st segment available)

          MOVE space   TO  w_xfields.

        WHEN others.

      ENDCASE" case <SEGMENT> - move <FIELDNAMES>   <IDOCTYPE>

*     try to get reference information (consider 'ONLY09AZ')

      FREE is_fieldname.

      SPLIT w_xfields AT c_delimite INTO TABLE is_fieldname.

      CLEAR w_xvalue.

      UNASSIGN <fs_fieldname>.

      FREEts_filecontent.

      LOOP AT is_fieldname
        ASSIGNING <fs_fieldname>.

        PERFORM f_get_idoc_field_value
          USING <fs_edidd> <fs_fieldname> CHANGING w_xvalue.

        PERFORM f_fnchfx_only09az
          CHANGING w_xvalue.

        IF NOT w_xvalue IS INITIAL.

          EXIT.

        ENDIF.

      ENDLOOP.

*     if value found: add to filename

      IF NOT w_xvalue IS INITIAL.

        CONCATENATE w_caddfile c_dash w_xvalue INTO w_caddfile.

      ENDIF.

    ENDIF"for downloading of each file

*   print / provide data

    IF p_idocpl EQ c_x.

      PERFORM f_display_idoc_plain_ai.

    ELSE.

      PERFORM f_display_idoc_tagged_pi.

    ENDIF.

*   Download data for single file mode (only for EACH file download.)

    IF p_dspdln EQ c_x.

      PERFORM f_download_to_pcgui
        USING w_caddfile.

      CLEAR w_filecontent.

      FREE ts_filecontent.

    ENDIF.

  ENDLOOP.

* Download content, if filename is specified (only for EACH file download)

  IF p_dspdln EQ c_x.

    MESSAGE i000(0hWITH text-i02 p_file.

  ENDIF.

* only for BUNDLE file download.
  IF p_dspdl1 EQ c_x.

    PERFORM f_download_to_pcgui
      USING space.

    MESSAGE i000(0hWITH text-i02 p_file.

  ENDIF.

ENDFORM.                    "F_GENERATE_DATA

*-----------------------------------------------------------------------
*  Display IDoc in plain format
*-----------------------------------------------------------------------

FORM f_display_idoc_plain_ai.

  DATA:   w_xsegdef      TYPE edisegdef,
          w_xversion     TYPE segmentvrs,
          w_xdot         TYPE char3.

  CLEAR w_xdot.

* write EDIDC information in layout of EDI_DC40

  IF p_dspscn EQ c_x.

*  data are written here field-by-field via write for EDI_DC40:
*  if user presses F1, then technical field name will be displayed
*  only for screen display

    WRITE/  c_edidc40,
          AT   11 w_edidc-mandt,
          AT   14 w_edidc-docnum,
          AT   30 w_edidc-docrel,
          AT   34 w_edidc-status,
          AT   36 w_edidc-direct,
          AT   37 w_edidc-outmod,
          AT   38 w_edidc-exprss,
          AT   39 w_edidc-test,
          AT   40 w_edidc-idoctp,
          AT   70 w_edidc-cimtyp,
          AT  100 w_edidc-mestyp,
          AT  130 w_edidc-mescod,
          AT  133 w_edidc-mesfct,
          AT  136 w_edidc-std,
          AT  137 w_edidc-stdvrs,
          AT  143 w_edidc-stdmes,
          AT  149 w_edidc-sndpor,
          AT  159 w_edidc-sndprt,
          AT  161 w_edidc-sndpfc,
          AT  163 w_edidc-sndprn,
          AT  173 w_edidc-sndsad,
          AT  194 w_edidc-sndlad,
          AT  264 w_edidc-rcvpor,
          AT  274 w_edidc-rcvprt,
          AT  276 w_edidc-rcvpfc,
          AT  278 w_edidc-rcvprn,
          AT  288 w_edidc-rcvsad,
          AT  309 w_edidc-rcvlad,
          AT  379 w_edidc-credat+0(4),
          AT  383 w_edidc-credat+4(2),
          AT  385 w_edidc-credat+6(2),
          AT  387 w_edidc-cretim+0(2),
          AT  389 w_edidc-cretim+2(2),
          AT  391 w_edidc-cretim+4(2),
          AT  393 w_edidc-refint,
          AT  407 w_edidc-refgrp,
          AT  421 w_edidc-refmes,
          AT  435 w_edidc-arckey,
          AT  505 w_edidc-serial.

    HIDE w_docnum.

  ELSE.

*  only for file download
    CLEAR w_filecontent-e_xline.

    MOVEc_edidc40           TO w_filecontent-e_xline+000,
          w_edidc-mandt       TO w_filecontent-e_xline+010,
          w_edidc-docnum      TO w_filecontent-e_xline+013,
          w_edidc-docrel      TO w_filecontent-e_xline+029,
          w_edidc-status      TO w_filecontent-e_xline+033,
          w_edidc-direct      TO w_filecontent-e_xline+035,
          w_edidc-outmod      TO w_filecontent-e_xline+036,
          w_edidc-exprss      TO w_filecontent-e_xline+037,
          w_edidc-test        TO w_filecontent-e_xline+038,
          w_edidc-idoctp      TO w_filecontent-e_xline+039,
          w_edidc-cimtyp      TO w_filecontent-e_xline+069,
          w_edidc-mestyp      TO w_filecontent-e_xline+099,
          w_edidc-mescod      TO w_filecontent-e_xline+129,
          w_edidc-mesfct      TO w_filecontent-e_xline+132,
          w_edidc-std         TO w_filecontent-e_xline+135,
          w_edidc-stdvrs      TO w_filecontent-e_xline+136,
          w_edidc-stdmes      TO w_filecontent-e_xline+142,
          w_edidc-sndpor      TO w_filecontent-e_xline+148,
          w_edidc-sndprt      TO w_filecontent-e_xline+158,
          w_edidc-sndpfc      TO w_filecontent-e_xline+160,
          w_edidc-sndprn      TO w_filecontent-e_xline+162,
          w_edidc-sndsad      TO w_filecontent-e_xline+172,
          w_edidc-sndlad      TO w_filecontent-e_xline+193,
          w_edidc-rcvpor      TO w_filecontent-e_xline+263,
          w_edidc-rcvprt      TO w_filecontent-e_xline+273,
          w_edidc-rcvpfc      TO w_filecontent-e_xline+275,
          w_edidc-rcvprn      TO w_filecontent-e_xline+277,
          w_edidc-rcvsad      TO w_filecontent-e_xline+287,
          w_edidc-rcvlad      TO w_filecontent-e_xline+308,
          w_edidc-credat+0(4TO w_filecontent-e_xline+378,
          w_edidc-credat+4(2TO w_filecontent-e_xline+382,
          w_edidc-credat+6(2TO w_filecontent-e_xline+384,
          w_edidc-cretim+0(2TO w_filecontent-e_xline+386,
          w_edidc-cretim+2(2TO w_filecontent-e_xline+388,
          w_edidc-cretim+4(2TO w_filecontent-e_xline+390,
          w_edidc-refint      TO w_filecontent-e_xline+392,
          w_edidc-refgrp      TO w_filecontent-e_xline+406,
          w_edidc-refmes      TO w_filecontent-e_xline+420,
          w_edidc-arckey      TO w_filecontent-e_xline+434,
          w_edidc-serial      TO w_filecontent-e_xline+504.

    APPEND w_filecontent          TO ts_filecontent.

  ENDIF"for file download

* loop all IDOC lines in found IDOC and print to screen

  UNASSIGN <fs_edidd>.

  LOOP AT is_edidd
    ASSIGNING <fs_edidd>.

*   get segment definition for download
    IF  sy-subrc EQ 0.

*     default: if not found in table
      MOVE <fs_edidd>-segnam To w_xsegdef.

    ENDIF.

    SELECT segdef version
     UP TO ROWS
      FROM edisdef
      INTO (w_xsegdefw_xversion)
     WHERE segtyp EQ <fs_edidd>-segnam
     ORDER BY version DESCENDING.

*     terminate after first processing
      EXIT.

    ENDSELECT.

    IF  sy-subrc EQ 0.

    ENDIF.

*   determine, if 2nd line is needed from position 950

    clear w_xdot.

    IF NOT <fs_edidd>-sdata+950(50IS INITIAL.

      MOVE c_dots TO w_xdot.

    ENDIF.

*   only for screen display
    IF p_dspscn EQ c_x.

*     print IDOC item information (1st line)

      WRITE/  w_xsegdef,
                31 <fs_edidd>-mandt,
                34 <fs_edidd>-docnum,
                50 <fs_edidd>-segnum,
                56 <fs_edidd>-psgnum,
                62 <fs_edidd>-hlevel.

      WRITE:    64 <fs_edidd>-sdata+0(100),
               164 <fs_edidd>-sdata+100(100),
               264 <fs_edidd>-sdata+200(100),
               364 <fs_edidd>-sdata+300(100),
               464 <fs_edidd>-sdata+400(100),
               564 <fs_edidd>-sdata+500(100),
               664 <fs_edidd>-sdata+600(100),
               764 <fs_edidd>-sdata+700(100),
               864 <fs_edidd>-sdata+800(100),
               964 <fs_edidd>-sdata+900(50),
              1014 w_xdot.

      HIDE w_docnum.

*     print IDOC item information (2nd line) after some dots

      IF NOT w_xdot IS INITIAL.

        WRITE/ space,
            64 <fs_edidd>-sdata+950(50).

        HIDE w_docnum.

      ENDIF.

* only for file download
    ELSE.

      CLEAR w_filecontent-e_xline.

      MOVEw_xsegdef         TO w_filecontent-e_xline+000,
            <fs_edidd>-mandt  TO w_filecontent-e_xline+030,
            <fs_edidd>-docnum TO w_filecontent-e_xline+033,
            <fs_edidd>-segnum TO w_filecontent-e_xline+049,
            <fs_edidd>-psgnum TO w_filecontent-e_xline+055,
            <fs_edidd>-hlevel TO w_filecontent-e_xline+061,
            <fs_edidd>-sdata  TO w_filecontent-e_xline+063.

      APPEND w_filecontent          TO ts_filecontent.

    ENDIF." IF p_dspscn EQ c_x

  ENDLOOP.

ENDFORM.                    "f_display_idoc_plain_ai

*-----------------------------------------------------------------------
*  Display IDoc in tagged format
*-----------------------------------------------------------------------

FORM f_display_idoc_tagged_pi.

  DATA:   w_ccon      TYPE string.

  CLEAR w_ccon.

* XML basis and format information

  MOVE c_xml28 TO w_ccon.

  PERFORM f_display_add_line
    USING w_ccon.

* IDoc type (open-tag)
  IF w_edidc-cimtyp IS INITIAL.

    CONCATENATE c_lesst w_edidc-idoctp c_moret INTO w_ccon.

  ELSE.

    CONCATENATE c_lesst w_edidc-cimtyp c_moret INTO w_ccon.

  ENDIF.

  PERFORM f_display_add_line
    USING w_ccon.

* IDoc envelope (open-tag)
  MOVE c_xml29 TO w_ccon.

  PERFORM f_display_add_line
    USING w_ccon.

* IDoc EDI_DC40 (open-tag / data / close-tag)
  PERFORM f_display_idoc_tagged_pi_head.

* IDoc lines (open-tag / data / close-tag)
  PERFORM f_display_idoc_tagged_pi_line.

* IDoc envelope (close-tag)
  MOVE c_xml33 TO w_ccon.

  PERFORM f_display_add_line
    USING w_ccon.

* IDoc type (close-tag)
  IF w_edidc-cimtyp IS INITIAL.

    CONCATENATE c_lesslash w_edidc-idoctp c_moret INTO w_ccon.

  ELSE.

    CONCATENATE c_lesslash w_edidc-cimtyp c_moret INTO w_ccon.

  ENDIF.

  PERFORM f_display_add_line
    USING w_ccon.

ENDFORM.                    "f_display_idoc_tagged_pi

*-----------------------------------------------------------------------
*  Print IDOC head: EDI_DC40 (open-tag / data / close-tag)
*-----------------------------------------------------------------------

FORM f_display_idoc_tagged_pi_head.

  DATA:   w_ccon      TYPE string,
          w_ctemp     TYPE char200.

  CLEAR w_ctemp.

  MOVEc_xml34 TO  w_ccon.

  SHIFT w_ccon RIGHT BY PLACES.

  PERFORM f_display_add_line
    USING w_ccon.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_tabnam c_edidc40.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_mandt w_edidc-mandt.

  MOVE w_edidc-docnum TO w_ctemp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_docnum w_ctemp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_docrel w_edidc-docrel.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_status w_edidc-status.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_direct w_edidc-direct.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_outmod w_edidc-outmod.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_exprss w_edidc-exprss.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_test   w_edidc-test.

* xml tag name is 'IDOCTYP', but table field name is 'IDOCTP'
  PERFORM f_display_idoc_tagged_pi_print
    USING c_idoctyp w_edidc-idoctp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_cimtyp w_edidc-cimtyp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_mestyp w_edidc-mestyp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_mescod w_edidc-mescod.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_mesfct w_edidc-mesfct.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_std    w_edidc-std.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_stdvrs w_edidc-stdvrs.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_stdmes w_edidc-stdmes.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndpor w_edidc-sndpor.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndprt w_edidc-sndprt.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndpfc w_edidc-sndpfc.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndprn w_edidc-sndprn.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndsad w_edidc-sndsad.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_sndlad w_edidc-sndlad.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvpor w_edidc-rcvpor.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvprt w_edidc-rcvprt.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvpfc w_edidc-rcvpfc.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvprn w_edidc-rcvprn.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvsad w_edidc-rcvsad.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_rcvlad w_edidc-rcvlad.

  MOVE w_edidc-credat TO w_ctemp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_credat w_ctemp.

  MOVE w_edidc-cretim TO w_ctemp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_cretim w_ctemp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_refint w_edidc-refint.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_refgrp w_edidc-refgrp.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_refmes w_edidc-refmes.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_arckey w_edidc-arckey.

  PERFORM f_display_idoc_tagged_pi_print
    USING c_serial w_edidc-serial.

  MOVE c_xml35 TO w_ccon.

  SHIFT w_ccon RIGHT BY PLACES.

  PERFORM f_display_add_line
    USING w_ccon.

ENDFORM.                    "f_display_idoc_tagged_pi_HEAD

*-----------------------------------------------------------------------
*  Print IDOC lines: segments (open-tag / data / close-tag)
*-----------------------------------------------------------------------

FORM f_display_idoc_tagged_pi_line.

  TYPESBEGIN OF y_xline,
           zhlevel   TYPE edi_hlevel,
           zsegclose TYPE char200,
         END OF y_xline.

  DATAw_ccon     TYPE string,
        w_levelcorrect(1TYPE c,
        w_xhlevel  TYPE edi_hlevel,
        e_xline    TYPE y_xline.

  CLEARw_ccon,
         w_levelcorrect,
         w_xhlevel.

  FREE:  e_xline.

  DATAts_xtable  TYPE SORTED TABLE OF y_xline WITH UNIQUE KEY zhlevel.

* initializiation of level

  MOVE TO w_xhlevel.

  UNASSIGN <fs_edidd>.

  LOOP AT is_edidd
    ASSIGNING <fs_edidd>.

    IF  <fs_edidd>-PSGNUM EQ 0
    AND <fs_edidd>-hlevel GT 1.

      move c_x TO w_levelcorrect.

    ENDIF.

    IF  w_levelcorrect    EQ c_x
    AND <fs_edidd>-hlevel GT 1.

      <fs_edidd>-hlevel <fs_edidd>-hlevel ).

    ENDIF.

*   handling of old close tags (for next line)

    WHILE w_xhlevel GE <fs_edidd>-hlevel.

      READ TABLE ts_xtable
        WITH KEY zhlevel w_xhlevel
        INTO e_xline.

      IF sy-subrc EQ 0.

        MOVE e_xline-zsegclose TO w_ccon.

        PERFORM f_display_add_line
          USING w_ccon.

        DELETE ts_xtable
          WHERE zhlevel EQ e_xline-zhlevel.

      ENDIF.

      w_xhlevel w_xhlevel 1.

    ENDWHILE.

    MOVE <fs_edidd>-hlevel TO w_xhlevel.

    PERFORM f_display_idoc_tagged_pi_segm
      USING    <fs_edidd>
      CHANGING e_xline-zsegclose.

*   add hierachy information to internal table

    MOVE <fs_edidd>-hlevel TO e_xline-zhlevel.

*    LINE-zSEGCLOSE defined via PERFORM above...
    APPEND e_xline TO ts_xtable.

  ENDLOOP.

* handling of old close tags (for last)

  WHILE w_xhlevel GE 1.

    CLEAR e_xline.

    READ TABLE ts_xtable
      WITH KEY zhlevel w_xhlevel
      INTO e_xline.

    IF sy-subrc EQ 0.

      MOVE e_xline-zsegclose TO w_ccon.

      PERFORM f_display_add_line
        USING w_ccon.

      DELETE  ts_xtable
        WHERE zhlevel EQ e_xline-zhlevel.

    ENDIF.

    w_xhlevel w_xhlevel 1.

  ENDWHILE.

  CLEAR w_xhlevel.

  FREE ts_xtable.

ENDFORM.                    "f_display_idoc_tagged_pi_LINE

*-----------------------------------------------------------------------
*  Print single IDoc segment (EDIDD) - without close tag
*-----------------------------------------------------------------------

FORM f_display_idoc_tagged_pi_segm
     USING    us_xedidd      TYPE edidd
     CHANGING ch_xsegclose   TYPE char200.

  DATAw_xversion  TYPE segmentvrs,
        w_nspaces   TYPE i,
        w_ccon      TYPE string,
        w_ctemp     TYPE char200,
        w_cvalue    TYPE char200,
        w_offset    TYPE i.

  DATAis_edisegstru TYPE STANDARD TABLE OF edisegstru.

  FIELD-SYMBOLS<fs_edisegstru> TYPE edisegstru.

  CLEARw_xversion,
         w_ctemp.

* change segment name via hardcoding...

  w_nspaces + <fs_edidd>-hlevel 3.

  CONCATENATE c_lesst <fs_edidd>-segnam c_xml39 INTO w_ccon.

  SHIFT w_ccon RIGHT BY w_nspaces PLACES.

  PERFORM f_display_add_line
    USING w_ccon.

* close segment tag (for changing)...

  CONCATENATE c_lesslash <fs_edidd>-segnam c_moret INTO ch_xsegclose.

  SHIFT ch_xsegclose RIGHT BY w_nspaces PLACES.

* read segment information...

  FREE is_edisegstru.

  CALL FUNCTION 'SEGMENT_READ'
    EXPORTING
      segmenttyp                 <fs_edidd>-segnam
*   IMPORTING
*     RESULT                     =
*     DEVCLASS                   =
*     SEGMENTHEADER              =
   TABLES
*     SEGMENTDEFINITION          =
      segmentstructure           is_edisegstru
   EXCEPTIONS
      no_authority               1
      segment_not_existing       2
      OTHERS                     3.

  IF sy-subrc EQ 2.

    MESSAGE e000(0hWITH text-e02 <fs_edidd>-segnam text-e03.

  ELSEIF sy-subrc NE 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

* print fields of current segment...

  MOVE TO w_offset.

  UNASSIGN <fs_edisegstru>.

  LOOP AT is_edisegstru
    ASSIGNING <fs_edisegstru>.

    w_cvalue us_xedidd-sdata+w_offset(<fs_edisegstru>-expleng).

    IF NOT w_cvalue IS INITIAL.

      PERFORM f_display_idoc_tagged_pi_print
        USING <fs_edidd>-hlevel <fs_edisegstru>-fieldname w_cvalue.

    ENDIF.

    w_offset w_offset + <fs_edisegstru>-expleng.

  ENDLOOP.

ENDFORM.                    "f_display_idoc_tagged_pi_segm

*-----------------------------------------------------------------------
*  Determine value out of specific IDoc segment
*-----------------------------------------------------------------------

FORM f_get_idoc_field_value
     USING    us_xedidd      TYPE edidd
              us_xfield      TYPE fieldname
     CHANGING ch_xvalue      TYPE char200.

  DATAw_nspaces   TYPE i,
        w_cvalue    TYPE char200,
        w_xsegdef   TYPE edisegdef,
        w_offset    TYPE i.

  DATAis_edisegstru TYPE STANDARD TABLE OF edisegstru.

  FIELD-SYMBOLS<fs_edisegstru> TYPE edisegstru.

  CLEARw_nspaces,
         w_cvalue,
         w_xsegdef,
         w_offset.

* clear return value

  CLEARch_xvalue.

* if not fieldname is given: return

  IF us_xfield IS INITIAL.

    EXIT.

  ENDIF.

* read segment information...

  CLEAR w_offset.

  CALL FUNCTION 'SEGMENT_READ'
    EXPORTING
      segmenttyp                 <fs_edidd>-segnam
*   IMPORTING
*     RESULT                     =
*     DEVCLASS                   =
*     SEGMENTHEADER              =
   TABLES
*     SEGMENTDEFINITION          =
      segmentstructure           is_edisegstru
   EXCEPTIONS
      no_authority               1
      segment_not_existing       2
      OTHERS                     3.

  IF sy-subrc NE 0.

    EXIT.

  ENDIF.

* determine field value...

  MOVE TO w_offset.

  UNASSIGN <fs_edisegstru>.

  LOOP AT is_edisegstru
    ASSIGNING <fs_edisegstru>.

    IF <fs_edisegstru>-fieldname  EQ us_xfield.

      ch_xvalue us_xedidd-sdata+w_offset(<fs_edisegstru>-expleng).

      IF ch_xvalue IS INITIAL.

        EXIT.

      ENDIF.

      SHIFT ch_xvalue LEFT DELETING LEADING c_0.

      IF ch_xvalue IS INITIAL.

        MOVE c_0 TO ch_xvalue.

      ENDIF.

      EXIT.

    ENDIF.

    w_offset w_offset + <fs_edisegstru>-expleng.

  ENDLOOP.


ENDFORM.                    "F_GET_IDOC_FIELD_VALUE

*-----------------------------------------------------------------------
*  Print specific XML field value
*-----------------------------------------------------------------------

FORM f_display_idoc_tagged_pi_print
     USING us_nlevel      TYPE edi_hlevel
           us_cfield      TYPE edisegdef
           us_cdata       TYPE c.

  DATA:   w_ccon      TYPE string,
          w_temp      TYPE string,
          w_nspaces   TYPE i.


  IF NOT us_cdata IS INITIAL.

    CLEARw_temp.

    move us_cdata to w_temp.

*   no need to convert local characters anymore,
*   as download via UTF-8 coverts it (= SAP-PI approach)

    replace all occurrences of c_a   in w_temp with c_amp.

    replace all occurrences of c_gt  in w_temp with c_htmgt.

    replace all occurrences of c_lt  in w_temp with c_htmlt.

    CONCATENATE c_lesst us_cfield c_moret w_temp c_lesslash us_cfield c_moret INTO w_ccon.

    w_nspaces + us_nlevel 3.

    SHIFT w_ccon RIGHT BY w_nspaces PLACES.

    PERFORM f_display_add_line
      USING w_ccon.

  ENDIF.

ENDFORM.                    "f_display_idoc_tagged_pi_PRINT

*-----------------------------------------------------------------------
*  Return only characters from 0..9
*-----------------------------------------------------------------------

FORM f_fnchfx_only09 CHANGING ch_ctext TYPE char200.

  DATAw_creturn  TYPE char200,
        w_nlen     TYPE i,
        w_nlaufer  TYPE i.

  CLEAR w_creturn.

  MOVE strlench_ctext TO  w_nlen.

  MOVE TO w_nlaufer.

  WHILE w_nlaufer <= w_nlen.

    IF  c_0 LE ch_ctext+w_nlaufer(1)
    AND ch_ctext+w_nlaufer(1LE c_9 ).

      CONCATENATE w_creturn ch_ctext+w_nlaufer(1INTO w_creturn.

    ENDIF.

    w_nlaufer w_nlaufer + 1.

  ENDWHILE.

  MOVE w_creturn TO ch_ctext.

ENDFORM.                    "F_FNCHFX_ONLY09

*-----------------------------------------------------------------------
*  Return only characters from 0..9, a..z, A..Z
*-----------------------------------------------------------------------

FORM f_fnchfx_only09az CHANGING ch_ctext TYPE char200.

  DATAw_creturn  TYPE char200,
        w_nlen     TYPE i,
        w_nlaufer  TYPE i.

  CLEARw_creturn,
         w_nlen,
         w_nlaufer.

  MOVE strlench_ctext TO w_nlen.

  MOVE TO w_nlaufer.

  WHILE w_nlaufer <= w_nlen.

    IF  c_0 LE ch_ctext+w_nlaufer(1)
    AND ch_ctext+w_nlaufer(1LE c_9 )
    OR  c_lowa LE ch_ctext+w_nlaufer(1)
    AND ch_ctext+w_nlaufer(1LE c_lowz )
    OR  c_capA LE ch_ctext+w_nlaufer(1)
    AND ch_ctext+w_nlaufer(1LE c_capZ ).

      CONCATENATE w_creturn ch_ctext+w_nlaufer(1INTO w_creturn.

    ENDIF.

    w_nlaufer w_nlaufer + 1.

  ENDWHILE.

  MOVE w_creturn TO ch_ctext.

ENDFORM.                    "F_FNCHFX_ONLY09AZ

*-----------------------------------------------------------------------
*  File download to local SAP GUI client
*-----------------------------------------------------------------------

FORM f_download_to_pcgui
  USING us_cfile      TYPE localfile.

  DATAw_cfilelength TYPE i,
        w_cmessage    TYPE char50,
        w_cfullname   TYPE string.

  CLEARw_cfilelength,
         w_cmessage,
         w_cfullname,
         w_text .

* create full file name (without suffix)

  CONCATENATE p_file us_cfile INTO w_cfullname.

  IF p_idocpl EQ c_x.

    CONCATENATE w_cfullname c_txtext INTO w_cfullname.

  ELSE.

    CONCATENATE w_cfullname c_xmltxt INTO w_cfullname.

  ENDIF.

* save file (function may be not used in an open select statement)

  CALL FUNCTION 'GUI_DOWNLOAD'
       EXPORTING
*           BIN_FILESIZE        = ' '
            CODEPAGE            c_4310
            filename            w_cfullname
            filetype            c_asc
*           MODE                = ' '
*           WK1_N_FORMAT        = ' '
*           WK1_N_SIZE          = ' '
*           WK1_T_FORMAT        = ' '
*           WK1_T_SIZE          = ' '
*           COL_SELECT          = ' '
*           COL_SELECTMASK      = ' '
       IMPORTING
            filelength          w_cfilelength
       TABLES
            data_tab            ts_filecontent
*           FIELDNAMES          =
       EXCEPTIONS
            file_open_error     1
            file_write_error    2
            invalid_filesize    3
            invalid_table_width 4
            invalid_type        5
            no_batch            6
            unknown_error       7
            OTHERS              8.

  IF sy-subrc NE 0.

    CASE sy-subrc.

      WHEN 01.

* 'File Open Error
        MOVE text-e04 TO w_cmessage.

      WHEN 02.
* 'File Write Error'.
        MOVE text-e05 TO w_cmessage.

      WHEN 03.
*'Invalid Filesize'.
        MOVE text-e06 TO w_cmessage.

      WHEN 04.
*'Invalid Table Width'.
        MOVE text-e07 TO w_cmessage.

      WHEN 05.
*'Invalid Type'.
        MOVE text-e08 TO w_cmessage.

      WHEN 06.
* 'No Batch'.
        MOVE text-e09 TO w_cmessage.

      WHEN 07.
*'Unknown Error'.
        MOVE text-e10 TO w_cmessage.

      WHEN 08.
*'Others Error'.
        MOVE text-e11 TO w_cmessage.

      WHEN others.

    ENDCASE"validation after file download

    MESSAGE i000(0hWITH w_cmessage.

  ENDIF" file downloaded

  MOVE c_text TO w_text.

  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      percentage sy-index
      text       w_text.

  IF sy-subrc EQ 0.

  ENDIF.

ENDFORM.                    "F_DOWNLOAD_TO_PCGUI

*-----------------------------------------------------------------------
*  add line to export table
*-----------------------------------------------------------------------

FORM f_display_add_line
  USING us_ccon TYPE string.

* only for screen display
  if p_dspscn eq c_x.

    write/ us_ccon.

    hide w_docnum.

* only for file download
  else.

    clear w_filecontent-e_xline.

    move us_ccon to w_filecontent-e_xline.

    append w_filecontent to ts_filecontent.

  endif.

ENDFORM"f_display_add_line.

*-----------------------------------------------------------------------
* end of report
*-----------------------------------------------------------------------




2. How to Upload Text File to UNIX server.


   - GUI_UPLOAD
   - GUI_DOWNLOAD

TCODE :

CZ3Y - DOWNLOAD FILE FROM UNIX
CZ3Y - UPLOAD FILE TO UNIX

AL11 - TO OUR FILE IN UNIX

First use FM :  GUI_UPLOAD for uploading into internal table
after that use Open dataset Write for storing in the application server.

after
Open data set Read for download to internal table
after that use GUI_DOWNLOAD.


REPORT zspc2unx MESSAGE-ID ys.
*eject
*****************************  Internal Tables  ************************

DATABEGIN OF buffertab OCCURS 0,
*{ Begin of BM4263-080423
*        PLINE(2056)      TYPE   C,
*{Begin of BO0660-090528
*         PLINE(65535)     TYPE   C,          "BM1318-080409
        pline     TYPE   string,
*} End of BO0660-090528
*} End of BM4263-080423
         END   OF buffertab.

*eject
*************   Constants        C_   **********************************
CONSTANTSc_sid(5)        TYPE VALUE '<SID>',
           c_vogsssd(19)   TYPE VALUE '/var/opt/gsss/data/',
           c_optsapapp(12TYPE VALUE '/opt/sapapp/',
           c_osaglob(19)   TYPE VALUE '/opt/sapapp/Global/',
           c_tmp(5)        TYPE VALUE '/tmp/',
           c_x(1)          TYPE VALUE 'X',
           c_blank(1)      TYPE VALUE ' ',
           c_conv_err(16)  TYPE VALUE 'Conversion Error',
           c_foerr(15)     TYPE VALUE 'File Open Error',
           c_fr_err(15)    TYPE VALUE 'File Read Error',
           c_fw_err(16)    TYPE VALUE 'File Write Error',
           c_inv_tabw(19)  TYPE VALUE 'Invalid Table Width',
           c_inv_type(12)  TYPE VALUE 'Invalid Type',
           c_inv_fs(16)    TYPE VALUE 'Invalid Filesize',
           c_no_batch(8)   TYPE VALUE 'No Batch',
           c_unk_err(13)   TYPE VALUE 'Unknown Error',
           c_oth_help(25)  TYPE VALUE 'Others Error ... Big Help'.


*eject
***************  Work variables  ***************************************
DATArc                LIKE sy-subrc.
DATAlen               TYPE i.
*DATA: FILELENGTH(10)   TYPE C.                        "SOM240506
DATAfilelength        TYPE i.                             "SOM240506
DATAmsg(80)           TYPE c.
DATAd_uxfile(70)      TYPE c,
      d_uxfile1(70)     TYPE c,
      d_uxfilea(70)     TYPE c,
      d_uxfileb(70)     TYPE c.
DATAsysid(3)          TYPE c.
SKIP 2.
* Begin of change by SOM230506
DATAw_wsfile          TYPE string,
      w_f_type(10)      TYPE c.
* End of change by SOM230506
*EJECT
************************************************************************
*                    Parameters & Select-Options                       *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK param1 WITH FRAME TITLE text-h01.
PARAMETER choosw1 DEFAULT 'X' RADIOBUTTON GROUP rad.
PARAMETER choosw2             RADIOBUTTON GROUP rad.
SELECTION-SCREEN END OF BLOCK param1.

SELECTION-SCREEN BEGIN OF BLOCK param2 WITH FRAME TITLE text-h02.
PARAMETERSp_wsfile LIKE rlgrap-filename DEFAULT
                             'd:',
*           unixfile(70) default '/opt/sapapp/Global/' "SK020999
            p_uxfile(70DEFAULT '/opt/sapapp/<SID>/'       "SH280601
                             LOWER CASE,
            p_f_type LIKE rlgrap-filetype DEFAULT 'ASC'.
SELECTION-SCREEN END OF BLOCK param2.

SELECTION-SCREEN BEGIN OF BLOCK param3 WITH FRAME TITLE text-h03.
PARAMETER choosw3 DEFAULT 'X' RADIOBUTTON GROUP qad.
PARAMETER choosw4             RADIOBUTTON GROUP qad.
SELECTION-SCREEN END OF BLOCK param3.

*EJECT
************************************************************************
*                    Initialization                                    *
************************************************************************
INITIALIZATION.                                             "SH280601
  REPLACE c_sid WITH sy-sysid INTO p_uxfile.                "SH280601
  CONDENSE p_uxfile NO-GAPS.                                "SH280601

*eject
*----------------------------------------------------------------------*
*                       START OF SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.                                         "SH280601
  MOVEp_wsfile TO w_wsfile,                               "SOM240506
        p_f_type TO w_f_type.                               "SOM240506
*  AUTHORITY-CHECK OBJECT 'S_DEVELOP'
*           ID 'DEVCLASS' FIELD 'A'
*           ID 'OBJTYPE' FIELD 'TABL'
*           ID 'OBJNAME' DUMMY
*           ID 'P_GROUP' DUMMY
*           ID 'ACTVT' FIELD '02'.
*  If you can change SAP Object, You can upload file.
*  Temporary Check.  Normal Users should be accessing EDI only.
*  IF SY-SUBRC NE 0.
*    SYSID = SY-SYSID. TRANSLATE SYSID TO LOWER CASE.        "SH210102
*- create file string with SID ----------------------------- "SH280601
*    CONCATENATE C_VOGSSSD SY-SYSID '/'                      "SH280601
*                                      INTO D_UXFILE.        "SH280601
*    CONCATENATE C_VOGSSSD SYSID '/'                         "SH210102
*                                      INTO D_UXFILEA.       "SH210102
*    CONCATENATE C_OPTSAPAPP SY-SYSID '/'                    "SH280601
*                                      INTO D_UXFILE1.       "SH280601
*    CONCATENATE C_OPTSAPAPP SYSID '/'                       "SH210102
*                                      INTO D_UXFILEB.       "SH210102
*------------------------------------------------------- SH280601
*    IF P_UXFILE(19) NE C_OSAGLOB             AND            "SK020999
*       P_UXFILE(23) NE D_UXFILE              AND            "SH280601
*       P_UXFILE(23) NE D_UXFILEA             AND            "SH210102
*       P_UXFILE(16) NE D_UXFILE1             AND            "SH280601
*       P_UXFILE(16) NE D_UXFILEB             AND            "SH210102
*       P_UXFILE(5)  NE C_TMP.                               "SK020999
*     message i000 with 'Only for EDI or /tmp/ Directory at present.'. "
*      MESSAGE I000 WITH
*         'The directory you are trying to access is restricted!'(001). "
*      EXIT.
*    ENDIF.
*  ENDIF.
  IF choosw1 EQ c_x.
    PERFORM 0400_getfrompc.
    PERFORM 0500_tounix.
  ELSE.
    PERFORM 0200_getfromunix.
    PERFORM 0300_topc.
  ENDIF.

*eject
*&---------------------------------------------------------------------*
*&      Form  0100_UPLOADERROR
*&---------------------------------------------------------------------*
*       check for upload errors
*----------------------------------------------------------------------*
*FORM 0100_UPLOADERROR USING RC.                     "SOM240506
FORM 0100_uploaderror USING rc LIKE sy-subrc.               "SOM240506
  DATAbuf(60TYPE c.
  CASE rc.
    WHEN 01buf c_conv_err.
    WHEN 02buf c_foerr.
    WHEN 03buf c_fr_err.
    WHEN 04buf c_inv_tabw.
    WHEN 05buf c_inv_type.
    WHEN 06buf c_no_batch.
    WHEN 07buf c_unk_err.
    WHEN 08buf c_oth_help.
  ENDCASE.
  MESSAGE i000 WITH buf.
ENDFORM.                               " 0100_UPLOADERROR

*eject
*&---------------------------------------------------------------------*
*&      Form  0200_GETFROMUNIX
*&---------------------------------------------------------------------*
*       get the file from Unix
*----------------------------------------------------------------------*
FORM 0200_getfromunix.

  IF choosw3 EQ 'X'.
    OPEN DATASET p_uxfile FOR INPUT IN TEXT MODE ENCODING UTF-MESSAGE
    msg.
  ELSE.
    OPEN DATASET p_uxfile FOR INPUT IN TEXT MODE ENCODING NON-UNICODE MESSAGE
    msg.
  ENDIF.



  IF sy-subrc NE 0.
    WRITE/ msg.
    MESSAGE i000 WITH msg.
    STOP.
  ELSE.
*{ Begin of BO0660-090528
*    DO.
*      READ DATASET P_UXFILE INTO BUFFERTAB-PLINE.
*      IF SY-SUBRC NE 0.
*        EXIT.
*      ENDIF.
*      LEN  = STRLEN( BUFFERTAB ).
*      WRITE: AT /(LEN) BUFFERTAB.
*      APPEND BUFFERTAB.
*    ENDDO.

    DO.

      READ DATASET p_uxfile INTO buffertab-pline.

      IF sy-subrc NE 0.

        EXIT.

      ENDIF.

      len STRLENbuffertab-pline ).

      WRITEAT /(lenbuffertab-pline.

      APPEND buffertab.

    ENDDO.

    CLOSE DATASET p_uxfile.
*} End of BO0660-090528


  ENDIF.
ENDFORM.                    " 0200_GETFROMUNIX

*eject
*&---------------------------------------------------------------------*
*&      Form  0300_TOPC
*&---------------------------------------------------------------------*
*       download the file to the PC
*----------------------------------------------------------------------*
FORM 0300_topc.
* Begin of change by SOM240506
*CALL FUNCTION 'WS_DOWNLOAD'
*     EXPORTING
**         BIN_FILESIZE        = c_blank
**         CODEPAGE            = c_blank
*          FILENAME            = P_WSFILE
*          FILETYPE            = P_F_TYPE
**         MODE                = c_blank
**         WK1_N_FORMAT        = c_blank
**         WK1_N_SIZE          = c_blank
**         WK1_T_FORMAT        = c_blank
**         WK1_T_SIZE          = c_blank
**         COL_SELECT          = c_blank
**         COL_SELECTMASK      = c_blank
*     IMPORTING
*          FILELENGTH          = FILELENGTH
*     TABLES
*          DATA_TAB            = BUFFERTAB
**         FIELDNAMES          =
*     EXCEPTIONS
*          FILE_OPEN_ERROR     = 1
*          FILE_WRITE_ERROR    = 2
*          INVALID_FILESIZE    = 3
*          INVALID_TABLE_WIDTH = 4
*          INVALID_TYPE        = 5
*          NO_BATCH            = 6
*          UNKNOWN_ERROR       = 7
*          OTHERS              = 8.


  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                w_wsfile
      filetype                w_f_type
    IMPORTING
      filelength              filelength
    TABLES
      data_tab                buffertab
    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.

* End of change by SOM240506
  IF sy-subrc NE 0.
    rc sy-subrc.
    PERFORM 0600_downloaderror USING rc.
    IF rc EQ 0EXITENDIF.
  ELSE.
    MESSAGE i000 WITH 'Download Complete'.
  ENDIF.
ENDFORM.                                                    " 0300_TOPC

*eject
*&---------------------------------------------------------------------*
*&      Form  0400_GETFROMPC
*&---------------------------------------------------------------------*
*       get the file from the PC
*----------------------------------------------------------------------*
FORM 0400_getfrompc.
* Begin of change by SOM240506
*  CALL FUNCTION 'WS_UPLOAD'
*       EXPORTING
**         CODEPAGE            = c_blank
*            FILENAME            = P_WSFILE
*            FILETYPE            = P_F_TYPE
**         HEADLEN             = c_blank
**         LINE_EXIT           = c_blank
**         TRUNCLEN            = c_blank
**         USER_FORM           = c_blank
**         USER_PROG           = c_blank
*       IMPORTING
*            FILELENGTH          = FILELENGTH
*       TABLES
*            DATA_TAB            = BUFFERTAB
*       EXCEPTIONS
*            CONVERSION_ERROR    = 1
*            FILE_OPEN_ERROR     = 2
*            FILE_READ_ERROR     = 3
*            INVALID_TABLE_WIDTH = 4
*            INVALID_TYPE        = 5
*            NO_BATCH            = 6
*            UNKNOWN_ERROR       = 7
*            OTHERS              = 8.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                w_wsfile
      filetype                w_f_type
    IMPORTING
      filelength              filelength
    TABLES
      data_tab                buffertab
    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.

* End of change by SOM240506
  IF sy-subrc NE 0.
    rc sy-subrc.
    PERFORM 0100_uploaderror USING rc.
    IF rc EQ 0EXITENDIF.
  ENDIF.
ENDFORM.                    " 0400_GETFROMPC

*eject
*&---------------------------------------------------------------------*
*&      Form  0500_TOUNIX2
*&---------------------------------------------------------------------*
*       send the file to Unix
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM 0500_tounix.

*{GWC00000081
  CONSTANTSl_c_seg1(7)  TYPE VALUE 'E1EDKT1',
             l_c_seg2(7)  TYPE VALUE 'E1EDKT2',
             l_c_id(5)    TYPE VALUE 'Z031E',
             l_c_tag(1)   TYPE VALUE '/'.
  DATA:      l_cont       TYPE i.
*}GWC00000081

*{ Begin of BO0660-090528
*  LOOP AT BUFFERTAB.
*    LEN  = STRLEN( BUFFERTAB ).
*    WRITE: AT /(LEN) BUFFERTAB.
*  ENDLOOP.

  LOOP AT buffertab.

    len  STRLENbuffertab-pline ).

    WRITEAT /(lenbuffertab-pline.

  ENDLOOP.
*} End of BO0660-090528

  OPEN DATASET p_uxfile FOR OUTPUT IN TEXT MODE ENCODING DEFAULT MESSAGE
  msg.
  IF sy-subrc NE 0.
    WRITE/ msg.
    MESSAGE i000 WITH msg.
    STOP.
  ELSE.

*{ Begin of BO0660-090528
*    LOOP AT BUFFERTAB.
**{    GWC00000081
*      IF BUFFERTAB(7)    = L_C_SEG2 AND   "E1EDKT2
*         L_CONT          > 0.
*         BUFFERTAB+125(1) = L_C_TAG.      "/
*         ADD 1 TO L_CONT.
*         IF L_CONT > 4.
*           CLEAR L_CONT.
*         ENDIF.
*      ELSE.
*        CLEAR L_CONT.
*      ENDIF.
*
*      IF BUFFERTAB(7)    = L_C_SEG1 AND   "E1EDKT1
*         BUFFERTAB+55(5) = L_C_ID.        "Z031E
*         ADD 1 TO L_CONT.
*      ENDIF.
**}    GWC00000081
*      LEN  = STRLEN( BUFFERTAB ).

    DATAw_buffer1  TYPE string,
          w_buffer2  TYPE string.

    LOOP AT buffertab.
*{    GWC00000081

*{ Begin of BQ3669-090529
      CLEAR len.

      len  STRLENbuffertab-pline ).

      IF len GT 125.
*} End of BQ3669-090529

        w_buffer1 buffertab-pline(7).

        w_buffer2 buffertab-pline+125(1).

        IF  w_buffer1 EQ l_c_seg2
        AND l_cont GT  0.                                   "E1EDKT2


          MOVE l_c_tag TO w_buffer2.      "/

          ADD TO l_cont.

          IF l_cont > 4.

            CLEAR l_cont.

          ENDIF.

        ELSE.

          CLEAR l_cont.

        ENDIF.

*{ Begin of BQ3669-090529
      ENDIF.
*} End of BQ3669-090529

      IF  buffertab-pline(7)    EQ l_c_seg1                 "E1EDKT1

      AND buffertab-pline+55(5EQ l_c_id.                  "Z031E

        ADD TO l_cont.

      ENDIF.
*}    GWC00000081
      len  STRLENbuffertab-pline ).

      CLEARw_buffer1,
             w_buffer2,
             len.

*} End of BO0660-090528

      TRANSFER buffertab-pline TO p_uxfile.
      IF sy-subrc NE 0.
        MESSAGE i000 WITH 'Transfer Problem'.
        STOP.
      ENDIF.
    ENDLOOP.
  ENDIF.
  CLOSE DATASET p_wsfile.
  MESSAGE i000 WITH 'Upload Complete'.
ENDFORM.                    " 0500_TOUNIX2

*eject
*&---------------------------------------------------------------------*
*&      Form  0600_DOWNLOADERROR
*&---------------------------------------------------------------------*
*       check for download errors
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*FORM 0600_DOWNLOADERROR USING RC.                          "SOM240506
FORM 0600_downloaderror USING rc LIKE sy-subrc.             "SOM240506
  DATAbuf(60TYPE c.
  CASE rc.
    WHEN 01buf c_foerr.
    WHEN 02buf c_fw_err.
    WHEN 03buf c_fw_err.
    WHEN 04buf c_inv_tabw.
    WHEN 05buf c_inv_type.
    WHEN 06buf c_no_batch.
    WHEN 07buf c_unk_err.
    WHEN 08buf c_oth_help.
  ENDCASE.
  MESSAGE i000 WITH buf.
ENDFORM.                    " 0600_DOWNLOADERROR

3. Process Idoc via the uploaded idoc text file via WE16

 - Trigger Inbound Processing.