[SAP ABAP] ABAP Article - Display Total of Sales of The Year perMonth

Objective
Result
Transaction Code
Tables
BKPF - Accounting Document Header
VBAK - Sales Order Header
Support
Sometimes in addition to reports in the form of tabular data, we also need a report in the form of charts or graphs. Report in the form of graphs simplify the management to see visually from numeric values. In any case, representing data in a graph can make easy to compare data, analyze trends, visualize results. There is several chart reporting tools such as Fushionchart, JPGraph, Jasper Report, etc. In this session, chart report will created in HTML using web reporting tools : FusionChart.
Prerequisite File.
1. Download source of Fusioncharts from its website or fron link here.
2. Copy folder grafik contains Fusioncharts files into directory C:\.
Creating Report
1.Open SE38
2.Copy Program "YPRACTICE_301" to "YPRACTICE_303"
ar0296
3.Select All Object.
ar0297
Click Local Object to save in local object.
ar0298
4.Change Program "YPRACTICE_303"
5.Open Goto > Attributes, and change Title with "303 – Report Sales Graphical"
6.Modified Report Declaration
Before
REPORT ypractice_301.
After
REPORT ypractice_303.
7.Modified Tables.
Before.
*----------------------------------------------------------------------*
* Tables *
*----------------------------------------------------------------------*
TABLES : mara, makt, t006a.
After.
*----------------------------------------------------------------------*
* Tables *
*----------------------------------------------------------------------*
TABLES : bkpf, vbak.
8.Modified Global variables.
Before.
*----------------------------------------------------------------------*
*GLOBAL VARIABLE DECRALATION *
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_header,
matkl LIKE mara-matkl, " Material Group
matnr LIKE mara-matnr, " Material Number
maktx LIKE makt-maktx, " Material Descriptiop
bismt LIKE mara-bismt, " Old material number
meins LIKE mara-meins, " Base Unit of Measure
mseht LIKE t006a-mseht, " Unit of Measurement Text
END OF ty_header.
DATA: gi_header TYPE STANDARD TABLE OF ty_header WITH HEADER LINE,
gi_makt TYPE STANDARD TABLE OF makt WITH HEADER LINE,
gi_t006a TYPE STANDARD TABLE OF t006a WITH HEADER LINE.
DATA: gv_width TYPE i. " Width of list
DATA: ok_code LIKE sy-ucomm,
save_ok TYPE sy-ucomm.
After.
*----------------------------------------------------------------------*
* GLOBAL VARIABLE DECRALATION *
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_header,
bulan TYPE n LENGTH 2,
total LIKE vbak-netwr,
END OF ty_header.
DATA: gi_header TYPE STANDARD TABLE OF ty_header WITH HEADER LINE,
gi_vbak LIKE STANDARD TABLE OF vbak WITH HEADER LINE.
DATA: gv_width TYPE i. " Width of list
DATA: ok_code LIKE sy-ucomm,
save_ok TYPE sy-ucomm.
9.Change the Input Screen / Selection.
Before.
*----------------------------------------------------------------------*
*Input Screen/Selection
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK groupbox1 WITH FRAME TITLE text_101. "Declare Group Box 1
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(14) text_102 FOR FIELD so_matkl.
SELECT-OPTIONS so_matkl FOR mara-matkl OBLIGATORY DEFAULT '001'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(14) text_103 FOR FIELD so_matnr.
SELECT-OPTIONS so_matnr FOR mara-matnr.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK groupbox1. "End Group Box 1
SELECTION-SCREEN BEGIN OF BLOCK groupbox2 WITH FRAME TITLE text_201. "Declare Group Box 2
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(18) text_202 FOR FIELD p_fname.
PARAMETERS p_fname LIKE rlgrap-filename OBLIGATORY DEFAULT 'C:\abap_report_01.html'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK groupbox2. "End Group Box 2
After.
*----------------------------------------------------------------------*
*Input Screen/Selection
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK groupbox1 WITH FRAME TITLE text_101. "Declare Group Box 1
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(14) text_102 FOR FIELD so_gjahr.
SELECT-OPTIONS so_gjahr FOR bkpf-gjahr OBLIGATORY DEFAULT '2000' NO-EXTENSION NO INTERVALS.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(14) text_103 FOR FIELD so_waerk.
SELECT-OPTIONS so_waerk FOR vbak-waerk OBLIGATORY DEFAULT 'USD' NO-EXTENSION NO INTERVALS.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK groupbox1. "End Group Box 1
SELECTION-SCREEN BEGIN OF BLOCK groupbox2 WITH FRAME TITLE text_201. "Declare Group Box 2
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(18) text_202 FOR FIELD p_fname.
PARAMETERS p_fname LIKE rlgrap-filename OBLIGATORY DEFAULT 'C:\grafik\grafik_test.html'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK groupbox2. "End Group Box 2

