SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Create Equipment using 'BAPI_EQUI_CREATE'



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> PP
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Sun Oct 14, 2007 8:30 pm    Post subject: Create Equipment using 'BAPI_EQUI_CREATE' Reply with quote

Requirement:
Create Equipment along with the long text by uploading data from a tab delimited file.

Processing:
This program is to upload equipment master data from a tab delimited file using BAPI 'BAPI_EQUI_CREATE'. The first line of input file will have the header description of data-fields and hence the fiirst line on the file will will not be processed. Further, In case of error the progran also creates an error datafile containing all the error records. This file will be created in the same input directory but name of the file will be .err<.txt>. At the end of the program-run a report containing record number, equipment number ( if there ), status and message will be displayed. This report will have the information for error as well as successful records.


Code:

REPORT Z_EQUIPMENT_MASTER_UPLOAD  LINE-SIZE 200.
*----------------------------------------------------------------------*
*       Written By: Ram Manohar Tiwari                
*----------------------------------------------------------------------*
*       Presented By: http://www.rmtiwari.com
*----------------------------------------------------------------------*

********************************************************************
* This program is used to upload equipment master data from a tab
* delimited file.
*
* The first line of input file will have the header description of
* data-fields and hence the fiirst line on the file will  will not
* be processed.
*
* Further all the fields ( or at least the numeric fields like 0010 )
* should be specified as "text" while formatting the data in Excel file
* or else it will be uploaded as 10 and hence SAP might reject these
* records.

* Further, In case of error the progran also creates an error datafile
* containing all the error records. This file will be created in the
* same input directory but name of the file will be
*.err<.txt>.

* At the end of the program-run a report containing record number,
* equipment number ( if there ), status and message will be displayed.
* This report will have the information for error as well as successful
* records.

********************************************************************

*-----------------------------------------------------------------------
* SELECTION SCREEN
*-----------------------------------------------------------------------


SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE text-001.

SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: P_INFILE LIKE rlgrap-filename obligatory.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B1.

*-----------------------------------------------------------------------

data : gv_file_name LIKE rlgrap-filename.
DATA: BEGIN OF gt_upload OCCURS 0,
        rec(3000) TYPE c,
      END OF gt_upload.

DATA: BEGIN OF gt_header_fields OCCURS 0,
        rec(3000) TYPE c,
      END OF gt_header_fields.

data: Begin of gt_equipment_data occurs 0,
        EQUI_NUMBER  TYPE BAPI_ITOB_PARMS-EQUIPMENT,
        VALID_DATE   TYPE BAPI_ITOB_PARMS-INST_DATE,
        FUNCLOC      TYPE BAPI_ITOB_EQ_INSTALL-FUNCLOC,
        SUPEQUI      TYPE BAPI_ITOB_EQ_INSTALL-SUPEQUI,
        MATERIAL     TYPE BAPI_ITOB_EQ_ONLY-MATERIAL,
        SERIALNO     TYPE BAPI_ITOB_EQ_ONLY-SERIALNO,
        EQUICATGRY   TYPE BAPI_ITOB_EQ_ONLY-EQUICATGRY,
        BATCH        TYPE BAPI_ITOB_EQ_ONLY-BATCH,
        READ_SUPEQ   TYPE BAPI_ITOB_EQ_ONLY-READ_SUPEQ,
        READ_FLOC    TYPE BAPI_ITOB_EQ_ONLY-READ_FLOC,
        OBJECTTYPE   TYPE BAPI_ITOB-OBJECTTYPE,
        OBJ_SIZE     TYPE BAPI_ITOB-OBJ_SIZE,
        OBJ_WEIGHT(16)  TYPE C,                   "BAPI_ITOB-OBJ_WEIGHT,
        UNIT_OF_WT   TYPE BAPI_ITOB-UNIT_OF_WT,
        MANFACTURE   TYPE BAPI_ITOB-MANFACTURE,
        MANCOUNTRY   TYPE BAPI_ITOB-MANCOUNTRY,
        COUNTR_ISO   TYPE BAPI_ITOB-COUNTR_ISO,
        MANSERNO     TYPE BAPI_ITOB-MANSERNO,
        MANMODEL     TYPE BAPI_ITOB-MANMODEL,
        CONSTYEAR    TYPE BAPI_ITOB-CONSTYEAR,
        CONSTMONTH   TYPE BAPI_ITOB-CONSTMONTH,
        START_FROM   TYPE BAPI_ITOB-START_FROM,
        PLANPLANT    TYPE BAPI_ITOB-PLANPLANT,
        MANPARNO     TYPE BAPI_ITOB-MANPARNO,
        PLANGROUP    TYPE BAPI_ITOB-PLANGROUP,
        CATPROFILE   TYPE BAPI_ITOB-CATPROFILE,
        WCTR_PLANT   TYPE BAPI_ITOB-MAINTPLANT,
        WORK_CTR(8)  TYPE C,                       "BAPI_ITOB-WORK_CTR,
        DESCRIPT     TYPE BAPI_ITOB-DESCRIPT,
        ABCINDIC     TYPE BAPI_ITOB-ABCINDIC,
        SORTFIELD    TYPE BAPI_ITOB-SORTFIELD,
        MAINTPLANT   TYPE BAPI_ITOB-MAINTPLANT,
        MAINTLOC     TYPE BAPI_ITOB-MAINTLOC,
        PP_WKCTR(8)  TYPE C,                        "BAPI_ITOB-PP_WKCTR,
        COSTCENTER   TYPE BAPI_ITOB-COSTCENTER,
        COMP_CODE    TYPE BAPI_ITOB-COMP_CODE,
        LONG_TEXT(2500)    TYPE C,
      end of gt_equipment_data.

