Sie sind auf Seite 1von 6

*&---------------------------------------------------------------------*

*& Report ZPMCST_FNAR


*& Cost by function area
*& Program author: danai piya
*& : danai@abapth.com
*& Date: 25 Mar 2011
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZPMCST_FNAR.
tables: faglflexa.
selection-screen begin of block blk1 with frame title text-001.
parameters: p_rbukr type faglflexa-rbukrs memory id BUK
default '165',
p_ryear type faglflexa-ryear default sy-datum+0(4).
select-options: s_poper for faglflexa-poper default '1' to '16',
s_coste for faglflexa-cost_elem.
selection-screen end of block blk1.
data: begin of gt_faglflexa occurs 0,
ryear type faglflexa-ryear,
docnr type faglflexa-docnr,
rbukrs type faglflexa-rbukrs,
racct type faglflexa-racct,
kokrs type faglflexa-kokrs,
rfarea type faglflexa-rfarea,
hsl type faglflexa-hsl,
poper type faglflexa-poper,
rwcur type faglflexa-rwcur,
gjahr type faglflexa-gjahr,
belnr type faglflexa-belnr,
buzei type faglflexa-buzei,
end of gt_faglflexa,
begin of gt_bseg occurs 0,
bukrs type bseg-bukrs,
belnr type bseg-belnr,
gjahr type bseg-gjahr,
kostl type bseg-kostl,
aufnr type bseg-aufnr,
end of gt_bseg,
begin of gt_aufk occurs 0,
aufnr type aufk-aufnr,
kostl type VIAUFKST-kostl,
end of gt_aufk,
begin of wa_report,
kostl type VIAUFKST-kostl,
kstar type csku-kstar,
ktext type csku-ktext,
total type faglflext-hsl01,
production type faglflext-hsl01,
sales type faglflext-hsl01,
admin type faglflext-hsl01,
account type faglflext-hsl01,
end of wa_report,
gt_report like table of wa_report,
begin of wa_report_root,
expand type char01,
kostl type VIAUFKST-kostl,
ltext type cskt-ltext,
end of wa_report_root,
gt_report_root like table of wa_report_root.

*start-of-selection.
start-of-selection.
perform get_fl_data.
perform prepare_fl_data.
perform display_report.

*&---------------------------------------------------------------------*
*& Form get_fl_data
*&---------------------------------------------------------------------*
form get_fl_data .
data: lt_bseg like table of gt_bseg,
wa_bseg like line of lt_bseg.
select ryear docnr rbukrs racct kokrs
rfarea hsl poper rwcur gjahr
belnr buzei
from faglflexa
into table gt_faglflexa
where rldnr = '0L'
and rbukrs = p_rbukr
and racct in s_coste
and ryear = p_ryear
and poper in s_poper
and rfarea in ('0001', '0002', '0003', '0004')
and kokrs = 'CPMJ'.
check gt_faglflexa[] is not initial.
sort gt_faglflexa by rbukrs belnr gjahr buzei.
select bukrs belnr gjahr kostl aufnr into table gt_bseg
from bseg
for all entries in gt_faglflexa
where bukrs = gt_faglflexa-rbukrs
and belnr = gt_faglflexa-belnr
and gjahr = gt_faglflexa-gjahr
and buzei = gt_faglflexa-buzei.
loop at gt_bseg where aufnr <> ''.
wa_bseg = gt_bseg.
append wa_bseg to lt_bseg.
endloop.
check lt_bseg[] is not initial.
select aufnr kostl into table gt_aufk
from VIAUFKST
for all entries in lt_bseg
where aufnr = lt_bseg-aufnr.
endform. " get_fl_data
*&---------------------------------------------------------------------*
*& Form prepare_fl_data
*&---------------------------------------------------------------------*
form prepare_fl_data .
loop at gt_faglflexa.
clear: wa_report,
gt_bseg,
gt_aufk.
read table gt_bseg with key
bukrs = gt_faglflexa-rbukrs
belnr = gt_faglflexa-belnr
gjahr = gt_faglflexa-gjahr.
read table gt_aufk with key
aufnr = gt_bseg-aufnr.
wa_report-kostl = gt_aufk-kostl.
wa_report-kstar = gt_faglflexa-racct.
case gt_faglflexa-rfarea.
when '0001'.
wa_report-production = wa_report-production + gt_faglflexa-hsl.
when '0002'.
wa_report-sales = wa_report-sales + gt_faglflexa-hsl.
when '0003'.
wa_report-admin = wa_report-admin + gt_faglflexa-hsl.
when '0004'.
wa_report-account = wa_report-account + gt_faglflexa-hsl.
endcase.
collect wa_report into gt_report.
endloop.
*sum
loop at gt_report into wa_report.
clear: wa_report_root.
select single ktext into wa_report-ktext
from csku
where spras = 'E'
and ktopl = 'CPMJ'
and kstar = wa_report-kstar.
wa_report-total = wa_report-production +
wa_report-sales +
wa_report-admin +
wa_report-account.
modify gt_report from wa_report
transporting total ktext.

