Hi Ravindar,
Calculation depends on your logic.
1. find days in every month and apply do .. Enddo.
2. Get all records in internal table according to date in DO .. ENDDO.
3. Sum the value in loop.
I am sending sample code that display SO Summary details of every month.
SAMPLE CODE:
*&---------------------------------------------------------------------*
*& Report ZSD_DESPATCH_REGISTER
*& Author Ranjit Kumar
*& Request No.
*&---------------------------------------------------------------------*
REPORT ZSD_DESPATCH_REGISTER LINE-COUNT 65 LINE-SIZE 190.
TABLES: VBAK, VBAP, LIKP, LIPS.
TYPE-POOLS:SLIS.
TYPES: BEGIN OF TYVBAK,
VBELN TYPE VBAK-VBELN,
ERDAT TYPE VBAK-ERDAT,
VKORG TYPE VBAK-VKORG,
VTWEG TYPE VBAK-VTWEG,
SPART TYPE VBAK-SPART,
END OF TYVBAK.
TYPES: BEGIN OF TYVBAP,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
ERDAT TYPE VBAP-ERDAT,
WERKS TYPE VBAP-WERKS,
MATNR TYPE VBAP-MATNR,
KWMENG TYPE VBAP-KWMENG,
ABGRU TYPE VBAP-ABGRU,
END OF TYVBAP.
*TYPES: BEGIN OF TYLIKP,
* VBELN TYPE LIKP-VBELN,
* VKORG TYPE LIKP-VKORG,
* ERDAT TYPE LIKP-ERDAT,
* END OF TYLIKP.
*TYPES: BEGIN OF TYLIPS,
* VBELN TYPE LIPS-VBELN,
* POSNR TYPE LIPS-POSNR,
* ERDAT TYPE LIPS-ERDAT,
* LFIMG TYPE LIPS-LFIMG,
* DQTY TYPE LIPS-LFIMG,
* END OF TYLIPS.
TYPES: BEGIN OF TYLIKPLIPS,
VBELN TYPE LIKP-VBELN,
POSNR_I TYPE LIPS-POSNR,
ERDAT TYPE LIPS-ERDAT,
* WERKS TYPE LIPS-WERKS,
LFIMG_I TYPE LIPS-LFIMG,
BWART_I TYPE LIPS-BWART,
DQTY TYPE LIPS-LFIMG,
END OF TYLIKPLIPS.
TYPES: BEGIN OF TYVBFA,
VBELV TYPE VBFA-VBELV,
POSNV TYPE VBFA-POSNV,
VBELN TYPE VBFA-VBELN,
VBTYP_N TYPE VBFA-VBTYP_N,
RFMNG TYPE VBFA-RFMNG,
PLMIN TYPE VBFA-PLMIN,
BWART TYPE VBFA-BWART,
V_QTY TYPE VBFA-RFMNG,
END OF TYVBFA.
DATA: WAVBAK TYPE TYVBAK,
ITVBAK TYPE STANDARD TABLE OF TYVBAK WITH HEADER LINE,
WAVBAP TYPE TYVBAP,
ITVBAP TYPE STANDARD TABLE OF TYVBAP WITH HEADER LINE,
* WALIKP TYPE TYLIKP,
* ITLIKP TYPE STANDARD TABLE OF TYLIKP WITH HEADER LINE,
* WALIPS TYPE TYLIPS,
* ITLIPS TYPE STANDARD TABLE OF TYLIPS WITH HEADER LINE,
WAVBFA TYPE TYVBFA,
ITVBFA TYPE STANDARD TABLE OF TYVBFA WITH HEADER LINE,
WALIKPLIPS TYPE TYLIKPLIPS,
ITLIKPLIPS TYPE STANDARD TABLE OF TYLIKPLIPS WITH HEADER LINE.
DATA : T_LISTHEADER TYPE SLIS_T_LISTHEADER WITH HEADER LINE,
T_FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
FS_LAYOUT TYPE SLIS_LAYOUT_ALV,
T_EVENT TYPE SLIS_T_EVENT WITH HEADER LINE.
DATA : V_DAY(2) TYPE P,
V_DATE TYPE MKPF-BUDAT,
V_DAYS(2) TYPE P,
V_STDATE TYPE DATS,
V_EDDATE TYPE DATS,
V_TDAYS(2) TYPE P,
V_DESPQTY(13) TYPE C,
V_DESPQTY1(13) TYPE C,
V_DESPCANCQTY(13) TYPE C.
DATA : BEGIN OF ITTOT OCCURS 0,
DAT(2) TYPE C,
OPQTY(13) TYPE P,
SOQTY(13) TYPE P,
DESPQTY(13) TYPE P,
BLQTY(13) TYPE P,
CLQTY(13) TYPE P,
CLQTY1(13) TYPE P,
CANQTY(13) TYPE P,
BLQTY1(13) TYPE P,
DATE(12) TYPE C,
END OF ITTOT.
SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (25) A1LINE FOR FIELD P_WERKS.
PARAMETERS: P_WERKS TYPE VBAP-WERKS DEFAULT 'HETC' OBLIGATORY.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (25) A2LINE FOR FIELD P_VKORG.
PARAMETERS: P_VKORG TYPE VBAK-VKORG DEFAULT '1000' OBLIGATORY.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (22) A3LINE FOR FIELD S_VTWEG.
SELECT-OPTIONS: S_VTWEG FOR VBAK-VTWEG.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (22) A4LINE FOR FIELD S_SPART.
SELECT-OPTIONS: S_SPART FOR VBAK-SPART.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (25) A5LINE FOR FIELD P_MONTH.
PARAMETERS: P_MONTH LIKE S012-SPMON OBLIGATORY.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: END OF BLOCK B1.
SELECTION-SCREEN: BEGIN OF BLOCK B2 WITH FRAME TITLE A2TITLE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (25) A7LINE FOR FIELD P_CLASS.
PARAMETERS: P_CLASS RADIOBUTTON GROUP RAD1 DEFAULT 'X'.",P_MOD AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (25) A6LINE FOR FIELD P_GRID.
PARAMETERS: P_GRID RADIOBUTTON GROUP RAD1.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: END OF BLOCK B2.
INITIALIZATION.
A2TITLE = 'Display Option'.
A1LINE = 'Plant'.
A2LINE = 'Sales Org.'.
A3LINE = 'Dist. Channel'.
A4LINE = 'Division'.
A5LINE = 'Month'.
A6LINE = 'Grid View'.
A7LINE = 'Classic View'.
START-OF-SELECTION.
AUTHORITY-CHECK OBJECT 'ZWERK_REP'
ID 'WERKS' FIELD P_WERKS.
IF SY-SUBRC <> 0.
MESSAGE E045(ZMSG) WITH P_WERKS.
ENDIF.
PERFORM GET_DATA.
IF P_CLASS = 'X'.
PERFORM DISPLAY_CLASSIC_DATA.
ELSEIF P_GRID = 'X'.
PERFORM DISPLAY_DATA.
ENDIF.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form DISPLAY_CLASSIC_DATA
*&---------------------------------------------------------------------*
FORM DISPLAY_CLASSIC_DATA.
WRITE:/4 'USER : ', 12 SY-UNAME.
WRITE:/4 'Plant: ',12 P_WERKS,25 'Sales Org.:',35 P_VKORG.
WRITE:/4 'Month: ',12 P_MONTH.
WRITE:/2 'Date',32 'Opening Quantity',60'New SO Quantity',85 'Despatch Quantity',115 'Cancel Quantity',140 'Closing Quantity'.
ULINE.
LOOP AT ITTOT.
WRITE:/2 ITTOT-DATE,
ITTOT-OPQTY,
ITTOT-SOQTY,
ITTOT-DESPQTY,
ITTOT-CANQTY,
ITTOT-CLQTY.
ENDLOOP.
ENDFORM. "DISPLAY_CLASSIC_DATA
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
FORM GET_DATA.
CONCATENATE P_MONTH+0(4) P_MONTH+4(2) '01' INTO V_STDATE.
CONDENSE V_STDATE.
CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
EXPORTING
DAY_IN = V_STDATE
IMPORTING
LAST_DAY_OF_MONTH = V_EDDATE.
V_TDAYS = V_EDDATE - V_STDATE + 1.
SELECT VBELN ERDAT VKORG VTWEG SPART INTO CORRESPONDING FIELDS OF
TABLE ITVBAK FROM VBAK
WHERE ERDAT BETWEEN V_STDATE AND V_EDDATE AND VKORG = P_VKORG
AND VTWEG IN S_VTWEG AND SPART IN S_SPART AND AUART NOT IN ('ZRE1', 'ZRE2', 'ZRE3', 'ZRE4', 'ZRE5', 'ZRT1').
IF SY-SUBRC <> 0.
MESSAGE 'No records found against selection' TYPE 'E'.
ENDIF.
SELECT VBELN POSNR ERDAT WERKS MATNR KWMENG ABGRU INTO CORRESPONDING FIELDS OF
TABLE ITVBAP FROM VBAP FOR ALL ENTRIES IN ITVBAK
WHERE VBELN = ITVBAK-VBELN AND WERKS = P_WERKS AND ERDAT BETWEEN V_STDATE AND V_EDDATE.
IF SY-SUBRC <> 0.
MESSAGE 'No Sales Data found against Plant' TYPE 'E'.
ENDIF.
* SELECT VBELN VKORG ERDAT INTO CORRESPONDING FIELDS OF
* TABLE ITLIKP FROM LIKP
* WHERE VKORG = P_VKORG AND ERDAT BETWEEN V_STDATE AND V_EDDATE.
*
* SELECT VBELN POSNR ERDAT LFIMG WERKS INTO CORRESPONDING FIELDS OF
* TABLE ITLIPS FROM LIPS FOR ALL ENTRIES IN ITLIKP
* WHERE VBELN = ITLIKP-VBELN AND VTWEG IN S_VTWEG AND SPART IN S_SPART AND WERKS = P_WERKS.
*
* SELECT VBELV POSNV VBELN RFMNG PLMIN BWART FROM VBFA INTO CORRESPONDING FIELDS OF
* TABLE ITVBFA FOR ALL ENTRIES IN ITLIPS WHERE VBELV = ITLIPS-VBELN AND POSNV = ITLIPS-POSNR
* AND BWART IN ('601','602').
SELECT VBELN POSNR_I ERDAT LFIMG_I BWART_I INTO CORRESPONDING FIELDS OF TABLE ITLIKPLIPS FROM WB2_V_LIKP_LIPS2 WHERE WERKS_I = P_WERKS AND VKORG = P_VKORG
AND ERDAT BETWEEN V_STDATE AND V_EDDATE AND VTWEG_I IN S_VTWEG AND SPART_I IN S_SPART AND BWART_I IN ('601','602').
LOOP AT ITLIKPLIPS INTO WALIKPLIPS.
IF WALIKPLIPS-BWART_I = '601'.
V_DESPQTY = V_DESPQTY + WALIKPLIPS-LFIMG_I.
ELSEIF WALIKPLIPS-BWART_I = '602'.
V_DESPCANCQTY = V_DESPCANCQTY + WALIKPLIPS-LFIMG_I.
ENDIF.
V_DESPQTY1 = V_DESPQTY - V_DESPCANCQTY.
WALIKPLIPS-DQTY = V_DESPQTY1.
MODIFY ITLIKPLIPS FROM WALIKPLIPS.
CLEAR: V_DESPQTY, V_DESPCANCQTY.
ENDLOOP.
* SORT ITVBFA BY VBELV.
* LOOP AT ITLIPS INTO WALIPS.
* LOOP AT ITVBFA INTO WAVBFA WHERE VBELV = WALIPS-VBELN AND POSNV = WALIPS-POSNR.
* IF WAVBFA-BWART = '601' .
* V_DESPQTY = V_DESPQTY + WAVBFA-RFMNG.
* ELSEIF WAVBFA-BWART = '602'.
* V_DESPCANCQTY = V_DESPCANCQTY + WAVBFA-RFMNG.
* ENDIF.
*
* ENDLOOP.
* V_DESPQTY1 = V_DESPQTY - V_DESPCANCQTY.
* WALIPS-DQTY = V_DESPQTY1.
* MODIFY ITLIPS FROM WALIPS.
* CLEAR: V_DESPQTY, V_DESPCANCQTY.", V_DESPQTY1.
* CLEAR: V_DESPQTY1.
* ENDLOOP.
DATA : V_QTY(13) TYPE P,
V_GQTY(13) TYPE P,
V_CNT(2) TYPE C,
V_DEPQTY(13) TYPE C,
V_CLQTY(13) TYPE C,
V_CLQTY1(13) TYPE C,
V_CANCQTY(13) TYPE C,
V_BLQTY(13) TYPE C,
V_DISPOPQTY(13) TYPE C,
V_NEWQTY(13) TYPE C,
V_DAT(2) TYPE C.
V_QTY = 0.
V_CNT = 1.
DO V_TDAYS TIMES.
ITTOT-DAT = V_CNT.
APPEND ITTOT.
V_CNT = V_CNT + 1.
ENDDO.
V_QTY = 0.
V_DEPQTY = 0.
V_CLQTY = 0.
LOOP AT ITTOT.
* LOOP AT ITLIPS WHERE ERDAT+6(2) = ITTOT-DAT.
* V_DEPQTY = V_DEPQTY + ITLIPS-DQTY."ITLIPS-LFIMG.
* ENDLOOP.
LOOP AT ITLIKPLIPS WHERE ERDAT+6(2) = ITTOT-DAT.
V_DEPQTY = V_DEPQTY + ITLIKPLIPS-DQTY.
ENDLOOP.
ITTOT-DESPQTY = V_DEPQTY.
LOOP AT ITVBAP WHERE ERDAT+6(2) = ITTOT-DAT.
IF ITVBAP-ABGRU <> ' '.
V_CANCQTY = V_CANCQTY + ITVBAP-KWMENG.
ENDIF.
V_QTY = V_QTY + ITVBAP-KWMENG.
ENDLOOP.
ITTOT-SOQTY = V_QTY.
ITTOT-CANQTY = V_CANCQTY.
V_BLQTY = ITTOT-OPQTY + ITTOT-SOQTY - ITTOT-DESPQTY.
ITTOT-BLQTY = V_BLQTY.
V_CLQTY = V_BLQTY - V_CANCQTY.
ITTOT-CLQTY = V_CLQTY.
IF V_CLQTY <> 0.
V_DISPOPQTY = ITTOT-CLQTY.
V_CLQTY1 = ITTOT-CLQTY.
ENDIF.
IF ITTOT-BLQTY = 0.
ITTOT-OPQTY = V_CLQTY1." + ITTOT-OPQTY.
ELSE.
ITTOT-OPQTY = V_CLQTY1 + V_NEWQTY.
ENDIF.
ON CHANGE OF ITTOT-CLQTY.
ITTOT-OPQTY = ITTOT-OPQTY - ITTOT-CLQTY." - V_CLQTY1.
ENDON.
ITTOT-OPQTY = V_NEWQTY.
ITTOT-BLQTY1 = ITTOT-OPQTY + ITTOT-SOQTY - ITTOT-DESPQTY.
ITTOT-CLQTY = ITTOT-BLQTY1 - ITTOT-CANQTY. "ITTOT-CANQTY.
ON CHANGE OF ITTOT-CLQTY.
V_NEWQTY = ITTOT-CLQTY.
ENDON.
MODIFY ITTOT.
CLEAR: V_QTY, V_DEPQTY, V_CANCQTY, V_CLQTY1.
ENDLOOP.
LOOP AT ITTOT.
DATA: V_DATE(10) TYPE C,
V_LEN(3) TYPE C,
CUR_DATE(10) TYPE C.
V_LEN = STRLEN( ITTOT-DAT ).
IF V_LEN = 1 .
CONCATENATE P_MONTH '0' ITTOT-DAT INTO V_DATE.
ELSE.
CONCATENATE P_MONTH ITTOT-DAT INTO V_DATE.
ENDIF.
ITTOT-DATE = V_DATE.
CALL FUNCTION 'CONVERSION_EXIT_PDATE_OUTPUT'
EXPORTING
INPUT = ITTOT-DATE
IMPORTING
OUTPUT = ITTOT-DATE.
MODIFY ITTOT TRANSPORTING DATE.
ENDLOOP.
CALL FUNCTION 'CONVERSION_EXIT_PDATE_OUTPUT'
EXPORTING
INPUT = SY-DATUM
IMPORTING
OUTPUT = CUR_DATE.
IF ITTOT-DATE+6(4) = CUR_DATE+6(4) AND ITTOT-DATE+3(2) = CUR_DATE+3(2) AND ITTOT-DATE(2) > CUR_DATE(2).
DELETE ITTOT WHERE DATE > CUR_DATE.
ENDIF.
ENDFORM. "GET_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM DISPLAY_DATA.
PERFORM FILL_LISTHEADER.
PERFORM FILL_FIELDCATALOG.
PERFORM FILL_LAYOUT.
PERFORM DISPLAY_GRID.
ENDFORM. "DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FIELDCATALOG
*&---------------------------------------------------------------------*
FORM FILL_FIELDCATALOG.
PERFORM FIELDCAT_APPEND USING 'DATE' 'Date' 'X' 'ITTOT' ' ' ' ' ' '.
PERFORM FIELDCAT_APPEND USING 'OPQTY' 'Opening Quantity' 'X' 'ITTOT' ' ' 'X' ' '.
PERFORM FIELDCAT_APPEND USING 'SOQTY' 'New SO Quantity' 'X' 'ITTOT' ' ' 'X' ' '.
PERFORM FIELDCAT_APPEND USING 'DESPQTY' 'Despatch Quantity' 'X' 'ITTOT' ' ' 'X' ' '.
PERFORM FIELDCAT_APPEND USING 'CANQTY' 'Cancel Quantity' 'X' 'ITTOT' ' ' 'X' ' '.
PERFORM FIELDCAT_APPEND USING 'CLQTY' 'Closing Quantity' 'X' 'ITTOT' ' ' 'X' ' '.
ENDFORM. "FILL_FIELDCATALOG
*&---------------------------------------------------------------------*
*& Form DISPLAY_GRID
*&---------------------------------------------------------------------*
FORM DISPLAY_GRID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_PAGE'
IS_LAYOUT = FS_LAYOUT
* IS_PRINT =
IT_FIELDCAT = T_FIELDCATALOG[]
IT_EVENTS = T_EVENT[]
* I_SAVE =
I_CALLBACK_USER_COMMAND = 'HANDLE_USER_COMMAND'
* IS_VARIANT =
TABLES
T_OUTTAB = ITTOT
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
ENDIF.
ENDFORM. " display_grid
*&---------------------------------------------------------------------*
*& Form f_listheader
*&---------------------------------------------------------------------*
FORM FILL_LISTHEADER.
DATA : FRMDT(10) TYPE C,
TODT(10) TYPE C,
V_STR TYPE STRING,
V_MONTH(8) TYPE C.
WRITE: P_MONTH TO V_MONTH.
T_LISTHEADER-TYP = 'H'.
T_LISTHEADER-KEY = ' '.
T_LISTHEADER-INFO = 'Daily Despatch register'.
APPEND T_LISTHEADER.
T_LISTHEADER-TYP = 'S'.
T_LISTHEADER-KEY = 'Plant'.
T_LISTHEADER-INFO = P_WERKS.
APPEND T_LISTHEADER.
T_LISTHEADER-TYP = 'S'.
T_LISTHEADER-KEY = 'Sales Org.'.
T_LISTHEADER-INFO = P_VKORG.
APPEND T_LISTHEADER.
T_LISTHEADER-TYP = 'S'.
T_LISTHEADER-KEY = 'Month'.
T_LISTHEADER-INFO = V_MONTH.
APPEND T_LISTHEADER.
ENDFORM. " f_listheader
*&---------------------------------------------------------------------*
*& Form FIELDCAT_APPEND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_FNAME) text
* -->VALUE(P_FTEXT) text
* -->VALUE(P_FIXCOL) text
* -->VALUE(P_OUTLEN) text
* -->VALUE(P_TABLE) text
* -->VALUE(P_DOSUM) text
* -->VALUE(P_HOTSPOT) text
*----------------------------------------------------------------------*
FORM FIELDCAT_APPEND USING VALUE(P_FNAME)
VALUE(P_FTEXT)
VALUE(P_FIXCOL)
VALUE(P_TABLE)
VALUE(P_DOSUM)
VALUE(P_ZERO)
VALUE(P_NOOUT).
T_FIELDCATALOG-FIELDNAME = P_FNAME.
T_FIELDCATALOG-SELTEXT_L = P_FTEXT.
T_FIELDCATALOG-FIX_COLUMN = P_FIXCOL.
T_FIELDCATALOG-TABNAME = P_TABLE.
T_FIELDCATALOG-DO_SUM = P_DOSUM.
T_FIELDCATALOG-NO_ZERO = P_ZERO.
T_FIELDCATALOG-NO_OUT = P_NOOUT.
APPEND T_FIELDCATALOG.
ENDFORM. " FIELDCAT_APPEND
*&---------------------------------------------------------------------*
*& Form f_layout
*&---------------------------------------------------------------------*
FORM FILL_LAYOUT.
T_EVENT-NAME = SLIS_EV_TOP_OF_PAGE.
T_EVENT-FORM = 'TOP_OF_PAGE'.
APPEND T_EVENT.
FS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM. " fill_layout
*&---------------------------------------------------------------------*
*& Form top_of_page *
*&---------------------------------------------------------------------*
FORM TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = T_LISTHEADER[].
ENDFORM. " top_of_page