Difference between revisions of "SAP MM BAPI MATERIAL QMAT"

From SapWiki
(Created page with " <nowiki> REPORT zqm_crear_bi. *& Descripcion : Creación de vista calidad materiales *--------------------------------------------------------------------* TYPE-POOLS...")
 
 
Line 1: Line 1:
  <nowiki> REPORT  zqm_crear_bi.
+
<nowiki>REPORT  zqm_crear_bi.
*& Descripcion  : Creación de vista calidad materiales
+
*& Descripcion  : Creación de vista calidad materiales
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
  TYPE-POOLS : slis.
+
TYPE-POOLS : slis.
  TABLES qmat.
+
TABLES qmat.
+
 
*-> Tabla para el ingreso de datos
+
*-> Tabla para el ingreso de datos
  DATA: BEGIN OF gs_data,
+
DATA: BEGIN OF gs_data,
          matnr    LIKE mara-matnr,
+
        matnr    LIKE mara-matnr,
          werks    LIKE mard-werks,
+
        werks    LIKE mard-werks,
          kzdkz    LIKE marc-kzdkz,
+
        kzdkz    LIKE marc-kzdkz,
          prfrq    LIKE marc-prfrq,
+
        prfrq    LIKE marc-prfrq,
          qmata    LIKE marc-qmata,
+
        qmata    LIKE marc-qmata,
          ssqss    LIKE marc-ssqss,
+
        ssqss    LIKE marc-ssqss,
          clinsp01 LIKE rmqam-art,
+
        clinsp01 LIKE rmqam-art,
          actins01 LIKE rmqam-aktiv,
+
        actins01 LIKE rmqam-aktiv,
          clinsp02 LIKE rmqam-art,
+
        clinsp02 LIKE rmqam-art,
          actins02 LIKE rmqam-aktiv,
+
        actins02 LIKE rmqam-aktiv,
          clinsp03 LIKE rmqam-art,
+
        clinsp03 LIKE rmqam-art,
          actins03 LIKE rmqam-aktiv,
+
        actins03 LIKE rmqam-aktiv,
          clinsp04 LIKE rmqam-art,
+
        clinsp04 LIKE rmqam-art,
          actins04 LIKE rmqam-aktiv,
+
        actins04 LIKE rmqam-aktiv,
          clinsp05 LIKE rmqam-art,
+
        clinsp05 LIKE rmqam-art,
          actins05 LIKE rmqam-aktiv,
+
        actins05 LIKE rmqam-aktiv,
          clinsp06 LIKE rmqam-art,
+
        clinsp06 LIKE rmqam-art,
          actins06 LIKE rmqam-aktiv,
+
        actins06 LIKE rmqam-aktiv,
          clinsp07 LIKE rmqam-art,
+
        clinsp07 LIKE rmqam-art,
          actins07 LIKE rmqam-aktiv,
+
        actins07 LIKE rmqam-aktiv,
          clinsp08 LIKE rmqam-art,
+
        clinsp08 LIKE rmqam-art,
          actins08 LIKE rmqam-aktiv,
+
        actins08 LIKE rmqam-aktiv,
          clinsp09 LIKE rmqam-art,
+
        clinsp09 LIKE rmqam-art,
          actins09 LIKE rmqam-aktiv,
+
        actins09 LIKE rmqam-aktiv,
          clinsp10 LIKE rmqam-art,
+
        clinsp10 LIKE rmqam-art,
          actins10 LIKE rmqam-aktiv,
+
        actins10 LIKE rmqam-aktiv,
        END OF gs_data.
+
      END OF gs_data.
  DATA gt_data LIKE TABLE OF gs_data.
+
DATA gt_data LIKE TABLE OF gs_data.
   
+
 
  DATA gt_qmat LIKE TABLE OF qmat.
+
  DATA gt_qmat LIKE TABLE OF qmat.
  DATA gs_qmat LIKE LINE OF gt_qmat.
+
DATA gs_qmat LIKE LINE OF gt_qmat.
   
+
 
  DATA: BEGIN OF gs_header,
+
  DATA: BEGIN OF gs_header,
          colname TYPE scrtext_l,
+
        colname TYPE scrtext_l,
        END OF gs_header.
+
      END OF gs_header.
  DATA gt_header LIKE TABLE OF gs_header.
