Generate IDOCs from a report

*&---------------------------------------------------------------------*
*& Report  ZHR_PERMITTED_ACT_TO_SCANNERS                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*
* This report reads permitted activities from table ZHR_AKTIVITITER,
* generates idocs of basis type ZHRACT01 and message type ZHRACT01
* and sends them to XI.
*
*
* Functionality/program flow:
* ============================
* - Read data from tables ZHR_AKTIVITITER and PRPS
* - Create IDoc control record
* - Create IDoc segments
* - Distribute IDoc using function module MASTER_IDOC_DISTRIBUTE
* - Show report
*
*---------------------------------------------------------------------

REPORT zhr_permitted_act_to_scanners.

TABLES: zhr_aktivititer.

TYPES:
  BEGIN OF t_activities,
    orgeh    LIKE zhr_aktivititer-orgeh,
    actid    LIKE zhr_aktivititer-actid,
    descr    LIKE zhr_aktivititer-descr,
    posid    LIKE prps-posid,
    post1    LIKE prps-post1,
  END OF t_activities.

DATA:
  gt_activities TYPE STANDARD TABLE OF t_activities,
  wa_activities TYPE t_activities,
* Number of idoc created
  g_num_of_idocs TYPE i,
* IDOC control record
  g_idoc_control LIKE edidc,
* Return data from MASTER_IDOC_DISTRIBUTE
  gt_comm_idocs TYPE STANDARD TABLE OF edidc,
  wa_comm_idocs TYPE edidc,
* IDOC data record
  gt_idoc_data TYPE STANDARD TABLE OF edidd,
  wa_idoc_data TYPE edidd,
* Structure for idoc segment Z!ACTIV
  g_z1activ LIKE z1activ.

*------------------------------------------------------------------------------
* SELECTION SCREEN
*------------------------------------------------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK SELECTION WITH FRAME TITLE text-001.

* Data selection
SELECT-OPTIONS:
  s_orgeh FOR zhr_aktivititer-orgeh.
PARAMETERS:
  p_date LIKE sy-datum DEFAULT sy-datum OBLIGATORY.
SELECTION-SCREEN END OF BLOCK SELECTION.

* Communication parameters
SELECTION-SCREEN BEGIN OF BLOCK COMMUNICATION WITH FRAME TITLE text-002.
PARAMETERS:
  p_idoctp LIKE edidc-idoctp OBLIGATORY DEFAULT 'ZHRACT01',   "Idoc type
  p_mestyp LIKE edidc-mestyp OBLIGATORY DEFAULT 'ZHRACT01',   "Message type
  p_rcvpor LIKE edidc-rcvpor OBLIGATORY DEFAULT 'SAPUXI',     "Receiver port
  p_rcvprt LIKE edidc-rcvprt OBLIGATORY DEFAULT 'LS',         "Receiver partner type
  p_rcvprn LIKE edidc-rcvprn OBLIGATORY DEFAULT 'UXICLNT100'.  "Receiver partner
SELECTION-SCREEN END OF BLOCK COMMUNICATION.

* Output
SELECTION-SCREEN BEGIN OF BLOCK OUTPUT WITH FRAME TITLE text-010.
PARAMETERS:
  p_idoc   AS CHECKBOX,                                       "Create idoc
  p_report AS CHECKBOX.                                       "Show report
SELECTION-SCREEN END OF BLOCK OUTPUT.

*------------------------------------------------------------------------------
* SELECTION SCREEN OUTPUT
*------------------------------------------------------------------------------
AT SELECTION-SCREEN OUTPUT.
* Protect parameters for idoc type and message type
  LOOP AT SCREEN.
    IF screen-name = 'P_IDOCTP' OR screen-name = 'P_MESTYP'.
      screen-input = '0'.
      MODIFY SCREEN.

    ENDIF.
  ENDLOOP.



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

START-OF-SELECTION.
  PERFORM read_data.

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

END-OF-SELECTION.
  PERFORM create_control_record.

  PERFORM create_data_records.

  IF p_idoc = 'X'.
    PERFORM distribute_idoc.
  ENDIF.

  IF p_report = 'X'.
    PERFORM show_report.
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form  read_data
*&---------------------------------------------------------------------*
*  Read activities from ZHR_AKTIVITITER and the corresponding
*  WSB-elements and texts from PRPS
*----------------------------------------------------------------------*
FORM read_data.

  REFRESH gt_activities.

  SELECT zhr_aktivititer~orgeh
         zhr_aktivititer~actid
         zhr_aktivititer~descr
         prps~posid
         prps~post1
    INTO CORRESPONDING FIELDS OF TABLE gt_activities
    FROM zhr_aktivititer LEFT OUTER JOIN prps
    ON prps~pspnr = zhr_aktivititer~pspnr
    WHERE zhr_aktivititer~orgeh IN s_orgeh AND
          zhr_aktivititer~begda <= p_date AND
          zhr_aktivititer~endda >= p_date.

  SORT gt_activities BY orgeh actid.

