SAP PM BAPI CA01

From SapWiki
Revision as of 16:00, 5 April 2020 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
*&---------------------------------------------------------------------*
*& Report  ZPP_CREAR_HR
*&
*&---------------------------------------------------------------------*
*& Descripcion   : Creación de Hojas de Ruta (CA01)
*&---------------------------------------------------------------------*

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

DATA: BEGIN OF gs_data,
        material                       LIKE bapi1012_mtk_c-material,         "Material
        plant                          LIKE bapi1012_mtk_c-plant,            "Centro
        task_list_group                LIKE bapi1012_tsk_c-task_list_group,  "Grp. Hoja de Ruta
        group_counter                  LIKE bapi1012_tsk_c-group_counter,    "Contador
        ident_key                      LIKE bapi1012_tsk_c-ident_key,        "Pto. Inspección
        insppoint_partiallot_assgnmt   LIKE bapi1012_tsk_c-insppoint_partiallot_assgnmt, "Datos Parciales
        dyn_modif_level                LIKE bapi1012_tsk_c-dyn_modif_level,  "Nivel Dinamizacion
        activity                       LIKE bapi1012_opr_c-activity,         "Numero de Operacion
        work_cntr                      LIKE bapi1012_opr_c-work_cntr,        "Puesto de Trabajo
        control_key                    LIKE bapi1012_opr_c-control_key,      "Clave de Control
        description                    LIKE bapi1012_opr_c-description,      "Descrip. Operación
        insp_point_complt_flow_variant LIKE bapi1012_opr_c-insp_point_complt_flow_variant, "Variante de Proceso
        time_grid                      LIKE bapi1012_opr_c-time_grid,        "Secuencial Hora
        time_factor                    LIKE bapi1012_opr_c-time_factor,      " Factor de Tiempo
        time_unit                      LIKE bapi1012_opr_c-time_unit,        "Unidad de Tiempo
        denominator                    LIKE bapi1012_opr_c-denominator,      "Denominador para conversión u.medida hoja ruta y operación
        nominator                      LIKE bapi1012_opr_c-nominator,        "Contador para conversión u.medida hoja de ruta y operación
        base_quantity                  LIKE bapi1012_opr_c-base_quantity,    "Cantidad base
        inspchar                       LIKE bapi1012_cha_c-inspchar,         "Numero de Caracteristica
        char_descr                     LIKE bapi1012_cha_c-char_descr,       "Texto breve para característica inspección
        mstr_char                      LIKE bapi1012_cha_c-mstr_char,        "Caracteristica
        smpl_procedure                 LIKE bapi1012_cha_c-smpl_procedure,   "Procedimiento
        smpl_unit                      LIKE bapi1012_cha_c-smpl_unit,        "Unidad de Medida
        smpl_quant                     LIKE bapi1012_cha_c-smpl_quant,       "Cantidad para muestra
        dyn_modif_rule                 LIKE bapi1012_cha_c-dyn_modif_rule,   "Regla Dinamizacion
        spc_criterion_key              LIKE bapi1012_cha_c-spc_criterion_key, "Criterio SPC
        up_tol_lmt                     LIKE bapi1012_cha_c-up_tol_lmt,        "Tolerancia Superior
        lw_tol_lmt                     LIKE bapi1012_cha_c-lw_tol_lmt,        "Tolerancia Inferior
        formula_check_by_sap           LIKE bapi1012_cha_c-formula_check_by_sap, "Verificar y calcular fórmula en QM
        formula_field_1                LIKE bapi1012_cha_c-formula_field_1,
        formula_field_2                LIKE bapi1012_cha_c-formula_field_2,
        task_measure_unit              LIKE bapi1012_tsk_c-task_measure_unit,
      END OF gs_data.
DATA gt_data LIKE TABLE OF gs_data.

