Sunday, May 18, 2014

Table Control BDC

*&---------------------------------------------------------------------*
*& Report  ZMM_MI04_BDC
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZMM_MI04_BDC.




DATA:  bdcdata_wa  TYPE bdcdata,
       bdcdata_tab TYPE TABLE OF bdcdata,
       opt         TYPE ctu_params.


DATA: it_bdcdata  LIKE bdcdata OCCURS 0 WITH HEADER LINE.

*----- Messages of call transaction
DATA: it_bdcmsgcoll  LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.


*----- Local Variable to Capture the G/L Account Data
DATA:      lv_dynnr      LIKE t019-dynnr,   "Screen Number
           lv_winfk      LIKE t019w-winfk,  "Window Function Code
           lv_winnr      LIKE t019w-winnr,  "Window Number
           lv_mpool      LIKE t019w-mpool,  "Module Pool
           lv_opt        LIKE ctu_params.   "Screen Mode
*lv_opt = 'N'.
*----- To Capture the File Path into the string
DATA:    lv_file_string  TYPE string,
         lv_messg        TYPE string,
         lv_ok_code(5)   TYPE c.

DATA: V_COUNT1 TYPE I.
DATA: V_COUNT(3) TYPE C.
DATA: V_CHAR(25).

*----------------------------------------------------------------------*
*  Constants Declaration
*----------------------------------------------------------------------*
CONSTANTS:  c_delim      TYPE c                  VALUE cl_abap_char_utilities=>horizontal_tab,
            c_nodata                             VALUE ' ',
            c_char_a     TYPE c                  VALUE 'A',
            c_char_d     TYPE c                  VALUE 'D',
            c_char_f     TYPE c                  VALUE 'F',
            c_char_k     TYPE c                  VALUE 'K',
            c_char_o     TYPE c                  VALUE 'O',
            c_char_p     TYPE c                  VALUE 'P',
            c_char_m     TYPE c                  VALUE 'M',
            c_char_s     TYPE c                  VALUE 'S',
            c_char_w     TYPE c                  VALUE 'W',
            c_char_x     TYPE c                  VALUE 'x'.

TYPES: begin of ty_final,
* data element: IBLNR
        IBLNR_001(010),
* data element: GJAHR
        GJAHR_002(004),
* data element: DZLDAT
        ZLDAT_003(010),
* data element: I_ERFMG
        ERFMG(017),
*  * data element: XNULL
        XNULL(001),
     end of ty_final.

 TYPES :BEGIN OF ty_final_temp,
* data element: I_ERFMG
        ERFMG(017),
*  * data element: XNULL
        XNULL(001),
END OF ty_final_temp.

          DATA : it_final TYPE STANDARD TABLE OF ty_final,
                 wa_final TYPE ty_final,
                 it_final_temp TYPE STANDARD TABLE OF ty_final_temp,
                 wa_final_temp TYPE ty_final_temp.
*          Data: v_matnr like wa_final-matnr,
*       v_werks like wa_final-werks,
*       v_vornr like wa_final-vornr value '0010'.

daTA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.


PARAMETERS: P_FILE TYPE LOCALFILE DEFAULT 'D:\BDC\MI04.TXT'.
   START-OF-SELECTION.
DATA: W_FILE TYPE STRING.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.

START-OF-SELECTION.

*  v_mode = p_mode.
  w_file = p_file.

CALL FUNCTION 'GUI_UPLOAD'
   EXPORTING
      filename                = w_file
      filetype                = 'DAT'
      has_field_separator     = 'X'
   TABLES
     DATA_TAB                      = IT_FINAL
          .
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*  CALL FUNCTION 'F4_FILENAME'
*    IMPORTING
*      file_name = p_file.

BREAK 10115.
LOOP AT it_final INTO wa_final.

  MOVE-CORRESPONDING wa_final to wa_final_temp.

  APPEND wa_final_temp to it_final_temp.
ENDLOOP.


*PERFORM getdata.
PERFORM bdc.

END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  BDC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BDC .


*  BREAK 10115.
*if sy-subrc <> 0. exit. endif.
*  READ TABLE it_final INTO wa_final INDEX sy-tabix.
LOOP AT it_final INTO wa_final.

perform bdc_dynpro      using 'SAPMM07I' '0701'.
perform bdc_field       using 'BDC_CURSOR'
                              'RM07I-IBLNR'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'RM07I-IBLNR'
                              wa_final-IBLNR_001.
perform bdc_field       using 'RM07I-GJAHR'
                              wa_final-GJAHR_002.
perform bdc_field       using 'RM07I-ZLDAT'
                              wa_final-ZLDAT_003.
perform bdc_dynpro      using 'SAPMM07I' '0731'.
perform bdc_field       using 'BDC_CURSOR'
                              'ISEG-XNULL(06)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=P++'.