data: Begin of gt_equipment_error_data occurs 0,
        EQUI_NUMBER  TYPE BAPI_ITOB_PARMS-EQUIPMENT,
        VALID_DATE   TYPE BAPI_ITOB_PARMS-INST_DATE,
        FUNCLOC      TYPE BAPI_ITOB_EQ_INSTALL-FUNCLOC,
        SUPEQUI      TYPE BAPI_ITOB_EQ_INSTALL-SUPEQUI,
        MATERIAL     TYPE BAPI_ITOB_EQ_ONLY-MATERIAL,
        SERIALNO     TYPE BAPI_ITOB_EQ_ONLY-SERIALNO,
        EQUICATGRY   TYPE BAPI_ITOB_EQ_ONLY-EQUICATGRY,
        BATCH        TYPE BAPI_ITOB_EQ_ONLY-BATCH,
        READ_SUPEQ   TYPE BAPI_ITOB_EQ_ONLY-READ_SUPEQ,
        READ_FLOC    TYPE BAPI_ITOB_EQ_ONLY-READ_FLOC,
        OBJECTTYPE   TYPE BAPI_ITOB-OBJECTTYPE,
        OBJ_SIZE     TYPE BAPI_ITOB-OBJ_SIZE,
        OBJ_WEIGHT(16)  TYPE C,                   "BAPI_ITOB-OBJ_WEIGHT,
        UNIT_OF_WT   TYPE BAPI_ITOB-UNIT_OF_WT,
        MANFACTURE   TYPE BAPI_ITOB-MANFACTURE,
        MANCOUNTRY   TYPE BAPI_ITOB-MANCOUNTRY,
        COUNTR_ISO   TYPE BAPI_ITOB-COUNTR_ISO,
        MANSERNO     TYPE BAPI_ITOB-MANSERNO,
        MANMODEL     TYPE BAPI_ITOB-MANMODEL,
        CONSTYEAR    TYPE BAPI_ITOB-CONSTYEAR,
        CONSTMONTH   TYPE BAPI_ITOB-CONSTMONTH,
        START_FROM   TYPE BAPI_ITOB-START_FROM,
        PLANPLANT    TYPE BAPI_ITOB-PLANPLANT,
        MANPARNO     TYPE BAPI_ITOB-MANPARNO,
        PLANGROUP    TYPE BAPI_ITOB-PLANGROUP,
        CATPROFILE   TYPE BAPI_ITOB-CATPROFILE,
        WCTR_PLANT   TYPE BAPI_ITOB-MAINTPLANT,
        WORK_CTR(8)  TYPE C,                      "BAPI_ITOB-WORK_CTR,
        DESCRIPT     TYPE BAPI_ITOB-DESCRIPT,
        ABCINDIC     TYPE BAPI_ITOB-ABCINDIC,
        SORTFIELD    TYPE BAPI_ITOB-SORTFIELD,
        MAINTPLANT   TYPE BAPI_ITOB-MAINTPLANT,
        MAINTLOC     TYPE BAPI_ITOB-MAINTLOC,
        PP_WKCTR(8)  TYPE C,                      "BAPI_ITOB-PP_WKCTR,
        COSTCENTER   TYPE BAPI_ITOB-COSTCENTER,
        COMP_CODE    TYPE BAPI_ITOB-COMP_CODE,
        LONG_TEXT(2500)    TYPE C,
      end of gt_equipment_error_data.

data: Begin of gt_equipment_error_rep occurs 0,
        EQUI_NUMBER  TYPE BAPI_ITOB_PARMS-EQUIPMENT,
        VALID_DATE   TYPE BAPI_ITOB_PARMS-INST_DATE,
        FUNCLOC      TYPE BAPI_ITOB_EQ_INSTALL-FUNCLOC,
        SUPEQUI      TYPE BAPI_ITOB_EQ_INSTALL-SUPEQUI,
        MATERIAL     TYPE BAPI_ITOB_EQ_ONLY-MATERIAL,
        SERIALNO     TYPE BAPI_ITOB_EQ_ONLY-SERIALNO,
        EQUICATGRY   TYPE BAPI_ITOB_EQ_ONLY-EQUICATGRY,
        BATCH        TYPE BAPI_ITOB_EQ_ONLY-BATCH,
        READ_SUPEQ   TYPE BAPI_ITOB_EQ_ONLY-READ_SUPEQ,
        READ_FLOC    TYPE BAPI_ITOB_EQ_ONLY-READ_FLOC,
        OBJECTTYPE   TYPE BAPI_ITOB-OBJECTTYPE,
        OBJ_SIZE     TYPE BAPI_ITOB-OBJ_SIZE,
        OBJ_WEIGHT   TYPE BAPI_ITOB-OBJ_WEIGHT,
        UNIT_OF_WT   TYPE BAPI_ITOB-UNIT_OF_WT,
        MANFACTURE   TYPE BAPI_ITOB-MANFACTURE,
        MANCOUNTRY   TYPE BAPI_ITOB-MANCOUNTRY,
        COUNTR_ISO   TYPE BAPI_ITOB-COUNTR_ISO,
        MANSERNO     TYPE BAPI_ITOB-MANSERNO,
        MANMODEL     TYPE BAPI_ITOB-MANMODEL,
        CONSTYEAR    TYPE BAPI_ITOB-CONSTYEAR,
        CONSTMONTH   TYPE BAPI_ITOB-CONSTMONTH,
        START_FROM   TYPE BAPI_ITOB-START_FROM,
        PLANPLANT    TYPE BAPI_ITOB-PLANPLANT,
        MANPARNO     TYPE BAPI_ITOB-MANPARNO,
        PLANGROUP    TYPE BAPI_ITOB-PLANGROUP,
        CATPROFILE   TYPE BAPI_ITOB-CATPROFILE,
        WCTR_PLANT   TYPE BAPI_ITOB-MAINTPLANT,
        WORK_CTR(8)  TYPE C,                     "BAPI_ITOB-WORK_CTR,
        DESCRIPT     TYPE BAPI_ITOB-DESCRIPT,
        ABCINDIC     TYPE BAPI_ITOB-ABCINDIC,
        SORTFIELD    TYPE BAPI_ITOB-SORTFIELD,
        MAINTPLANT   TYPE BAPI_ITOB-MAINTPLANT,
        MAINTLOC     TYPE BAPI_ITOB-MAINTLOC,
        PP_WKCTR(8)  TYPE C,                    "BAPI_ITOB-PP_WKCTR,
        COSTCENTER   TYPE BAPI_ITOB-COSTCENTER,
        COMP_CODE    TYPE BAPI_ITOB-COMP_CODE,
        MESSAGE      TYPE bad_patch-descript,
        STATUS(10)   TYPE C,
        RECORD       TYPE i,
      end of gt_equipment_error_rep.


