Schedule Agreement Data Uploading

*& Report  ZMM_ME31L_UPLOAD
*& Developed by  : HARI
*& Description   : Schedule agreement data uploading

*&---------------------------------------------------------------------

REPORT  ZMMA_ME31L_UPLOAD
  NO STANDARD PAGE HEADING
  LINE-SIZE 255.

*----------------------------------------------------------------------*
* Include for Types, WA, Itab, Constants, Global Variable declarations *
*----------------------------------------------------------------------*

include ZMMA_ME31L_UPLOAD_TOP.

*----------------------------------------------------------------------*
*   At Selection-screen event
*----------------------------------------------------------------------*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM f_get_filename USING p_file.

*----------------------------------------------------------------------*
*   Start-of-Selection event
*----------------------------------------------------------------------*

START-OF-SELECTION.

* Subroutine to upload the data into Internal table I_Upload.

PERFORM get_data.

i_ctu_params-defsize = 'X'.
i_ctu_params-NOBINPT = ''.
*i_ctu_params-DISMODE = p_mode.
i_ctu_params-UPDMODE = p_upd.


LOOP AT i_upload where lifnr is not initial.

  v_index = sy-tabix.

** Create Scheduling Agreement: Initial Screen

    perform bdc_dynpro      using 'SAPMM06E' '0200'.

    perform bdc_field       using 'BDC_CURSOR'
                                  'RM06E-LGORT'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=KOPF'.
    perform bdc_field       using 'EKKO-LIFNR'
                                  I_Upload-Lifnr.
    perform bdc_field       using 'RM06E-EVART'
                                  I_Upload-evart.
    perform bdc_field       using 'RM06E-VEDAT'
                                  i_upload-vedat.
    perform bdc_field       using 'EKKO-EKORG'
                                  i_upload-ekorg.
    perform bdc_field       using 'EKKO-EKGRP'
                                  i_upload-ekgrp.
    perform bdc_field       using 'RM06E-WERKS'
                                  i_upload-werks.
    perform bdc_field       using 'RM06E-LGORT'
                                i_upload-lgort.

* Create Scheduling Agreement: Header Data

    perform bdc_dynpro      using 'SAPMM06E' '0201'.

    perform bdc_field       using 'BDC_CURSOR'
                                  'EKKO-KDATE'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=AB'.
    perform bdc_field       using 'EKKO-KDATB'
                                  i_upload-kdatb.
    perform bdc_field       using 'EKKO-KDATE'
                                  i_upload-kdate.
  cnt1 = 0.

  j = 0.
  i = 1.

  DO.

* this loop for item values.
*    cnt1 = cnt1 + 1.
*    g_count1 = cnt1.
*
*
** filling the item details.
*
*** Begin of New changes

  if j EQ 14.

   v_ebelp = i - 1.

    perform bdc_dynpro  using 'SAPMM06E'  '0220'.

    perform bdc_field   using 'BDC_CURSOR'
                               'RM06E-EBELP'.
    perform bdc_field   using 'BDC_OKCODE'
*                               '=P+'.
                                '/00'.
    perform bdc_field   using  'RM06E-EBELP'
                                v_ebelp.
    j = 1.

    cnt1 = 2.
*    cnt1 = cnt1 + 1.
    g_count1 = cnt1.

      PERFORM f_fill_item_details.

   else.

    cnt1 = cnt1 + 1.
    g_count1 = cnt1.

      PERFORM f_fill_item_details.

   endif.


* filling the item details.

   i = i + 1.
   j = j + 1.

*** Ended New changes
    cnt2 = 0.
   g_count2 = 0.

   do.

* this loop for item condition condition values.
     cnt2  = cnt2 + 1.
     g_count2 = cnt2.

* filling the item conditions per each item value.

   PERFORM f_fill_item_condns.

     v_index = v_index + 1.
     read table i_upload index v_index.
     if sy-subrc eq 0.
       if not i_upload-ematn is initial.
         exit.
       endif.
     else.
       exit.
     endif.

    enddo.

  perform bdc_dynpro  using 'SAPMV13A' '0201'.

  perform bdc_field   using 'BDC_CURSOR'
                            'RV13A-DATAB'.
  perform bdc_field   using 'BDC_OKCODE'
                              '=BACK  '.
*   perform bdc_dynpro  using 'SAPMM06E' '0211'.

   read table i_upload index v_index.
   if sy-subrc eq 0.
     if not i_upload-lifnr is initial.
       exit.
     endif.
   else.
     exit.
   endif.

  ENDDO.

