BDC PROGRAM UPDATES THE BOM WITH DEPENDANCY

*&---------------------------------------------------------------------*
*& Report Z_PP_BOM_CHG_DEPENDANCY *
*& TCODE : CS02 *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
* Author : Sambasivara. M *
* Date : 19 May 2006
* Description : This BDC program Updates the BOM with Dependancy *
*&---------------------------------------------------------------------*
*Change History
*----------------------------------------------------------------------*
*USER | date | Detailed Description
*----------------------------------------------------------------------*
* | |
* | |
* | |
* | |
* | |
* | |
*----------------------------------------------------------------------*
***** Comments
*This BDC program uploads 12 dependancies, though the program is
*generalised to upload 'N' dependancies. The scrolling down part
*doesn't work. Also the dependencies get uploaded from row1 assuming
*that there are no dependencies previously.
************************************************************************

REPORT Z_PP_BOM_CHG_DEPENDANCY
NO STANDARD PAGE HEADING LINE-SIZE 255.


TABLES:T100, " Messages
MAST,
STPO,
CUOB.


************************************************************************
* DECLARATION OF TABLES *
************************************************************************


DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
*
DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

************************************************************************
* DATA DECLARATIONS *
************************************************************************

************************** Upload File *********************************

DATA: BEGIN OF I_DATA OCCURS 0,
MATNR LIKE RC29N-MATNR,
WERKS LIKE RC29N-WERKS,
STLAN LIKE RC29N-STLAN,
IDNRK LIKE RC29N-MATNR,
KNNAM LIKE RCUKD-KNNAM,
END OF I_DATA.

DATA: BEGIN OF I_HEAD OCCURS 0,
MATNR LIKE RC29N-MATNR,
WERKS LIKE RC29N-WERKS,
STLAN LIKE RC29N-STLAN,
IDNRK LIKE RC29N-MATNR,
END OF I_HEAD.

DATA: BEGIN OF I_IDNRK OCCURS 0,
IDNRK LIKE RC29N-MATNR,
KNNAM LIKE RCUKD-KNNAM,
END OF I_IDNRK.

DATA: WA_IDNRK LIKE LINE OF I_IDNRK.
DATA: ITAB LIKE I_DATA OCCURS 0 WITH HEADER LINE.
DATA: WA_IDATA LIKE LINE OF I_DATA.

*--------- INTERNAL TABLE FOR MESSAGES.

DATA: BEGIN OF I_MESSTAB OCCURS 0,
MATNR LIKE RC29N-MATNR,
MSGNR LIKE BDCMSGCOLL-MSGNR,
MSGID LIKE BDCMSGCOLL-MSGID,
MSGTYP LIKE BDCMSGCOLL-MSGTYP,
L_MSTRING(100),
MSGV1 LIKE BDCMSGCOLL-MSGV1,
MSGV2 LIKE BDCMSGCOLL-MSGV2,
MSGV3 LIKE BDCMSGCOLL-MSGV3,
MSGV4 LIKE BDCMSGCOLL-MSGV4,
DYNAME LIKE BDCMSGCOLL-DYNAME,
DYNUMB LIKE BDCMSGCOLL-DYNUMB,
END OF I_MESSTAB.

*----- INTERNAL TABLE TO DISPLAY LOG

DATA: BEGIN OF I_ERROR OCCURS 0,
MATNR LIKE RC29N-MATNR,
MSGTYP LIKE BDCMSGCOLL-MSGTYP, " Message Type
L_MSTRING(480), " Message Text
END OF I_ERROR.

DATA: P_FNAME1 TYPE STRING,
V_MSGV1(18),
V_MSGV2(18),
V_COUNT TYPE I,
A(15),
M TYPE I VALUE 1,
N TYPE I VALUE 1,
V_IDNRK LIKE RC29N-MATNR.

A = 'RCUKD-KNNAM( )'.


************************************************************************
* SELECTION SCREEN *
************************************************************************

SELECTION-SCREEN: BEGIN OF BLOCK A WITH FRAME.
PARAMETERS: P_FILE LIKE RLGRAP-FILENAME.
PARAMETERS: P_ERROR LIKE RLGRAP-FILENAME DEFAULT 'C:\bomdep_log.xls'.
SELECTION-SCREEN: END OF BLOCK A.