+
DATA gt_header LIKE TABLE OF gs_header.
  DATA gd_tfile LIKE ibipparms-path.
+
DATA gd_tfile LIKE ibipparms-path.
   
+
 
  TYPES: BEGIN OF ty_log,
+
  TYPES: BEGIN OF ty_log,
          matnr  TYPE matnr,
+
          matnr  TYPE matnr,
          werks  TYPE werks,
+
          werks  TYPE werks,
          type    TYPE bapi_mtype,
+
          type    TYPE bapi_mtype,
          message TYPE bapi_msg,
+
          message TYPE bapi_msg,
        END OF ty_log.
+
        END OF ty_log.
   
+
 
  DATA gt_log TYPE TABLE OF ty_log.
+
  DATA gt_log TYPE TABLE OF ty_log.
  DATA gs_log LIKE LINE OF gt_log.
+
DATA gs_log LIKE LINE OF gt_log.
+
 
*alv data
+
*alv data
  DATA gr_table  TYPE REF TO cl_salv_table.
+
DATA gr_table  TYPE REF TO cl_salv_table.
  DATA: gr_selections TYPE REF TO cl_salv_selections,
+
DATA: gr_selections TYPE REF TO cl_salv_selections,
        gt_rows      TYPE salv_t_row.
+
      gt_rows      TYPE salv_t_row.
  DATA: gr_functions TYPE REF TO cl_salv_functions_list.
+
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
+
 
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
  PARAMETERS: p_file TYPE string LOWER CASE OBLIGATORY.
+
PARAMETERS: p_file TYPE string LOWER CASE OBLIGATORY.
+
 
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
  AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
+
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
    gd_tfile = p_file.
+
  gd_tfile = p_file.
+
 
    CALL FUNCTION 'F4_FILENAME'
+
  CALL FUNCTION 'F4_FILENAME'
      EXPORTING
+
    EXPORTING
        field_name = 'P_FILE'
+
      field_name = 'P_FILE'
      IMPORTING
+
    IMPORTING
        file_name  = gd_tfile.
+
      file_name  = gd_tfile.
+
 
    MOVE gd_tfile TO p_file.
+
  MOVE gd_tfile TO p_file.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
  START-OF-SELECTION.
+
START-OF-SELECTION.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
+
 
    PERFORM upload.
+
  PERFORM upload.
+
 
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
  END-OF-SELECTION.
+
END-OF-SELECTION.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
    IF gt_data[] IS INITIAL.
+
  IF gt_data[] IS INITIAL.
      WRITE:/ 'No se seleccionaron datos'.
+
    WRITE:/ 'No se seleccionaron datos'.
      RETURN.
+
    RETURN.
    ENDIF.
+
  ENDIF.
+
 
    PERFORM crear_vista_qm.
+
  PERFORM crear_vista_qm.
+
 
    PERFORM display_log.
+
  PERFORM display_log.
+
 
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  upload
+
*&      Form  upload
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
  FORM upload.
+
FORM upload.
+
 
    CALL FUNCTION 'GUI_UPLOAD'
+
  CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
+
    EXPORTING
        filename                = p_file
+
      filename                = p_file
        filetype                = 'DAT'
+
      filetype                = 'DAT'
      TABLES
+
    TABLES
        data_tab                = gt_data
+
      data_tab                = gt_data
      EXCEPTIONS
+
    EXCEPTIONS
        file_open_error        = 1
+
      file_open_error        = 1
        file_read_error        = 2
+
      file_read_error        = 2
        no_batch                = 3
+
      no_batch                = 3
        gui_refuse_filetransfer = 4
+
      gui_refuse_filetransfer = 4
        invalid_type            = 5
+
      invalid_type            = 5
        no_authority            = 6
+
      no_authority            = 6
        unknown_error          = 7
+
      unknown_error          = 7
        bad_data_format        = 8
+
      bad_data_format        = 8
        header_not_allowed      = 9
+
      header_not_allowed      = 9
        separator_not_allowed  = 10
+
      separator_not_allowed  = 10
        header_too_long        = 11
+
      header_too_long        = 11
        unknown_dp_error        = 12
+
      unknown_dp_error        = 12
        access_denied          = 13
+
      access_denied          = 13
        dp_out_of_memory        = 14