* ALV stuff
type-pools: slis.
data: gt_fieldcat type slis_t_fieldcat_alv,
      gs_layout   type slis_layout_alv,
      gt_sort     type slis_t_sortinfo_alv.


*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
  PERFORM authorization_check.
  PERFORM get_file_name using    'P_INFILE'
                        changing gv_file_name.


START-OF-SELECTION.
  PERFORM read_input_file_from_pc tables   gt_upload
                                  using    p_infile.

  PERFORM prepare_itab_data tables   gt_upload
                                     gt_equipment_data.

  PERFORM call_bapi_create_equipment tables gt_equipment_data
                                            gt_equipment_error_data
                                            gt_equipment_error_rep.

END-OF-SELECTION.
  PERFORM write_error_file_to_pc tables gt_equipment_error_data
                                 using  p_infile.

  perform alv_grid.

*&---------------------------------------------------------------------*
*&      Form  get_file_name
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file_name using    x_file_field_name
                   changing y_file_name_with_path.

  data : lv_file_path type rlgrap-filename,
         lv_file_name type rlgrap-filename.

  PERFORM read_screen_values USING    x_file_field_name
                             CHANGING y_file_name_with_path.

  PERFORM split_path USING    y_file_name_with_path
                     CHANGING lv_file_path
                              lv_file_name.

  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename = lv_file_name
            def_path     = lv_file_path
            mask         = ',*.*,*.*.'
            mode         = 'O'
            title        = 'Directory'
       IMPORTING
            filename     = p_infile
       EXCEPTIONS
            OTHERS       = 1.


ENDFORM.                    " get_file_name
*&--------------------------------------------------------------------*
*&      Form  read_screen_values
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->P_FIELD    text
*      -->P_VALUE    text
*---------------------------------------------------------------------*
FORM read_screen_values USING    x_field
                        CHANGING y_value.

  DATA: lv_dynpname LIKE d020s-prog,
        lv_dynpnumb LIKE d020s-dnum.

  DATA: BEGIN OF lt_dynpvaluetab OCCURS 1.
          INCLUDE STRUCTURE dynpread.
  DATA: END   OF lt_dynpvaluetab.

  lv_dynpname = sy-repid.
  lv_dynpnumb = sy-dynnr.

  REFRESH lt_dynpvaluetab.
  lt_dynpvaluetab-fieldname = x_field.
  APPEND lt_dynpvaluetab.

  CALL FUNCTION 'DYNP_VALUES_READ'
       EXPORTING
            dyname               = lv_dynpname
            dynumb               = lv_dynpnumb
       TABLES
            dynpfields           = lt_dynpvaluetab
       EXCEPTIONS
            invalid_abapworkarea = 1
            invalid_dynprofield  = 2
            invalid_dynproname   = 3
            invalid_dynpronummer = 4
            invalid_request      = 5
            no_fielddescription  = 6
            invalid_parameter    = 7
            undefind_error       = 8
            double_conversion    = 9
            OTHERS               = 10.
  IF sy-subrc = 0.

    READ TABLE lt_dynpvaluetab INDEX 1.
    MOVE: lt_dynpvaluetab-fieldvalue TO y_value.

  ENDIF.

ENDFORM.                    " read_screen_values
*&--------------------------------------------------------------------*
*&      Form  split_path
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->P_WHOLE    text
*      -->P_PATH     text
*      -->P_FNAME    text
*      -->P_SCHARS   text
*---------------------------------------------------------------------*
FORM split_path USING    x_file_name_with_path
                CHANGING y_file_path
                         y_file_name.

  DATA: lv_length TYPE i,
        lv_cpos    TYPE i.

  check not x_file_name_with_path is initial.
  lv_length = strlen( x_file_name_with_path ).
  lv_cpos = lv_length.
  DO lv_length times.
    lv_cpos = lv_cpos - 1.
    check x_file_name_with_path+lv_cpos(1) eq '\'.

    exit.

  ENDDO.
  y_file_path = x_file_name_with_path(lv_cpos).
  lv_cpos = lv_cpos + 1.
  y_file_name = x_file_name_with_path+lv_cpos(lv_length).
