SAP MM BAPI MATERIAL QMAT

From SapWiki
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