Program Loads the Bill of Material

*&---------------------------------------------------------------------*
*&   REPORT ZPP0120                                                    *
*&---------------------------------------------------------------------*
*& Module : PP                                                         |
*& Application : The program loads the Bill of Material                |
*&                                                                     |
*----------------------------------------------------------------------*
*| Modification Log                                                    |
*| ----------------                                                    |
*| Programmer                                                          |
*|                                                       Change        |
*| ------------- -----------  ----  -----------  ----------------------|
*| Rajesh Singh                                                        |
*----------------------------------------------------------------------*

REPORT zpp0120 NO STANDARD PAGE HEADING
                                  MESSAGE-ID z0
                                  LINE-SIZE  132
                                  LINE-COUNT 65(2).


*----------------------------------------------------------------------*
*                      Internal Tables                                 *
*----------------------------------------------------------------------*


*Internal table for the BOM file.

DATA: BEGIN OF i_bom OCCURS 0,

      matnr(18),
      werks(4),
      stlan(1),
      stlal(2),
      stktx(40),
      bmeng(16),
      menge(16),
      posnr(4),
      postp(1),
      meins(3),
      datuv(10),
      fmeng(1),
      idnrk(18),
      potx1(40),
      ausch(7),

      END OF i_bom.

* Extracting Document and Doc type for POSTP = D.

DATA : BEGIN OF i_stpo OCCURS 1,

       doknr(25),
       dokar(3),
       posnr(4),
       stlnr LIKE mast-stlnr,

       END   OF i_stpo.

* Local work area

DATA:  g_my_rec_in   LIKE i_bom.

** Declare internal table for Call Transaction and BDC Session

DATA: i_bdc_table LIKE bdcdata OCCURS 0 WITH HEADER LINE.


*----------------------------------------------------------------------*
*                      Global Variables                                *
*----------------------------------------------------------------------*

DATA: g_counter(2) TYPE n,
      g_field_name(18) TYPE c,
      zc_yes  TYPE syftype VALUE 'X'.


*----------------------------------------------------------------------*
*                      Selection Screen                                *
*----------------------------------------------------------------------*

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

PARAMETERS: p_fname1 TYPE localfile .

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b WITH FRAME TITLE text-002.
PARAMETERS: p_rloc1 AS CHECKBOX  DEFAULT 'X'.


SELECTION-SCREEN BEGIN OF BLOCK c WITH FRAME TITLE text-005.
PARAMETERS p_group(12) OBLIGATORY DEFAULT 'ZBOM'.

SELECTION-SCREEN END OF BLOCK c.
SELECTION-SCREEN END OF BLOCK b.
SELECTION-SCREEN END OF BLOCK a.

**WRITE the report header

TOP-OF-PAGE.
  INCLUDE zheading.

*----------------------------------------------------------------------*
*                      Start of selection                              *
*----------------------------------------------------------------------*

START-OF-SELECTION.

* Load Input file
  PERFORM f_load_input_file.

* Create BDC records.
  PERFORM create_bdc_records .


*&---------------------------------------------------------------------*
*&      Form  Create_BDC_records
*&---------------------------------------------------------------------*
*       text:* perform the BDC for the records in the internal table
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_bdc_records .

  DATA: v_stlnr LIKE mast-stlnr.
  DATA: v_postp(1) VALUE 'D'.
  IF NOT i_bom[] IS INITIAL.

** Open BDC session

    PERFORM open_bdc_session.

    SORT i_bom BY matnr werks posnr.

    SELECT  SINGLE stlnr  INTO v_stlnr
    FROM mast
    WHERE    matnr = i_bom-matnr
     AND     werks = i_bom-werks.


    SELECT doknr dokar posnr stlnr
                INTO TABLE i_stpo
                FROM stpo
                FOR ALL ENTRIES IN i_bom
                WHERE postp = v_postp
                AND   posnr = i_bom-posnr
                AND   stlnr = v_stlnr.

    SORT i_stpo BY posnr stlnr.

    LOOP AT i_bom.

      g_my_rec_in = i_bom.

      READ TABLE i_stpo WITH KEY posnr = i_bom-posnr
                                 stlnr = v_stlnr
                                 BINARY SEARCH.


      AT NEW matnr.

        CLEAR i_bdc_table[].
        PERFORM insert_screen_header.

      ENDAT.

* Setting up counter for 20 line items

      IF g_counter = 20.

        g_counter = 1.


      ENDIF.

      PERFORM bdc_field  USING 'BDC_OKCODE'
                                    '/00'.

