Recursion with Loop Checking in SAP

* This program can be a model for any ABAP recursion.
* This is often needed to navigate down hierarchical data.
* One example is BOM.

* Important point is to chack for stack overflow
* and give RIGHT MESSAGE so that corrective action can be taken.

*&---------------------------------------------------------------------*
* Sample Program Showing Recursion in ABAP
* Simple BOM Tree walking in ABAP
* Takes Care of Diagnostics as well - detects Loops
* Hence never experience Stack Overflow!
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
* http://www.erpgreat.com/abap/reincarnation-of-reuse-alv-fieldcatalog-merge.htm
*     has INCLUDE zjncinclude with FORM zjnc_dump_list
*&---------------------------------------------------------------------*

* Author Jayanta Narayan Choudhuri
*         Flat 302
*         395 Jodhpur Park
*         Kolkata 700 068
*       Email sss@cal.vsnl.net.in
*       URL:  http://www.geocities.com/ojnc

REPORT zjncrecursion.

INCLUDE zjncinclude. " From www.erpgreat.com

TABLES: makt.

CONSTANTS: maxlevels TYPE i VALUE 10.

TYPES: BEGIN OF ty_usage,
          parent  TYPE  makt-matnr,
          child   TYPE  makt-matnr,
          usage   TYPE  mseg-menge,
       END OF ty_usage.

DATA: wa_usage  TYPE ty_usage,
      it_usage  TYPE STANDARD TABLE OF ty_usage.    " USAGE data

TYPES: BEGIN OF ty_stack,
          level   TYPE  h_level,
          matnr   TYPE  makt-matnr,
          menge   TYPE  mseg-menge,
       END OF ty_stack.

DATA: istackpos TYPE i,
      wa_stack  TYPE ty_stack,
      it_stack  TYPE STANDARD TABLE OF ty_stack.   "BOM Stack

TYPES: BEGIN OF ty_reqd,
          matnr   TYPE  makt-matnr,
          menge   TYPE  mseg-menge,
       END OF ty_reqd.

DATA: wa_reqd   TYPE ty_reqd,
      it_reqd   TYPE STANDARD TABLE OF ty_reqd.    "NET Requirement of 
RAW Material

PARAMETERS: p_build TYPE makt-matnr,    "Target   to Build
            p_menge TYPE mseg-menge.    "Quantity to Build

INITIALIZATION.
  PERFORM f_usage_data.


START-OF-SELECTION.

  istackpos = 0.

  MOVE istackpos  TO wa_stack-level.
  MOVE p_build    TO wa_stack-matnr.
  MOVE p_menge    TO wa_stack-menge.
  APPEND wa_stack TO it_stack.                              " 1st PUSH

  PERFORM f_explode USING p_build.

  SORT it_reqd BY matnr.

  PERFORM zjnc_dump_list USING 'IT_REQD' 'WA_REQD' 'Net RAW 
Requirement'.

*&--------------------------------------------------------------------*
*&      Form  f_Explode
*&--------------------------------------------------------------------*
FORM f_explode USING p_pmatnr.
  DATA: icount   TYPE i,
        totalreq TYPE mseg-menge.

  istackpos = istackpos + 1.                          " PUSH

  IF istackpos GE maxlevels.
    PERFORM f_dumpstack.    " terminate
    LEAVE PROGRAM.
  ENDIF.

  icount = 0.

  LOOP AT it_usage INTO wa_usage WHERE parent = p_pmatnr.
    icount = icount + 1.

    MOVE istackpos      TO wa_stack-level.
    MOVE wa_usage-child TO wa_stack-matnr.
    MOVE wa_usage-usage TO wa_stack-menge.
    APPEND wa_stack     TO it_stack.                  " PUSH

    PERFORM f_explode USING wa_usage-child.

  ENDLOOP.

  IF icount = 0.                                  " then LEAF
    totalreq = '1.0'.
    LOOP AT it_stack INTO wa_stack.
      totalreq = totalreq * wa_stack-menge.
    ENDLOOP.

    MOVE p_pmatnr   TO wa_reqd-matnr.
    MOVE totalreq   TO wa_reqd-menge.
    COLLECT wa_reqd INTO it_reqd.

  ENDIF.

  DELETE it_stack INDEX istackpos.                      " POP

  istackpos = istackpos - 1.                            " POP

ENDFORM.                    "f_Explode

*&--------------------------------------------------------------------*
*&      Form  f_dumpstack
*&--------------------------------------------------------------------*
FORM f_dumpstack.

  PERFORM zjnc_dump_list USING 'IT_STACK' 'WA_STACK' 'LOOP in BOM 
STACK'.

ENDFORM.                    "f_dumpstack

*&--------------------------------------------------------------------*
*&      Form  f_usage_data for Hungry Bengali ABAPer in Kolkata
*&--------------------------------------------------------------------*
FORM f_usage_data.
  MOVE 'FISHCURRY'   TO wa_usage-parent.
  MOVE 'SPICES'      TO wa_usage-child.
  MOVE 10            TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

  MOVE 'FISHCURRY'   TO wa_usage-parent.
  MOVE 'MIRCHI'      TO wa_usage-child.
  MOVE 5             TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

  MOVE 'FISHCURRY'   TO wa_usage-parent.
  MOVE 'ALOO'        TO wa_usage-child.
  MOVE 8             TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

  MOVE 'SPICES'      TO wa_usage-parent.
  MOVE 'MIRCHI'      TO wa_usage-child.
  MOVE 2             TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

  MOVE 'SPICES'      TO wa_usage-parent.
  MOVE 'JEERA'       TO wa_usage-child.
  MOVE 3             TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

  MOVE 'SPICES'      TO wa_usage-parent.
  MOVE 'HALUD'       TO wa_usage-child.
  MOVE 7             TO wa_usage-usage.
  APPEND wa_usage TO it_usage.

ENDFORM.                    "f_usage_data
ABAP Tips by :  Jayanta Narayan Choudhuri
Author Email:      sss@cal.vsnl.net.in
Author Website:  http://www.geocities.com/ojnc

Fast Links:
A Sample Tree Type Report
Tree type report in ABAP

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

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

ABAP Tips
ABAP Forum for Discussion and Samples Program Codes for Abapers

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.