+
      dp_out_of_memory        = 14
        disk_full              = 15
+
      disk_full              = 15
        dp_timeout              = 16
+
      dp_timeout              = 16
        OTHERS                  = 17.
+
      OTHERS                  = 17.
    IF sy-subrc <> 0.
+
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
+
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
+
*        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
+
  ENDIF.
+
 
   
+
 
  ENDFORM.                    "upload
+
  ENDFORM.                    "upload
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  CREAR_VISTA_QM
+
*&      Form  CREAR_VISTA_QM
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*  -->  p1        text
+
*  -->  p1        text
*  <--  p2        text
+
*  <--  p2        text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
  FORM crear_vista_qm .
+
FORM crear_vista_qm .
+
 
+
 
*Internal table to store messages returned from BAPI
+
*Internal table to store messages returned from BAPI
    DATA:
+
  DATA:
    ws_return LIKE " To store status
+
  ws_return LIKE " To store status
    bapiret2 OCCURS 0 WITH HEADER LINE.
+
  bapiret2 OCCURS 0 WITH HEADER LINE.
+
 
* Internal table to store Inspection details to pass
+
* Internal table to store Inspection details to pass
* as parameter to BAPI
+
* as parameter to BAPI
    DATA:
+
  DATA:
      BEGIN OF ws_qmat OCCURS 0.
+
    BEGIN OF ws_qmat OCCURS 0.
        INCLUDE STRUCTURE bapi1001004_qmat.
+
      INCLUDE STRUCTURE bapi1001004_qmat.
    DATA:
+
  DATA:
    END OF ws_qmat.
+
  END OF ws_qmat.
    DATA:
+
  DATA:
      ws_insp(2).
+
    ws_insp(2).
    DATA ls_tq34 LIKE tq34.
+
  DATA ls_tq34 LIKE tq34.
+
 
    DATA ld_cont(2) TYPE n.
+
  DATA ld_cont(2) TYPE n.
    DATA ld_fieldname(40).
+
  DATA ld_fieldname(40).
    FIELD-SYMBOLS <f1>.
+
  FIELD-SYMBOLS <f1>.
    FIELD-SYMBOLS <f2>.
+
  FIELD-SYMBOLS <f2>.
    LOOP AT gt_data INTO gs_data.
+
  LOOP AT gt_data INTO gs_data.
+
 
      DO 10 TIMES.
+
    DO 10 TIMES.
        ADD 1 TO ld_cont.
+
      ADD 1 TO ld_cont.
+
 
        CONCATENATE 'CLINSP' ld_cont INTO ld_fieldname.
+
      CONCATENATE 'CLINSP' ld_cont INTO ld_fieldname.
        ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f1>.
+
      ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f1>.
        CONCATENATE 'ACTINS' ld_cont INTO ld_fieldname.
+
      CONCATENATE 'ACTINS' ld_cont INTO ld_fieldname.
        ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f2>.
+
      ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f2>.
+
 
        CHECK <f1> IS NOT INITIAL.
+
      CHECK <f1> IS NOT INITIAL.
+
 
* Retrieving Quality Score procedure from TQ34 table
+
* Retrieving Quality Score procedure from TQ34 table
        SELECT SINGLE * INTO ls_tq34
+
      SELECT SINGLE * INTO ls_tq34
        FROM tq34
+
      FROM tq34
        WHERE art = <f1>.
+
      WHERE art = <f1>.
        CHECK sy-subrc = 0.
+
      CHECK sy-subrc = 0.
+
 
        ws_qmat-insptype = <f1>.
+
      ws_qmat-insptype = <f1>.
        ws_qmat-material = gs_data-matnr.
+
      ws_qmat-material = gs_data-matnr.
        ws_qmat-plant = gs_data-werks.
+
      ws_qmat-plant = gs_data-werks.
+
 
        ws_qmat-ind_insp_with_tsk_list = ls_tq34-ppl.
+
      ws_qmat-ind_insp_with_tsk_list = ls_tq34-ppl.
        ws_qmat-ind_spec_matspec = ls_tq34-spezueber.
+
      ws_qmat-ind_spec_matspec = ls_tq34-spezueber.
        ws_qmat-ind_spec_config = ls_tq34-conf.