*next screen
      PERFORM bdc_dynpro USING 'SAPLCSDI' '0140'.

      PERFORM bdc_field  USING 'BDC_CURSOR' 'RC29P-FMENG(01)'.

      PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.

      CONCATENATE 'RC29P-POSNR('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-posnr.

      CONCATENATE 'RC29P-IDNRK('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-idnrk.

      CONCATENATE 'RC29P-MENGE('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-menge.

      CONCATENATE 'RC29P-MEINS('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-meins.

      CONCATENATE 'RC29P-POSTP('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-postp.

      CONCATENATE 'RC29P-FMENG('  g_counter  ')' INTO g_field_name.

      PERFORM bdc_field USING g_field_name i_bom-fmeng.

*next screen
      PERFORM bdc_dynpro USING 'SAPLCSDI' '0130'.

      PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.


* Check the Item category to direct the screen flow accordingly.

      CASE i_bom-postp.

        WHEN 'B'.

          PERFORM item_text_insert.

        WHEN 'Z'.

          PERFORM item_text_insert.

        WHEN 'T'.

          PERFORM item_text_insert.


        WHEN 'N'.

          PERFORM bdc_field   USING 'BDC_CURSOR' 'RC29P-AUSCH'.

          PERFORM bdc_field   USING 'RC29P-AUSCH' i_bom-ausch.

* next screen

          PERFORM item_text_insert.

*next screen

          PERFORM bdc_dynpro  USING 'SAPLCSDI' '0133'.

          PERFORM bdc_field   USING 'BDC_OKCODE' '/00'.

          PERFORM bdc_field   USING 'RC29P-EKORG' ' '.


        WHEN 'D'.


          PERFORM bdc_field   USING 'RC29P-DOKNR' i_stpo-doknr.

          PERFORM bdc_field   USING 'RC29P-DOKAR' i_stpo-dokar.


* next screen


          PERFORM item_text_insert.

* When POSTP = X, L.

        WHEN OTHERS.

          PERFORM bdc_field   USING 'BDC_CURSOR' 'RC29P-AUSCH'.

          PERFORM bdc_field   USING 'RC29P-AUSCH' i_bom-ausch.

* next screen

          PERFORM item_text_insert.

      ENDCASE.

      g_counter = g_counter + 1.


* next screen

      PERFORM bdc_dynpro      USING 'SAPLCSDI' '0140'.

      PERFORM bdc_field       USING 'BDC_CURSOR' 'RC29P-POSNR(01)'.

      PERFORM bdc_field       USING 'BDC_OKCODE' '=FCBU'.


      AT END OF matnr.

        PERFORM insert_bdc_new.

      ENDAT.

    ENDLOOP.

    CLEAR i_bom[].

    PERFORM close_bdc_session.


** Release the BDC sessions created

    PERFORM release_bdc.

  ENDIF.


ENDFORM.                    " open_group


*&---------------------------------------------------------------------*
*&      Form  bdc_dynpro_start
*&---------------------------------------------------------------------*
*       Initialize the screen
*----------------------------------------------------------------------*
*      -->P_G_PROGRAM_1
*      -->P_G_SCREEN
*----------------------------------------------------------------------*
FORM bdc_dynpro USING    p_g_program_1
                                p_g_screen.

  CLEAR i_bdc_table.
  i_bdc_table-program  = p_g_program_1.
  i_bdc_table-dynpro   = p_g_screen.
  i_bdc_table-dynbegin = 'X'.
  APPEND i_bdc_table.

ENDFORM.                    " bdc_dynpro_start

*&---------------------------------------------------------------------*
*&      Form  bdc_field
*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*

FORM bdc_field USING f_name f_value.

  CLEAR i_bdc_table.
  i_bdc_table-fnam = f_name.
  i_bdc_table-fval = f_value.
  APPEND i_bdc_table.

ENDFORM.                    "bdc_insert_field



*&--------------------------------------------------------------------*
*&      Form  open_bdc_session
*&--------------------------------------------------------------------*
*      Open the BDC session
*---------------------------------------------------------------------*
FORM open_bdc_session .


** Open BDC session and creat and update condition records

  CALL FUNCTION 'BDC_OPEN_GROUP'
    EXPORTING
      client                    = sy-mandt
*       DEST                      = FILLER8
      group                     = p_group
*       HOLDDATE                  = FILLER8
      keep                      = 'X'
      user                      = sy-uname
*       RECORD                    = FILLER1
*       PROG                      = SY-CPROG
*     IMPORTING
*       QID                       =
 EXCEPTIONS
   client_invalid            = 1
   destination_invalid       = 2
   group_invalid             = 3
   group_is_locked           = 4
   holddate_invalid          = 5
   internal_error            = 6
   queue_error               = 7
   running                   = 8
   system_lock_error         = 9
   user_invalid              = 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.                    " create_bdc_session