10.Change the initialization.
Before.
*----------------------------------------------------------------------*
*INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
text_101 = 'Selection Parameter'.
text_102 = 'Material Group'.
text_103 = 'Material Number'.
text_201 = 'Save Html File'.
text_202 = 'File Location'.
PERFORM fm_initialization.
After.
*----------------------------------------------------------------------*
*INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
text_101 = 'Selection Parameter'.
text_102 = 'Year'.
text_103 = 'Currency'.
text_201 = 'Save Html File'.
text_202 = 'File Location'.
PERFORM fm_initialization.

11.Change form collect data.
Before.
*&--------------------------------------------------------------------*
*& Form fm_collect_data
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_collect_data.
* Collect Master Material
SELECT matnr matkl bismt meins
INTO CORRESPONDING FIELDS OF TABLE gi_header
FROM mara
WHERE
matkl IN so_matkl AND
matnr IN so_matnr.
* Collect Material Decription
SELECT matnr maktx
INTO CORRESPONDING FIELDS OF TABLE gi_makt
FROM makt
FOR ALL ENTRIES IN gi_header
WHERE
matnr = gi_header-matnr AND
spras = sy-langu.
* Collect Measurement Text
SELECT msehi mseht
INTO CORRESPONDING FIELDS OF TABLE gi_t006a
FROM t006a
FOR ALL ENTRIES IN gi_header
WHERE
msehi = gi_header-meins AND
spras = sy-langu.
* Notes :
* Avoid using inner join
ENDFORM. "FM_COLLECT_DATA
After.
*&--------------------------------------------------------------------*
*& Form fm_collect_data
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_collect_data.
DATA : lv_low LIKE sy-datum,
lv_high LIKE sy-datum.
CONCATENATE so_gjahr-low '01' '01' INTO lv_low.
CONCATENATE so_gjahr-low '12' '31' INTO lv_high.
* Collect
SELECT vbeln audat netwr waerk
INTO CORRESPONDING FIELDS OF TABLE gi_vbak
FROM vbak
WHERE
( audat BETWEEN lv_low AND lv_high ) AND
waerk IN so_waerk.
ENDFORM. "FM_COLLECT_DATA
12.Change fm_process_data
Before.
*&--------------------------------------------------------------------*
*& Form FM_PROCESS_DATA
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_process_data.
*Get other requirement information
LOOP AT gi_header.
* Get Material Description
READ TABLE gi_makt WITH KEY matnr = gi_header-matnr.
IF sy-subrc = 0.
gi_header-maktx = gi_makt-maktx.
ENDIF.
* Get Measurement Text
READ TABLE gi_t006a WITH KEY msehi = gi_header-meins.
IF sy-subrc = 0.
gi_header-mseht = gi_t006a-mseht.
ENDIF.
MODIFY gi_header.
ENDLOOP.
ENDFORM. "FM_PROCESS_DATA
After.
*&--------------------------------------------------------------------*
*& Form FM_PROCESS_DATA
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_process_data.
DATA : lv_date1 LIKE sy-datum,
lv_date2 LIKE sy-datum,
lv_bulan TYPE n LENGTH 2,
lv_total LIKE vbak-netwr.
DO 12 TIMES.
ADD 1 TO lv_bulan.
CONCATENATE so_gjahr-low lv_bulan '01' INTO lv_date1.
CALL FUNCTION 'SG_PS_GET_LAST_DAY_OF_MONTH'
EXPORTING
day_in = lv_date1
IMPORTING
last_day_of_month = lv_date2.
CLEAR : gi_header, lv_total.
SORT gi_vbak BY audat.
LOOP AT gi_vbak WHERE audat BETWEEN lv_date1 AND lv_date2.
ADD gi_vbak-netwr TO lv_total.
ENDLOOP.
gi_header-bulan = lv_bulan.
gi_header-total = lv_total.
APPEND gi_header.
ENDDO.
ENDFORM. "FM_PROCESS_DATA

