SAP PM BAPI IE01

From SapWiki
Revision as of 22:14, 4 April 2020 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
*&---------------------------------------------------------------------*
*& Report  ZQM_CREAR_EQUI
*&
*&---------------------------------------------------------------------*
*& Descripcion   : Creación de Equipos
*&
*&---------------------------------------------------------------------*
REPORT  zpm_crear_equi.
*--------------------------------------------------------------------*
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 arbpl, "bapi_itob-work_ctr,   " puesto de trabajo
        pp_wkctr   TYPE arbpl, "bapi_itob-pp_wkctr,   " puesto de trabajo responsable en medidas de mantenimiento
        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 arbpl, "bapi_itob-pp_wkctr,   " puesto de trabajo responsable en medidas de mantenimiento
        work_ctr   TYPE arbpl, "bapi_itob-work_ctr,   " puesto de trabajo
        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.)
        classnum   TYPE klasse_d,
      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 decstel(2) TYPE c.

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

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.

TYPES: BEGIN OF ty_log,
         equipment   TYPE equnr,
         equipment_o TYPE equnr,
         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.

DATA gt_return LIKE TABLE OF bapiret2.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
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_equipos.

  PERFORM display_log.

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

  DATA: ls_data_general_exp  LIKE  bapi_itob,
        ls_data_specific_exp LIKE  bapi_itob_eq_only,
        ls_data_install      LIKE  bapi_itob_eq_install.

  DATA ld_monto TYPE  bapicurr-bapicurr.
  DATA ld_equipo TYPE equnr.

  LOOP AT  gt_data INTO gs_data.
    CLEAR: ls_data_specific_exp,
           ls_data_general_exp,
           ls_data_install,
           ls_bapiret2.

    SELECT   SINGLE objid INTO gs_data-work_ctr FROM  crhd
           WHERE  objty  = 'A'
           AND    arbpl  = gs_data-work_ctr
           AND    werks = gs_data-planplant.
    IF sy-subrc <> 0.
      CLEAR gs_data-work_ctr.
    ENDIF.

    SELECT   SINGLE objid INTO gs_data-pp_wkctr FROM  crhd
           WHERE  objty  = 'A'
           AND    arbpl  = gs_data-pp_wkctr
           AND    werks  = gs_data-maintplant.
    IF sy-subrc <> 0.
      CLEAR gs_data-pp_wkctr.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = gs_data-costcenter
      IMPORTING
        output = gs_data-costcenter.

    MOVE-CORRESPONDING gs_data TO ls_data_general_exp.
    MOVE gs_data-read_supeq TO ls_data_specific_exp-read_supeq.
    MOVE gs_data-read_floc TO ls_data_specific_exp-read_floc.
    MOVE gs_data-inst_pos  TO ls_data_specific_exp-inst_pos.
    MOVE gs_data-equicatgry TO   ls_data_specific_exp-equicatgry.
*    gs_data-EQUIPMENT = gs_equipos-EQUIPMENT.

    MOVE gs_data-read_supeq TO ls_data_install-supequi.
    MOVE gs_data-read_floc TO ls_data_install-funcloc.
    MOVE gs_data-inst_pos  TO ls_data_install-inst_pos.

    MOVE gs_data-acquisval TO ld_monto.
    CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_INTERNAL'
      EXPORTING
        currency             = gs_data-currency
        amount_external      = ld_monto
        max_number_of_digits = '15'
      IMPORTING
        amount_internal      = ls_data_general_exp-acquisval
*       RETURN               =
      .

    CALL FUNCTION 'BAPI_EQUI_CREATE'
      EXPORTING
*       EXTERNAL_NUMBER         =
        data_general  = ls_data_general_exp
        data_specific = ls_data_specific_exp
*       DATA_FLEET    =
*       VALID_DATE    = SY-DATUM
        data_install  = ls_data_install
      IMPORTING
        equipment     = ld_equipo
*       DATA_GENERAL_EXP        =
*       DATA_SPECIFIC_EXP       =
*       DATA_FLEET_EXP          =
        return        = ls_bapiret2.
    IF ld_equipo IS NOT INITIAL.
      CLEAR gt_return[].

      IF gs_data-classnum IS NOT INITIAL.
        DATA ld_object TYPE bapi1003_key-object.

        ld_object = ld_equipo.

        CALL FUNCTION 'BAPI_OBJCL_CREATE'
          EXPORTING
            objectkeynew   = ld_object
            objecttablenew = 'EQUI'
            classnumnew    = gs_data-classnum
            classtypenew   = '002'
*           STATUS         = '1'
*           STANDARDCLASS  =
*           CHANGENUMBER   =
*           KEYDATE        = SY-DATUM
*           NO_DEFAULT_VALUES       = ' '
*       IMPORTING
*           CLASSIF_STATUS =
          TABLES
*           ALLOCVALUESNUM =
*           ALLOCVALUESCHAR         =
*           ALLOCVALUESCURR         =
            return         = gt_return.
      ENDIF.
      LOOP AT gt_return INTO ls_bapiret2 WHERE type = 'E'.
      ENDLOOP.
      IF sy-subrc <> 0.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'
*       IMPORTING
*           RETURN        =
          .
*         write:/ 'Se creo equipo',
*                  ld_equipo,
*                  'copia de',
*                  gs_data-EQUIPMENT.
        gs_log-equipment = ld_equipo.
        gs_log-equipment_o = gs_data-equipment.
        gs_log-type = 'S'.
        gs_log-message = 'Se creo equipo'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        gs_log-equipment = ld_equipo.
        gs_log-equipment_o = gs_data-equipment.
        gs_log-type = 'E'.
        gs_log-message = ls_bapiret2-message.
      ENDIF.
    ELSE.
      gs_log-equipment = ld_equipo.
      gs_log-equipment_o = gs_data-equipment.
      gs_log-type = 'E'.
      gs_log-message = ls_bapiret2-message.
*          write:/ ls_bapiret2-message.
    ENDIF.
    APPEND gs_log TO gt_log.
  ENDLOOP.

ENDFORM.                    "select_data

*&---------------------------------------------------------------------*
*&      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  display_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_log.
*... Create Instance
  TRY.
    cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table)
                             CHANGING t_table = gt_log ).
    CATCH cx_salv_msg.
  ENDTRY.

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

  data(lr_columns) = gr_table->get_columns( ).

* set column text
  TRY.
      data(lr_column) = lr_columns->get_column( 'EQUIPMENT_O' ).
      lr_column->set_long_text( 'Equipo Original' ).
      lr_column->set_medium_text( 'Equi.Ori.' ).
      lr_column->set_short_text( 'Equi.Ori.' ).
    CATCH cx_salv_not_found.
    CATCH cx_salv_existing.
    CATCH cx_salv_data_error.
  ENDTRY.

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

ENDFORM.                    "display_log