wa_report_root-kostl = wa_report-kostl.
if not wa_report-kostl is initial.
select single ltext into wa_report_root-ltext
from cskt
where spras = 'E'
and kokrs = 'CPMJ'
and kostl = wa_report-kostl
and datbi >= sy-datum.
endif.
collect wa_report_root into gt_report_root.
endloop.
sort gt_report_root by kostl.
sort gt_report by kostl kstar.
endform. " prepare_fl_data
*&---------------------------------------------------------------------*
*& Form display_report
*&---------------------------------------------------------------------*
form display_report .
data: r_salv_hier type ref to CL_SALV_HIERSEQ_TABLE,
lr_functions type ref to cl_salv_functions,
lr_columns type ref to cl_salv_columns_hierseq,
lr_column type ref to cl_salv_column_hierseq,
lr_salv_column type ref to cl_salv_column,
lr_level type ref to cl_salv_hierseq_level,
lr_sorts type ref to cl_salv_sorts,
lr_aggregations type ref to cl_salv_aggregations,
lt_bind type salv_t_hierseq_binding,
lw_bind like line of lt_bind.
lw_bind-master = 'KOSTL'.
lw_bind-slave = 'KOSTL'.
append lw_bind to lt_bind.
try.
cl_salv_hierseq_table=>factory(
EXPORTING
t_binding_level1_level2 = lt_bind
IMPORTING
R_HIERSEQ = R_salv_hier
CHANGING
t_table_level1 = gt_report_root
t_table_level2 = gt_report
).
CATCH CX_SALV_DATA_ERROR .
CATCH CX_SALV_NOT_FOUND .
endtry.
lr_functions = r_salv_hier->get_functions( ).
lr_functions->set_all( abap_true ).
try.
lr_columns = r_salv_hier->get_columns( 1 ).
catch cx_salv_not_found.
endtry.
try.
lr_columns->set_expand_column( 'EXPAND' ).
catch cx_salv_data_error. "#EC NO_HANDLER
endtry.
*... set items expanded
try.
lr_level = r_salv_hier->get_level( 1 ).
catch cx_salv_not_found.
endtry.
lr_level->set_items_expanded( ).
*SLAVE
try.
lr_columns = r_salv_hier->get_columns( 2 ).
catch cx_salv_not_found.
endtry.
lr_columns->set_optimize( abap_true ).
try.
lr_salv_column = lr_columns->get_column( 'KOSTL' ).
lr_salv_column->set_technical( if_salv_c_bool_sap=>true ).
lr_salv_column = lr_columns->get_column( 'TOTAL' ).
lr_salv_column->SET_MEDIUM_TEXT( 'Total' ).
lr_salv_column->SET_LONG_TEXT( 'Total' ).
lr_salv_column->SET_OPTIMIZED( 'X' ).
lr_salv_column->set_output_length( 20 ).

lr_salv_column = lr_columns->get_column( 'PRODUCTION' ).


lr_salv_column->SET_MEDIUM_TEXT( 'Production' ).
lr_salv_column->SET_LONG_TEXT( 'Production' ).
lr_salv_column->SET_OPTIMIZED( 'X' ).
lr_salv_column->set_output_length( 20 ).
lr_salv_column = lr_columns->get_column( 'SALES' ).
lr_salv_column->SET_MEDIUM_TEXT( 'Sales' ).
lr_salv_column->SET_LONG_TEXT( 'Sales' ).
lr_salv_column->SET_OPTIMIZED( 'X' ).
lr_salv_column->set_output_length( 20 ).

lr_salv_column = lr_columns->get_column( 'ADMIN' ).


lr_salv_column->SET_MEDIUM_TEXT( 'Administration' ).
lr_salv_column->SET_LONG_TEXT( 'Administration' ).
lr_salv_column->SET_OPTIMIZED( 'X' ).
lr_salv_column->set_output_length( 20 ).
lr_salv_column = lr_columns->get_column( 'ACCOUNT' ).
lr_salv_column->SET_MEDIUM_TEXT( 'Account & Finance' ).
lr_salv_column->SET_LONG_TEXT( 'Account & Finance' ).
lr_salv_column->SET_short_TEXT( 'Account' ).
lr_salv_column->SET_OPTIMIZED( 'X' ).
lr_salv_column->set_output_length( 20 ).
* lr_sorts = r_salv_hier->get_sorts( level = '1' ).
* lr_sorts->add_sort( columnname = 'KOSTL'
* subtotal = abap_true ).

lr_sorts = r_salv_hier->get_sorts( level = '2' ).


lr_sorts->add_sort( columnname = 'KSTAR'
sequence = if_salv_c_sort=>sort_down ).
catch cx_salv_existing.
catch cx_salv_data_error.
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.
try.
lr_aggregations = r_salv_hier->get_aggregations( 2 ).
lr_aggregations->add_aggregation( columnname = 'TOTAL' ).
lr_aggregations->add_aggregation( columnname = 'PRODUCTION' ).
lr_aggregations->add_aggregation( columnname = 'SALES' ).
lr_aggregations->add_aggregation( columnname = 'ADMIN' ).
lr_aggregations->add_aggregation( columnname = 'ACCOUNT' ).
catch cx_salv_data_error.
catch cx_salv_not_found.
catch cx_salv_existing.
endtry.

*sort
r_salv_hier->display( ).
endform. " display_report

Das könnte Ihnen auch gefallen