************************************************************************
* INPUT SCREEN VALIDATIONS *
************************************************************************

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM GET_FILE.

************************************************************************
* START OF SELECTION *
************************************************************************


START-OF-SELECTION.

PERFORM UPLOAD_ITAB.
PERFORM PROCESS_ITAB.

LOOP AT I_DATA.

MOVE-CORRESPONDING I_DATA TO WA_IDATA.

AT NEW MATNR.

PERFORM BDC_DYNPRO USING 'SAPLCSDI' '0100'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RC29N-STLAN'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.
PERFORM BDC_FIELD USING 'RC29N-MATNR' WA_IDATA-MATNR.
PERFORM BDC_FIELD USING 'RC29N-WERKS' WA_IDATA-WERKS.
PERFORM BDC_FIELD USING 'RC29N-STLAN' WA_IDATA-STLAN.
*perform bdc_field using 'RC29N-DATUV'
* '05/19/2005'.
ENDAT.

AT NEW IDNRK.

*--------- Logic to count the No. of dependancies already present
* and to upload from next row / line.

*select single * from mast into corresponding fields of mast
*where matnr eq itab-matnr and
* werks eq itab-werks and
* stlan eq itab-stlan.
* if sy-subrc eq 0.
* select single * from stpo into corresponding fields of stpo
*
* where stlnr eq mast-stlnr and
* idnrk eq itab-idnrk.
* if sy-subrc eq 0.
* select count(*) from cuob into v_count where knobj eq
* stpo-knobj.
*case v_count.
*when 0.
* m = 1.
*when 1.
* m = 2.
*when 2.
* m = 3.
*when 3.
* m = 4.
*when 4.
* m = 5.
*when 5.
* m = 6.
*when 6.
* m = 7.
*when 7.
* m = 8.
*when 8.
* m = 9.
*when 9.
* m = 10.
*when 10.
* m = 11.
*when 11.
* m = 12.
*endcase.


