*&---------------------------------------------------------------------*
*& Report  ZSDN_UPG_AQ_TCODE_CHECK
*&
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* This report check the consistency of converted transactions
* ( See SAP Note 393160 - SAP Query: Using queries)
* The selection part of this report has been generated by
* ABAP Report Wizard v. 1.0
*----------------------------------------------------------------------*
* Report Name: Step 3 - Step 3 - Consistency Check                     *
* Created by: TECHEDGE (Andrea Olivieri)                               *
* Created on: 12.12.2014 at 10:02:57 CET                               *
*----------------------------------------------------------------------*
REPORT zsdn_upg_aq_tcode_check.
* Tables ( To support selection screen )
TABLES:
       zsdn_upg_aq.

* Types
TYPES: BEGIN OF ty_target,
          tcode     TYPE zsdn_upg_aq-tcode,
          pgmna     TYPE zsdn_upg_aq-pgmna,
          wsid      TYPE zsdn_upg_aq-wsid,
          quname    TYPE zsdn_upg_aq-quname,
          bgname    TYPE zsdn_upg_aq-bgname,
          vari      TYPE zsdn_upg_aq-vari,
          client    TYPE zsdn_upg_aq-client,
          conv_done TYPE xfeld,
      END OF ty_target.

* Internal tables and work areas
DATA:
      it_data        TYPE TABLE OF ty_target,
      wa_data        LIKE LINE OF it_data,
      ls_tstct       TYPE tstct,
      trans_langu    TYPE tadir-masterlang.

DATA: returned(1),
      ld_error TYPE sy-subrc.

DATA:
l_log_handle TYPE balloghndl,
l_s_log      TYPE bal_s_log,
l_s_msg      TYPE bal_s_msg,
l_s_display_profile TYPE bal_s_prof.

*---------------------------------------------*
*      S E L E C T I O N   S C R E E N        *
*---------------------------------------------*

SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME TITLE title01.
SELECT-OPTIONS: so_bgna1                              FOR zsdn_upg_aq-bgname.
SELECT-OPTIONS: so_pgmna                              FOR zsdn_upg_aq-pgmna.
SELECT-OPTIONS: so_quna1                              FOR zsdn_upg_aq-quname.
SELECT-OPTIONS: so_tcode                              FOR zsdn_upg_aq-tcode.
SELECT-OPTIONS: so_wsid                               FOR zsdn_upg_aq-wsid.
SELECTION-SCREEN: END OF BLOCK b01.

INITIALIZATION.
  SELECT SINGLE MAX( ddtext ) FROM dd02t INTO title01 WHERE tabname    = 'ZSDN_UPG_AQ'
                                                        AND ddlanguage = sy-langu. "#EC *
  %_so_bgna1_%_app_%-text   = 'User Group'.
  %_so_pgmna_%_app_%-text   = 'Program'.
  %_so_quna1_%_app_%-text   = 'Query name'.
  %_so_tcode_%_app_%-text   = 'Customer Transaction Code'.
  %_so_wsid_%_app_%-text    = 'Work area'.


*---------------------------------------------*
*       D A T A   S E L E C T I O N           *
*---------------------------------------------*
START-OF-SELECTION.

* Step 1: Get Data
  SELECT a~tcode a~pgmna a~wsid a~quname a~bgname a~vari a~client
    FROM zsdn_upg_aq AS a
    INTO TABLE it_data   " <--  Attention ! Field sequence !
    WHERE a~tcode     IN so_tcode AND
          a~pgmna     IN so_pgmna AND
          a~wsid      IN so_wsid  AND
          a~quname    IN so_quna1 AND
          a~bgname    IN so_bgna1 AND
          a~conv_done =  abap_true.
  IF it_data[] IS INITIAL.
    MESSAGE i333(s1) WITH 'Nothing to do...Bye!'(e01).
    RETURN.
  ENDIF.

  DATA: error_list TYPE STANDARD TABLE OF rsmp_check,
        ls_error_list LIKE LINE OF error_list,
        ltext(120) TYPE c.

* create an initial log file
  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log      = l_s_log
    IMPORTING
      e_log_handle = l_log_handle
    EXCEPTIONS
      OTHERS       = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  "Add Begin of...message
  l_s_msg-msgid = 'S1'.
  l_s_msg-msgty = 'I'.
  l_s_msg-msgno = '333'.
  l_s_msg-msgv1 = 'Start of TCODEs Consistency Check...'(M01).
  CALL FUNCTION 'BAL_LOG_MSG_ADD'
    EXPORTING
      i_log_handle = l_log_handle
      i_s_msg      = l_s_msg
    EXCEPTIONS
      OTHERS       = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.



  LOOP AT it_data INTO wa_data.
    CLEAR: error_list, ls_error_list, ltext, trans_langu, l_s_msg.

    CALL FUNCTION 'RS_TRANSACTION_INCONSISTENCIES'
      EXPORTING
        transaction_code = wa_data-tcode
      TABLES
        error_list       = error_list
      EXCEPTIONS
        object_not_found = 0
        OTHERS           = 0.

    "Get original language
    SELECT SINGLE masterlang FROM tadir INTO trans_langu
                             WHERE pgmid      = 'R3TR'
                             AND   object     = 'TRAN'
                             AND   obj_name   = wa_data-tcode.

    SELECT SINGLE * FROM tstct INTO ls_tstct
             WHERE tcode = wa_data-tcode
             AND   sprsl = trans_langu.

    READ TABLE error_list INTO ls_error_list INDEX 1.
    IF sy-subrc = 0.
      MOVE-CORRESPONDING ls_error_list TO l_s_msg.
    ELSE.
      l_s_msg-msgid = 'EU'.
      l_s_msg-msgty = 'I'.
      l_s_msg-msgno = '200'.
      l_s_msg-msgv1 = wa_data-tcode.
    ENDIF.

    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        i_log_handle = l_log_handle
        i_s_msg      = l_s_msg
      EXCEPTIONS
        OTHERS       = 1.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDLOOP.

* get display profile
  CALL FUNCTION 'BAL_DSP_PROFILE_NO_TREE_GET'
    IMPORTING
      e_s_display_profile = l_s_display_profile
    EXCEPTIONS
      OTHERS              = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* use grid for online display
  IF sy-batch IS INITIAL.
    l_s_display_profile-use_grid = abap_true.
  ENDIF.

* set report to allow saving of variants
  l_s_display_profile-disvariant-report = sy-repid.
  l_s_display_profile-title = sy-title.

  CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
    EXPORTING
      i_s_display_profile = l_s_display_profile
    EXCEPTIONS
      OTHERS              = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