*endon.



LOOP at it_final_temp INTO wa_final_temp.
  BREAK 10115.
 IF  V_COUNT = 7.
  CLEAR V_COUNT.
  CLEAR V_COUNT1.
perform bdc_dynpro      using 'SAPMM07I' '0731'.
perform bdc_field       using 'BDC_CURSOR'
                              'ISEG-XNULL(06)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=P++'.
 ENDIF.
*IF  v_count LE 8.
V_COUNT1 = V_COUNT1 + 1.
V_COUNT = V_COUNT1.
CONDENSE V_COUNT.
CLEAR V_CHAR.

*perform bdc_field       using 'ISEG-MATNR(01)'
*                              wa_final-MATNR_01_008.
*perform bdc_field       using 'ISEG-ERFMG(01)'
*                              record-ERFMG_01_004.

CLEAR V_CHAR.
CONCATENATE 'ISEG-ERFMG(' V_COUNT ')' INTO V_CHAR.
perform bdc_field       using v_char
                            wa_final_TEMP-ERFMG.

*perform bdc_field       using 'ISEG-XNULL(01)'
*                              record-XNULL_01_010.

CLEAR V_CHAR.
CONCATENATE 'ISEG-XNULL(' V_COUNT ')' INTO V_CHAR.
perform bdc_field       using v_char
                            wa_final_TEMP-XNULL.

*IF  V_COUNT GE 16.
*
*
*
*perform bdc_dynpro      using 'SAPMM07I' '0721'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'ISEG-MATNR(02)'.
*perform bdc_field       using 'BDC_OKCODE'
*                              '=P++'.
*
*CLEAR V_CHAR.
*CONCATENATE 'ISEG-MATNR(' V_COUNT ')' INTO V_CHAR.
*perform bdc_field       using v_char
*                            wa_final_TEMP-MATNR.
*
*
*perform bdc_field       using 'ISEG-MATNR(01)'
*                              wa_final_TEMP-MATNR.
*ENDIF.







*ENDIF.



endloop.
perform bdc_dynpro      using 'SAPMM07I' '0731'.
perform bdc_field       using 'BDC_CURSOR'
                              'ISEG-ERFMG(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.


lv_opt = 'A'.
*------ Call the Tcode for the data upload
     CALL TRANSACTION 'MI04'
          USING it_bdcdata
          OPTIONS FROM lv_opt
          MESSAGES INTO it_bdcmsgcoll.

     CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         wait = 'X'.
*perform bdc_field   using 'BDC_OKCODE'
*                               '=BU'.
    REFRESH it_bdcdata.
*  endat.
*ENDLOOP.
COMMIT WORK.

*ENDIF.
ENDLOOP.

*------ Looping at BDCMSGCOLL to Track the Errors
   LOOP AT it_bdcmsgcoll.
     CALL FUNCTION 'FORMAT_MESSAGE'
       EXPORTING
         id        = it_bdcmsgcoll-msgid
         lang      = it_bdcmsgcoll-msgspra
         no        = it_bdcmsgcoll-msgnr
         v1        = it_bdcmsgcoll-msgv1
         v2        = it_bdcmsgcoll-msgv2
         v3        = it_bdcmsgcoll-msgv3
         v4        = it_bdcmsgcoll-msgv4
       IMPORTING
         msg       = lv_messg
       EXCEPTIONS
         not_found = 1
         OTHERS    = 2.
     IF sy-subrc <> 0.                                       "#EC
       MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
     ELSE.
       WRITE:/ it_bdcmsgcoll-msgtyp, lv_messg.
     ENDIF.
   ENDLOOP.

ENDFORM.                    " BDC
*&---------------------------------------------------------------------*
**&      Form  BDC_DYNPRO
**&---------------------------------------------------------------------*
**       text
**----------------------------------------------------------------------*
**      -->P_0323   text
**      -->P_0324   text
**----------------------------------------------------------------------*
FORM bdc_dynpro  USING  program dynpro.
*                          value(p_0324).

   CLEAR it_bdcdata.
   it_bdcdata-program  = program.
   it_bdcdata-dynpro   = dynpro.
   it_bdcdata-dynbegin = 'X'.
   APPEND it_bdcdata.
ENDFORM.                    " BDC_DYNPRO
*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0328   text
*      -->P_0329   text
*----------------------------------------------------------------------*
FORM bdc_field  USING   fnam fval.
   "  value(p_0328)
   "value(p_0329).
  BREAK 10115.
   IF fval <> c_nodata.
     CLEAR it_bdcdata.
     it_bdcdata-fnam = fnam.
     it_bdcdata-fval = fval.
     APPEND it_bdcdata.
  ENDIF.

ENDFORM.