PERFORM BDC_DYNPRO USING 'SAPLCSDI' '0150'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RC29P-POSNR(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SETP'.
PERFORM BDC_DYNPRO USING 'SAPLCSDI' '0708'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=CLWI'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RC29P-SELID'.
PERFORM BDC_FIELD USING 'RC29P-SELID' WA_IDATA-IDNRK.

PERFORM BDC_DYNPRO USING 'SAPLCSDI' '0150'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RC29P-AUSKZ(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=WIZU'.
PERFORM BDC_FIELD USING 'RC29P-AUSKZ(01)' 'X'.
ENDAT.


MOVE M TO A+12(2).

M = M + 1.
N = N + 1.
IF M GT 12.
M = 12.
ENDIF.

PERFORM BDC_DYNPRO USING 'SAPLCUKD' '0130'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' A.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.
PERFORM BDC_FIELD USING A WA_IDATA-KNNAM.

* endif.
*endif.
*endif.
*endif.
AT END OF IDNRK.

PERFORM BDC_DYNPRO USING 'SAPLCUKD' '0130'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' A.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SICH'.
M = 1.
ENDAT.


AT END OF MATNR.
PERFORM BDC_DYNPRO USING 'SAPLCSDI' '0150'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RC29P-POSNR(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=FCBU'.

CALL TRANSACTION 'CS02' USING BDCDATA MODE 'N' UPDATE 'S'
MESSAGES INTO MESSTAB.

LOOP AT MESSTAB.
*----- Displaying Messages on the screen
IF MESSTAB-MSGTYP = 'E'.
FORMAT COLOR COL_NEGATIVE ON.
WRITE:/ 'The Material : ', WA_IDATA-MATNR, ' was not updated'.
ELSE.
V_MSGV1 = MESSTAB-MSGV1.
V_MSGV2 = MESSTAB-MSGV2.
PERFORM MESS_TEXT USING MESSTAB-MSGID
MESSTAB-MSGNR.

REPLACE '&' WITH V_MSGV1 INTO T100-TEXT.
REPLACE '&' WITH V_MSGV2 INTO T100-TEXT.

FORMAT COLOR COL_POSITIVE ON.
WRITE:/ T100-TEXT.
ENDIF.
*----- Moving the contents of Messtab to internal table for further
*----- download
I_MESSTAB-MATNR = WA_IDATA-MATNR.
I_MESSTAB-MSGTYP = MESSTAB-MSGTYP.
I_MESSTAB-MSGID = MESSTAB-MSGID.
I_MESSTAB-MSGNR = MESSTAB-MSGNR.
I_MESSTAB-MSGV1 = MESSTAB-MSGV1.
CONDENSE I_MESSTAB-MSGV1.
I_MESSTAB-MSGV2 = MESSTAB-MSGV2.
I_MESSTAB-MSGV3 = MESSTAB-MSGV3.
I_MESSTAB-MSGV4 = MESSTAB-MSGV4.
I_MESSTAB-DYNAME = MESSTAB-DYNAME.
I_MESSTAB-DYNUMB = MESSTAB-DYNUMB.
APPEND I_MESSTAB. CLEAR I_MESSTAB.
ENDLOOP.

*---- Clear Messtab and BDCDATA after each posting

CLEAR MESSTAB. REFRESH MESSTAB.
REFRESH BDCDATA.
ENDAT.
ENDLOOP.


*------ Batch data Processing ends.....

CLEAR: V_MSGV1, V_MSGV2.

*---- Retrieving Message text and modfying message tab

LOOP AT I_MESSTAB.
PERFORM MESS_TEXT USING I_MESSTAB-MSGID
I_MESSTAB-MSGNR.

V_MSGV1 = I_MESSTAB-MSGV1.
V_MSGV2 = I_MESSTAB-MSGV2.
REPLACE '&' WITH V_MSGV1 INTO T100-TEXT.
REPLACE '&' WITH V_MSGV2 INTO T100-TEXT.
I_MESSTAB-L_MSTRING = T100-TEXT.
MODIFY I_MESSTAB INDEX SY-TABIX.
ENDLOOP.


*------ Move required contents of messages for downloading....

LOOP AT I_MESSTAB.
MOVE-CORRESPONDING I_MESSTAB TO I_ERROR.
APPEND I_ERROR.
ENDLOOP.

*----- Downloading the Log file

P_FNAME1 = P_ERROR.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
FILENAME = P_FNAME1
FILETYPE = 'ASC'
* APPEND = ' '
WRITE_FIELD_SEPARATOR = ';'
TABLES
DATA_TAB = I_ERROR.

IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


*&---------------------------------------------------------------------*
*& Form GET_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM GET_FILE . CALL FUNCTION 'WS_FILENAME_GET' EXPORTING * DEF_FILENAME = ' ' DEF_PATH = 'C:\ ' MASK = '****' MODE = 'O' TITLE = 'Select File Name ' IMPORTING FILENAME = P_FILE * RC = EXCEPTIONS INV_WINSYS = 1 NO_BATCH = 2 SELECTION_CANCEL = 3 SELECTION_ERROR = 4 OTHERS = 5 . 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_FILE
*&---------------------------------------------------------------------*
*& Form UPLOAD_ITAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM UPLOAD_ITAB . P_FNAME1 = P_FILE. CALL FUNCTION 'GUI_UPLOAD' EXPORTING FILENAME = P_FNAME1 FILETYPE = 'ASC' HAS_FIELD_SEPARATOR = ';' TABLES DATA_TAB = I_DATA. 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. " UPLOAD_ITAB
*&---------------------------------------------------------------------*
*& Form MESS_TEXT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_I_MESSTAB_MSGID text
* -->P_I_MESSTAB_MSGNR text
*----------------------------------------------------------------------*
FORM MESS_TEXT USING P_MSGID P_MSGNR.

SELECT SINGLE * FROM T100
INTO CORRESPONDING FIELDS OF T100
WHERE SPRSL = SY-LANGU AND
ARBGB = P_MSGID AND
MSGNR = P_MSGNR.
IF SY-SUBRC NE 0.
CLEAR T100.
ENDIF.
ENDFORM. " mess_text


*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDFORM. "BDC_DYNPRO
*
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*

FORM BDC_FIELD USING FNAM FVAL.
* IF FVAL <> NODATA.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
* ENDIF.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form process_itab
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM PROCESS_ITAB .
*itab[] = i_data[].
LOOP AT I_DATA.
MOVE-CORRESPONDING I_DATA TO WA_IDATA.
AT NEW IDNRK.
MOVE-CORRESPONDING WA_IDATA TO I_HEAD.
APPEND I_HEAD.
ENDAT.
ENDLOOP.

ENDFORM. " process_itab