DATA: BEGIN OF gs_data_material,
        material                     LIKE bapi1012_mtk_c-material,            "Material
        plant                        LIKE bapi1012_mtk_c-plant,                  "Centro
        task_list_group              LIKE bapi1012_tsk_c-task_list_group, "Grp. Hoja de Ruta
        group_counter                LIKE bapi1012_tsk_c-group_counter,  "Contador
        ident_key                    LIKE bapi1012_tsk_c-ident_key,          "Pto. Inspección
        insppoint_partiallot_assgnmt LIKE bapi1012_tsk_c-insppoint_partiallot_assgnmt, "Datos Parciales
        dyn_modif_level              LIKE bapi1012_tsk_c-dyn_modif_level, "Nivel Dinamizacion
      END OF gs_data_material.

DATA: BEGIN OF gs_data_operacion,
        material                       LIKE bapi1012_mtk_c-material,            "Material
        plant                          LIKE bapi1012_mtk_c-plant,                  "Centro
        task_list_group                LIKE bapi1012_tsk_c-task_list_group, "Grp. Hoja de Ruta
        group_counter                  LIKE bapi1012_tsk_c-group_counter,  "Contador
        activity                       LIKE bapi1012_opr_c-activity,            "Numero de Operacion
        work_cntr                      LIKE bapi1012_opr_c-work_cntr,          "Puesto de Trabajo
        control_key                    LIKE bapi1012_opr_c-control_key,      "Clave de Control
        description                    LIKE bapi1012_opr_c-description,      "Descrip. Operación
        insp_point_complt_flow_variant LIKE bapi1012_opr_c-insp_point_complt_flow_variant, "Variante de Proceso
        time_grid                      LIKE bapi1012_opr_c-time_grid,          "Secuencial Hora
        time_factor                    LIKE bapi1012_opr_c-time_factor,      " Factor de Tiempo
        time_unit                      LIKE bapi1012_opr_c-time_unit,          "Unidad de Tiempo
        denominator                    LIKE bapi1012_opr_c-denominator,
        nominator                      LIKE bapi1012_opr_c-nominator,
        base_quantity                  LIKE bapi1012_opr_c-base_quantity,
      END OF gs_data_operacion.

DATA: BEGIN OF gs_data_cha,
        material             LIKE bapi1012_mtk_c-material,            "Material
        plant                LIKE bapi1012_mtk_c-plant,                  "Centro
        task_list_group      LIKE bapi1012_tsk_c-task_list_group, "Grp. Hoja de Ruta
        group_counter        LIKE bapi1012_tsk_c-group_counter,  "Contador
        activity             LIKE bapi1012_opr_c-activity,            "Numero de Operacion
        inspchar             LIKE bapi1012_cha_c-inspchar,            "Numero de Caracteristica
        char_descr           LIKE bapi1012_cha_c-char_descr,
        mstr_char            LIKE bapi1012_cha_c-mstr_char,          "Caracteristica
        smpl_procedure       LIKE bapi1012_cha_c-smpl_procedure, "Procedimiento
        smpl_unit            LIKE bapi1012_cha_c-smpl_unit,          "Unidad de Medida
        smpl_quant           LIKE bapi1012_cha_c-smpl_quant,        "Cantidad para muestra
        dyn_modif_rule       LIKE bapi1012_cha_c-dyn_modif_rule, "Regla Dinamizacion
        spc_criterion_key    LIKE bapi1012_cha_c-spc_criterion_key, "Criterio SPC
        up_tol_lmt           LIKE bapi1012_cha_c-up_tol_lmt,        "Tolerancia Superior
        lw_tol_lmt           LIKE bapi1012_cha_c-lw_tol_lmt,        "Tolerancia Inferior
        formula_check_by_sap LIKE bapi1012_cha_c-formula_check_by_sap,
        formula_field_1      LIKE bapi1012_cha_c-formula_field_1,
        formula_field_2      LIKE bapi1012_cha_c-formula_field_2,
      END OF gs_data_cha.

