2007 Aug 25 4:00 AM
Hi friends,
My scenario is :
I have a itab in the following manner
Field1(MATNR) Field2(SHORTAGE) Field3(PDATU)
Cylinder 50 22.11.2007
Cylinder 40 23.11.2007
Crankcase 50 22.11.2007
Crankcase 60 23.11.2007
Here the main issue is i need a alv report
The output of the alv headings is in the format
MATNR 22/11 23/11
Cylinder 50 40
Crankase 50 60
Here i need the date as the field.
So how to bulid a itab with the dates as field.
Points will be rewarded
Hi friends,
My scenario is :
I have a itab in the following manner
Field1(MATNR) Field2(SHORTAGE) Field3(PDATU)
Cylinder 50 22.11.2007
Cylinder 40 23.11.2007
Crankcase 50 22.11.2007
Crankcase 60 23.11.2007
Here the main issue is i need a alv report
The output of the alv headings is in the format
MATNR 22/11 23/11
Cylinder 50 40
Crankase 50 60
Here i need the date as the field.
So how to bulid a itab with the dates as field.
Points will be rewarded
2007 Aug 25 5:26 AM
What you need to do is make your main internal table dynamic, which you are going to pass to the ALV FM in the end.
I'm assuming your report has a selection date range on the selection screen. Depending on user input on this date range, you need to build as many columns into your internal table where each column is a month - that's why it needs to be dynamic because the date range (start & end) will only be known at run-time by the ABAP processor.
See code below for one of the reports I wrote recently with similar requirement but only difference from yours is that each of its columns is a week instead of a month with a max of 9 weeks (columns) to be output. If more than 9 weeks are derived for the user date selection input then remaining balance are added onto the 9th column (with a + sign in the column label). Note: I've only included the relevant parts of the report for you to understand how to code the requirement and <b>the most important part of the code in bold face.</b>
report zxxxxx
no standard page heading
message-id zxx.
include zxxxxx_data.
include zxxxxx_sscreen.
include zxxxxx_forms.
start-of-selection.
perform get_inquiry_data.
end-of-selection.
perform display_report.
form get_inquiry_data.
* Local storage
types: begin of lty_vbpa,
vbeln type vbeln,
posnr type posnr,
parvw type parvw,
kunnr type kunag,
end of lty_vbpa.
data lw_vbpa type lty_vbpa.
data li_vbpa type standard table of lty_vbpa.
* Sales inquiry local data storage
data lw_header type ty_header.
data li_header type ty_header_t.
data lw_item type ty_item.
data li_item type ty_item_t.
* Retrieve all header level data for sales inquiries, based on values
* user has entered on selection screen
* The partner function selections below (VBPA) select on item number
* '000000', as this entry stores header info for sales inquiries
clear li_header.
select vbak~vbeln
vbak~vkgrp
vbak~vkbur
vbak~vdatu
vbak~hkunnr03
vbpa~kunnr
vbpa~adrnr
into table li_header
from vbak as vbak
join vbpa as vbpa
on vbak~vbeln eq vbpa~vbeln
where vbak~vbtyp eq 'A'
and vbak~augru eq p_augru
and vbak~vkorg eq p_vkorg
and vbak~vtweg eq p_vtweg
and vbak~spart eq p_spart
and vbak~vkgrp in s_vkgrp
and vbak~vkbur in s_vkbur
and vbak~vdatu in s_vdatu
and vbak~hkunnr03 in s_kunn03
and vbpa~posnr eq '000000'
and vbpa~parvw eq 'WE'
and vbpa~kunnr in s_kunnr.
[...] [...]
* Get all inquiry item data for all headers selected
select vbeln
posnr
matnr
arktx
kwmeng
vrkme
from vbap
into table li_item
for all entries in li_header
where vbeln = li_header-vbeln.
* Fill the consolidated tables
perform fill_consolidated_tables using li_item
li_header.
* Transpose the row values for weekly totals to columns
perform transpose_quantities.
endform. "get_inquiry_data
form fill_consolidated_tables using pi_item type ty_item_t
pi_header type ty_header_t.
data lw_item type ty_item.
data lw_header type ty_header.
data l_week type kweek.
* Get descriptions for Sales Groups
select * into table li_tvgrt from tvgrt
for all entries in pi_header
where spras eq sy-langu
and vkgrp eq pi_header-vkgrp.
sort li_tvgrt by vkgrp.
delete adjacent duplicates from li_tvgrt.
[...]
* Assign a calendar week to each line item in the consolidated table
call function 'DATE_GET_WEEK'
exporting
date = lw_header-vdatu
importing
week = l_week
exceptions
date_invalid = 1
others = 2.
if sy-subrc = 0.
w_consolidate-week = l_week.
clear l_week.
endif.
endif.
* Read the Cust.Material No.(KDMAT) from table KNMT
select single kdmat from knmt
into w_consolidate-kdmat
where vkorg = p_vkorg
and vtweg = p_vtweg
and kunnr = w_consolidate-kunnr
and matnr = w_consolidate-matnr.
* Add record to consolidated table
append w_consolidate to i_consolidate.
endloop.
endform. "fill_consolidated_tables
form transpose_quantities.
data li_table type ref to data.
data lv_first_week type kweek.
perform fieldcat_build changing lv_first_week.
<b> call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = i_fieldcat
importing
ep_table = li_table
exceptions
generate_subpool_dir_full = 1
others = 2.
if sy-subrc eq 0.
assign li_table->* to <i_table>.</b> else.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
* Fill the newly created table with data from the consolidated table
perform fill_table using lv_first_week.
endform. "transpose_quantities
form fieldcat_build changing cv_first_week type kweek.
data: lv_coltext type lvc_txtcol,
lv_last_week type kweek,
lv_temp_week type kweek,
lv_num_weeks type i,
l_date type datum,
l_date_c type c length 10,
l_indx type syindex.
case c_true.
when p_ship.
perform add_col_fcat using: 'KUNNR' 'Ship-to' 10 space space,
'MATNR' text-f03 18 space space,
'ADRNR' text-f02 10 space space,
'ARKTX' 'Description' 40 space space,
'KDMAT' text-f01 10 space space.
when p_hier.
perform add_col_fcat using: 'HKUNNR03' text-f00 10 space space,
'MATNR' text-f03 18 space space,
'ARKTX' 'Description' 40 space space.
when p_group.
perform add_col_fcat using: 'VKGRP' 'Sales Group' 23 space space,
'MATNR' text-f03 18 space space,
'ARKTX' 'Description' 40 space space.
when p_area.
perform add_col_fcat using: 'VKORG' 'Sales Area' 10 space space,
'MATNR' text-f03 18 space space,
'ARKTX' 'Description' 40 space space.
endcase.
* Other common fields 'MATNR' & 'ARKTX' could not be coded outside the CASE statement
* above because it corrupts the order of the HIERSQ ALV columns
perform add_col_fcat using 'TOT_QTY' 'Tot. Qty.' 10 c_true 'QUAN'.
* Get first week in the date range entered by user
call function 'DATE_GET_WEEK'
exporting
date = s_vdatu-low
importing
week = cv_first_week
exceptions
date_invalid = 1
others = 2.
* Get last week in the date range entered by user, if the user has entered a range
if not s_vdatu-high is initial.
call function 'DATE_GET_WEEK'
exporting
date = s_vdatu-high
importing
week = lv_last_week
exceptions
date_invalid = 1
others = 2.
if sy-subrc eq 0.
if lv_last_week(4) = cv_first_week(4).
lv_num_weeks = lv_last_week - cv_first_week.
else.
* year has changed, so weeks have to be accumulated
lv_num_weeks = abs( s_vdatu-high - s_vdatu-low ) / 7.
endif.
if lv_num_weeks < 1.
lv_num_weeks = 1.
endif.
endif.
else.
lv_num_weeks = 1.
endif.
* Build enough columns to cover all weeks in the range
lv_temp_week = cv_first_week.
* Max. 9 columns are to be displayed
if lv_num_weeks > 9.
lv_num_weeks = 9.
endif.
<b> do lv_num_weeks times.
add 1 to l_indx.
call function 'WEEK_GET_FIRST_DAY'
exporting
week = lv_temp_week
importing
date = l_date
exceptions
week_invalid = 1.
if sy-subrc = 1.
add 1 to lv_temp_week(4).
lv_temp_week+4(2) = '01'.
call function 'WEEK_GET_FIRST_DAY'
exporting
week = lv_temp_week
importing
date = l_date
exceptions
week_invalid = 1.
write l_date to l_date_c .
if l_indx = lv_num_weeks.
concatenate l_date_c '(+)' into lv_coltext.
else.
lv_coltext = l_date_c.
endif.
perform add_col_fcat using lv_temp_week lv_coltext 10 c_true 'QUAN'.
lv_temp_week = lv_temp_week + 1.
continue.
endif.
write l_date to l_date_c .
if l_indx = lv_num_weeks.
concatenate l_date_c '(+)' into lv_coltext.
else.
lv_coltext = l_date_c.
endif.
perform add_col_fcat using lv_temp_week lv_coltext 10 c_true 'QUAN'.
lv_temp_week = lv_temp_week + 1.
enddo.</b>
endform. "fieldcat_build
form add_col_fcat using col_name type any
col_lbl type c
col_len type i
do_sum type c
dat_typ type c.
data: lw_fieldcat type lvc_s_fcat.
lw_fieldcat-fieldname = col_name.
lw_fieldcat-coltext = col_lbl.
lw_fieldcat-outputlen = col_len.
lw_fieldcat-do_sum = do_sum.
lw_fieldcat-datatype = dat_typ.
append lw_fieldcat to i_fieldcat.
endform. " add_col_fcat
form fill_table using lv_first_week type kweek.
data: l_count type syindex,
dy_line type ref to data,
l_descr_ref type ref to cl_abap_structdescr.
field-symbols: <li_table> type any,
<lv_field> type any,
<dyn_wa> type any,
<comp_wa> type abap_compdescr.
* All internal table/data-type manipulations are done dynamically here so spcl.care
* must be taken when modifying any logic in future (otherwise it'll sh* itself!)
* Create dynamic work area and assign to Field Symbols we'll need hence forward
<b> create data dy_line like line of <i_table>.
assign dy_line->* to: <dyn_wa>, <li_table>.
loop at i_consolidate into w_consolidate.
shift: w_consolidate-matnr left deleting leading '0',
w_consolidate-kunnr left deleting leading '0'.
move-corresponding: w_consolidate to <li_table>,
w_consolidate to <dyn_wa>.
* we'll decide on the time bucket by some elegant programming here because our
* work structure <dyn_wa> is dynamic ie.its components are determined only at runtime
l_descr_ref ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).
read table l_descr_ref->components assigning <comp_wa>
with key name = w_consolidate-week.
if sy-subrc = 0.
assign component sy-tabix of structure <li_table> to <lv_field>.
else.
* out of date range selection i.e. sometimes in the future...so we'll add it
* to the last time bucket
describe table l_descr_ref->components lines l_count.
assign component l_count of structure <li_table> to <lv_field>.
endif.
<lv_field> = w_consolidate-kwmeng.
collect <li_table> into <i_table>.</b>
* retrieve ship-to detail info for display later if reporting by "Ship-to"
if not p_ship is initial.
at new kunnr.
move-corresponding <dyn_wa> to i_hierh.
select single name1 city1 house_num1 street city2
into (i_hierh-name, i_hierh-city, i_hierh-house,
i_hierh-street, i_hierh-distr)
from adrc where addrnumber = i_hierh-adrnr
and date_from <= sy-datum
and nation = space.
shift i_hierh-kunnr left deleting leading '0'.
translate i_hierh to upper case.
collect i_hierh.
clear i_hierh.
endat.
endif.
clear: <li_table>,
<dyn_wa>.
endloop.
* read the Sales Area text from the database
if not p_area is initial.
[...] [...]
endif.
endform. "fill_table
form display_report.
data: lw_layout type slis_layout_alv,
lw_events type slis_t_event,
lw_variant type disvariant,
li_groups type slis_t_sp_group_alv,
li_keyinfo type slis_keyinfo_alv,
li_sort type slis_t_sortinfo_alv,
l_repid like sy-repid,
lw_print type slis_print_alv.
field-symbols: <i_tab_hierh> type standard table.
data: li_fieldcat_reuse type slis_t_fieldcat_alv.
* Check if there are any result to report
if i_consolidate is initial.
message i114.
exit.
endif.
* Transfer the values from the current fieldcat structure (used
* by ALV grid) to the structure used be REUSE ALV functions
perform get_fieldcat_reuse changing li_fieldcat_reuse.
* Layout
perform get_layout changing lw_layout.
* Sort parameters
perform fill_sort changing li_sort.
* Define Keyinfo
perform define_keyinfo using li_keyinfo.
l_repid = sy-repid.
lw_print-no_print_listinfos = c_true.
lw_print-prnt_info = c_true.
perform eventtab_build using lw_events.
case c_true.
when p_ship.
assign i_hierh[] to <i_tab_hierh>.
when p_area.
assign i_hierh_vkorg[] to <i_tab_hierh>.
when p_hier.
assign li_name1 to <i_tab_hierh>.
when p_group.
assign li_tvgrt to <i_tab_hierh>.
endcase.
sort: <i_tab_hierh>,
<i_table>.
* Display the report
call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
exporting
i_callback_program = l_repid
is_keyinfo = li_keyinfo
is_layout = lw_layout
it_fieldcat = li_fieldcat_reuse
i_tabname_header = '1'
i_tabname_item = '2'
it_events = lw_events
it_sort = li_sort
is_print = lw_print
tables
t_outtab_header = <i_tab_hierh>
t_outtab_item = <i_table>.
endform. "display_report
form get_fieldcat_reuse changing pi_fieldcat type slis_t_fieldcat_alv.
data: lw_fieldcat_reuse type slis_fieldcat_alv,
lw_fieldcat type lvc_s_fcat.
* for header table
case c_true.
* when report by "Ship-to"
when p_ship.
clear lw_fieldcat_reuse.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'KUNNR'.
lw_fieldcat_reuse-seltext_m = 'Ship-to'.
lw_fieldcat_reuse-outputlen = 15.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'NAME'.
lw_fieldcat_reuse-seltext_m = 'Name'.
lw_fieldcat_reuse-outputlen = 40.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'HOUSE'.
lw_fieldcat_reuse-seltext_m = 'House No.'.
lw_fieldcat_reuse-outputlen = 15.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'STREET'.
lw_fieldcat_reuse-seltext_m = 'Street Name'.
lw_fieldcat_reuse-outputlen = 60.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'DISTR'.
lw_fieldcat_reuse-seltext_m = 'District'.
lw_fieldcat_reuse-outputlen = 30.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'CITY'.
lw_fieldcat_reuse-seltext_m = 'City'.
lw_fieldcat_reuse-outputlen = 20.
append lw_fieldcat_reuse to pi_fieldcat.
when p_area.
* when report by "Sales Area"
clear lw_fieldcat_reuse.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'VKORG'.
lw_fieldcat_reuse-seltext_m = 'Sales Area'.
lw_fieldcat_reuse-outputlen = 20.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'VTEXT'.
lw_fieldcat_reuse-seltext_m = 'Name'.
lw_fieldcat_reuse-outputlen = 40.
append lw_fieldcat_reuse to pi_fieldcat.
when p_hier.
* when report by "Customer Hierarchy"
clear lw_fieldcat_reuse.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'KUNNR'.
lw_fieldcat_reuse-seltext_m = 'Cust.Hier.3'.
lw_fieldcat_reuse-outputlen = 20.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'NAME1'.
lw_fieldcat_reuse-seltext_m = 'Name'.
lw_fieldcat_reuse-outputlen = 40.
append lw_fieldcat_reuse to pi_fieldcat.
when p_group.
* when report by "Sales Group"
clear lw_fieldcat_reuse.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'VKGRP'.
lw_fieldcat_reuse-seltext_m = 'Sales Group'.
lw_fieldcat_reuse-outputlen = 20.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat.
lw_fieldcat_reuse-tabname = '1'.
lw_fieldcat_reuse-fieldname = 'BEZEI'.
lw_fieldcat_reuse-seltext_m = 'Name'.
lw_fieldcat_reuse-outputlen = 40.
append lw_fieldcat_reuse to pi_fieldcat.
endcase.
* for item table
loop at i_fieldcat into lw_fieldcat.
move-corresponding lw_fieldcat to lw_fieldcat_reuse.
move lw_fieldcat-coltext to lw_fieldcat_reuse-seltext_m.
move '2' to lw_fieldcat_reuse-tabname.
case lw_fieldcat-fieldname.
* hide the following columns from displaying in the ALV
when 'WEEK' or 'KUNNR' or 'ADRNR' or 'HKUNNR03' or 'VKGRP' or 'VKORG'.
lw_fieldcat_reuse-no_out = c_true.
endcase.
at last.
lw_fieldcat_reuse-outputlen = 13.
endat.
if lw_fieldcat_reuse-datatype = 'QUAN'.
lw_fieldcat_reuse-decimals_out = 0.
lw_fieldcat_reuse-no_zero = c_true.
endif.
append lw_fieldcat_reuse to pi_fieldcat.
clear lw_fieldcat_reuse.
endloop.
endform. "get_fieldcat_reuse
form define_keyinfo using p_keyinfo type slis_keyinfo_alv.
clear p_keyinfo.
case c_true.
when p_ship.
p_keyinfo-header01 = 'KUNNR'.
p_keyinfo-item01 = 'KUNNR'.
when p_area.
p_keyinfo-header01 = 'VKORG'.
p_keyinfo-item01 = 'VKORG'.
when p_hier.
p_keyinfo-header01 = 'KUNNR'.
p_keyinfo-item01 = 'HKUNNR03'.
when p_group.
p_keyinfo-header01 = 'VKGRP'.
p_keyinfo-item01 = 'VKGRP'.
endcase.
endform. " define_keyinfoHope this helps.
Cheers,
Sougata.