ENDFORM.                    " read_data
*&---------------------------------------------------------------------*
*&      Form  create_control_record
*&---------------------------------------------------------------------*
*   Create control record for IDOC - The values for the control
*   are taken from the selection screen
*
*----------------------------------------------------------------------*
FORM create_control_record.
  CLEAR g_idoc_control.

  g_idoc_control-mestyp = p_mestyp.
  g_idoc_control-idoctp = p_idoctp.
  g_idoc_control-rcvpor = p_rcvpor.
  g_idoc_control-rcvprt = p_rcvprt.
  g_idoc_control-rcvprn = p_rcvprn.

ENDFORM.                    " create_control_record
*&---------------------------------------------------------------------*
*&      Form  create_data_records
*&---------------------------------------------------------------------*
*   Create segment data records for idoc segment Z1ACTIV
*
*----------------------------------------------------------------------*
FORM create_data_records.
  DATA:
    l_old_orgeh LIKE zhr_aktivititer-orgeh.

  REFRESH gt_idoc_data.

  CLEAR:
     wa_activities,
     l_old_orgeh.

  SORT gt_activities BY orgeh.

  LOOP AT gt_activities INTO wa_activities.
    CLEAR:
      wa_idoc_data,
      g_z1activ.

    wa_idoc_data-segnam = 'Z1ACTIV'.
    g_z1activ-orgeh = wa_activities-orgeh.
    g_z1activ-actid = wa_activities-actid.
    g_z1activ-descr = wa_activities-descr.
    g_z1activ-posid = wa_activities-posid.
    g_z1activ-post1 = wa_activities-post1.

    MOVE g_z1activ TO  wa_idoc_data-sdata.

    APPEND wa_idoc_data TO gt_idoc_data.
  ENDLOOP.


ENDFORM.                    " create_data_records
*&---------------------------------------------------------------------*
*&      Form  distribute_idoc
*&---------------------------------------------------------------------*
*       Create and distribute idoc
*----------------------------------------------------------------------*
FORM distribute_idoc .

  REFRESH gt_comm_idocs.

  CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
    EXPORTING
      master_idoc_control                  = g_idoc_control
*   OBJ_TYPE                             = ''
*   CHNUM                                = ''
    TABLES
      communication_idoc_control           = gt_comm_idocs
      master_idoc_data                     = gt_idoc_data
   EXCEPTIONS
     error_in_idoc_control                = 1
     error_writing_idoc_status            = 2
     error_in_idoc_data                   = 3
     sending_logical_system_unknown       = 4
     OTHERS                               = 5.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    COMMIT WORK.
  ENDIF.



ENDFORM.                    " distribute_idoc
*&---------------------------------------------------------------------*
*&      Form  show_report
*&---------------------------------------------------------------------*
*       Show report
*----------------------------------------------------------------------*
FORM show_report .
  IF p_idoc = 'X'.

*   Generated IDOCs
    WRITE : / text-003 COLOR COL_HEADING INTENSIFIED ON.
    LOOP AT gt_comm_idocs INTO wa_comm_idocs.
      WRITE : / wa_comm_idocs-docnum.
    ENDLOOP.
    SKIP 2.
  ENDIF.


* Show selected activities
  SKIP 1.
  FORMAT COLOR COL_HEADING.
  WRITE AT  2 text-004. "Team
  WRITE AT 12 text-005. "Aktivitets ID'.
  WRITE AT 22 text-006. "Aktivitets beskrivelse'.
  WRITE AT 59 text-007. "PSP-element'.
  WRITE AT 79 text-008. "PSP kort tekst'.
  FORMAT RESET.

  LOOP AT  gt_activities  INTO wa_activities.
    WRITE: /
       wa_activities-orgeh UNDER text-004,
       wa_activities-actid UNDER text-005,
       wa_activities-descr UNDER text-006,
       wa_activities-posid UNDER text-007,
       wa_activities-post1 UNDER text-008.
  ENDLOOP.



ENDFORM.                    " show_report

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)