*&----------------------------------------------------------------------*
*& Report  ZSDN_UPG_AQ_TCODE_CONV
*&
*&----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* This report converts report transactions following the guidelines
* specified in the 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 2 - Convert and transport transactions             *
* Created by: TECHEDGE (Andrea Olivieri)                               *
* Created on: 12.12.2014 at 10:02:57 CET                               *
*----------------------------------------------------------------------*
REPORT zsdn_upg_aq_tcode_conv LINE-SIZE 255.

* 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.
TYPES: BEGIN OF upg_aq_ty,
         upg_aq     TYPE zsdn_upg_aq,
         status     TYPE ZSDN_CONV_STATUS,
       END OF upg_aq_ty.
* Internal tables and work areas
DATA:
  it_data TYPE TABLE OF ty_target,
  wa_data LIKE LINE OF it_data,
  gs_tstc TYPE tstc.
DATA: returned(1),
      ld_error TYPE sy-subrc.
DATA: lt_alv_cnv TYPE TABLE OF upg_aq_ty,
      ls_alv_cnv LIKE LINE OF lt_alv_cnv.
CONSTANTS  hex_rep TYPE x VALUE '80'.               " Report   transaction            R
CONSTANTS  hex_par TYPE x VALUE '02'.               " Parametertrans.                 P


*---------------------------------------------*
*      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.
PARAMETER: p_tcode TYPE tstc-tcode OBLIGATORY DEFAULT 'REISSQMAIN'.
PARAMETER: ptest AS CHECKBOX DEFAULT abap_true.

INITIALIZATION.
  SELECT SINGLE MAX( ddtext ) FROM dd02t INTO title01 WHERE tabname    = 'ZSDN_UPG_AQ'
                                                        AND ddlanguage = sy-langu. "#EC *
  %_ptest_%_app_%-text      = '@12@ Simulation'.
  %_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'.
  %_p_tcode_%_app_%-text    = 'Report transaction'.


AT SELECTION-SCREEN ON p_tcode.
  CHECK NOT p_tcode IS INITIAL.
  SELECT SINGLE * FROM tstc INTO gs_tstc
                    WHERE tcode = p_tcode.
  IF sy-subrc = 0 AND gs_tstc-cinfo O hex_rep.
    IF gs_tstc-pgmna <> 'SAP_QUERY_CALL'.
      "Transaction not allowed for conversion
      MESSAGE E333(s1) with 'Transaction specified not allowed for conversion'(E01).
    ENDIF.
  ELSE.
    "Transaction unknown or incorrect type
      MESSAGE E333(s1) with 'Transaction unknown or incorrect type'(E02).
  ENDIF.

*---------------------------------------------*
*       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_false.
  IF it_data[] IS INITIAL.
    MESSAGE i333(s1) WITH 'Nothing to do...Bye!'(e01).
    RETURN.
  ENDIF.

* Step 2 Ask user if he would like to convert all transactions
  DATA: l_popuptext   TYPE  char100,
        l_count       TYPE  i,
        l_answer      TYPE c,
        l_cnt_str(10) TYPE c.

  l_count = lines( it_data ).
  IF l_count > 1 AND sy-batch = abap_false.

    l_cnt_str = l_count.
    CONDENSE l_cnt_str.

    CONCATENATE 'You have selected for conversion the following number of transactions:'(004)
                 l_cnt_str '. Start conversion Process?'(005)
                INTO l_popuptext SEPARATED BY space.

    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar              = 'Confirmation Prompt'(006)
        text_question         = l_popuptext
        default_button        = '2'
        display_cancel_button = abap_false
      IMPORTING
        answer                = l_answer.
    IF l_answer <> '1'.
      RETURN.
    ENDIF.
  ENDIF.

* Step 3 - Do the transactions conversion (From report to parameters)
  PERFORM do_conversion USING ptest.
  perform alv.
*&---------------------------------------------------------------------*
*&      Form  DO_CONVERSION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM do_conversion USING iv_test.
  DATA report      TYPE rsvar-report.
  DATA field_names TYPE STANDARD TABLE OF rsdynpar.
  DATA l_field_names LIKE LINE OF field_names.
  DATA tran_langu TYPE sy-langu.
  DATA: ls_tstc  TYPE tstc,
        ls_tstca TYPE tstca,
        ls_tstcc TYPE tstcc,
        ls_tstcp TYPE tstcp.
  DATA: lt_data  TYPE STANDARD TABLE OF zsdn_upg_aq,
        ls_data  TYPE  zsdn_upg_aq,
        lt_tstc  TYPE STANDARD TABLE OF tstc,
        lt_tstca TYPE STANDARD TABLE OF tstca,
        lt_tstcc TYPE STANDARD TABLE OF tstcc,
        lt_tstcp TYPE STANDARD TABLE OF tstcp.
  DATA: request              TYPE e070-trkorr,
        use_korrnum_immediatedly TYPE abap_bool,
        suppress_dialog          TYPE abap_bool.