+
      ws_qmat-ind_spec_config = ls_tq34-conf.
        ws_qmat-ind_spec_batch = ls_tq34-tls.
+
      ws_qmat-ind_spec_batch = ls_tq34-tls.
        ws_qmat-ind_auto_assign = ls_tq34-app.
+
      ws_qmat-ind_auto_assign = ls_tq34-app.
        ws_qmat-ind_insp_by_charac = ls_tq34-mer.
+
      ws_qmat-ind_insp_by_charac = ls_tq34-mer.
        ws_qmat-ind_post_to_insp_stock = ls_tq34-insmk.
+
      ws_qmat-ind_post_to_insp_stock = ls_tq34-insmk.
        ws_qmat-ind_automatic_ud = ls_tq34-ave.
+
      ws_qmat-ind_automatic_ud = ls_tq34-ave.
        ws_qmat-sampling_procedure = ls_tq34-stichprver.
+
      ws_qmat-sampling_procedure = ls_tq34-stichprver.
        ws_qmat-dyn_modif_rule = ls_tq34-dynregel.
+
      ws_qmat-dyn_modif_rule = ls_tq34-dynregel.
        ws_qmat-insp_percentage = ls_tq34-sproz.
+
      ws_qmat-insp_percentage = ls_tq34-sproz.
        ws_qmat-ind_100_percent_inspection = ls_tq34-hpz.
+
      ws_qmat-ind_100_percent_inspection = ls_tq34-hpz.
        ws_qmat-ind_skips_allowed = ls_tq34-dyn.
+
      ws_qmat-ind_skips_allowed = ls_tq34-dyn.
        ws_qmat-ind_manual_sample = ls_tq34-mpb.
+
      ws_qmat-ind_manual_sample = ls_tq34-mpb.
        ws_qmat-ind_manual_sample_calc = ls_tq34-mst.
+
      ws_qmat-ind_manual_sample_calc = ls_tq34-mst.
        ws_qmat-ind_single_units_possible = ls_tq34-ein.
+
      ws_qmat-ind_single_units_possible = ls_tq34-ein.
        ws_qmat-ave_insp_duration = ls_tq34-mpdau.
+
      ws_qmat-ave_insp_duration = ls_tq34-mpdau.
        ws_qmat-contr_insp_lot_create = ls_tq34-chg.
+
      ws_qmat-contr_insp_lot_create = ls_tq34-chg.
        ws_qmat-qual_score_procedure = ls_tq34-qkzverf.
+
      ws_qmat-qual_score_procedure = ls_tq34-qkzverf.
        ws_qmat-allowed_scrap_share = ls_tq34-qpmat.
+
      ws_qmat-allowed_scrap_share = ls_tq34-qpmat.
        ws_qmat-ind_hu_inspection = ls_tq34-afr.
+
      ws_qmat-ind_hu_inspection = ls_tq34-afr.
        ws_qmat-ms_flag = ls_tq34-ms_flag.
+
      ws_qmat-ms_flag = ls_tq34-ms_flag.
+
 
        ws_qmat-ind_insptype_mat_active = <f2>.
+
      ws_qmat-ind_insptype_mat_active = <f2>.
+
 
        APPEND ws_qmat.
+
      APPEND ws_qmat.
      ENDDO.
+
    ENDDO.
+
 
      CALL FUNCTION 'BAPI_MATINSPCTRL_SAVEREPLICA'
+
    CALL FUNCTION 'BAPI_MATINSPCTRL_SAVEREPLICA'
        TABLES
+
      TABLES
          return        = ws_return
+
        return        = ws_return
          inspectionctrl = ws_qmat.
+
        inspectionctrl = ws_qmat.
+
 
      IF sy-subrc NE 0.
+
    IF sy-subrc NE 0.
* No sy-subrc check is required
+
* No sy-subrc check is required
      ENDIF. " IF SY-SUBRC NE 0
+
    ENDIF. " IF SY-SUBRC NE 0
+
 
      MOVE-CORRESPONDING gs_data TO gs_log.
+
    MOVE-CORRESPONDING gs_data TO gs_log.
+
 
      LOOP AT ws_return WHERE type = 'E'.
+
    LOOP AT ws_return WHERE type = 'E'.
      ENDLOOP.
+
    ENDLOOP.
      IF sy-subrc <> 0.