** confirmation for each new vendor save yes/no.
    perform bdc_dynpro  using 'SAPMM06E' '0220'.

    PERFORM f_get_field USING 'RM06E-EVRTP'
                                 g_count1.
    perform bdc_field  using 'BDC_CURSOR'
                               g_field.
    perform bdc_field  using 'BDC_OKCODE'
*                               '=BACK'.
                                '=BU'.
*    perform bdc_dynpro      using 'SAPLSPO1' '0100'.
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '=YES'.

 PERFORM bdc_transaction using 'ME31L'.

 ENDLOOP.
 clear i_upload.
 refresh i_upload.
 free i_upload.
 PERFORM bdc_close_group.

*----------------------------------------------------------------------*
*   End-of-Selection event
*----------------------------------------------------------------------*
*
 END-OF-SELECTION.

 SKIP 2.

 write:/ 'Total Number of Transactions : ' color 6, g_tot.
    SKIP 1.

 write:/ 'Total Number of Success Transactions : ' color 5, g_success.
   SKIP 1.

 write:/ 'Total Number of Error Transactions : ' color 1, g_error.

   SKIP 1.
if g_error <> 0.
format color 3.
write:/ 'Please go to Transaction SM35 to process the errors' hotspot.
endif.

*----------------------------------------------------------------------*
*   At line-Selection event
*----------------------------------------------------------------------*
*
AT LINE-SELECTION.

if SY-LISEL = 'Please go to Transaction SM35 to process the errors'.
call transaction 'SM35'.
endif.
*
*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*

form bdc_dynpro using program dynpro.
  clear bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  append bdcdata.
endform.

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*

form bdc_field using fnam fval.
  clear bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  append bdcdata.
endform.

*----------------------------------------------------------------------*
*        Start new transaction according to parameters                 *
*----------------------------------------------------------------------*

form bdc_transaction using tcode.

data: l_mstring(480).
data: l_subrc like sy-subrc.
  refresh messtab.

CALL TRANSACTION TCODE USING BDCDATA
*                     MODE   p_mode
*                     UPDATE p_upd
                     messages into messtab
                     OPTIONS FROM i_ctu_params.

** counting for success records.
  IF sy-SUBRC <> 0.
    g_error = g_error + 1.
    IF E_GROUP_OPENED = ' '.
      CALL FUNCTION 'BDC_OPEN_GROUP'
        EXPORTING
          CLIENT = SY-MANDT
          GROUP  = p_sesion
          USER   = sy-uname
          KEEP   = 'X'.
      E_GROUP_OPENED = 'X'.
    ENDIF.
    CALL FUNCTION 'BDC_INSERT'
      EXPORTING
        TCODE     = TCODE
      TABLES
        DYNPROTAB = BDCDATA.
  else.

** counting for error records
   g_success = g_success + 1.
  ENDIF.

  g_tot = g_tot + 1.

  REFRESH BDCDATA.
*
*  l_subrc = sy-subrc.
*  write: / 'CALL_TRANSACTION',
*           tcode,
*           'returncode:'(i05),
*           l_subrc,
*           'RECORD:',
*           sy-index.
*  loop at messtab.
*    select single * from t100 where sprsl = messtab-msgspra
*                              and   arbgb = messtab-msgid
*                              and   msgnr = messtab-msgnr.
*    if sy-subrc = 0.
*      l_mstring = t100-text.
*      if l_mstring cs '&1'.
*        replace '&1' with messtab-msgv1 into l_mstring.
*        replace '&2' with messtab-msgv2 into l_mstring.
*        replace '&3' with messtab-msgv3 into l_mstring.
*        replace '&4' with messtab-msgv4 into l_mstring.
*      else.
*        replace '&' with messtab-msgv1 into l_mstring.
*        replace '&' with messtab-msgv2 into l_mstring.
*        replace '&' with messtab-msgv3 into l_mstring.
*        replace '&' with messtab-msgv4 into l_mstring.
*      endif.
*      condense l_mstring.
*      write: / messtab-msgtyp, l_mstring(250).
*    else.
*      write: / messtab.
*    endif.
*  endloop.
*  skip.
*  refresh bdcdata.
endform.

*&---------------------------------------------------------------------*
*&      Form  Get_data
* write the code to upload the file data in internal table I_UPLOAD.
*----------------------------------------------------------------------*

form get_data .

