Translate

Monday, 30 July 2012

BDC Program for BOM Creation (CS01)

REPORT zbdc_CS01
       NO STANDARD PAGE HEADING LINE-SIZE 255.

*&------------Structure Declaration-----------------------------------*&

TYPES: BEGIN OF record,
        matnr(018),        "Material
        werks(004),        "Plant
        stlan(001),        "Bom Usage
        stlal(002),        "Alternative Bom
        idnrk(018),        "Component
        MENGE(005),        "Quantity
        POSTP(001),        "Ict
END OF record.

DATA: IT_FINAL TYPE TABLE OF RECORD ,
      WA_FINAL TYPE RECORD.

*&-----------Selection Screen----------------------------------------*&
PARAMETERS : p_file TYPE rlgrap-filename.
*&-------------------------------------------------------------------*&

DATA : bdcdata TYPE bdcdata OCCURS WITH HEADER LINE.
DATA : d_file TYPE string.
DATA : i_msg TYPE bdcmsgcoll OCCURS WITH HEADER LINE,
       msgtext(100).


TYPES:fs_struct(4096TYPE OCCURS 0 .
DATA: w_struct TYPE fs_struct.

*&---------------Value Request---------------------------------------*&
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     program_name        syst-cprog
     dynpro_number       syst-dynnr
*        FIELD_NAME          = ' '
   IMPORTING
     file_name           p_file.
*&-------------------------------------------------------------------*&

*&------------------start-of-selection------------------------------*&
 START-OF-SELECTION.

  IF p_file IS NOT INITIAL.
    d_file p_file.
  ENDIF.


CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
  EXPORTING
   I_FIELD_SEPERATOR          'X'
*   I_LINE_HEADER              =
    I_TAB_RAW_DATA             W_STRUCT
    I_FILENAME                 P_FILE
  TABLES
    I_TAB_CONVERTED_DATA       IT_FINAL .


*---------------------Excel file format-------------------------------------------------------------*&
* Matnr          WERKS      stlan          stlal                   idnrk          menge       Postp
*(Material)     (Plant)    (Bom Usage)    (Alternative Bom)      (component)    (Quantity)    (ICT)
*
*
*&--------------------------------------------------------------------------------------------------*&


  LOOP AT IT_FINAL INTO WA_FINAL.

    PERFORM bdc_dynpro      USING      'SAPLCSDI' '0100'.

    PERFORM bdc_field       USING       'BDC_CURSOR'
                                        'RC29N-STLAL'.
    PERFORM bdc_field       USING       'RC29N-MATNR'           "material
                                         WA_FINAL-MATNR.
    PERFORM bdc_field       USING       'RC29N-WERKS'
                                         WA_FINAL-werks.        "Plant
    PERFORM bdc_field       USING       'RC29N-STLAN'
                                         WA_FINAL-stlan.        "bom usage
    PERFORM bdc_field       USING       'RC29N-STLAL'
                                         WA_FINAL-stlal.        "alternative bom
    PERFORM bdc_field       USING       'BDC_OKCODE'
                                          '/00'.                 "ENTER


    PERFORM BDC_DYNPRO     USING        'SAPLCSDI'  '0110'.
    PERFORM BDC_FIELD      USING        'BDC_OKCODE'
                                        '/00'.


    PERFORM BDC_DYNPRO    USING        'SAPLCSDI'  '0111'.
    PERFORM BDC_FIELD     USING        'BDC_OKCODE'
                                        '/00'.

    PERFORM BDC_DYNPRO    USING        'SAPLCSDI'  '0140'.
    PERFORM BDC_FIELD     USING        'BDC_CURSOR'
                                        'RC29P-POSTP(01)'.
    PERFORM BDC_FIELD     USING        'RC29P-IDNRK(01)'
                                         WA_FINAL-IDNRK.       "component
    PERFORM BDC_FIELD     USING        'RC29P-MENGE(01)'
                                         WA_FINAL-MENGE.       "Quantity
    PERFORM BDC_FIELD     USING        'RC29P-POSTP(01)'
                                         WA_FINAL-POSTP.       "ICT
    PERFORM  BDC_FIELD    USING       'BDC_OKCODE'
                                         '/00'.                 "ENTER


    PERFORM BDC_DYNPRO     USING          'SAPLCSDI'   '0130'.
    PERFORM BDC_FIELD      USING          'BDC_OKCODE'
                                          '/00'.
    PERFORM BDC_DYNPRO     USING          'SAPLCSDI'  '0131'.
    PERFORM BDC_FIELD      USING          'BDC_OKCODE'
                                          '/00'.
    PERFORM BDC_DYNPRO     USING          'SAPLCSDI'  '0140'.
    PERFORM BDC_FIELD      USING          'BDC_OKCODE'
                                          '=FCBU'.


  CALL TRANSACTION 'CS01' USING bdcdata MODE 'A' UPDATE 'A' MESSAGES INTO i_msg.

    LOOP AT i_msg.

            CALL FUNCTION 'FORMAT_MESSAGE'
              EXPORTING
                id        i_msg-msgid
                lang      sy-langu
                no        i_msg-msgnr
                v1        i_msg-msgv1
                v2        i_msg-msgv2
                v3        i_msg-msgv3
                v4        i_msg-msgv4
             IMPORTING
               msg       msgtext
              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 : / msgtext, WA_FINAL-matnr.
    ENDLOOP.
    REFRESH bdcdata.

  ENDLOOP.

*----------------------------------------------------------------------
*
*        Start new screen
*
*----------------------------------------------------------------------
*
FORM bdc_dynpro USING program dynpro.
  bdcdata-program  program.
  bdcdata-dynpro   dynpro.
  bdcdata-dynbegin 'X'.
  APPEND bdcdata.
  CLEAR bdcdata.
ENDFORM.                    "bdc_dynpro

*----------------------------------------------------------------------
*
*        Insert field
*
*----------------------------------------------------------------------
*
FORM bdc_field USING fnam fval.
  bdcdata-fnam fnam.
  bdcdata-fval fval.
  APPEND bdcdata.
  CLEAR bdcdata.
ENDFORM.                    "bdc_field

No comments: