Wednesday, May 9, 2018

SAP ABAP - SIMPLE CLASSICAL REPORT


CLASSICAL REPORT  IN SAP ABAP

*&---------------------------------------------------------------------*
*& Report  ZSIBLING_AGE_DIFF
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZSIBLING_AGE_DIFF.

TYPES: BEGIN OF TY_FATHER,
        FATHERID TYPE ZFATHERID,
        FATHERNAME TYPE ZFATHERNAME,
        FATHERDOB TYPE ZFATHERDOB,
       END OF TY_FATHER.
TYPES: BEGIN OF TY_MOTHER,
        MOTHERID TYPE ZMOTHERID,
        HUSBANDID TYPE ZFATHERID,
        MOTHERNAME TYPE ZMOTHERNAME,
        MOTHERDOB TYPE ZMOTHERDOB,
       END OF TY_MOTHER.
TYPES: BEGIN OF TY_CHILDREN,
        CHILDID TYPE ZCHILDID,
        FATHERID TYPE ZFATHERID,
        MOTHERID TYPE ZMOTHERID,
        CHILDNAME TYPE ZCHILDNAME,
        CHILDDOB TYPE ZCHILDDOB,
        CHILDGENDER TYPE ZCHILDGENDER,
       END OF TY_CHILDREN.




TYPES: BEGIN OF TY_CHILD_AGE,
       FATHERNAME TYPE ZFATHERNAME,
       CHILDNAME TYPE ZCHILDNAME,
       CHILDID TYPE ZCHILDID,
       YEARS TYPE NUM2,
       END OF TY_CHILD_AGE.

TYPES: BEGIN OF TY_FINAL,
        CHILDID TYPE ZCHILDID,
        FATHERNAME TYPE ZFATHERNAME,
        CHILDNAME TYPE ZCHILDNAME,
        CHILDAGE TYPE I,
        FIRSTDIFF TYPE I,
        SECONDDIFF TYPE I,
        THIRDDIFF TYPE I,
       END OF TY_FINAL.
DATA: IT_FINAL TYPE TABLE OF TY_FINAL,
      WA_FINAL TYPE TY_FINAL.

DATA: IT_CHILD_AGE1 TYPE TABLE OF TY_CHILD_AGE,
      WA_CHILD_AGE1 TYPE TY_CHILD_AGE,
      LINE_COUNT TYPE I,
      AGE TYPE I,
      AGE1 TYPE I,
      AGE_DIFF TYPE I.
DATA: IT_CHILD_AGE TYPE TABLE OF TY_CHILD_AGE,
      WA_CHILD_AGE TYPE TY_CHILD_AGE.

DATA: IT_FATHER TYPE TABLE OF TY_FATHER,
      WA_FATHER TYPE TY_FATHER.
DATA: IT_MOTHER TYPE TABLE OF TY_MOTHER,
      WA_MOTHER TYPE TY_MOTHER.
DATA: IT_CHILDREN TYPE TABLE OF TY_CHILDREN,
      WA_CHILDREN TYPE TY_CHILDREN.

"DATA: S_FATHERID TYPE ZFATHER-FATHERID.
DATA: DAYS TYPE NUM2,
      MONTHS TYPE NUM2,
      YEARS TYPE NUM2.
DATA: V_LINES TYPE I.

SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
  PARAMETERS: P_FID TYPE ZFATHER-FATHERID.
SELECTION-SCREEN: END OF BLOCK B1.

START-OF-SELECTION.
SELECT CHILDID FATHERID MOTHERID CHILDNAME CHILDDOB CHILDGENDER FROM ZCHILDREAN INTO TABLE IT_CHILDREN
  WHERE FATHERID EQ P_FID.
  IF SY-SUBRC EQ 0.
    SORT IT_CHILDREN BY CHILDID.

    SELECT FATHERID FATHERNAME FATHERDOB FROM ZFATHER INTO TABLE IT_FATHER FOR ALL ENTRIES IN IT_CHILDREN
      WHERE FATHERID = IT_CHILDREN-FATHERID.
  ENDIF.
END-OF-SELECTION.

LOOP AT IT_CHILDREN INTO WA_CHILDREN.

  READ TABLE IT_FATHER INTO WA_FATHER WITH KEY FATHERID = WA_CHILDREN-FATHERID BINARY SEARCH.
  WA_CHILD_AGE-FATHERNAME = WA_FATHER-FATHERNAME.

  WA_CHILD_AGE-CHILDNAME = WA_CHILDREN-CHILDNAME.
  WA_CHILD_AGE-CHILDID = WA_CHILDREN-CHILDID.

  CALL FUNCTION 'HRCM_TIME_PERIOD_CALCULATE'
  EXPORTING
  BEGDA = WA_CHILDREN-CHILDDOB
  ENDDA = SY-DATUM
  IMPORTING
  NOYRS = YEARS
  NOMNS = MONTHS
  NODYS = DAYS
  EXCEPTIONS
  INVALID_DATES = 1
  OVERFLOW = 2
  OTHERS = 3.
  WA_CHILD_AGE-YEARS = YEARS.


  APPEND WA_CHILD_AGE TO IT_CHILD_AGE.
  CLEAR WA_CHILD_AGE.
