Saturday, February 4, 2017

Total and Subtotal in ALV Hierarchical report using SAP ABAP OOPS

Cl_salv_hierseq_table : Basic Class for hierarchical sequential tables.

By Using Cl_salv_hierseq_table class methods we can display hierarchical report.

Procedure :

Cl_salv_hierseq_table contains Factory method(static method). By using this method ,Will get instance for ALV table object.

1 . 
TRY.
cl_salv_hierseq_table=>factory(
Exporting
  t_binding_level1_level2  =  itab_binding.
Importing
 r_hierseq = obj_hierseq
Changing
 t_table_level1  = itab_header[]
 t_table_level2  = itab_item[]
).

Catch .....

Endtry.

Declaration for Factory Method parameters:

DATA : itab_binding  TYPE  salv_t_hierseq_binding,
             wa_binding    TYPE salv_s_hierseq_binding,
             obj_hierseq   TYPE REF TO cl_salv_hierseq_table,
            itab_header  TYPE STANDARD TABLE OF (required database table/user defined structure),
           itab_item  TYPE STANDARD TABLE OF (required database table/user defined structure).

Data Fill :

Fill header and item tables based on your requirement.

wa_binding-master = 'XYZ' .   "  (Field common to header and item table)
wa_binding-slave = 'XYZ' .      " (Field common to header and item table)
Append wa_binding to itab_binding.


2.
PERFORM calculate_sub_total.

3.

obj_hierseq->display( ).


---------------------------------------------------------------------------------------------------------------
*in order to do subtotals.. first we have to perform the sort based on the column on which we are going to
*calculate subtotals

*cl_salv_sorts :  class used for sorting..

Note 1: Sub totals will work on header level fields only

Note 2 : Level 1 : Header , Level 2 : Item 



FORM  calculate_sub_total.

DATA : obj_sorts TYPE REF TO cl_salv_sorts.

CREATE OBJECT obj_sorts
   Exporting
       r_columns         =   obj_columns  "Alv Column  (TYPE REF TO cl_salv_columns)
       r_aggregations  =   obj_sum        "Alv Aggeregation (TYPE REF TO cl_salv_aggregations)
.


"Calculating  Sub Totals 

obj_sorts  =  obj_hierseq->get_sorts( level = 1 ).

CALL METHOD obj_sorts->add_sort
Exporting
Columnname  = 'XYZ'     "Sort column always key field
Position = 1
subtotal  = if_salv_c_bool_sap=>true     "" Subtptal = 'X'
group  =  if_salv_c_sort=>sort_up

Receiving
  Value = obj_sort_column     "Sorted column   (TYPE REF TO cl_salv_sort )
.


CALL METHOD obj_sort_column->set_subtotal   "Setting subtotal for returned column name from add_sort
Exporting
  Value = if_salv_c_bool_sap=>true
.


"Calculating Total

obj_sum  =  obj_hierseq->get_aggregations( level = 2 ).

obj_sum->add_aggregation(
Exporting

columnname  =  'TOTAL'    "Column name present in ITAB  itab_item
aggregation =  if_salv_c_aggregation=>total
Receiving
  Value = obj_total       "Obj contains total   ( TYPE REF TO cl_salv_aggregation )
).


ENDFORM.






No comments:

Post a Comment