SAP PM BAPI IE03

From SapWiki
*&---------------------------------------------------------------------*
*& Report  ZQM_EXTDAT_equi
*&
*&---------------------------------------------------------------------*
*& Descripcion   : Extractor de Datos Equipos
*&
*&---------------------------------------------------------------------*

REPORT  zqm_extdat_equi.
*--------------------------------------------------------------------*
TYPE-POOLS : slis.

DATA decstel(2) TYPE c.
DATA: BEGIN OF gs_data,
*        FUNCTLOCATION type bapi_itob_fl_sel_result-FUNCTLOCATION, "
        equipment  TYPE equnr,           "N° de equipo
*        EQUICATGRY type BAPI_ITOB_EQ_ONLY-EQUICATGRY, "Tipo de equipo
*STRIND type BAPI_ITOB-STRIND, " Indicador de estructura de ubicación técnica
        equicatgry TYPE bapi_itob_eq_only-equicatgry, " Tipo de equipo
        descript   TYPE bapi_itob-descript, " Denominación de la ubicación técnic
        authgrp    TYPE bapi_itob-authgrp , " Grupo de autorizaciones para objeto técnico
        obj_weight TYPE bapi_itob-obj_weight , " Peso del objeto
        unit_of_wt TYPE bapi_itob-unit_of_wt, " Unidad de peso
        obj_size   TYPE bapi_itob-obj_size , " Tamaño/Dimensión
        inventory  TYPE bapi_itob-inventory, " Número de inventario
        start_from TYPE bapi_itob-start_from, " Fecha de puesta en servicio de objeto técnico
        objecttype TYPE bapi_itob-objecttype, " Clase de objeto técnico
*       descript    type bapi_itob-descript,   "denominación   "!!!
        acquisval  TYPE bapi_itob-acquisval , " valor de adquisición
        currency   TYPE bapi_itob-currency ,  " clave de moneda
        acqdate    TYPE bapi_itob-acqdate ,   " fecha de adquisición
        manfacture TYPE bapi_itob-manfacture, " fabricante del activo fijo
        mancountry TYPE bapi_itob-mancountry, " país de fabricación
        manmodel   TYPE bapi_itob-manmodel,   " denominación de tipo del fabricante
        constyear  TYPE bapi_itob-constyear,  " año de construcción
        constmonth TYPE bapi_itob-constmonth, " mes de construcción
        manparno   TYPE bapi_itob-manparno,   " número de pieza de fabricante
        manserno   TYPE bapi_itob-manserno,   " número de serie según el fabricante
        maintplant TYPE bapi_itob-maintplant, " centro de emplazamiento
        maintloc   TYPE bapi_itob-maintloc  , "  emplazamiento del objeto de mantenimiento
        maintroom  TYPE bapi_itob-maintroom,  " local
        plsectn    TYPE bapi_itob-plsectn,    "  Área de empresa
        work_ctr   TYPE bapi_itob-work_ctr,   " puesto de trabajo
        abcindic   TYPE bapi_itob-abcindic,   " indicador abc para objeto técnico
        sortfield  TYPE bapi_itob-sortfield,  " campo de clasificación
        comp_code  TYPE bapi_itob-comp_code,  " sociedad
        bus_area   TYPE bapi_itob-bus_area,   " división
        asset_no   TYPE bapi_itob-asset_no ,  " número principal de activo fijo
        sub_number TYPE bapi_itob-sub_number, " subnúmero de activo fijo
        costcenter TYPE bapi_itob-costcenter, " centro de coste
        wbs_elem   TYPE bapi_itob-wbs_elem,   " elemento del plan de estructura de proyecto (elemento pep)
        standorder TYPE bapi_itob-standorder, " número de orden permanente
        settlorder TYPE bapi_itob-settlorder, " orden de liquidación
        planplant  TYPE bapi_itob-planplant , " centro de planificación del mantenimiento
        plangroup  TYPE bapi_itob-plangroup , " grupo planificador p.servicio cliente y mantenimiento
        pp_wkctr   TYPE bapi_itob-pp_wkctr,   " puesto de trabajo responsable en medidas de mantenimiento
        catprofile TYPE bapi_itob-catprofile, "  perfil de catálogo
*        eqinstall   type bapi_itob_fl_only-eqinstall, " montaje de equipos permitido en ubicación técnica
        read_supeq TYPE bapi_itob_eq_only-read_supeq,  "equipo superior
        read_floc  TYPE bapi_itob_eq_only-read_floc,   "Identificación de ubicación técnica
        inst_pos   TYPE bapi_itob_eq_only-inst_pos, "Posición de equipo en lugar de montaje (eq.sup./ubic.técn.)
      END OF gs_data.
DATA gt_data LIKE TABLE OF gs_data.

DATA: BEGIN OF gs_header,
        colname TYPE scrtext_l,
      END OF gs_header.
DATA gt_header LIKE TABLE OF gs_header.


DATA gt_equipos LIKE TABLE OF bapi_itob_eq_sel_result.
DATA gs_equipos LIKE LINE OF gt_equipos.