*---*

  report = gs_tstc-pgmna.

  LOOP AT it_data INTO wa_data.
    DATA: l_ta_params LIKE rsparam OCCURS 0 WITH HEADER LINE.

    CLEAR: ls_tstc, ls_tstcc, ls_tstca, ls_tstcp, ls_data.
    SELECT SINGLE * FROM tstc INTO ls_tstc WHERE tcode = wa_data-tcode.
    IF sy-subrc <> 0. CONTINUE. ENDIF.

    "Transaction already converted?
    IF ls_tstc-cinfo = hex_par.
      wa_data-conv_done = abap_true.
      MOVE-CORRESPONDING wa_data TO ls_data.
      APPEND ls_data TO lt_data.
      CONTINUE.
    ENDIF.

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

    SELECT SINGLE * FROM tstcc INTO ls_tstcc WHERE tcode = wa_data-tcode.
    IF sy-subrc <> 0.
      ls_tstcc-tcode = wa_data-tcode.
    ENDIF.
    SELECT SINGLE * FROM tstcp INTO ls_tstcp WHERE tcode = wa_data-tcode.
    IF sy-subrc <> 0.
      ls_tstcp-tcode = wa_data-tcode.
    ENDIF.

    " 1: TSTC;  Switch transaction Type
    ls_tstc-cinfo = hex_par.     "Parameter transaction
    CLEAR: ls_tstc-pgmna, ls_tstc-dypno.
    " 2: TSTCC; Other transaction data
    ls_tstcc-s_win32 = abap_true.
    " 3: TSTCP; Transaction's Parameters
    ls_tstcp-param = '/*&4 P_WSID=&3;P_UGROUP=&1;P_QUERY=&2;P_VARI=&5;'.
    "Query Area
    IF wa_data-wsid EQ 'G'.
      REPLACE ALL OCCURRENCES OF '&3' IN ls_tstcp-param WITH 'X'.
    ELSE.
      REPLACE ALL OCCURRENCES OF '&3' IN ls_tstcp-param WITH space.
    ENDIF.
    "User Group
    REPLACE ALL OCCURRENCES OF '&1' IN ls_tstcp-param WITH wa_data-bgname.
    "Query Name
    REPLACE ALL OCCURRENCES OF '&2' IN ls_tstcp-param WITH wa_data-quname.
    "Transaction Code
    REPLACE ALL OCCURRENCES OF '&4' IN ls_tstcp-param WITH p_tcode.
    "Query variant
    REPLACE ALL OCCURRENCES OF '&5' IN ls_tstcp-param WITH wa_data-VARI.

    IF iv_test IS INITIAL.
      IF request IS NOT INITIAL.
*        use_korrnum_immediatedly = abap_true.
        suppress_dialog          = abap_true.
        CALL FUNCTION 'RS_CORR_INSERT'
          EXPORTING
            global_lock              = abap_true
            object                   = ls_tstc-tcode
            object_class             = 'TRAN'
            master_language          = tran_langu
            korrnum                  = request
            suppress_dialog          = suppress_dialog