ENDFORM.                    " split_path
*&---------------------------------------------------------------------*
*&      Form  read_input_file_from_pc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM read_input_file_from_pc tables   yt_upload
                             using    x_infile.


* Use WS_Upload if file is located on the local PC.
* WS_Upload only works in foreground
  IF sy-batch EQ 'X'.
    WRITE: text-002.
<*   ERROR: Unable to upload locally stored files when running in
*   background
  ELSE.
    CALL FUNCTION 'WS_UPLOAD'
         EXPORTING
              filename                = x_infile
              filetype                = 'ASC'
         TABLES
              data_tab                = yt_upload
         EXCEPTIONS
              conversion_error        = 1
              file_open_error         = 2
              file_read_error         = 3
              invalid_table_width     = 4
              invalid_type            = 5
              no_batch                = 6
              unknown_error           = 7
              gui_refuse_filetransfer = 8
              OTHERS                  = 9.

* Status of upload
    CASE sy-subrc.
      WHEN 0.
      WHEN OTHERS.
*        Upload unsuccessful - error message
        MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO
           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDCASE.
  ENDIF.

  IF yt_upload[] is initial.
    MESSAGE E079(z1) with x_infile.
  ENDIF.
ENDFORM.                    " read_input_file_from_pc
*&---------------------------------------------------------------------*
*&      Form  prepare_itab_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_UPLOAD  text
*      <--P_GT_EQUIPMENT_DATA  text
*----------------------------------------------------------------------*
FORM prepare_itab_data
                TABLES  XT_UPLOAD         STRUCTURE GT_UPLOAD
                        YT_EQUIPMENT_DATA STRUCTURE GT_EQUIPMENT_DATA.

  constants :
    lc_tab type x value '09'.
  data :
    lv_length type i.

  loop at xt_upload.

* Skip the header line descriptions in the file.
    if sy-tabix eq 1.
      SPLIT XT_UPLOAD at lc_tab into table gt_header_fields.
      continue.
    endif.

* Populate file values to internal table.
    split xt_upload at lc_tab into
        yt_equipment_data-EQUI_NUMBER
        yt_equipment_data-VALID_DATE
        yt_equipment_data-FUNCLOC
        yt_equipment_data-SUPEQUI
        yt_equipment_data-MATERIAL
        yt_equipment_data-SERIALNO
        yt_equipment_data-EQUICATGRY
        yt_equipment_data-BATCH
        yt_equipment_data-READ_SUPEQ
        yt_equipment_data-READ_FLOC
        yt_equipment_data-OBJECTTYPE
        yt_equipment_data-OBJ_SIZE
        yt_equipment_data-OBJ_WEIGHT
        yt_equipment_data-UNIT_OF_WT
        yt_equipment_data-MANFACTURE
        yt_equipment_data-MANCOUNTRY
        yt_equipment_data-COUNTR_ISO
        yt_equipment_data-MANSERNO
        yt_equipment_data-MANMODEL
        yt_equipment_data-CONSTYEAR
        yt_equipment_data-CONSTMONTH
        yt_equipment_data-START_FROM
        yt_equipment_data-PLANPLANT
        yt_equipment_data-MANPARNO
        yt_equipment_data-PLANGROUP
        yt_equipment_data-CATPROFILE
        yt_equipment_data-WCTR_PLANT
        yt_equipment_data-WORK_CTR
        yt_equipment_data-DESCRIPT
        yt_equipment_data-ABCINDIC
        yt_equipment_data-SORTFIELD
        yt_equipment_data-MAINTPLANT
        yt_equipment_data-MAINTLOC
        yt_equipment_data-PP_WKCTR
        yt_equipment_data-COSTCENTER
        yt_equipment_data-COMP_CODE
        yt_equipment_data-LONG_TEXT.

    lv_length = strlen( yt_equipment_data-CONSTMONTH ).
    IF lv_length eq 1.
      concatenate '0' yt_equipment_data-CONSTMONTH
                      into yt_equipment_data-CONSTMONTH.
    endif.

* Populate other values ( system variables/constants - not from file )

    APPEND yt_equipment_data.
  endloop.

ENDFORM.                    " prepare_itab_data
*&---------------------------------------------------------------------*
*&      Form  call_bapi_create_equipment
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->XT_EQUIPMENT_DATA  text
*----------------------------------------------------------------------*
FORM call_bapi_create_equipment
          TABLES
            XT_EQUIPMENT_DATA STRUCTURE GT_EQUIPMENT_DATA
            XT_EQUIPMENT_error_DATA STRUCTURE GT_EQUIPMENT_DATA
            XT_EQUIPMENT_error_rep  STRUCTURE GT_EQUIPMENT_error_rep.

  data :
    lv_BAPI_ITOB            type BAPI_ITOB,
    lv_BAPI_ITOB_EQ_ONLY    type BAPI_ITOB_EQ_ONLY,
    lv_BAPI_ITOB_EQ_INSTALL type BAPI_ITOB_EQ_INSTALL,
    lv_BAPI_ITOBX           type BAPI_ITOBX,
    lv_BAPI_ITOB_EQ_ONLYX   type BAPI_ITOB_EQ_ONLYX,
    lv_valid_date           type sy-datum,
    lv_return               like bapiret2,
    lv_record_no            type sy-index,
    lv_edit_type(6)         type c,
    lv_no_error_flag        type c.

  loop at XT_EQUIPMENT_DATA.
    CLEAR : lv_return,
            lv_BAPI_ITOB,
            lv_BAPI_ITOB_EQ_ONLY,
            lv_BAPI_ITOB_EQ_INSTALL,
            lv_valid_date.

    lv_record_no = sy-tabix.

    PERFORM get_object_id_of_workcentres
       tables xt_EQUIPMENT_error_DATA
              xT_EQUIPMENT_error_rep
        using lv_record_no
     CHANGING XT_EQUIPMENT_DATA
              lv_no_error_flag.