13.Change fm_display_data.
Before.
*&--------------------------------------------------------------------*
*& Form fm_display_data
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_display_data.
REFRESH gi_html_code.
PERFORM fm_create_css.
write_code1 ''.
write_code1 '
'.
* Create Header Text
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
write_code1 '
'.
* Display Data to Screen
LOOP AT gi_header.
write_code1 '
'.
write_code3 '
'.
write_code3 '
'.
write_code3 '
'.
write_code3 '
'.
write_code3 '
'.
write_code3 '
'.
write_code1 '
'.
ENDLOOP.
* End of Line
write_code1 '
Mat. Group Mat. No. Description Old Mat. No. Base Unit of Measure Measure
' gi_header-matkl ' ' gi_header-matnr ' ' gi_header-maktx ' ' gi_header-bismt ' ' gi_header-meins ' ' gi_header-mseht '
'.
write_code1 ''.
PERFORM fm_create_html_file TABLES gi_html_code USING p_fname.
PERFORM fm_load_html USING p_fname.
CALL SCREEN 2000.
ENDFORM. "fm_display_data
After.
*&--------------------------------------------------------------------*
*& Form fm_display_data
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM fm_display_data.
DATA : lv_total(20),
title(50),
lv_bulan TYPE n LENGTH 2.
DATA : li_tmp LIKE STANDARD TABLE OF gi_html_code WITH HEADER LINE.
REFRESH gi_html_code.
CONCATENATE 'Report Grafik Penjualan Tahun &nbsp' so_gjahr-low INTO title.
* Create Header Text and referencing source of JQuery
write_code1 ''.
write_code1 ''.
write_code1 ''.
write_code1 ''.
perform fm_create_css.
write_code1 ''.
write_code1 ''.
write_code1 '
'.
write_code3 '

' title '

'.
* Display Data to Screen
write_code1 '
'.
write_code1 '
'.
DO 12 TIMES.
ADD 1 TO lv_bulan.
write_code3 '
'.
ENDDO.
write_code1 '
'.
write_code3 '
'.
LOOP AT gi_header.
REFRESH : li_tmp.
* Eliminate thousand separator and change decimals separator
WRITE gi_header-total TO lv_total CURRENCY so_waerk-low.
REPLACE ALL OCCURRENCES OF '.' IN lv_total WITH ''.
CONDENSE lv_total.
REPLACE ALL OCCURRENCES OF ',' IN lv_total WITH '.'.
write_code3 '
'.
ENDLOOP.
write_code1 '
'.
write_code1 '

' lv_bulan '
' so_gjahr-low ' ' lv_total '
'.
* Java Script for retrieving data from HTML tables.
write_code1 ' '.
* End of Line
write_code1 '
'.
write_code1 ''.
write_code1 ''.
PERFORM fm_create_html_file TABLES gi_html_code USING p_fname.
PERFORM fm_load_html USING p_fname.
CALL SCREEN 2000.
ENDFORM. "fm_display_data
14.Now change the CSS style on fm_create_css.
Before.
FORM fm_create_css.
write_code1 ''.
write_code1 '
-->'.
ENDFORM. "fm_create_css
After.
FORM fm_create_css.
write_code1 ''.
write_code1 '
-->'.
ENDFORM.
15.Activate and run.
17.Finished.
Result.
Input Selection
ar0299
Output
ar0300

Comments