clear i_upload.
refresh i_upload.

CALL FUNCTION 'WS_UPLOAD'
 EXPORTING
   FILENAME                      = p_file
   FILETYPE                      = 'DAT'
  TABLES
    data_tab                      = i_upload
 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.

endform.                    " Get_data

*&---------------------------------------------------------------------*
*&      Form  f_fill_item_details
*----------------------------------------------------------------------*

form f_fill_item_details.

 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = g_count1
    IMPORTING
      output = g_count1.


** Create Scheduling Agreement: Item overview

    perform bdc_dynpro   using 'SAPMM06E' '0220'.

    PERFORM f_get_field USING 'EKPO-KTMNG'
                                 g_count1.
    perform bdc_field   using 'BDC_CURSOR'
                                 g_field.
    perform bdc_field   using 'BDC_OKCODE'
*                                '=DETZ'.
                                 '/00'.
*    PERFORM f_get_field  USING  'RM06E-TCSELFLAG'
*                                 g_count1.
*    perform bdc_field   using g_field
*                              'X'.

* Begin of changes on DEVK903540 Request by Hari.

    PERFORM f_get_field  USING 'EKPO-KNTTP'
                                 g_count1.
    perform bdc_field   using g_field
                              i_upload-knttp.

* End of changes on DEVK903540 Request by Hari.

    PERFORM f_get_field  USING 'EKPO-EMATN'
                                 g_count1.
    perform bdc_field   using g_field
                              i_upload-ematn.

    PERFORM f_get_field  USING 'EKPO-KTMNG'
                                 g_count1.
    perform bdc_field  using g_field
                            i_upload-ktmng.

*    PERFORM f_get_field USING 'EKPO-NETPR'
*                                  g_count1.
*    perform bdc_field  using g_field
*                              '           1'.

* Begin of changes on DEVK903540 Request by Hari.

    IF i_upload-knttp NE SPACE.


** getting GL A/C Details.

    perform bdc_dynpro   using 'SAPMM06E' '0511'.

    perform bdc_field   using 'BDC_CURSOR'
                                  'EKKN-SAKTO'.
    perform bdc_field   using 'BDC_OKCODE'
                                  '=ENTE'.
    perform bdc_field   using 'EKKN-SAKTO'
                               i_upload-sakto.

** getting Cost Center value.

    perform bdc_dynpro   using 'SAPLKACB' '0002'.

    perform bdc_field   using 'BDC_CURSOR'
                                  'COBL-KOSTL'.
    perform bdc_field   using 'BDC_OKCODE'
                                  '=ENTE'.
    perform bdc_field   using 'COBL-KOSTL'
                               i_upload-kostl.

ENDIF.

** Create Scheduling Agreement: Item detials

    perform bdc_dynpro  using 'SAPMM06E'  '0220'.

    PERFORM f_get_field USING 'RM06E-EVRTP'
                                g_count1.
    perform bdc_field   using 'BDC_CURSOR'
                                   g_field.
    perform bdc_field   using 'BDC_OKCODE'
                                  '=DETA'.
*    perform bdc_field   using 'RM06E-EBELP'
*                                  '1'.
    PERFORM f_get_field USING  'RM06E-TCSELFLAG'
                                g_count1.
    perform bdc_field   using g_field
                               'X'.


    perform bdc_dynpro  using 'SAPMM06E'  '0211'.

    perform bdc_field   using 'BDC_CURSOR'
                               'EKPO-MWSKZ'.
    perform bdc_field   using 'BDC_OKCODE'
                                '/00'.
    perform bdc_field       using 'EKPO-BSTAE'
                                  i_upload-bstae.

    perform bdc_field   using 'EKPO-MWSKZ'
                               i_upload-mwskz.


* End of changes on DEVK903540 Request by Hari.


*** Create Scheduling Agreement: Item overview
*
    perform bdc_dynpro  using 'SAPMM06E'  '0220'.

    PERFORM f_get_field USING 'RM06E-EVRTP'
                                g_count1.
    perform bdc_field   using 'BDC_CURSOR'
                                   g_field.
    perform bdc_field   using 'BDC_OKCODE'
                                  '=DETZ'.
*    perform bdc_field   using 'RM06E-EBELP'
*                                  '1'.
    PERFORM f_get_field USING  'RM06E-TCSELFLAG'
                                g_count1.
    perform bdc_field   using g_field
                               'X'.