+
    IF sy-subrc <> 0.
* To commit the transation for succesfull records
+
* To commit the transation for succesfull records
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
+
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
+
        EXPORTING
            wait = 'X'.
+
          wait = 'X'.
*      write:/ gs_data-matnr,
+
*      write:/ gs_data-matnr,
*              'Se creo vista calidad para material'.
+
*              'Se creo vista calidad para material'.
        gs_log-type = 'S'.
+
      gs_log-type = 'S'.
        gs_log-message = 'Se creo vista calidad para material'.
+
      gs_log-message = 'Se creo vista calidad para material'.
      ELSE.
+
    ELSE.
        gs_log-type = 'E'.
+
      gs_log-type = 'E'.
        gs_log-message = ws_return-message .
+
      gs_log-message = ws_return-message .
      ENDIF.
+
    ENDIF.
      APPEND gs_log TO gt_log.
+
    APPEND gs_log TO gt_log.
+
 
    ENDLOOP.
+
  ENDLOOP.
   
+
 
  ENDFORM.                    " CREAR_VISTA_QM
+
  ENDFORM.                    " CREAR_VISTA_QM
+
 
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  display_log
+
*&      Form  display_log
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
  FORM display_log.
+
FORM display_log.
    DATA: lr_events  TYPE REF TO cl_salv_events_table,
+
  DATA: lr_events  TYPE REF TO cl_salv_events_table,
          lr_columns TYPE REF TO cl_salv_columns_table,
+
        lr_columns TYPE REF TO cl_salv_columns_table,
          lr_column  TYPE REF TO cl_salv_column,
+
        lr_column  TYPE REF TO cl_salv_column,
          lr_grid    TYPE REF TO cl_salv_form_layout_grid,
+
        lr_grid    TYPE REF TO cl_salv_form_layout_grid,
          lr_layout  TYPE REF TO cl_salv_layout,
+
        lr_layout  TYPE REF TO cl_salv_layout,
          ls_key    TYPE salv_s_layout_key,
+
        ls_key    TYPE salv_s_layout_key,
          lr_sorts  TYPE REF TO cl_salv_sorts,
+
        lr_sorts  TYPE REF TO cl_salv_sorts,
          lr_sort    TYPE REF TO cl_salv_sort.
+
        lr_sort    TYPE REF TO cl_salv_sort.
+
 
    TRY.
+
  TRY.
*... Create Instance
+
*... Create Instance
        CALL METHOD cl_salv_table=>factory
+
      CALL METHOD cl_salv_table=>factory
          IMPORTING
+
        IMPORTING
            r_salv_table = gr_table
+
          r_salv_table = gr_table
          CHANGING
+
        CHANGING
            t_table      = gt_log.
+
          t_table      = gt_log.
      CATCH cx_salv_msg.
+
    CATCH cx_salv_msg.
    ENDTRY.
+
  ENDTRY.
+
 
*... Enable Generic ALV functions
+
*... Enable Generic ALV functions
    gr_functions = gr_table->get_functions( ).
+
  gr_functions = gr_table->get_functions( ).
**  gr_functions->set_default( ).
+
**  gr_functions->set_default( ).
    gr_functions->set_all( ).
+
  gr_functions->set_all( ).
+
 
*... Display table
+
*... Display table
    gr_table->display( ).
+
  gr_table->display( ).
   
+
 
  ENDFORM.                    "display_log</nowiki>
+
  ENDFORM.                    "display_log</nowiki>

Latest revision as of 22:09, 4 April 2020

REPORT  zqm_crear_bi.
*& Descripcion   : Creación de vista calidad materiales
*--------------------------------------------------------------------*
 TYPE-POOLS : slis.
 TABLES qmat.