*&---------------------------------------------------------------------*
*&      Form  insert_screen_header
*&---------------------------------------------------------------------*
*       Insert the Header data into the screens
*----------------------------------------------------------------------*

FORM insert_screen_header .

  g_counter = 1.

  PERFORM bdc_dynpro USING 'SAPLCSDI' '0100'.

  PERFORM bdc_field  USING 'BDC_CURSOR' 'RC29N-STLAL'.

  PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.

  PERFORM bdc_field  USING 'RC29N-MATNR' g_my_rec_in-matnr.

  PERFORM bdc_field  USING 'RC29N-WERKS' g_my_rec_in-werks.

  PERFORM bdc_field  USING 'RC29N-STLAN' g_my_rec_in-stlan.

  PERFORM bdc_field  USING 'RC29N-STLAL' g_my_rec_in-stlal.

  PERFORM bdc_field  USING 'RC29N-DATUV' g_my_rec_in-datuv.


* next screen

  PERFORM bdc_dynpro USING 'SAPLCSDI' '0110'.

  PERFORM bdc_field  USING 'BDC_CURSOR' 'RC29K-BMENG'.

  PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.

  PERFORM bdc_field  USING 'RC29K-STKTX' g_my_rec_in-stktx.

  PERFORM bdc_field  USING 'RC29K-BMENG' g_my_rec_in-bmeng.


*next screen

  PERFORM bdc_dynpro USING 'SAPLCSDI' '0111'.

  PERFORM bdc_field  USING 'BDC_CURSOR' 'RC29K-LABOR'.


ENDFORM.                    " insert_screen_header

*&---------------------------------------------------------------------*
*&      Form  insert_bdc
*&---------------------------------------------------------------------*
*      Insert the BDC session
*----------------------------------------------------------------------*

FORM insert_bdc_new .

  CALL FUNCTION 'BDC_INSERT'
     EXPORTING
       tcode                  = 'CS01'
*       POST_LOCAL             = NOVBLOCAL
*       PRINTING               = NOPRINT
*       SIMUBATCH              = ' '
*       CTUPARAMS              = ' '
     TABLES
       dynprotab              = i_bdc_table
  EXCEPTIONS
    internal_error         = 1
    not_open               = 2
    queue_error            = 3
    tcode_invalid          = 4
    printing_invalid       = 5
    posting_invalid        = 6
    OTHERS                 = 7
             .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


  ENDIF.

  CLEAR i_bdc_table[].


ENDFORM.                    " insert_bdc


*&---------------------------------------------------------------------*
*&      Form  close_bdc_session
*&---------------------------------------------------------------------*
*       Close the BDC session
*----------------------------------------------------------------------*

FORM close_bdc_session .

  CALL FUNCTION 'BDC_CLOSE_GROUP'
       EXCEPTIONS
            not_open    = 1
            queue_error = 2
            OTHERS      = 3.

  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.                    " close_bdc_session


*&---------------------------------------------------------------------*
*&      Form  f_load_input_file
*&---------------------------------------------------------------------*
*      Load the file into the Internal table
*----------------------------------------------------------------------*

FORM f_load_input_file.


  IF p_rloc1 = zc_yes.
    CALL FUNCTION 'WS_UPLOAD'
         EXPORTING
              filename                = p_fname1
              filetype                = 'DAT'
         TABLES
              data_tab                = i_bom
         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
              OTHERS                  = 10.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      STOP.
    ENDIF.

  ENDIF.

ENDFORM.                    " f_load_input_file
*&---------------------------------------------------------------------*
*&      Form  release_bdc
*&---------------------------------------------------------------------*
*      Release BDC session
*----------------------------------------------------------------------*

FORM release_bdc.

  SUBMIT rsbdcsub WITH mappe EQ p_group
                  WITH von EQ sy-datum
                  WITH bis EQ sy-datum
                  WITH fehler EQ '.'
                  EXPORTING LIST TO MEMORY
                  AND RETURN.


ENDFORM.                    " release_bdc
*&---------------------------------------------------------------------*
*&      Form  item_text_insert
*&---------------------------------------------------------------------*
*       Repetitive code in the screen flow
*----------------------------------------------------------------------*

FORM item_text_insert.

  PERFORM bdc_dynpro USING 'SAPLCSDI' '0131'.

  PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.

  PERFORM bdc_field  USING 'BDC_CURSOR' 'RC29P-POTX1'.

  PERFORM bdc_field  USING 'RC29P-POTX1' i_bom-potx1.

ENDFORM.                    " item_text_insert
ABAP Tips by : Raj

ABAP Tips

Fast Links:
BDC to Upload Schedule Agreement
Schedule Agreement Data Uploading

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

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

More ABAP Tips

Best regards,
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.