Friday, February 1, 2013

ME38_BDC

*&---------------------------------------------------------------------*
*& Report Name : ZME38_BDC_RPT
*& Transaction : ZMM_ME38
*& Description : BDC Report on ME38 to change the scheduling qty of line items
*& Date of Creation : 29/1/2013
*&---------------------------------------------------------------------*
*& Date       | Transport No |  Reason for Modification                *
*&
*&---------------------------------------------------------------------*

REPORT  zme38_bdc_rpt.

TYPES BEGIN OF ty_final,
          ebeln     TYPE ebeln,
          ebelp(5)  TYPE c,
          rowid(2)  TYPE c,
          menge(17TYPE c,
        END OF ty_final.

DATA it_final   TYPE STANDARD TABLE OF ty_final,
       it_bdcdata TYPE STANDARD TABLE OF bdcdata,
       it_error   TYPE STANDARD TABLE OF bdcmsgcoll,
       it_temp    TYPE STANDARD TABLE OF ty_final,
       it_eket    TYPE STANDARD TABLE OF ty_final,
       wa_error   TYPE                   bdcmsgcoll,
       wa_final   TYPE                   ty_final,
       wa_bdcdata TYPE                   bdcdata,
       wa_temp    TYPE ty_final,
       name_of_file LIKE ibipparms-path.

PARAMETERS p_file TYPE localfile.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM get_file.

START-OF-SELECTION.

  PERFORM refresh_data.

  PERFORM fetch_bdc_data.

*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PROGRAM    text
*      -->DYNPRO     text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR wa_bdcdata.
  wa_bdcdata-program  program.
  wa_bdcdata-dynpro   dynpro.
  wa_bdcdata-dynbegin 'X'.
  APPEND wa_bdcdata TO it_bdcdata.
ENDFORM.                    "BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FNAM       text
*      -->FVAL       text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
*  IF FVAL <> NODATA.
  CLEAR wa_bdcdata.
  wa_bdcdata-fnam fnam.
  wa_bdcdata-fval fval.
  APPEND wa_bdcdata TO it_bdcdata.
*  ENDIF.
ENDFORM.                    "BDC_FIELD
*&---------------------------------------------------------------------*
*&      Form  REFRESH_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_data .

  REFRESH it_final[]it_bdcdata[]it_error[],it_temp[]it_eket[].
  CLEAR wa_errorwa_finalwa_bdcdatawa_tempname_of_file.

ENDFORM.                    " REFRESH_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file .

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    'P_FILE'
    IMPORTING
      file_name     name_of_file.

  p_file name_of_file.

ENDFORM.                    " GET_FILE
*&---------------------------------------------------------------------*
*&      Form  FETCH_BDC_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fetch_bdc_data .

  DATA it_msg      TYPE STANDARD TABLE OF bdcmsgcoll,
         lv_count(2TYPE n,
         wa_eket     TYPE ty_final,
         lv_input    TYPE char50,
         lv_num      TYPE char2,
         wa_msg      TYPE bdcmsgcoll,
         msg(255)    TYPE c,
         lv_mode     TYPE c,
         lv_update   TYPE char1 VALUE 'S',
         lv_row(3)   TYPE n,
         lv_flg      TYPE c,
         lv_rowc     TYPE char3.

  CALL FUNCTION 'WS_UPLOAD'
   EXPORTING
*   CODEPAGE                      = ' '
     filename                      p_file
     filetype                      'DAT'
*   HEADLEN                       = ' '
*   LINE_EXIT                     = ' '
*   TRUNCLEN                      = ' '
*   USER_FORM                     = ' '
*   USER_PROG                     = ' '
*   DAT_D_FORMAT                  = ' '
* IMPORTING
*   FILELENGTH                    =
    TABLES
      data_tab                      it_final
   EXCEPTIONS
     conversion_error              1
     file_open_error               2
     file_read_error               3
     invalid_type                  4
     no_batch                      5
     unknown_error                 6
     invalid_table_width           7
     gui_refuse_filetransfer       8
     customer_error                9
     no_authority                  10
     OTHERS                        11
            .
  IF sy-subrc <> 0.
*    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  REFRESH it_msg[].
  CLEAR wa_eketwa_msglv_inputlv_countmsg.

  IF it_final IS NOT INITIAL.

    it_temp it_final.
    SORT it_temp BY ebeln ebelp.
    DELETE ADJACENT DUPLICATES FROM it_temp COMPARING ebeln ebelp.

  ENDIF.
  SORT it_final BY ebeln ebelp.

  CLEAR wa_tempwa_finalwa_eket.
  LOOP AT it_temp INTO wa_temp.

    PERFORM bdc_dynpro      USING 'SAPMM06E' '0205'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'RM06E-EVRTN'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '/00'.
    PERFORM bdc_field       USING 'RM06E-EVRTN'
                                  wa_temp-ebeln.

    LOOP AT it_final INTO wa_final WHERE ebeln wa_temp-ebeln AND ebelp wa_temp-ebelp.


      wa_eket wa_final.

*      AT NEW ebelp.
*        CLEAR: lv_row, lv_rowc.
*        lv_row = wa_temp-ebelp.
*        DIVIDE lv_row BY 10.
*        lv_rowc = lv_row.
*        lv_count = 01.
*
*        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
*
*        PERFORM bdc_field       USING 'BDC_OKCODE'
*                                      '=ET'.
*        CLEAR lv_input.
**        CONCATENATE 'RM06E-TCSELFLAG(' wa_eket-rowid ')' INTO lv_input.
**        CONCATENATE 'RM06E-TCSELFLAG(' lv_rowc ')' INTO lv_input.
*        CONCATENATE 'RM06E-TCSELFLAG(' lv_row ')' INTO lv_input.
*        PERFORM bdc_field       USING lv_input
*                                      'X'.
*        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
*
*      ENDAT.
************************************************************
      AT NEW ebelp.
        CLEARlv_rowlv_rowclv_flg.
        lv_row wa_temp-ebelp.
        lv_rowc lv_row.
        lv_flg 'X'.
        SHIFT lv_rowc LEFT DELETING LEADING '0'.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'RM06E-EBELP'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '/00'.
        PERFORM bdc_field       USING 'RM06E-EBELP'
                                      lv_rowc.


        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'RM06E-EVRTP(01)'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=ET'.
        PERFORM bdc_field       USING 'RM06E-EBELP'
                                      lv_rowc.
      ENDAT.


*---> Begin of insertion DEVK903470
**  Added page up logic when control comes to screen 1117 for first time
      IF lv_flg 'X'.
        CLEAR lv_flg.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=P-'.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=P-'.
      ENDIF.
*<--- End of insertion DEVK903470
      PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.

      PERFORM bdc_field       USING 'BDC_CURSOR'
                                    'EKET-MENGE(01)'.
      PERFORM bdc_field       USING 'BDC_OKCODE'
                                '/00'.

*      PERFORM bdc_field       USING 'RM06E-ETNR1'
*                                wa_eket-rowid.
*
*      PERFORM bdc_field       USING 'BDC_OKCODE'
*                                '/00'.

      CLEARlv_numlv_input.
*---> Begin of insertion DEVK903470
      lv_num wa_eket-rowid.
      SHIFT lv_num RIGHT DELETING TRAILING space.
      OVERLAY lv_num WITH '00'.
      CONCATENATE 'EKET-MENGE(' lv_num ')' INTO lv_input.
*<--- End of insertion DEVK903470
*---> Begin of deletion DEVK903470
*      CONCATENATE 'EKET-MENGE(' wa_eket-rowid ')' INTO lv_input.
*<--- End of deletion DEVK903470

      PERFORM bdc_field       USING lv_input
                                wa_eket-menge.
      AT END OF ebelp.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BACK'.
      ENDAT.
************************************************************
*---> Commented code begin
*      IF lv_count = 01.
*        PERFORM bdc_field       USING 'BDC_OKCODE'
*                                  '=BACK'.
**    perform bdc_field       using 'RM06E-ETNR1'
**                              '   1'.
*      ENDIF.
*
*      CLEAR lv_input.
**      CONCATENATE 'EKET-MENGE(' lv_count ')' INTO lv_input.
*      CONCATENATE 'EKET-MENGE(' wa_eket-rowid ')' INTO lv_input.
*      PERFORM bdc_field       USING lv_input
*                                  wa_eket-menge.
*      lv_count = lv_count + 1.
*      CLEAR : wa_eket, wa_final.
*<---- Commented end

    ENDLOOP.

    PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'RM06E-EVRTP(01)'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BU'.

    PERFORM bdc_dynpro      USING 'SAPLSPO1' '0300'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=YES'.

    CLEARlv_modewa_temp.
    lv_mode 'N'.

    CALL TRANSACTION 'ME38' USING it_bdcdata MODE lv_mode UPDATE lv_update MESSAGES INTO it_msg.
    REFRESH it_bdcdata[].

    LOOP AT it_msg INTO wa_msg WHERE msgtyp NE 'W'.

      CALL FUNCTION 'FORMAT_MESSAGE'
       EXPORTING
         id              wa_msg-msgid
*   LANG            = '-D'
         no              wa_msg-msgnr
         v1              wa_msg-msgv1
         v2              wa_msg-msgv2
         v3              wa_msg-msgv3
         v4              wa_msg-msgv4
       IMPORTING
         msg             msg
       EXCEPTIONS
         not_found       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.

      WRITE:/ msg.

    ENDLOOP.

    REFRESH it_msg[].

  ENDLOOP.

ENDFORM.                    " FETCH_BDC_DATA

No comments:

Post a Comment