DATA: gt_qpmk LIKE TABLE OF qpmk,
      gs_qpmk LIKE LINE OF gt_qpmk.

DATA gd_tfile TYPE ibipparms-path.

DATA: ls_itob_sel_maintplant LIKE TABLE OF  bapi_itob_sel_maintplant WITH HEADER LINE.
DATA: ls_bapiret2  LIKE TABLE OF bapiret2 WITH HEADER LINE.
DATA: ls_itob_fl_sel_result LIKE TABLE OF  bapi_itob_fl_sel_result WITH HEADER LINE.
DATA:  es_bapi_itob LIKE TABLE OF bapi_itob WITH HEADER LINE.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
SELECT-OPTIONS: s_equnr FOR gs_data-equipment,
                s_swerk FOR gs_data-maintplant.
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 select_data.

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

  PERFORM download.

*&---------------------------------------------------------------------*
*&      Form  select_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_data.

  CALL FUNCTION 'BAPI_EQUI_GETLIST'
    TABLES
      equipment_ra   = s_equnr
      maintplant_ra  = s_swerk
      equipment_list = gt_equipos.

  DATA: ls_data_general_exp  LIKE  bapi_itob,
        ls_data_specific_exp LIKE  bapi_itob_eq_only.
  DATA ld_monto TYPE  bapicurr-bapicurr.

  LOOP AT  gt_equipos INTO gs_equipos.
    CALL FUNCTION 'BAPI_EQUI_GETDETAIL'
      EXPORTING
        equipment         = gs_equipos-equipment
      IMPORTING
        data_general_exp  = ls_data_general_exp
        data_specific_exp = ls_data_specific_exp
*       DATA_FLEET_EXP    =
*       RETURN            =
      .

    MOVE-CORRESPONDING ls_data_general_exp TO gs_data.
    CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
      EXPORTING
        currency        = ls_data_general_exp-currency
        amount_internal = ls_data_general_exp-acquisval
      IMPORTING
        amount_external = ld_monto.

    MOVE ld_monto TO gs_data-acquisval.
    gs_data-read_supeq  = ls_data_specific_exp-read_supeq.
    gs_data-read_floc = ls_data_specific_exp-read_floc.
    gs_data-inst_pos  = ls_data_specific_exp-inst_pos.
    gs_data-equicatgry =   ls_data_specific_exp-equicatgry.
    gs_data-equipment = gs_equipos-equipment.

    APPEND gs_data TO gt_data.
  ENDLOOP.

ENDFORM.                    "select_data
*&---------------------------------------------------------------------*
*&      Form  download
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM download.

  PERFORM crear_header_2 USING gs_data CHANGING gt_header[].

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = p_file
      filetype                = 'DAT'
    TABLES
      data_tab                = gt_data
      fieldnames              = gt_header
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      OTHERS                  = 22.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    MESSAGE i000(fb) WITH 'Se genero archivo'
                          p_file.
  ENDIF.
ENDFORM.                    "download

*&---------------------------------------------------------------------*
*&      Form  crear_header
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM crear_header.

  CLEAR gt_header[].

  DATA: l_index(2) TYPE n,
        l_string   TYPE string,
        l_0033     TYPE p0033.
  FIELD-SYMBOLS <fs> TYPE any.
  DATA: descr_struc_ref TYPE REF TO cl_abap_structdescr,
        lt_components   TYPE abap_compdescr_tab,
        ls_components   LIKE LINE OF lt_components.
*descr_struc_ref ?= cl_abap_typedescr=>describe_by_name( gs_data ).
*lt_components = descr_struc_ref->components.
  DATA comp_tab TYPE abap_compdescr_tab.
  DATA stru_ref TYPE REF TO cl_abap_structdescr.
  DATA type_ref TYPE REF TO cl_abap_typedescr.
  DATA is_ddic TYPE abap_bool.
  DATA: lt_ddic TYPE dd_x031l_table,
        wa_ddic TYPE x031l,
        lt_fcat TYPE slis_t_fieldcat_alv,
        wa_fcat TYPE slis_fieldcat_alv,
        ls_layo TYPE slis_layout_alv,
        l_alv   TYPE REF TO cl_gui_alv_grid.

  DATA: one_comp TYPE abap_compdescr,
        one_name TYPE string.

  stru_ref ?= cl_abap_structdescr=>describe_by_data( gs_data ).

  comp_tab = stru_ref->components.

  FIELD-SYMBOLS <f3> TYPE any.
  DATA gd_field TYPE string.

  LOOP AT comp_tab INTO one_comp.
    CONCATENATE 'GS_DATA-' one_comp-name INTO gd_field.

    ASSIGN (gd_field) TO <f3>.

    type_ref ?= cl_abap_typedescr=>describe_by_data( <f3> ).
    is_ddic = type_ref->is_ddic_type( ).

    IF is_ddic = abap_true.
      lt_ddic = type_ref->get_ddic_object( ).

      LOOP AT lt_ddic INTO wa_ddic.
        CLEAR wa_ddic-tabname.