DATA gt_data_material LIKE TABLE OF gs_data_material.
DATA gt_data_operacion LIKE TABLE OF gs_data_operacion.
DATA gt_data_cha LIKE TABLE OF gs_data_cha.

DATA decstel(2) TYPE c.

DATA gd_tfile TYPE ibipparms-path.

TYPES: BEGIN OF ty_log,
         material        LIKE bapi1012_mtk_c-material,            "Material
         plant           LIKE bapi1012_mtk_c-plant,                  "Centro
         task_list_group LIKE bapi1012_tsk_c-task_list_group, "Grp. Hoja de Ruta
         group_counter   LIKE bapi1012_tsk_c-group_counter,  "Contador
         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.

*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
PARAMETERS: p_file TYPE string LOWER CASE OBLIGATORY,
            p_test TYPE bapiflag DEFAULT 'X'.

*--------------------------------------------------------------------*
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_hoja_de_ruta.

  PERFORM display_log.
*&---------------------------------------------------------------------*
*&      Form  get_task_list_group
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_task_list_group USING i_change
                               gs_bapi_mtk TYPE bapi1012_mtk_c
                                gs_bapi_tsk  TYPE bapi1012_tsk_c.

  DATA: BEGIN OF ls_h,
          valid_to TYPE sydatum,
          lin      TYPE i,
        END OF ls_h,
        ls_mtk_int TYPE maplb,
        lt_mtk_int TYPE TABLE OF maplb,
        ls_tsk_int TYPE plko.

  ls_h-valid_to = sy-datum.
* read material task allocation
  CALL FUNCTION 'CP_DB_MAPL_READ'
    EXPORTING
      datub      = ls_h-valid_to
      datuv      = gs_bapi_mtk-valid_from
      matnr      = gs_bapi_mtk-material
      werks      = gs_bapi_mtk-plant
    TABLES
      matpl      = lt_mtk_int
    EXCEPTIONS
      no_records = 1
      OTHERS     = 2.
  CHECK: sy-subrc = 0.
* delete deleted material task allocations
  DELETE lt_mtk_int WHERE loekz = 'X'.
*  IF gs_cntrl-flg_change_mode = const-flg_yes.
  IF i_change = 'X'.
    DELETE lt_mtk_int WHERE plnal <> gs_bapi_mtk-group_counter.
  ENDIF.
  DESCRIBE TABLE lt_mtk_int LINES ls_h-lin.
  IF ls_h-lin = 1 OR i_change IS INITIAL.
    READ TABLE lt_mtk_int INTO ls_mtk_int INDEX 1.
    CHECK: sy-subrc = 0.
*    gs_cntrl-task_list_group = ls_mtk_int-plnnr.
  ELSE.
    SORT lt_mtk_int BY datuv DESCENDING.
    LOOP AT lt_mtk_int INTO ls_mtk_int.
* read task list header
      CALL FUNCTION 'CI03_READ_PLKO'
        EXPORTING
          i_plnty         = ls_mtk_int-plnty
          i_plnnr         = ls_mtk_int-plnnr
          i_plnal         = ls_mtk_int-plnal
          i_date          = ls_mtk_int-datuv
        IMPORTING
          e_plko          = ls_tsk_int
        EXCEPTIONS
          tl_not_existent = 1
          tl_not_valid    = 2
          OTHERS          = 3.
      CHECK: sy-subrc = 0.
      IF ls_tsk_int-verwe = gs_bapi_tsk-task_list_usage.
*        gs_cntrl-task_list_group = ls_tsk_int-plnnr.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDIF.

