Sample ABAP on Multi-Level Bom with Location Balance

REPORT ZMULTIBOM NO STANDARD PAGE HEADING LINE-SIZE 195
                LINE-COUNT 60(2) MESSAGE-ID Z1.

TABLES: MBEW,           "Material Valuation
        MSLB,           "Special stocks with vendor
        MARA,           "Material Master
        MAKT,           "Material Descriptions
        MARD,           "Material Master: Storage Location/Batch Segment
        MAST,           "BOM Header
        STKO,           "BOM Detail
        STPO,           "Bom Components
        STAS.           "BOM Alternative

* Text Elements
* Plant
SELECT-OPTIONS: S_WERKS FOR MAST-WERKS DEFAULT 'CA',
* Material
                S_MATNR FOR MAST-MATNR,
* Material Type
                S_MTART FOR MARA-MTART.
* Base Quantity
PARAMETERS: S_BASE TYPE I DEFAULT '1'.
* Display up to Level
PARAMETERS: S_LVL  TYPE I DEFAULT '99'.
SELECTION-SCREEN SKIP.
* Include Vendor Stock
PARAMETERS: X_MSLB  AS CHECKBOX.

DATA: W_MATNR LIKE MAPL-MATNR.

DATA: PARENT-MATNR LIKE MAST-MATNR,
      CHILD-MATNR LIKE STPO-IDNRK,
      CHILD-WERKS LIKE MAST-WERKS,
      CHILD-STLAL LIKE MAST-STLAL,
      W_MAKTX LIKE MAKT-MAKTX,
      W_STD TYPE P DECIMALS 5,
      W_MAV TYPE P DECIMALS 5,
      W_MENGE TYPE P DECIMALS 3,
      W_LEVEL TYPE I,
      W_DOT(1),
      W_LVL(12),
      W_LVL1(12),
      W_LVL2(2),
      W_COL TYPE I,
      W_LFLAG,
      W_TTL LIKE MARD-LABST.

DATA: BEGIN OF INT1 OCCURS 50,
        LGORT(6),
        LABST LIKE MARD-LABST,
      END OF INT1.

IF S_BASE EQ 0.
  S_BASE = 1.
ENDIF.

** append multi level routing
SELECT * FROM MAST WHERE WERKS IN S_WERKS AND
     MATNR IN S_MATNR.
  SELECT SINGLE * FROM MARA WHERE MATNR = MAST-MATNR AND
       MTART IN S_MTART.
  IF SY-SUBRC NE 0.
    CONTINUE.
  ENDIF.
  IF MARA-LVORM <> 'X'.
    PARENT-MATNR = MAST-MATNR.
    CHILD-MATNR  = MAST-MATNR.
    CHILD-WERKS  = MAST-WERKS.
    CHILD-STLAL  = MAST-STLAL.
    SELECT SINGLE * FROM MAKT WHERE MATNR = MAST-MATNR AND
         SPRAS = 'E'.
    W_MAKTX = MAKT-MAKTX.
    REFRESH INT1.
    SELECT * FROM MARD WHERE MATNR = MAST-MATNR AND
         WERKS = MAST-WERKS AND
         LABST NE 0.
      MOVE MARD-LGORT TO INT1-LGORT.
      MOVE MARD-LABST TO INT1-LABST.
      APPEND INT1.
    ENDSELECT.
    IF X_MSLB EQ 'X'.
      SELECT * FROM MSLB WHERE MATNR EQ MAST-MATNR AND
           WERKS EQ MAST-WERKS AND
           LBLAB NE 0.
        MOVE MSLB-LIFNR TO INT1-LGORT.
        MOVE MSLB-LBLAB TO INT1-LABST.
        APPEND INT1.
      ENDSELECT.
    ENDIF.
    W_LEVEL = 1.
    NEW-PAGE.
    PERFORM GETCHILD.
  ENDIF.
ENDSELECT.

** get child parts
FORM GETCHILD.
  SELECT * FROM STPO WHERE STLNR = MAST-STLNR
        ORDER BY POSNR.
    SELECT SINGLE * FROM STKO WHERE STLNR = STPO-STLNR
          AND STLAL = MAST-STLAL.
    SELECT SINGLE * FROM STAS WHERE STLNR = STPO-STLNR AND
          STLKN = STPO-STLKN AND
          STLAL = MAST-STLAL.
    IF SY-SUBRC EQ 0.
      CLEAR: W_DOT, W_LVL, W_LVL1.
      PERFORM WRT_DTL.
      CHILD-MATNR = STPO-IDNRK.
      W_LEVEL = W_LEVEL + 1.
      IF W_LEVEL <= S_LVL.
        PERFORM SUBCHILD.
      ENDIF.
      W_LEVEL = W_LEVEL - 1.
    ENDIF.
  ENDSELECT.
ENDFORM.