** Create Scheduling Agreement: Additional data

    perform bdc_dynpro  using 'SAPMM06E' '0212'.

    perform bdc_field   using 'BDC_CURSOR'
                                  'EKPO-ETFZ2'.
    perform bdc_field   using 'BDC_OKCODE'
                                  '/00'.
    perform bdc_field   using 'EKPO-ETFZ1'
                               i_upload-etfz1.
    perform bdc_field   using  'EKPO-ETFZ2'
                               i_upload-etfz2.

* Begin of Add DEVK903540 Request by Hari.

    perform bdc_field   using 'EKPO-KZSTU'
                               i_upload-kzstu.

* End of Add on DEVK903540 Request by Hari.

** Create Scheduling Agreement: Item overview

    perform bdc_dynpro  using 'SAPMM06E'  '0220'.

    PERFORM f_get_field  USING 'RM06E-EVRTP'
                                  g_count1.
    perform bdc_field   using 'BDC_CURSOR'
                                   g_field.
    perform bdc_field   using 'BDC_OKCODE'
                                  '=KO'.
*    perform bdc_field   using 'RM06E-EBELP'
*                                  '1'.
    PERFORM f_get_field USING  'RM06E-TCSELFLAG'
                                g_count1.
    perform bdc_field   using g_field
                                 'X'.

  perform bdc_dynpro  using 'SAPMV13A' '0201'.

  PERFORM f_get_field USING 'KONP-KBETR'
                               g_count1.
  perform bdc_field   using 'BDC_CURSOR'
                              g_field.
  perform bdc_field   using 'BDC_OKCODE'
                               '/00'.
  perform bdc_field   using  'RV13A-DATAB'
                             i_upload-datab.
  perform bdc_field   using  'RV13A-DATBI'
                              i_upload-datbi.

endform.                    " f_fill_operation

*&---------------------------------------------------------------------*
*&      Form  f_get_field
*----------------------------------------------------------------------*

form f_get_field  using  p_name  p_count1  .

 CLEAR: g_field.

   CONCATENATE p_name '(' p_count1 ')' INTO g_field.

endform.                    " f_get_field

*&---------------------------------------------------------------------*
*&      Form  f_fill_item_condns
*----------------------------------------------------------------------*

form f_fill_item_condns .


 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = g_count2
    IMPORTING
      output = g_count2.

  PERFORM f_get_field2 USING 'KONP-KSCHL'
                              g_count2.
  perform bdc_field   using  g_field2
                                i_upload-kschl.

  PERFORM f_get_field2 USING 'KONP-KBETR'
                               g_count2.
  perform bdc_field   using  g_field2
                                i_upload-kbetr.

* Begin of changes on DEVK903540 Request by Hari.

  PERFORM f_get_field2 USING 'KONP-KPEIN'
                               g_count2.
  perform bdc_field   using  g_field2
                                i_upload-kpein.

* End of changes on DEVK903540 Request by Hari.

endform.                    " f_fill_item_condns

*&---------------------------------------------------------------------*
*&      Form  f_get_field2
*----------------------------------------------------------------------*

form f_get_field2  using   p_name2  p_count2.

 clear: g_field2.

 CONCATENATE p_name2 '(' p_count2 ')' INTO g_field2.


endform.                    " f_get_field2

*&---------------------------------------------------------------------*
*&      Form  f_get_filename
*----------------------------------------------------------------------*

form f_get_filename  using   p_file.

CALL FUNCTION 'WS_FILENAME_GET'
   EXPORTING
*   DEF_FILENAME           = ' '
*   DEF_PATH               = ' '
   mask                   = ',*.*,*.*.'
   mode                   = 'O'
   title                  = ' '
   IMPORTING
     filename              = p_file
*   RC                     =
 EXCEPTIONS
   inv_winsys             = 1
   no_batch               = 2
   selection_cancel       = 3
   selection_error        = 4
   OTHERS                 = 5   .

endform.                    " f_get_filename

*&---------------------------------------------------------------------*
*&      Form  bdc_close_group
*----------------------------------------------------------------------*

form bdc_close_group .

*   close batchinput group

  IF E_GROUP_OPENED = 'X'.
    CALL FUNCTION 'BDC_CLOSE_GROUP'.
  ENDIF.

endform.                    " bdc_close_group
ABAP Tips by :  Hari

ABAP Tips

Related ABAP Topics:
BDC to Upload Material Master
Upload Material Master - Finish Goods

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Interview Questions, Functional, Basis Administration and ABAP Programming Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.