ENDLOOP.



IT_CHILD_AGE1[] = IT_CHILD_AGE.

DESCRIBE TABLE IT_CHILD_AGE LINES V_LINES.
WRITE:/ 'TOTAL CHILD:-',V_LINES.
ULINE.

WRITE:/ 'FATHER NAME',
        20 'CHILD NAME',
        40 'CHILD AGE',
        60 'FIRST DIFF',
        80 'SECOND DIFF',
        100 'THIRD DIFF'.
ULINE.

LOOP AT IT_CHILD_AGE INTO WA_CHILD_AGE.
  WA_FINAL-CHILDID = WA_CHILD_AGE-CHILDID.
  WA_FINAL-FATHERNAME = WA_CHILD_AGE-FATHERNAME.
  WA_FINAL-CHILDNAME = WA_CHILD_AGE-CHILDNAME.
  WA_FINAL-CHILDAGE = WA_CHILD_AGE-YEARS.
*  WA_FINAL-FIRSTDIFF = ''.
*  WA_FINAL-SECONDDIFF = ''.
*  WA_FINAL-THIRDDIFF = ''.

  APPEND WA_FINAL TO IT_FINAL.
  CLEAR WA_FINAL.
ENDLOOP.



LOOP AT IT_CHILD_AGE INTO WA_CHILD_AGE.
*  WRITE:/ WA_CHILD_AGE-FATHERNAME,
*          20 WA_CHILD_AGE-CHILDNAME,
*          40 WA_CHILD_AGE-YEARS.

          WA_FINAL-FATHERNAME = WA_CHILD_AGE-FATHERNAME.
          WA_FINAL-CHILDNAME = WA_CHILD_AGE-CHILDNAME.
          WA_FINAL-CHILDAGE = WA_CHILD_AGE-YEARS.

          LINE_COUNT = 1.
          LOOP AT IT_CHILD_AGE1 INTO WA_CHILD_AGE1.
            IF WA_CHILD_AGE-CHILDID NE WA_CHILD_AGE1-CHILDID.
              IF WA_CHILD_AGE-YEARS GT WA_CHILD_AGE1-YEARS.
                AGE = WA_CHILD_AGE-YEARS.
                AGE1 = WA_CHILD_AGE1-YEARS.
                AGE_DIFF = AGE - AGE1.
                IF LINE_COUNT EQ 1.
                  WA_FINAL-FIRSTDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING FIRSTDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                IF LINE_COUNT EQ 2.
                  WA_FINAL-SECONDDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING SECONDDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                IF LINE_COUNT EQ 3.
                  WA_FINAL-THIRDDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING THIRDDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                "WRITE:/ AGE_DIFF.
              ELSE.
                AGE = WA_CHILD_AGE1-YEARS.
                AGE1 = WA_CHILD_AGE-YEARS.
                AGE_DIFF = AGE - AGE1.
                IF LINE_COUNT EQ 1.
                  WA_FINAL-FIRSTDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING FIRSTDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                IF LINE_COUNT EQ 2.
                  WA_FINAL-SECONDDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING SECONDDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                IF LINE_COUNT EQ 3.
                  WA_FINAL-THIRDDIFF = AGE_DIFF.
                  MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING THIRDDIFF WHERE CHILDID = WA_CHILD_AGE-CHILDID.
                ENDIF.
                "WRITE:/ AGE_DIFF.
              ENDIF.
              LINE_COUNT = LINE_COUNT + 1.
            ENDIF.

*            APPEND WA_FINAL TO IT_FINAL.
*            CLEAR WA_FINAL.

          ENDLOOP.
ENDLOOP.

LOOP AT IT_FINAL INTO WA_FINAL.
  WRITE:/ WA_FINAL-FATHERNAME,
          20 WA_FINAL-CHILDNAME,
          40 WA_FINAL-CHILDAGE,
          60 WA_FINAL-FIRSTDIFF,
          80 WA_FINAL-SECONDDIFF,
          100 WA_FINAL-THIRDDIFF.
ENDLOOP.

*
*TYPES: BEGIN OF TY_CHILD_AGE,
*       FATHERNAME TYPE ZFATHERNAME,
*       CHILDNAME TYPE ZCHILDNAME,
*       CHILDID TYPE ZCHILDID,
*       YEARS TYPE NUM2,
*       END OF TY_CHILD_AGE.
*DATA: IT_CHILD_AGE TYPE TABLE OF TY_CHILD_AGE,
*      WA_CHILD_AGE TYPE TY_CHILD_AGE.

SAP ABAP- MODULE PULL


SAP ABAP- MODULE PULL

REPORT NAME:---SAPMZ_SWRAN.
 INCLUDE MZ_SWRANTOP                             .  " global Data
 
INCLUDE MZ_SWRANO01                             .  " PBO-Modules
 
INCLUDE MZ_SWRANI01                             .  " PAI-Modules
 