** get grand children parts
FORM SUBCHILD.
  SELECT SINGLE * FROM MAST WHERE MATNR = CHILD-MATNR
     AND WERKS = CHILD-WERKS.
  IF SY-SUBRC NE 0.
    EXIT.
  ENDIF.
  SELECT * FROM STPO WHERE STLNR = MAST-STLNR
        ORDER BY POSNR.
    SELECT SINGLE * FROM STKO WHERE STLNR = STPO-STLNR AND
      STLAL = MAST-STLAL.
    SELECT SINGLE * FROM STAS WHERE STLNR = STPO-STLNR AND
        STLKN = STPO-STLKN AND
        STLAL = STKO-STLAL.
      IF SY-SUBRC EQ 0.
        W_DOT = '.'.
        PERFORM WRT_DTL.
        CHILD-MATNR = STPO-IDNRK.
        W_LEVEL = W_LEVEL + 1.
        IF W_LEVEL <= S_LVL.
          PERFORM SUBCHILD.
        ENDIF.
        W_LEVEL = W_LEVEL - 1.
        SHIFT W_LVL.
      ENDIF.
    ENDSELECT.
ENDFORM.

TOP-OF-PAGE.
WRITE:/ SY-DATUM,SY-UZEIT,
       85 'ABC PTE LTD',
      182 'Page', SY-PAGNO.
WRITE: / SY-REPID,
         75 'BOM STRUCTURE  (WITH LOCATION BALANCES)',
         182 SY-UNAME.
SKIP.
CLEAR W_LFLAG.
WRITE:/ 'Material No.:', PARENT-MATNR,
        60 'Plant    :', MAST-WERKS.
W_COL = 90.
CLEAR W_TTL.
LOOP AT INT1.
  IF W_COL > 195.
    IF W_LFLAG IS INITIAL.
      WRITE:/ 'Description :', W_MAKTX,
           60 'Base Qty :', S_BASE LEFT-JUSTIFIED.
      W_LFLAG = 'X'.
      W_COL = 90.
    ELSE.
      W_COL = 90.
      SKIP.
    ENDIF.
  ENDIF.
  WRITE AT W_COL 'Loc :'.
  W_COL = W_COL + 6.
  WRITE AT W_COL INT1-LGORT.
  W_COL = W_COL + 7.
  WRITE AT W_COL(12) INT1-LABST LEFT-JUSTIFIED.
  W_COL = W_COL + 16.
  W_TTL = W_TTL + INT1-LABST.
ENDLOOP.
IF W_LFLAG IS INITIAL.
  WRITE:/ 'Description :', W_MAKTX,
       60 'Base Qty :', S_BASE LEFT-JUSTIFIED,
       90 'Total :', W_TTL LEFT-JUSTIFIED.
ELSE.
  WRITE AT W_COL 'Total :'.
  W_COL = W_COL + 8.
  WRITE AT W_COL W_TTL LEFT-JUSTIFIED.
ENDIF.
SKIP.
ULINE.
WRITE: /001 'Level',
        011 'Item',
        017 'Component',
        037 'Description',
        079 '    Per',
        089 'UOM',
        094 ' Std Price',
        106 'Moving Avg',
        119 'Location',
        128 ' Balance',
        139 'Location',
        148 ' Balance',
        159 'Location',
        168 ' Balance',
        179 'Total Quantity'.
ULINE.

FORM WRT_DTL.
  CLEAR: MAKT-MAKTX, W_MAV, W_STD.
  SELECT SINGLE * FROM MAKT WHERE MATNR = STPO-IDNRK AND
       SPRAS = 'E'.
  SELECT SINGLE * FROM MBEW WHERE MATNR = STPO-IDNRK AND
       BWKEY = MAST-WERKS.
  IF SY-SUBRC = 0.
    W_MAV = MBEW-VERPR / MBEW-PEINH.
    W_STD = MBEW-STPRS / MBEW-PEINH.
  ENDIF.
  W_MENGE = ( STPO-MENGE / STKO-BMENG ) * S_BASE.
  W_LVL2 = W_LEVEL.
  CONCATENATE W_DOT W_LVL INTO W_LVL.
  CONCATENATE W_LVL W_LVL2 INTO W_LVL1.
  WRITE: /001 W_LVL1,
          011 STPO-POSNR,
          017 STPO-IDNRK,
          037 MAKT-MAKTX,
          079(8) W_MENGE,
          089 STPO-MEINS,
          094(10) W_STD,
          106(10) W_MAV.
  W_COL = 119.
  CLEAR W_TTL.
  SELECT * FROM MARD WHERE MATNR = STPO-IDNRK AND
       WERKS = MAST-WERKS AND
       LABST NE 0.
    IF W_COL > 166.
      W_COL = 119.
      WRITE AT /W_COL ' '.
    ENDIF.
    WRITE AT W_COL MARD-LGORT.
    W_COL = W_COL + 5.
    WRITE AT W_COL(13) MARD-LABST.
    W_COL = W_COL + 15.
    W_TTL = W_TTL + MARD-LABST.
  ENDSELECT.
  IF X_MSLB EQ 'X'.
    SELECT * FROM MSLB WHERE MATNR = STPO-IDNRK AND
         WERKS = MAST-WERKS AND
         LBLAB NE 0.
      IF W_COL > 166.
        W_COL = 119.
        WRITE AT /W_COL ' '.
      ENDIF.
      WRITE AT W_COL(6) MSLB-LIFNR.
      W_COL = W_COL + 6.
      WRITE AT W_COL(12) MSLB-LBLAB.
      W_COL = W_COL + 14.
      W_TTL = W_TTL + MSLB-LBLAB.
    ENDSELECT.
  ENDIF.
  WRITE AT 179(15) W_TTL.
ENDFORM.

More Function Module
Functions / SAP Script / ALV

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.