ENDFORM.                    " get_task_list_group
*&---------------------------------------------------------------------*
*&      Form  select_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM crear_hoja_de_ruta.

  DATA lt_materialtaskallocation TYPE TABLE OF bapi1012_mtk_c .
  DATA lt_task TYPE TABLE OF bapi1012_tsk_c.
  DATA lt_operation TYPE TABLE OF bapi1012_opr_c.
  DATA lt_inspcharacteristic TYPE TABLE OF bapi1012_cha_c.

  DATA ls_materialtaskallocation TYPE bapi1012_mtk_c .
  DATA ls_task TYPE bapi1012_tsk_c.
  DATA ls_operation TYPE bapi1012_opr_c.
  DATA ls_inspcharacteristic TYPE bapi1012_cha_c.

  DATA lt_return TYPE TABLE OF bapiret2.
  DATA ls_return LIKE LINE OF lt_return.


  LOOP AT gt_data INTO gs_data.
    CLEAR: gt_data_material[].
    CLEAR: gt_data_operacion[].
    CLEAR: gt_data_cha[].

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

    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = gs_data-material
      IMPORTING
        output       = gs_data-material
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.
    IF sy-subrc <> 0.
      CLEAR gs_data-material.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_NUMCV_INPUT'
      EXPORTING
        input  = gs_data-activity
      IMPORTING
        output = gs_data-activity.

    MOVE-CORRESPONDING gs_data TO gs_data_material.
    MOVE-CORRESPONDING gs_data TO gs_data_operacion.
    MOVE-CORRESPONDING gs_data TO gs_data_cha.

    APPEND gs_data_material TO gt_data_material.
    APPEND gs_data_operacion TO gt_data_operacion.
    APPEND gs_data_cha TO gt_data_cha.
*  ENDLOOP.


    LOOP AT gt_data_material INTO gs_data_material.
      CLEAR: lt_operation[],
             lt_inspcharacteristic[],
             lt_materialtaskallocation[],
             lt_task[],
             lt_return[].

*    clear gs_data-TASK_LIST_GROUP.
*    gs_data-GROUP_COUNTER = 2.

      MOVE-CORRESPONDING gs_data TO ls_materialtaskallocation.
      IF ls_materialtaskallocation-material IS NOT INITIAL.
        APPEND ls_materialtaskallocation TO lt_materialtaskallocation.
      ENDIF.

      MOVE-CORRESPONDING gs_data TO ls_task.
      ls_task-task_list_usage = '1'.
      ls_task-task_list_status = '4'.

      ls_task-valid_from = sy-datum.
      ls_task-valid_to_date = '99991231'.