*-> Tabla para el ingreso de datos
 DATA: BEGIN OF gs_data,
         matnr    LIKE mara-matnr,
         werks    LIKE mard-werks,
         kzdkz    LIKE marc-kzdkz,
         prfrq    LIKE marc-prfrq,
         qmata    LIKE marc-qmata,
         ssqss    LIKE marc-ssqss,
         clinsp01 LIKE rmqam-art,
         actins01 LIKE rmqam-aktiv,
         clinsp02 LIKE rmqam-art,
         actins02 LIKE rmqam-aktiv,
         clinsp03 LIKE rmqam-art,
         actins03 LIKE rmqam-aktiv,
         clinsp04 LIKE rmqam-art,
         actins04 LIKE rmqam-aktiv,
         clinsp05 LIKE rmqam-art,
         actins05 LIKE rmqam-aktiv,
         clinsp06 LIKE rmqam-art,
         actins06 LIKE rmqam-aktiv,
         clinsp07 LIKE rmqam-art,
         actins07 LIKE rmqam-aktiv,
         clinsp08 LIKE rmqam-art,
         actins08 LIKE rmqam-aktiv,
         clinsp09 LIKE rmqam-art,
         actins09 LIKE rmqam-aktiv,
         clinsp10 LIKE rmqam-art,
         actins10 LIKE rmqam-aktiv,
       END OF gs_data.
 DATA gt_data LIKE TABLE OF gs_data.

 DATA gt_qmat LIKE TABLE OF qmat.
 DATA gs_qmat LIKE LINE OF gt_qmat.

 DATA: BEGIN OF gs_header,
         colname TYPE scrtext_l,
       END OF gs_header.
 DATA gt_header LIKE TABLE OF gs_header.
 DATA gd_tfile LIKE ibipparms-path.

 TYPES: BEGIN OF ty_log,
          matnr   TYPE matnr,
          werks   TYPE werks,
          type    TYPE bapi_mtype,
          message TYPE bapi_msg,
        END OF ty_log.

 DATA gt_log TYPE TABLE OF ty_log.
 DATA gs_log LIKE LINE OF gt_log.

*alv data
 DATA gr_table  TYPE REF TO cl_salv_table.
 DATA: gr_selections TYPE REF TO cl_salv_selections,
       gt_rows       TYPE salv_t_row.
 DATA: gr_functions TYPE REF TO cl_salv_functions_list.

*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
 PARAMETERS: p_file TYPE string LOWER CASE OBLIGATORY.

*--------------------------------------------------------------------*
 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*--------------------------------------------------------------------*
   gd_tfile = p_file.

   CALL FUNCTION 'F4_FILENAME'
     EXPORTING
       field_name = 'P_FILE'
     IMPORTING
       file_name  = gd_tfile.

   MOVE gd_tfile TO p_file.
*--------------------------------------------------------------------*
 START-OF-SELECTION.
*--------------------------------------------------------------------*

   PERFORM upload.

*--------------------------------------------------------------------*
 END-OF-SELECTION.
*--------------------------------------------------------------------*
   IF gt_data[] IS INITIAL.
     WRITE:/ 'No se seleccionaron datos'.
     RETURN.
   ENDIF.

   PERFORM crear_vista_qm.

   PERFORM display_log.

*&---------------------------------------------------------------------*
*&      Form  upload
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
 FORM upload.

   CALL FUNCTION 'GUI_UPLOAD'
     EXPORTING
       filename                = p_file
       filetype                = 'DAT'
     TABLES
       data_tab                = gt_data
     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.
   IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ENDIF.


 ENDFORM.                    "upload
*&---------------------------------------------------------------------*
*&      Form  CREAR_VISTA_QM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
 FORM crear_vista_qm .


*Internal table to store messages returned from BAPI
   DATA:
   ws_return LIKE " To store status
   bapiret2 OCCURS 0 WITH HEADER LINE.

* Internal table to store Inspection details to pass
* as parameter to BAPI
   DATA:
     BEGIN OF ws_qmat OCCURS 0.
       INCLUDE STRUCTURE bapi1001004_qmat.
   DATA:
   END OF ws_qmat.
   DATA:
     ws_insp(2).
   DATA ls_tq34 LIKE tq34.

   DATA ld_cont(2) TYPE n.
   DATA ld_fieldname(40).
   FIELD-SYMBOLS <f1>.
   FIELD-SYMBOLS <f2>.
   LOOP AT gt_data INTO gs_data.

     DO 10 TIMES.
       ADD 1 TO ld_cont.

       CONCATENATE 'CLINSP' ld_cont INTO ld_fieldname.
       ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f1>.
       CONCATENATE 'ACTINS' ld_cont INTO ld_fieldname.
       ASSIGN COMPONENT ld_fieldname OF STRUCTURE gs_data TO <f2>.

       CHECK <f1> IS NOT INITIAL.