INCLUDE MZ_SWRANF01  

TOP.
TYPE-POOLS:SLIS.
TABLES MARA .
DATA :MLOW(18),MHIGH(18).
DATA :MLOW1(18),MHIGH1(18).
DATABEGIN OF T_MARA OCCURS 1,
        MATNR 
TYPE MARA-MATNR,
        ERSDA 
TYPE MARA-ERSDA,
        ERNAM 
TYPE MARA-ERNAM,
        PSTAT 
TYPE MARA-PSTAT,
        MBRSH 
TYPE MARA-MBRSH,
      
END OF T_MARA.
DATA BEGIN OF RANGE OCCURS ,
       
SIGN(1),
       OPTION
(2),
       LOW
(18),
       HIGH
(18),
       
END OF RANGE .
DATA T_FCAT TYPE TABLE OF SLIS_FIELDCAT_ALV ,
       W_FCAT 
TYPE SLIS_FIELDCAT_ALV ,
       W_LAYO 
TYPE SLIS_LAYOUT_ALV .

PBO MODULE
MODULE STATUS_9001 OUTPUT.
  
SET PF-STATUS 'ZGUI_STATUS'.
*  SET TITLEBAR 'xxx'.

ENDMODULE.   
PAI MODULE.
*&---------------------------------------------------------------------*
*&  Include           MZ_SWRANI01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9001 INPUT.

   
CASE SY-UCOMM.
    
WHEN 'EXECUTE'."FC-CODE OF ENTER BUTTON
      
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT' "CONVERSION ROUTINE FOR MLOW
        
EXPORTING
          
INPUT              MLOW
       
IMPORTING
         
OUTPUT             MLOW1
       
EXCEPTIONS
         LENGTH_ERROR       
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.
      
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'"CONVERSION ROUTINE FOR MHIGH
        
EXPORTING
          
INPUT              MHIGH
       
IMPORTING
         
OUTPUT             MHIGH1
       
EXCEPTIONS
         LENGTH_ERROR       
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.
******************************************************************
* LOGICALLY BUILD A INTERNAL TABLE (RANGE)
******************************************************************
      RANGE
-SIGN 'I'" I OR E
      RANGE
-OPTION 'BT'.
      RANGE
-LOW MLOW1 .
      RANGE
-HIGH MHIGH1 .
      
APPEND RANGE ."APPEND DATA IN INTERNAL TABLE
******************************************************************
      
PERFORM FETCH_DATA .
      
PERFORM DISPLAY_DATA .
    
WHEN 'BACK' OR 'EXIT' or 'CANCEL'.
      
LEAVE PROGRAM.

  
ENDCASE.

ENDMODULE.                 " USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*&      Form  FETCH_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FETCH_DATA .
           
SELECT MATNR
                  ERNAM
                  ERSDA
                  PSTAT
                  MBRSH
                  
FROM MARA
                  
INTO CORRESPONDING FIELDS OF TABLE T_MARA
                  
WHERE MATNR IN RANGE .

ENDFORM.                    " FETCH_DATA
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DISPLAY_DATA .

  
LEAVE TO LIST-PROCESSING .
  W_FCAT
-TABNAME 'MARA'.
  W_FCAT
-FIELDNAME 'MATNR'.
  W_FCAT
-SELTEXT_M 'Material No'.
  
APPEND w_fcat to t_Fcat .

  W_FCAT
-TABNAME 'MARA'.
  W_FCAT
-FIELDNAME 'ERSDA'.
  W_FCAT
-SELTEXT_M 'Created On'.
  
APPEND w_fcat to t_Fcat .

  W_FCAT
-TABNAME 'MARA'.
  W_FCAT
-FIELDNAME 'ERNAM'.
  W_FCAT
-SELTEXT_M 'Name of Person'.
  
APPEND w_fcat to t_Fcat .

  W_FCAT
-TABNAME 'MARA'.
  W_FCAT
-FIELDNAME 'PSTAT'.
  W_FCAT
-SELTEXT_M 'Maintenance status'.
  
APPEND w_fcat to t_Fcat .

  W_FCAT
-TABNAME 'MARA'.
  W_FCAT
-FIELDNAME 'MBRSH'.
  W_FCAT
-SELTEXT_M 'Industry sector'.
  
APPEND w_fcat to T_FCAT.

  W_LAYO
-ZEBRA 'X'.
  W_LAYO
-COLWIDTH_OPTIMIZE 'X'.

  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  
EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
*   I_CALLBACK_PROGRAM                = ' '
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME             = 'MARA'
*   I_BACKGROUND_ID                   = ' '
     I_GRID_TITLE                      
='MATERIAL REPORT'
*   I_GRID_SETTINGS                   =
   IS_LAYOUT                         
W_LAYO
   IT_FIELDCAT                       
T_FCAT
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
    
TABLES
      T_OUTTAB                          
T_MARA[]
* EXCEPTIONS
*   PROGRAM_ERROR                     = 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.

ENDFORM.                    " DISPLAY_DATA