*    ls_task-TASK_MEASURE_UNIT = 'KI'.  "CA
      APPEND ls_task TO lt_task.


      LOOP AT gt_data_operacion INTO gs_data_operacion WHERE
           material =  gs_data_material-material
           AND plant    = gs_data_material-plant
           AND task_list_group = gs_data_material-task_list_group
           AND group_counter  = gs_data_material-group_counter
        AND activity IS NOT INITIAL.

        ls_operation-task_list_group = gs_data_material-task_list_group.
        ls_operation-group_counter   = gs_data_material-group_counter.
        ls_operation-activity = gs_data_operacion-activity.
        ls_operation-work_cntr  = gs_data_operacion-work_cntr.
        ls_operation-control_key  = gs_data_operacion-control_key.
        ls_operation-description  = gs_data_operacion-description.
        ls_operation-insp_point_complt_flow_variant  = gs_data_operacion-insp_point_complt_flow_variant.
        ls_operation-time_grid  = gs_data_operacion-time_grid.
        ls_operation-time_factor  = gs_data_operacion-time_factor.
        ls_operation-time_unit  = gs_data_operacion-time_unit.
        ls_operation-denominator  = gs_data_operacion-denominator.
        ls_operation-nominator   = gs_data_operacion-nominator.
        ls_operation-base_quantity = gs_data_operacion-base_quantity.

        APPEND ls_operation TO lt_operation.

        LOOP AT gt_data_cha INTO gs_data_cha WHERE
                 material =  gs_data_material-material
                 AND plant    = gs_data_material-plant
                 AND task_list_group = gs_data_material-task_list_group
                 AND group_counter  = gs_data_material-group_counter
                 AND activity = gs_data_operacion-activity
                 AND inspchar IS NOT INITIAL.

          ls_inspcharacteristic-task_list_group = gs_data_material-task_list_group.
          ls_inspcharacteristic-group_counter   = gs_data_material-group_counter.
          ls_inspcharacteristic-activity = gs_data_cha-activity.
          ls_inspcharacteristic-inspchar  = gs_data_cha-inspchar.
          ls_inspcharacteristic-mstr_char  = gs_data_cha-mstr_char.
          ls_inspcharacteristic-smpl_procedure  = gs_data_cha-smpl_procedure.
          ls_inspcharacteristic-smpl_unit  = gs_data_cha-smpl_unit.
          ls_inspcharacteristic-smpl_quant  = gs_data_cha-smpl_quant.
          ls_inspcharacteristic-dyn_modif_rule  = gs_data_cha-dyn_modif_rule.
          ls_inspcharacteristic-spc_criterion_key  = gs_data_cha-spc_criterion_key .
          ls_inspcharacteristic-up_tol_lmt  = gs_data_cha-up_tol_lmt.
          ls_inspcharacteristic-lw_tol_lmt  = gs_data_cha-lw_tol_lmt.
          ls_inspcharacteristic-formula_check_by_sap  = gs_data_cha-formula_check_by_sap.
          ls_inspcharacteristic-formula_field_1  = gs_data_cha-formula_field_1 .
          ls_inspcharacteristic-formula_field_2  = gs_data_cha-formula_field_2 .
          ls_inspcharacteristic-char_descr = gs_data_cha-char_descr.

          APPEND ls_inspcharacteristic TO lt_inspcharacteristic.

        ENDLOOP.

      ENDLOOP.

*    MOVE-CORRESPONDING gs_data to ls_OPERATION.
*    MOVE-CORRESPONDING gs_data to ls_INSPCHARACTERISTIC.
*    append ls_OPERATION to lt_OPERATION.
*    APPEND ls_INSPCHARACTERISTIC to lt_INSPCHARACTERISTIC.

      DATA ld_testrun TYPE  bapiflag.

      ld_testrun = 'X'.

      DATA ld_bomusage TYPE  bapi1012_control_data-bom_usage .

      ld_bomusage = '1'. "fabricacion

      CALL FUNCTION 'BAPI_ROUTING_CREATE'
        EXPORTING
          testrun                = p_test
*         PROFILE                =
          bomusage               = ld_bomusage
*         APPLICATION            =
* IMPORTING
*         GROUP                  =
*         GROUPCOUNTER           =
        TABLES
          task                   = lt_task
          materialtaskallocation = lt_materialtaskallocation
*         SEQUENCE               =
          operation              = lt_operation
*         LENGTHCALCULATION      =
*         SUBOPERATION           =
*         REFERENCEOPERATION     =
*         WORKCENTERREFERENCE    =
*         COMPONENTALLOCATION    =
*         PRODUCTIONRESOURCE     =
          inspcharacteristic     = lt_inspcharacteristic
*         TEXTALLOCATION         =
*         TEXT                   =
          return                 = lt_return.

      MOVE-CORRESPONDING gs_data_material TO gs_log.
      LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
        MOVE-CORRESPONDING ls_return TO gs_log.
        APPEND gs_log TO gt_log.
      ENDLOOP.

      IF sy-subrc <> 0.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*     EXPORTING
*       WAIT          =
*     IMPORTING
*       RETURN        =
          .
        gs_log-type = 'S'.
        gs_log-message = 'Hoja de ruta creada'.
        APPEND gs_log TO gt_log.
      ENDIF.
*    BREAK-POINT.
    ENDLOOP.
  ENDLOOP.
*PERFORM get_task_list_group using 'X'
*                               ls_MATERIALTASKALLOCATION
*                                ls_task.



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( ).

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

ENDFORM.                    "display_log