*&----------------------------------------------------------------------*
*& Report  ZSDN_UPG_AQ_REPORT_DECODE
*&
*&----------------------------------------------------------------------*
*& Report Name: ECC Upgrade: Get Report transactions linked to a query  *
*&              report AQ*Z*                                            *
*& Created by: TECHEDGE (Andrea Olivieri)                               *
*& Created on: 21.03.2008 at 14:26:55 CET                               *
*&----------------------------------------------------------------------*
*& This report prepares data for mass AQ* reports transaction conversion*
*& 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 zsdn_upg_aq_reports_decode LINE-SIZE 255..

TABLES: tstc.                     "(To support Selection Screen)
TYPES: BEGIN OF upg_aq_ty,
         upg_aq     TYPE zsdn_upg_aq,
         status     TYPE zsdn_conv_status,
       END OF upg_aq_ty.DATA: ltstct TYPE tstct.
DATA: xtstc TYPE STANDARD TABLE OF tstc WITH HEADER LINE.
DATA: it_upg_aq TYPE STANDARD TABLE OF zsdn_upg_aq,
      ls_upg_aq LIKE LINE OF it_upg_aq.

DATA: lt_alv_cnv TYPE TABLE OF upg_aq_ty,
      ls_alv_cnv LIKE LINE OF lt_alv_cnv.


DATA: db_count_max TYPE sy-tabix VALUE 500,
      returned(1),
      ld_error     TYPE sy-subrc.


RANGES: aqabaps FOR tstc-pgmna,
        rcinfo  FOR tstc-cinfo.
CONSTANTS  hex_rep1 TYPE x VALUE '80'.               " Report   transaction            R
CONSTANTS  hex_rep2 TYPE x VALUE '90'.               " Report   transaction            R


SELECT-OPTIONS custcode FOR tstc-tcode.
PARAMETER: ptest AS CHECKBOX DEFAULT abap_true.

INITIALIZATION.

  custcode-sign = 'I'.
  custcode-option = 'CP'.
  custcode-low = 'Z*'.
  APPEND custcode.
  custcode-low = 'Y*'.
  APPEND custcode.

  rcinfo-sign = 'I'.
  rcinfo-option = 'EQ'.
  rcinfo-low = hex_rep1.
  APPEND rcinfo.
  rcinfo-low = hex_rep2.
  APPEND rcinfo.


  %_ptest_%_app_%-text      = '@12@ Simulation'.
  %_custcode_%_app_%-text   = 'Customer Transaction Code'.


START-OF-SELECTION.
  SELECT * FROM  tstc INTO TABLE xtstc
         WHERE  tcode  IN custcode
         AND    pgmna  LIKE 'AQ%'
         AND    cinfo IN rcinfo.          "= hex_rep.

  IF sy-subrc <> 0.
    MESSAGE i333(s1) WITH 'Nothing to do...Bye!!'(I01).
    RETURN.
  ENDIF.
  LOOP AT xtstc.
    PERFORM aq_decode_report_name USING xtstc-tcode xtstc-pgmna CHANGING it_upg_aq.
  ENDLOOP.
  PERFORM save_data USING ptest.
  PERFORM alv.

*&--------------------------------------------------------------------*
*&      Form  AQ_DECODE_REPORT_NAME
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->REPORT     text
*---------------------------------------------------------------------*
FORM aq_decode_report_name USING    transaction TYPE tstc-tcode
                                    report      TYPE sy-repid
                           CHANGING ptupg_aq    LIKE it_upg_aq.

  DATA: prog       TYPE sy-repid,
        wspace(1),
        query(14),
        bgroup(12),
        mandt      TYPE sy-mandt.

  CLEAR ls_upg_aq.
  prog = report.

  CALL FUNCTION 'RSAQ_DECODE_REPORT_NAME'
    EXPORTING
      reportname      = prog
    IMPORTING
      workspace       = wspace
      usergroup       = bgroup
      query           = query
      client          = mandt
    EXCEPTIONS
      no_query_report = 0
      OTHERS          = 0.

  ls_upg_aq-mandt = sy-mandt.
  ls_upg_aq-tcode  = transaction.
  ls_upg_aq-pgmna  = report.
  ls_upg_aq-wsid   = wspace.
  ls_upg_aq-quname = query.
  ls_upg_aq-bgname = bgroup.

  DATA o_headqu      TYPE aqhdqu.
  CALL FUNCTION 'RSAQ_IMPORT_QUERY_AND_INFOSET'
    EXPORTING
      i_wspace    = wspace
      i_query     = query
      i_usergroup = bgroup
    IMPORTING
      o_headqu    = o_headqu
    EXCEPTIONS
      no_query    = 0
      no_infoset  = 0
      OTHERS      = 0.

  ls_upg_aq-vari   = o_headqu-quvariant.
  ls_upg_aq-client = mandt.
  APPEND ls_upg_aq TO ptupg_aq.


ENDFORM.                               "AQ_DECODE_REPORT_NAME
*&---------------------------------------------------------------------*
*&      Form  SAVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM save_data USING iv_test.

  IF iv_test IS INITIAL.
*--------------------------------------------------------------------*
* Save Data
*--------------------------------------------------------------------*
    PERFORM  zsdn_upg_aq_post        USING it_upg_aq.
  ELSE.
*--------------------------------------------------------------------*
* Simulation Mode (List tcodes to be converted)
*--------------------------------------------------------------------*
    LOOP AT it_upg_aq INTO ls_upg_aq.
      ls_alv_cnv-upg_aq = ls_upg_aq.
      ls_alv_cnv-status = 'To be converted'(010).
      APPEND  ls_alv_cnv TO lt_alv_cnv.
      CLEAR ls_alv_cnv.
    ENDLOOP.
  ENDIF.

ENDFORM.                    " SAVE_DATA
*&---------------------------------------------------------------------*
*&      Form  ZSDN_UPG_AQ_POST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P__LT_SWW_CONT_DIAL  text
*----------------------------------------------------------------------*
FORM zsdn_upg_aq_post  USING   pt_upg_aq LIKE it_upg_aq.

  DATA: laq_db_count    LIKE sy-tabix.
  CHECK NOT  pt_upg_aq[] IS INITIAL.

  CLEAR returned.
  CLEAR ld_error.
  CALL FUNCTION 'ZSDN_UPG_AQ_POST_DATA'
    STARTING NEW TASK 'UPG_AQ_POST'
    DESTINATION 'NONE'
    PERFORMING return_posting ON END OF TASK
    TABLES
      it_upg_aq      = pt_upg_aq
    EXCEPTIONS
      error_happened = 1
      OTHERS         = 2.

  WAIT UNTIL returned = 'X'.

* Customer transations Saved
  LOOP AT pt_upg_aq INTO ls_upg_aq.
    ls_alv_cnv-upg_aq = ls_upg_aq.
    IF ld_error IS INITIAL.
      ls_alv_cnv-status = 'Saved'(008).
    ELSE.
      ls_alv_cnv-status = 'ERROR'(009).
    ENDIF.
    APPEND  ls_alv_cnv TO lt_alv_cnv.
    CLEAR ls_alv_cnv.
  ENDLOOP.

ENDFORM.                    " CUST_OBJS_POST

*&---------------------------------------------------------------------*
*&      Form  RETURN_POSTING
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM return_posting USING taskname.

  CASE taskname.
    WHEN 'UPG_AQ_POST' .
      RECEIVE RESULTS FROM FUNCTION 'ZSDN_UPG_AQ_POST_DATA'
           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_POSTING
*&---------------------------------------------------------------------*
*&      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 = '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