*   Error encuntered in work centre id determination.
    IF  lv_no_error_flag is initial.
      continue.
    ENDIF.


* Check if equipment number was specified in file.
    if XT_EQUIPMENT_DATA-EQUI_NUMBER is initial.
      lv_edit_type = 'CREATE'.
    else.
      lv_edit_type = 'CHANGE'.
    endif.

    PERFORM populate_bapi_structures using    XT_EQUIPMENT_DATA
                                              lv_edit_type
                                     changing lv_BAPI_ITOB
                                              lv_BAPI_ITOB_EQ_ONLY
                                              lv_BAPI_ITOB_EQ_INSTALL
                                              lv_valid_date
                                              lv_BAPI_ITOBX
                                              lv_BAPI_ITOB_EQ_ONLYX.

    CASE lv_edit_type.

      WHEN 'CREATE'.

*       Create Equipment
        CALL FUNCTION 'BAPI_EQUI_CREATE'
             EXPORTING
                  EXTERNAL_NUMBER   = XT_EQUIPMENT_DATA-EQUI_NUMBER
                  DATA_GENERAL      = lv_BAPI_ITOB
                  DATA_SPECIFIC     = lv_BAPI_ITOB_EQ_ONLY
                  VALID_DATE        = lv_valid_date
                  DATA_INSTALL      = lv_BAPI_ITOB_EQ_INSTALL
             IMPORTING
                  EQUIPMENT         = XT_EQUIPMENT_DATA-EQUI_NUMBER
                  DATA_GENERAL_EXP  = lv_BAPI_ITOB
                  DATA_SPECIFIC_EXP = lv_BAPI_ITOB_EQ_ONLY
                  RETURN            = lv_return.

      WHEN 'CHANGE'.

        SELECT SINGLE EQUNR
           INTO XT_EQUIPMENT_DATA-EQUI_NUMBER
           FROM EQUI
          WHERE EQUNR eq XT_EQUIPMENT_DATA-EQUI_NUMBER.

        if sy-subrc ne 0.
          append XT_EQUIPMENT_DATA to XT_EQUIPMENT_error_DATA.
          XT_EQUIPMENT_error_rep = XT_EQUIPMENT_DATA.
          XT_EQUIPMENT_error_rep-MESSAGE = text-010.
          XT_EQUIPMENT_error_rep-RECORD = lv_record_no.
          append XT_EQUIPMENT_error_rep.
          exit.
        endif.

*       Change Equipment
        CALL FUNCTION 'BAPI_EQUI_CHANGE'
             EXPORTING
                  EQUIPMENT         = XT_EQUIPMENT_DATA-EQUI_NUMBER
                  DATA_GENERAL      = lv_BAPI_ITOB
                  DATA_GENERALX     = lv_BAPI_ITOBX
                  DATA_SPECIFIC     = lv_BAPI_ITOB_EQ_ONLY
                  DATA_SPECIFICX    = lv_BAPI_ITOB_EQ_ONLYX
             IMPORTING
                  DATA_GENERAL_EXP  = lv_BAPI_ITOB
                  DATA_SPECIFIC_EXP = lv_BAPI_ITOB_EQ_ONLY
                  RETURN            = lv_return.

    ENDCASE.

    if lv_return-type = 'E'.
*     For Error data file.
      append XT_EQUIPMENT_data to XT_EQUIPMENT_error_DATA.

*     For Error reporting.
      XT_EQUIPMENT_error_rep = XT_EQUIPMENT_data.
      PERFORM prepare_error_text
                            using    lv_return
                            changing XT_EQUIPMENT_error_rep-MESSAGE.
      XT_EQUIPMENT_error_rep-RECORD = lv_record_no.
      XT_EQUIPMENT_error_rep-STATUS = 'Error'.
      append XT_EQUIPMENT_error_rep.

    else.
*     Equipment created successfully.
      XT_EQUIPMENT_error_rep = XT_EQUIPMENT_data.
      XT_EQUIPMENT_error_rep-MESSAGE = 'Equipment created successfully'.
      XT_EQUIPMENT_error_rep-RECORD = lv_record_no.
      XT_EQUIPMENT_error_rep-STATUS = 'Success'.
      append XT_EQUIPMENT_error_rep.

      COMMIT WORK AND WAIT.

*     Save Long Text for Equipment.
      check not XT_EQUIPMENT_DATA-long_text is initial.
      PERFORM save_long_text using XT_EQUIPMENT_DATA-equi_number
                                   XT_EQUIPMENT_DATA-long_text.



    endif.


  endloop.