* Retrieving Quality Score procedure from TQ34 table
       SELECT SINGLE * INTO ls_tq34
       FROM tq34
       WHERE art = <f1>.
       CHECK sy-subrc = 0.

       ws_qmat-insptype = <f1>.
       ws_qmat-material = gs_data-matnr.
       ws_qmat-plant = gs_data-werks.

       ws_qmat-ind_insp_with_tsk_list = ls_tq34-ppl.
       ws_qmat-ind_spec_matspec = ls_tq34-spezueber.
       ws_qmat-ind_spec_config = ls_tq34-conf.
       ws_qmat-ind_spec_batch = ls_tq34-tls.
       ws_qmat-ind_auto_assign = ls_tq34-app.
       ws_qmat-ind_insp_by_charac = ls_tq34-mer.
       ws_qmat-ind_post_to_insp_stock = ls_tq34-insmk.
       ws_qmat-ind_automatic_ud = ls_tq34-ave.
       ws_qmat-sampling_procedure = ls_tq34-stichprver.
       ws_qmat-dyn_modif_rule = ls_tq34-dynregel.
       ws_qmat-insp_percentage = ls_tq34-sproz.
       ws_qmat-ind_100_percent_inspection = ls_tq34-hpz.
       ws_qmat-ind_skips_allowed = ls_tq34-dyn.
       ws_qmat-ind_manual_sample = ls_tq34-mpb.
       ws_qmat-ind_manual_sample_calc = ls_tq34-mst.
       ws_qmat-ind_single_units_possible = ls_tq34-ein.
       ws_qmat-ave_insp_duration = ls_tq34-mpdau.
       ws_qmat-contr_insp_lot_create = ls_tq34-chg.
       ws_qmat-qual_score_procedure = ls_tq34-qkzverf.
       ws_qmat-allowed_scrap_share = ls_tq34-qpmat.
       ws_qmat-ind_hu_inspection = ls_tq34-afr.
       ws_qmat-ms_flag = ls_tq34-ms_flag.

       ws_qmat-ind_insptype_mat_active = <f2>.

       APPEND ws_qmat.
     ENDDO.

     CALL FUNCTION 'BAPI_MATINSPCTRL_SAVEREPLICA'
       TABLES
         return         = ws_return
         inspectionctrl = ws_qmat.

     IF sy-subrc NE 0.
* No sy-subrc check is required
     ENDIF. " IF SY-SUBRC NE 0

     MOVE-CORRESPONDING gs_data TO gs_log.

     LOOP AT ws_return WHERE type = 'E'.
     ENDLOOP.
     IF sy-subrc <> 0.
* To commit the transation for succesfull records
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
         EXPORTING
           wait = 'X'.
*      write:/ gs_data-matnr,
*              'Se creo vista calidad para material'.
       gs_log-type = 'S'.
       gs_log-message = 'Se creo vista calidad para material'.
     ELSE.
       gs_log-type = 'E'.
       gs_log-message = ws_return-message .
     ENDIF.
     APPEND gs_log TO gt_log.

   ENDLOOP.

 ENDFORM.                    " CREAR_VISTA_QM

*&---------------------------------------------------------------------*
*&      Form  display_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
 FORM display_log.
   DATA: lr_events  TYPE REF TO cl_salv_events_table,
         lr_columns TYPE REF TO cl_salv_columns_table,
         lr_column  TYPE REF TO cl_salv_column,
         lr_grid    TYPE REF TO cl_salv_form_layout_grid,
         lr_layout  TYPE REF TO cl_salv_layout,
         ls_key     TYPE salv_s_layout_key,
         lr_sorts   TYPE REF TO cl_salv_sorts,
         lr_sort    TYPE REF TO cl_salv_sort.

   TRY.
*... Create Instance
       CALL METHOD cl_salv_table=>factory
         IMPORTING
           r_salv_table = gr_table
         CHANGING
           t_table      = gt_log.
     CATCH cx_salv_msg.
   ENDTRY.

*... Enable Generic ALV functions
   gr_functions = gr_table->get_functions( ).
**  gr_functions->set_default( ).
   gr_functions->set_all( ).

*... Display table
   gr_table->display( ).

 ENDFORM.                    "display_log