*        SELECT SINGLE
*        dd03l~tabname
*        INTO wa_ddic-tabname
*        FROM dd03l
*        WHERE dd03l~fieldname = wa_ddic-fieldname
*        AND dd03l~tabname NOT LIKE '/%'.

        SELECT SINGLE dd04t~scrtext_s
                      dd04t~scrtext_m
                      dd04t~scrtext_l
        INTO (wa_fcat-seltext_s, wa_fcat-seltext_m, wa_fcat-seltext_l) FROM dd04t
        WHERE dd04t~rollname = wa_ddic-fieldname
          AND dd04t~ddlanguage = sy-langu.

        APPEND wa_fcat TO lt_fcat.
        CONCATENATE wa_fcat-seltext_l wa_ddic-fieldname INTO gd_field  SEPARATED BY space.
        APPEND gd_field TO gt_header.
      ENDLOOP.
    ELSE.
      APPEND one_comp-name TO gt_header.
    ENDIF.

  ENDLOOP.

ENDFORM.                    "crear_header


*&---------------------------------------------------------------------*
*&      Form  read_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM read_text.

  DATA l_v_name TYPE thead-tdname.   " Declare l_v_name as this type
  DATA l_fs_line TYPE tline.
  DATA l_it_line LIKE TABLE OF l_fs_line.

  CONCATENATE gs_qpmk-mandt
              gs_qpmk-zaehler
              gs_qpmk-mkmnr
              gs_qpmk-version
              sy-langu INTO l_v_name RESPECTING BLANKS.

  REFRESH l_it_line.
  CALL FUNCTION 'READ_TEXT'     "get long text
    EXPORTING
      client   = sy-mandt
      id       = 'QPMT'
      language = sy-langu
      name     = l_v_name
      object   = 'QPMERKAL'
    TABLES
      lines    = l_it_line.

ENDFORM.                    "read_text

FORM crear_header_2 USING  VALUE(i_data)
                 CHANGING i_header TYPE ANY TABLE.
  DATA lt_text TYPE pt_t_text.
  DATA ls_text LIKE LINE OF lt_text.

  FIELD-SYMBOLS <wa>.
  ASSIGN i_data TO <wa>.

  DATA: l_index(2) TYPE n,
        l_string   TYPE string,
        l_0033     TYPE p0033.
  FIELD-SYMBOLS <fs> TYPE any.
  DATA: descr_struc_ref TYPE REF TO cl_abap_structdescr,
        lt_components   TYPE abap_compdescr_tab,
        ls_components   LIKE LINE OF lt_components.
*descr_struc_ref ?= cl_abap_typedescr=>describe_by_name( gs_data ).
*lt_components = descr_struc_ref->components.
  DATA comp_tab TYPE abap_compdescr_tab.
  DATA stru_ref TYPE REF TO cl_abap_structdescr.
  DATA type_ref TYPE REF TO cl_abap_typedescr.
  DATA is_ddic TYPE abap_bool.
  DATA: lt_ddic TYPE dd_x031l_table,
        wa_ddic TYPE x031l,
        lt_fcat TYPE slis_t_fieldcat_alv,
        wa_fcat TYPE slis_fieldcat_alv,
        ls_layo TYPE slis_layout_alv,
        l_alv   TYPE REF TO cl_gui_alv_grid.

  DATA: one_comp TYPE abap_compdescr,
        one_name TYPE string.

  stru_ref ?= cl_abap_structdescr=>describe_by_data( <wa> ).

  comp_tab = stru_ref->components.

  FIELD-SYMBOLS <f3> TYPE any.
  DATA gd_field TYPE string.

  LOOP AT comp_tab INTO one_comp.
    CONCATENATE '<WA>-' one_comp-name INTO gd_field.

    ASSIGN (gd_field) TO <f3>.

    type_ref ?= cl_abap_typedescr=>describe_by_data( <f3> ).
    is_ddic = type_ref->is_ddic_type( ).

    IF is_ddic = abap_true.
      lt_ddic = type_ref->get_ddic_object( ).

      LOOP AT lt_ddic INTO wa_ddic.
        CLEAR wa_ddic-tabname.

        SELECT SINGLE dd04t~scrtext_s
                      dd04t~scrtext_m
                      dd04t~scrtext_l
                INTO (wa_fcat-seltext_s, wa_fcat-seltext_m, wa_fcat-seltext_l) FROM dd04t
               WHERE dd04t~rollname = wa_ddic-fieldname
                 AND dd04t~ddlanguage = sy-langu.

        APPEND wa_fcat TO lt_fcat.
        CONCATENATE wa_fcat-seltext_l wa_ddic-fieldname INTO gd_field  SEPARATED BY space.
        APPEND gd_field TO lt_text.
      ENDLOOP.
    ELSE.
      APPEND one_comp-name TO lt_text.
    ENDIF.

  ENDLOOP.

  i_header[] = lt_text[].
ENDFORM.                    "crear_header