ENDFORM.                    " call_bapi_create_equipment
*&---------------------------------------------------------------------*
*&      Form  populate_bapi_structures
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->XT_EQUIPMENT_DATA  text
*      <--Y_BAPI_ITOB  text
*      <--Y_BAPI_ITOB_EQ_ONLY  text
*      <--Y_BAPI_ITOB_EQ_INSTALL  text
*      <--Y_VALID_DATE  text
*----------------------------------------------------------------------*
FORM populate_bapi_structures
        USING    XT_EQUIPMENT_DATA STRUCTURE GT_EQUIPMENT_DATA
                 x_edit_type  TYPE C
        CHANGING y_BAPI_ITOB  STRUCTURE BAPI_ITOB
                 y_BAPI_ITOB_EQ_ONLY STRUCTURE BAPI_ITOB_EQ_ONLY
                 y_BAPI_ITOB_EQ_INSTALL STRUCTURE BAPI_ITOB_EQ_INSTALL
                 y_VALID_DATE
                 y_BAPI_ITOBX  STRUCTURE BAPI_ITOBX
                 y_BAPI_ITOB_EQ_ONLYX STRUCTURE BAPI_ITOB_EQ_ONLYX.

  move-corresponding XT_EQUIPMENT_DATA to y_BAPI_ITOB.
  move-corresponding XT_EQUIPMENT_DATA to y_BAPI_ITOB_EQ_ONLY.
  move-corresponding XT_EQUIPMENT_DATA to y_BAPI_ITOB_EQ_INSTALL.
  y_VALID_DATE = XT_EQUIPMENT_DATA-VALID_DATE.

  IF x_edit_type eq 'CHANGE'.
    Y_BAPI_ITOB_EQ_ONLYX-MATERIAL     = 'X'.
    Y_BAPI_ITOB_EQ_ONLYX-SERIALNO     = 'X'.
    Y_BAPI_ITOB_EQ_ONLYX-EQUICATGRY   = 'X'.
    Y_BAPI_ITOB_EQ_ONLYX-BATCH        = 'X'.
    Y_BAPI_ITOB_EQ_ONLYX-READ_SUPEQ   = 'X'.
    Y_BAPI_ITOB_EQ_ONLYX-READ_FLOC    = 'X'.

    Y_BAPI_ITOBX-OBJECTTYPE            = 'X'.
    Y_BAPI_ITOBX-OBJ_SIZE              = 'X'.
    Y_BAPI_ITOBX-OBJ_WEIGHT            = 'X'.
    Y_BAPI_ITOBX-UNIT_OF_WT            = 'X'.
    Y_BAPI_ITOBX-MANFACTURE            = 'X'.
    Y_BAPI_ITOBX-MANCOUNTRY            = 'X'.
    Y_BAPI_ITOBX-COUNTR_ISO            = 'X'.
    Y_BAPI_ITOBX-MANSERNO              = 'X'.
    Y_BAPI_ITOBX-MANMODEL              = 'X'.
    Y_BAPI_ITOBX-CONSTYEAR             = 'X'.
    Y_BAPI_ITOBX-CONSTMONTH            = 'X'.
    Y_BAPI_ITOBX-START_FROM            = 'X'.
    Y_BAPI_ITOBX-PLANPLANT             = 'X'.
    Y_BAPI_ITOBX-MANPARNO              = 'X'.
    Y_BAPI_ITOBX-PLANGROUP             = 'X'.
    Y_BAPI_ITOBX-CATPROFILE            = 'X'.
    Y_BAPI_ITOBX-WORK_CTR              = 'X'.
    Y_BAPI_ITOBX-DESCRIPT              = 'X'.
    Y_BAPI_ITOBX-ABCINDIC              = 'X'.
    Y_BAPI_ITOBX-SORTFIELD             = 'X'.
    Y_BAPI_ITOBX-MAINTPLANT            = 'X'.
    Y_BAPI_ITOBX-MAINTLOC              = 'X'.
    Y_BAPI_ITOBX-PP_WKCTR              = 'X'.
    Y_BAPI_ITOBX-COSTCENTER            = 'X'.
    Y_BAPI_ITOBX-COMP_CODE             = 'X'.
    Y_BAPI_ITOBX-READ_CRDAT            = 'X'.
    Y_BAPI_ITOBX-READ_CRNAM            = 'X'.
    Y_BAPI_ITOBX-READ_CHDAT            = 'X'.
    Y_BAPI_ITOBX-READ_CHNAM            = 'X'.

  ENDIF.

ENDFORM.                    " populate_bapi_structures
*&---------------------------------------------------------------------*
*&      Form  write_error_file_to_pc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_EQUIPMENT_ERROR_DATA  text
*----------------------------------------------------------------------*
FORM write_error_file_to_pc  tables   xt_download
                             using    x_infile.

  check not xt_download[] is initial.
  replace '.' with '.err.' into x_infile.

  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      FILENAME                      = x_infile
      FILETYPE                      = 'DAT'
    TABLES
      DATA_TAB                      = xt_download
      FIELDNAMES                    = gt_header_fields
   EXCEPTIONS
     FILE_OPEN_ERROR               = 1
     FILE_WRITE_ERROR              = 2
     INVALID_FILESIZE              = 3
     INVALID_TYPE                  = 4
     NO_BATCH                      = 5
     UNKNOWN_ERROR                 = 6
     INVALID_TABLE_WIDTH           = 7
     GUI_REFUSE_FILETRANSFER       = 8
     CUSTOMER_ERROR                = 9
     OTHERS                        = 10
            .
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE 'W' NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


ENDFORM.                    " write_error_file_to_pc

*&---------------------------------------------------------------------*
*&      Form  alv_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form alv_grid.

  if gt_fieldcat[] is initial.
    perform fieldcat_init.
    perform layout_init.
    perform sort_init.
  endif.

  perform grid_display.

endform.
*&---------------------------------------------------------------------*
*&      Form  layout_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form layout_init.
  gs_layout-zebra             = 'X'.
  gs_layout-cell_merge        = 'X'.
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-no_vline          = ' '.
  gs_layout-totals_before_items = ' '.