*           use_korrnum_immediatedly = use_korrnum_immediatedly
          IMPORTING
            korrnum                  = request
          EXCEPTIONS
            cancelled                = 01
            permission_failure       = 02
            unknown_objectclass      = 03.
      ELSE.
        CALL FUNCTION 'RS_CORR_INSERT'
          EXPORTING
            global_lock         = abap_true
            object              = ls_tstc-tcode
            object_class        = 'TRAN'
            master_language     = tran_langu
            korrnum             = request
          IMPORTING
            korrnum             = request
          EXCEPTIONS
            cancelled           = 01
            permission_failure  = 02
            unknown_objectclass = 03.
      ENDIF.


      IF sy-subrc NE 0.
        MESSAGE ID     sy-msgid
                TYPE   'S'
                NUMBER sy-msgno
                WITH   sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        CONTINUE.
      ELSE.
        wa_data-conv_done = abap_true.
        MOVE-CORRESPONDING wa_data TO ls_data.
        APPEND ls_data TO lt_data.
        APPEND: ls_tstc TO  lt_tstc,
                ls_tstcc TO lt_tstcc,
                ls_tstcp TO lt_tstcp.


      ENDIF.
    ELSE.
      wa_data-conv_done = abap_true.
      MOVE-CORRESPONDING wa_data TO ls_data.
      APPEND ls_data TO lt_data.
      APPEND: ls_tstc TO  lt_tstc,
              ls_tstcc TO lt_tstcc,
              ls_tstcp TO lt_tstcp.
    ENDIF.
  ENDLOOP.

  IF iv_test IS INITIAL.
    CLEAR: returned, ld_error.
    CALL FUNCTION 'ZSDN_UPG_AQ_CONV_TCODE'
      STARTING NEW TASK 'UPG_AQ_CONV'
      DESTINATION 'NONE'
      PERFORMING return_conversion ON END OF TASK
      TABLES
        it_upg_aq      = lt_data
        it_tstc        = lt_tstc
        it_tstcc       = lt_tstcc
        it_tstcp       = lt_tstcp
      EXCEPTIONS
        error_happened = 1
        OTHERS         = 2.

    WAIT UNTIL returned = 'X'.

    LOOP AT lt_data INTO ls_data.
      ls_alv_cnv-upg_aq = ls_data.
      IF ld_error IS INITIAL.
        ls_alv_cnv-status = 'Converted'(008).
      ELSE.
        ls_alv_cnv-status = 'ERROR'(009).
      ENDIF.
      APPEND  ls_alv_cnv TO lt_alv_cnv.
      CLEAR ls_alv_cnv.
    ENDLOOP.

  ELSE.
    "write...............
    LOOP AT lt_data INTO ls_data.
      ls_alv_cnv-upg_aq = ls_data.
      CLEAR ls_tstcp.
      READ TABLE lt_tstcp  INTO ls_tstcp WITH KEY tcode = ls_data-tcode.
      IF sy-subrc = 0.
        ls_alv_cnv-status = 'To be converted'(010).
      ELSE.
        ls_alv_cnv-status = 'Already Converted; Update Conversion status'(011).
      ENDIF.
      APPEND  ls_alv_cnv TO lt_alv_cnv.
      CLEAR ls_alv_cnv.
    ENDLOOP.
  ENDIF.


ENDFORM.                    " DO_CONVERSION
*&---------------------------------------------------------------------*
*&      Form  RETURN_CONVERSION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM return_conversion USING taskname.

  CASE taskname.
    WHEN 'UPG_AQ_CONV' .
      RECEIVE RESULTS FROM FUNCTION 'ZSDN_UPG_AQ_CONV_TCODE'
           EXCEPTIONS
                syst_except           = 1
                communication_failure = 2
                system_failure        = 3
                error_happened        = 4
                error                 = 6.

    WHEN OTHERS.
  ENDCASE.

  ld_error = sy-subrc.
  returned = 'X'.


ENDFORM.                    " RETURN_conversion
*&---------------------------------------------------------------------*
*&      Form  ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form alv .
  check not lt_alv_cnv is initial.
  data:  gr_table     type ref to cl_salv_table,
         gr_display   type ref to cl_salv_display_settings,
         gr_functions type ref to cl_salv_functions,
         gr_sorts     type ref to cl_salv_sorts,
         gr_agg       type ref to cl_salv_aggregations,
         l_title type lvc_title.
  try.
*     Create ALV table
      cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = lt_alv_cnv ).
*     Set zebra layout
      gr_display = gr_table->get_display_settings( ).
      gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
*   set title
      l_title = 'CONVERSION of Customer Transation Codes with AQ* generated report'(003).
      gr_display->set_list_header( l_title ).
*     Display all standard function
      gr_functions = gr_table->get_functions( ).
      gr_functions->set_all( abap_true ).
      gr_table->display( ).
    catch cx_salv_msg.
      write: / 'Exception CX_SALV_MSG'.
    catch cx_salv_not_found.
      write: / 'Exception CX_SALV_NOT_FOUND'.
    catch cx_salv_data_error.
      write: / 'Exception CX_SALV_DATA_ERROR'.
    catch cx_salv_existing.
      write: / 'Exception CX_SALV_EXISTING'.
  endtry.
endform.                    " ALV