endform.                    " layout_init
*&---------------------------------------------------------------------*
*&      Form  fieldcat_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fieldcat_init.
  data: ls_fieldcat type slis_fieldcat_alv.

  clear ls_fieldcat.
  ls_fieldcat-fieldname    = 'RECORD'.
  ls_fieldcat-key          = 'X'.
  ls_fieldcat-reptext_ddic = 'Record Number'.
  ls_fieldcat-outputlen    = 10.
* Fix for ALV print bug,
* Set type to 'N' to stop corruption of printed ALV cell.
  ls_fieldcat-inttype = 'N'.
  append ls_fieldcat to gt_fieldcat.


  clear ls_fieldcat.
  ls_fieldcat-fieldname    = 'EQUI_NUMBER'.
  ls_fieldcat-reptext_ddic = 'Equipment'.
  ls_fieldcat-outputlen    = 10.
  append ls_fieldcat to gt_fieldcat.

  clear ls_fieldcat.
  ls_fieldcat-fieldname    = 'STATUS'.
  ls_fieldcat-reptext_ddic = 'Status'.
  ls_fieldcat-outputlen    = 10.
  append ls_fieldcat to gt_fieldcat.

  clear ls_fieldcat.
  ls_fieldcat-fieldname    = 'MESSAGE'.
  ls_fieldcat-reptext_ddic = 'Message'.
  ls_fieldcat-outputlen    = 100.
  append ls_fieldcat to gt_fieldcat.

endform.

*&---------------------------------------------------------------------*
*&      Form  sort_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form sort_init.
  data: ls_sort type slis_sortinfo_alv.

  clear ls_sort.
  ls_sort-fieldname = 'STATUS'.
  ls_sort-spos      = 1.
  ls_sort-up        = 'X'.
  append ls_sort to gt_sort.

  clear ls_sort.
  ls_sort-fieldname = 'RECORD'.
  ls_sort-spos      = 2.
  ls_sort-up        = 'X'.
  append ls_sort to gt_sort.

  clear ls_sort.
  ls_sort-fieldname = 'EQUI_NUMBER'.
  ls_sort-spos      = 3.
  ls_sort-up        = 'X'.
  append ls_sort to gt_sort.

endform.
*&---------------------------------------------------------------------*
*&      Form  grid_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form grid_display.

  call function 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            is_layout     = gs_layout
            it_fieldcat   = gt_fieldcat
            it_sort       = gt_sort
            i_default     = ' '
            i_save        = 'X'
       TABLES
            t_outtab      = gt_equipment_error_rep
       EXCEPTIONS
            PROGRAM_ERROR = 1
            OTHERS        = 2.
  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.
*&---------------------------------------------------------------------*
*&      Form  authorization_check
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM authorization_check.
  CALL FUNCTION 'AUTHORITY_CHECK_EQUI'
       EXPORTING
            TCODE                          = 'IE01'
       EXCEPTIONS
            NO_AUTHORITY_TCODE             = 1
            NO_AUTHORITY_BEGRP             = 2
            NO_AUTHORITY_IWERK             = 3
            NO_AUTHORITY_INGRP             = 4
            NO_AUTHORITY_SWERK             = 5
            NO_AUTHORITY_KOSTL             = 6
            NO_PROFILE_IN_USER_MASTER_DATA = 7
            ERROR_IN_USER_MASTER_DATA      = 8
            OTHERS                         = 9.
  IF SY-SUBRC <> 0.
    message E078(z1).
  ENDIF.

ENDFORM.                    " authorization_check
*&---------------------------------------------------------------------*
*&      Form  prepare_error_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->XT_RETURN  text
*      <--YT_EQUIPMENT_ERROR_REP_MESSAGE  text
*----------------------------------------------------------------------*
FORM prepare_error_text USING    X_RETURN  TYPE BAPIRET2
                        CHANGING Y_MESSAGE TYPE bad_patch-descript.

* Convert the message
  MOVE : x_return-id TO sy-msgid,
         x_return-number TO sy-msgno,
         x_return-message_v1 TO sy-msgv1,
         x_return-message_v2 TO sy-msgv2,
         x_return-message_v3 TO sy-msgv3,
         x_return-message_v4 TO sy-msgv4.

  CALL FUNCTION 'K_MESSAGE_TRANSFORM'
       EXPORTING
            par_langu         = sy-langu
            par_msgid         = sy-msgid
            par_msgno         = sy-msgno
            par_msgv1         = sy-msgv1
            par_msgv2         = sy-msgv2
            par_msgv3         = sy-msgv3
            par_msgv4         = sy-msgv4
       IMPORTING
            par_msgtx         = Y_MESSAGE
       EXCEPTIONS
            no_message_found  = 1
            par_msgid_missing = 2
            par_msgno_missing = 3
            par_msgty_missing = 4
            OTHERS            = 5.


ENDFORM.                    " prepare_error_text
*&---------------------------------------------------------------------*
*&      Form  save_long_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->x_EQUI_NUMBER  text
*      -->x_long_text    text
*----------------------------------------------------------------------*
FORM save_long_text USING    x_EQUI_NUMBER
                             x_long_text.

  data :
    lt_long_text  type table of TLINE with header line,
    lv_text_length     type i,
    lv_loop_times      type i,
    lv_char_pos_last   type i,
    lv_char_pos_curr   type i,
    lv_header          type thead.

  lv_text_length = strlen( x_long_text ).
  lv_char_pos_curr = 0.
  lv_char_pos_last = 0.

* Split long text into table.

  lv_loop_times = lv_text_length DIV 132.
  DO lv_loop_times times.
    lv_char_pos_curr = lv_char_pos_curr + 132.

    lt_long_text-tdline =
      x_long_text+lv_char_pos_last(132).
    append lt_long_text.
    lv_char_pos_last = lv_char_pos_curr - 1.
  ENDDO.

* Write the left-over.
  lt_long_text-tdline =
    x_long_text+lv_char_pos_last.
  append lt_long_text.


  lv_header-TDOBJECT = 'EQUI'.
  lv_header-TDNAME   = x_EQUI_NUMBER.
  lv_header-TDID     = 'LTXT'.
  lv_header-TDSPRAS  = 'E'.

  CALL FUNCTION 'SAVE_TEXT'
    EXPORTING
      HEADER                = lv_header
      SAVEMODE_DIRECT       = 'X'
    TABLES
      LINES                 = lt_long_text
   EXCEPTIONS
     ID                    = 1
     LANGUAGE              = 2
     NAME                  = 3
     OBJECT                = 4
     OTHERS                = 5
            .
  IF SY-SUBRC <> 0.
    CALL FUNCTION 'K_MESSAGE_TRANSFORM'
         EXPORTING
              par_langu         = sy-langu
              par_msgid         = sy-msgid
              par_msgno         = sy-msgno
              par_msgv1         = sy-msgv1
              par_msgv2         = sy-msgv2
              par_msgv3         = sy-msgv3
              par_msgv4         = sy-msgv4
         IMPORTING
              par_msgtx         = GT_EQUIPMENT_error_rep-MESSAGE
         EXCEPTIONS
              no_message_found  = 1
              par_msgid_missing = 2
              par_msgno_missing = 3
              par_msgty_missing = 4
              OTHERS            = 5.

*   For Error data file.
    modify gt_equipment_error_data from gt_equipment_data.


    gt_equipment_error_rep-status = 'Error'.
    modify GT_EQUIPMENT_error_rep transporting MESSAGE STATUS
                            where EQUI_NUMBER = x_EQUI_NUMBER.
  ELSE.
    COMMIT WORK.
  ENDIF.

ENDFORM.                    " save_long_text
*&---------------------------------------------------------------------*
*&      Form  get_object_id_of_workcentres
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_object_id_of_workcentres
       TABLES
             yt_EQUIPMENT_error_DATA structure gt_equipment_error_DATA
             YT_EQUIPMENT_error_rep structure gT_EQUIPMENT_error_rep
       using value(x_record_no) type sy-index
       CHANGING
             x_equipment_data structure gt_equipment_data
             y_no_error_flag type c.

  data : lv_main_work_centre like gt_equipment_data-WORK_CTR,
         lv_plan_work_centre like gt_equipment_data-PP_WKCTR.

  y_no_error_flag  = 'X'.
* Get Object ID of Work centre using work centre and plant.


  IF not x_equipment_data-WORK_CTR is initial.

    SELECT SINGLE OBJID
      INTO lv_main_work_centre
      FROM CRHD
     WHERE objty = 'A'
       AND arbpl = x_equipment_data-WORK_CTR
       AND werks = x_equipment_data-WCTR_PLANT.

    if sy-subrc <> 0.
      y_no_error_flag  = ' '.
*        For Error data file.
      append x_EQUIPMENT_data to YT_EQUIPMENT_error_DATA.

*        For Error reporting.
      YT_EQUIPMENT_error_rep = x_EQUIPMENT_data.
      CONCATENATE
      'Work Centre ID is not maintained for Maint Work center' space
      x_equipment_data-WORK_CTR space ' and ' space
      'Plant ' space  x_equipment_data-MAINTPLANT
      into YT_EQUIPMENT_error_rep-MESSAGE.

      YT_EQUIPMENT_error_rep-RECORD = x_record_no.
      YT_EQUIPMENT_error_rep-STATUS = 'Error'.
      append YT_EQUIPMENT_error_rep.

      exit.
    else.
      x_equipment_data-WORK_CTR = lv_main_work_centre.

    endif.
  endif.

  if not x_equipment_data-PP_WKCTR is initial.

    SELECT SINGLE OBJID
      INTO lv_plan_work_centre
      FROM CRHD
     WHERE objty = 'A'
       AND arbpl = x_equipment_data-PP_WKCTR
       AND werks = x_equipment_data-WCTR_PLANT.

    if sy-subrc <> 0.
      y_no_error_flag  = ' '.
      y_no_error_flag  = ' '.
*        For Error data file.
      append x_EQUIPMENT_data to YT_EQUIPMENT_error_DATA.

*        For Error reporting.
      YT_EQUIPMENT_error_rep = x_EQUIPMENT_data.
      CONCATENATE
      'Work Centre ID is not maintained for Plan Work center ' space
      x_equipment_data-PP_WKCTR space ' and ' space
      'Plant ' space x_equipment_data-PLANPLANT
      into YT_EQUIPMENT_error_rep-MESSAGE.

      YT_EQUIPMENT_error_rep-RECORD = x_record_no.
      YT_EQUIPMENT_error_rep-STATUS = 'Error'.
      append YT_EQUIPMENT_error_rep.

      exit.

    else.
      x_equipment_data-PP_WKCTR = lv_plan_work_centre.
    endif.

  endif.
ENDFORM.                    " get_object_id_of_workcentres
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> PP All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.