types: begin of t_alv.
include type t_list.
types: dispo like marc-dispo,
ekgrp like marc-ekgrp,
verid like blpk-verid,
spart like mara-spart,
bklas like mbew-bklas,
rows type epsssrows,
vendor_name type mepo_vendor,
grtxt like t157e-grtxt,
xabln like mkpf-xabln,
bldat type mkpf-bldat,
end of t_alv.
- If one of the additional select-options is used then it does preselection and manipulates select-options passed to MB51
- Call of MB51
- Import of results and additional selection done on a base of the result table
- Output display
report zabzmb51.
tables:
mara,
mseg,
mbew,
mkpf,
marc,
blpk,
blpp.
types: t_list type zab_mb51_call=>t_list.
types: begin of t_vendor,
lifnr type lfa1-lifnr,
name1 type lfa1-name1,
name2 type lfa1-name2,
end of t_vendor.
types: begin of t_mkpf,
mblnr type mkpf-mblnr,
mjahr type mkpf-mjahr,
bktxt type mkpf-bktxt,
xabln type mkpf-xabln,
bldat type mkpf-bldat,
end of t_mkpf.
data: go_grid type ref to cl_gui_alv_grid.
types: begin of t_alv.
include type t_list.
types: dispo like marc-dispo,
ekgrp like marc-ekgrp,
verid like blpk-verid,
spart like mara-spart,
bklas like mbew-bklas,
rows type epsssrows,
vendor_name type mepo_vendor,
grtxt like t157e-grtxt,
xabln like mkpf-xabln,
bldat type mkpf-bldat,
end of t_alv.
data: gt_mb51res type zab_mb51_call=>tt_list.
data: begin of gs_marc,
matnr type marc-matnr,
werks type marc-werks,
ekgrp type marc-ekgrp,
dispo type marc-dispo,
spart type mara-spart,
bklas type mbew-bklas,
end of gs_marc.
data: gt_marc like standard table of gs_marc.
data: gt_alv type table of t_alv.
data: gs_alv type t_alv.
data: gt_vendor type table of t_vendor.
data: gt_mkpf type table of t_mkpf.
type-pools: slis.
data: gt_fcat type slis_t_fieldcat_alv ,
gs_fcat like line of gt_fcat,
gs_layout type slis_layout_alv,
gs_variant_log type disvariant,
gs_variant type disvariant.
data: gt_fcatlvc type lvc_t_fcat.
data: g_grid type ref to cl_gui_alv_grid.
data: begin of gs_blpp,
belnr type blpp-belnr,
prtnr type blpp-prtnr,
verid type blpk-verid,
end of gs_blpp.
data: gt_blpp like table of gs_blpp.
data: begin of gs_prtnr,
prtnr type blpp-prtnr,
end of gs_prtnr.
data: gt_prtnr like table of gs_prtnr.
data: begin of gs_blpk,
prtnr type blpk-prtnr,
verid type blpk-verid,
end of gs_blpk.
data: gt_blpk like standard table of gs_blpk.
data: gt_t157e like standard table of t157e,
gs_t157e type t157e.
*--------------------------------------------------------------------*
* Selection screen
* I've copied selection screen from MB51 and added some of my own
* My own select-options begins "s_"
*--
selection-screen begin of block mseg with frame title text-001.
select-options matnr for mseg-matnr memory id mat
matchcode object mat1.
select-options werks for mseg-werks memory id wrk obligatory.
select-options lgort for mseg-lgort memory id lag.
select-options charg for mseg-charg memory id cha.
select-options lifnr for mseg-lifnr memory id lif.
select-options kunnr for mseg-kunnr memory id kun.
select-options bwart for mseg-bwart memory id bwa.
select-options sobkz for mseg-sobkz.
select-options aufnr for mseg-aufnr memory id anr.
select-options insmk for mseg-insmk.
select-options kostl for mseg-kostl memory id kos.
select-options sgtxt for mseg-sgtxt.
select-options umlgo for mseg-umlgo.
select-options umwrk for mseg-umwrk.
select-options wempf for mseg-wempf.
*additional fields
select-options s_dispo for marc-dispo.
select-options s_ekgrp for marc-ekgrp.
select-options s_verid for blpk-verid.
select-options s_spart for mara-spart.
select-options s_bklas for mbew-bklas.
"end of additional fields
selection-screen end of block mseg.
selection-screen begin of block mkpf with frame title text-002.
select-options budat for mkpf-budat obligatory.
select-options usnam for mkpf-usnam memory id usr.
select-options vgart for mkpf-vgart.
select-options bktxt for mkpf-bktxt.
select-options mblnr for mkpf-mblnr." memory id mbn.
select-options xblnr for mkpf-xblnr.
*additional fields
select-options s_bldat for mkpf-bldat.
"end of additional fields
selection-screen end of block mkpf.
*----------------------------------------------------------------------*
* definition of the selections screen
*----------------------------------------------------------------------*
selection-screen begin of block disp with frame title text-003.
parameters: p_layout like disvariant-variant.
selection-screen end of block disp.
selection-screen begin of block arch with frame title text-005.
parameters: database like am07m-ar_flag as checkbox default 'X',
shortdoc like am07m-ar_flag2 as checkbox default ' ',
archive like am07m-ar_flag1 as checkbox default ' ' user-command us_archive.
* parameter for the archive info structure "n732471
selection-screen : begin of line. "n732471
selection-screen comment 1(28) co_aistr "n732471
for field pa_aistr. "n732471
selection-screen position 33. "n732471
parameters : pa_aistr like aind_str1-archindex
default 'YOUR_ARCHIVE_HERE'. "n732471
selection-screen : end of line. "n732471
selection-screen end of block arch.
at selection-screen on value-request for p_layout.
perform f4_for_variant.
initialization.
tables: sscrfields.
perform authorization_check.
start-of-selection.
perform sapgui_progress using text-sp1 0.
perform pre-selection.
perform sapgui_progress using text-sp2 50.
perform mb51_call.
perform sapgui_progress using text-sp3 80.
perform additional_selection.
perform sapgui_progress using text-sp4 90.
perform data_collection.
end-of-selection.
perform sapgui_progress using text-sp5 95.
perform build_fcat.
perform reuse_alv.
*&---------------------------------------------------------------------*
*& Form pre-selection
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form pre-selection.
field-symbols: <marc> like gs_marc.
if s_ekgrp[] is not initial or s_dispo[] is not initial
or s_spart[] is not initial or s_bklas[] is not initial.
select marc~matnr marc~werks marc~dispo marc~ekgrp mara~spart mbew~bklas
into corresponding fields of table gt_marc
from marc
inner join mara
on marc~mandt eq mara~mandt
and marc~matnr eq mara~matnr
inner join mbew
on mbew~mandt eq marc~mandt
and mbew~matnr eq marc~matnr
and mbew~bwkey eq marc~werks
and mbew~bwtar eq space
where marc~matnr in matnr
and marc~werks in werks
and marc~dispo in s_dispo
and marc~ekgrp in s_ekgrp
and mara~spart in s_spart
and mbew~bklas in s_bklas.
refresh matnr[].
loop at gt_marc assigning <marc>.
clear matnr.
matnr-sign = 'I'.
matnr-option = 'EQ'.
matnr-low = <marc>-matnr.
collect matnr.
endloop.
endif.
endform. "pre-selection
*&---------------------------------------------------------------------*
*& Form mb51_call
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form mb51_call.
try.
zab_mb51_call=>call_mb51_static(
exporting
i_selection_from_program = 'X'
i_progname = sy-repid
changing
* it_seltab = it_seltab
ct_list = gt_mb51res
).
endtry.
endform. "mb51_call
*&---------------------------------------------------------------------*
*& Form build_fcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form build_fcat.
try.
cl_salv_table=>factory( importing
r_salv_table = data(salv_table)
changing
t_table = gt_alv ).
data(lvc_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = salv_table->get_columns( ) " ALV Filter
r_aggregations = salv_table->get_aggregations( ) " ALV Aggregations
).
call function 'LVC_TRANSFER_TO_SLIS'
exporting
it_fieldcat_lvc = lvc_fcat " Field Catalog
importing
et_fieldcat_alv = gt_fcat " Field Catalog
tables
it_data = gt_alv " Data Table
exceptions
it_data_missing = 1
it_fieldcat_lvc_missing = 2
others = 3
.
if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
catch cx_root.
endtry.
endform. "build_fcat
*&---------------------------------------------------------------------*
*& Form reuse_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form reuse_alv.
gs_layout-no_input = ''.
gs_layout-colwidth_optimize = 'X'.
gs_layout-coltab_fieldname = 'COLOR'.
gs_layout-zebra = 'X'.
gs_variant-variant = p_layout.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
* i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM
i_callback_user_command = 'USER_COMMAND'
* i_grid_title = outtext
is_layout = gs_layout
it_fieldcat = gt_fcat[]
* it_special_groups = gd_tabgroup
* it_events = gt_events
* is_print = gd_prntparams
i_save = 'A'
is_variant = gs_variant
tables
t_outtab = gt_alv[] "output table
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
endform. "reuse_alv
*&---------------------------------------------------------------------*
*& Form data_collection
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form data_collection.
field-symbols: <mb51> like line of gt_mb51res,
<mkpf> like line of gt_mkpf.
data: ls_vendor type t_vendor.
loop at gt_mb51res assigning <mb51>.
move-corresponding <mb51> to gs_alv.
move 1 to gs_alv-rows.
read table gt_blpp into gs_blpp
with key belnr = <mb51>-mblnr binary search.
if sy-subrc eq 0.
gs_alv-verid = gs_blpp-verid.
endif.
read table gt_marc into gs_marc
with key matnr = <mb51>-matnr
werks = <mb51>-werks binary search.
if sy-subrc eq 0.
move-corresponding gs_marc to gs_alv.
endif.
"select vendor name.
if <mb51>-lifnr is not initial.
read table gt_vendor into ls_vendor
with key lifnr = <mb51>-lifnr binary search.
if sy-subrc eq 0.
concatenate ls_vendor-name1 ls_vendor-name2
into gs_alv-vendor_name separated by space.
endif.
endif.
if <mb51>-bwart is not initial and <mb51>-grund is not initial.
read table gt_t157e into gs_t157e with key spras = sy-langu
bwart = <mb51>-bwart
grund = <mb51>-grund
binary search.
if sy-subrc eq 0.
gs_alv-grtxt = gs_t157e-grtxt.
clear gs_t157e.
endif.
endif.
read table gt_mkpf with key mjahr = <mb51>-mjahr
mblnr = <mb51>-mblnr
assigning <mkpf>
binary search.
if sy-subrc eq 0.
move <mkpf>-bktxt to gs_alv-bktxt.
move <mkpf>-xabln to gs_alv-xabln.
move <mkpf>-bldat to gs_alv-bldat.
endif.
if gs_alv-verid in s_verid and
gs_alv-bktxt in bktxt and
gs_alv-bldat in s_bldat.
append gs_alv to gt_alv.
clear gs_alv.
endif.
endloop.
refresh: gt_mkpf[], gt_t157e, gt_vendor[], gt_marc[], gt_blpp[],
gt_mb51res[].
endform. "data_collection
*&---------------------------------------------------------------------*
*& Form additional_selection
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form additional_selection.
data: begin of ls_matnr,
matnr type marc-matnr,
werks type marc-werks,
end of ls_matnr.
data: lt_matnr like table of ls_matnr.
data: ls_vendor type t_vendor.
data: lt_vendor like table of ls_vendor.
data: begin of ls_mblnr,
mblnr like mseg-mblnr,
mjahr like mseg-mjahr,
end of ls_mblnr.
data: lt_mblnr like table of ls_mblnr.
field-symbols: <mb51> like line of gt_mb51res.
check gt_mb51res[] is not initial.
refresh: lt_matnr[], lt_vendor[], gt_vendor[].
loop at gt_mb51res assigning <mb51>.
move-corresponding <mb51> to ls_matnr.
move <mb51>-lifnr to ls_vendor-lifnr.
move <mb51>-mblnr to ls_mblnr-mblnr.
move <mb51>-mjahr to ls_mblnr-mjahr.
collect ls_mblnr into lt_mblnr.
collect ls_matnr into lt_matnr.
collect ls_vendor into lt_vendor.
endloop.
sort lt_matnr by matnr werks.
select blpp~belnr blpp~prtnr verid
from blpp
inner join blpk as blpk
on blpk~mandt eq blpp~mandt
and blpk~prtnr eq blpp~prtnr
into corresponding fields of table gt_blpp
for all entries in gt_mb51res
where blpp~belnr eq gt_mb51res-mblnr
and verid in s_verid.
sort gt_blpp by belnr verid prtnr.
if lt_mblnr[] is not initial.
sort lt_mblnr by mjahr mblnr.
select mjahr mblnr bktxt xabln bldat from mkpf
into corresponding fields of table gt_mkpf
for all entries in lt_mblnr
where mjahr eq lt_mblnr-mjahr
and mblnr eq lt_mblnr-mblnr
and bldat in s_bldat.
sort gt_mkpf by mjahr mblnr.
endif.
if gt_marc[] is initial
and lt_matnr[] is not initial.
select marc~werks marc~matnr marc~dispo marc~ekgrp mara~spart
mbew~bklas
into corresponding fields of table gt_marc
from marc
inner join mara
on marc~mandt eq mara~mandt
and marc~matnr eq mara~matnr
inner join mbew
on mbew~mandt eq marc~mandt
and mbew~matnr eq marc~matnr
and mbew~bwkey eq marc~werks
and mbew~bwtar eq space
for all entries in lt_matnr
where marc~matnr eq lt_matnr-matnr
and marc~werks eq lt_matnr-werks.
endif.
sort gt_marc by matnr werks.
if lt_vendor[] is not initial.
select lifnr name1 name2
from lfa1 into corresponding fields of table gt_vendor
for all entries in lt_vendor
where lifnr eq lt_vendor-lifnr.
sort gt_vendor by lifnr.
endif.
select * from t157e into corresponding fields of table gt_t157e
where spras eq sy-langu.
sort gt_t157e by spras bwart grund ascending.
endform. "additional_selection
*&---------------------------------------------------------------------*
*& Form f4_for_variant
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form f4_for_variant.
data: f_variant type disvariant.
f_variant-report = sy-repid.
* move 'MAIN' to f_variant-log_group.
call function 'REUSE_ALV_VARIANT_F4'
exporting
is_variant = f_variant
i_save = 'A'
i_display_via_grid = 'X'
importing
es_variant = f_variant
exceptions
not_found = 1
program_error = 2
others = 3.
if sy-subrc = 0.
p_layout = f_variant-variant.
endif.
endform. "f4_for_variant
*&---------------------------------------------------------------------*
*& Form sapgui_progress
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->TEXT text
* -->PERCENTAGE text
*----------------------------------------------------------------------*
form sapgui_progress using text type c percentage type n.
call function 'SAPGUI_PROGRESS_INDICATOR'
exporting
percentage = percentage
text = text
exceptions
others = 1.
check sy-subrc = 0.
endform. "sapgui_progress
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->F_UCOMM text
* -->FS_SELFIELD text
*----------------------------------------------------------------------*
form user_command using f_ucomm like sy-ucomm
fs_selfield type slis_selfield.
field-symbols: <alv> like line of gt_alv.
data: f_vbak type vbak.
case f_ucomm.
when '&IC1'. "double-click
if fs_selfield-tabindex > 0 and fs_selfield-sumindex le 0.
read table gt_alv assigning <alv> index fs_selfield-tabindex.
if sy-subrc eq 0.
case fs_selfield-fieldname.
when 'EBELN'.
set parameter id 'BSP' field <alv>-ebelp.
set parameter id 'BES' field <alv>-ebeln.
call transaction 'ME23N' and skip first screen.
when 'EBELP'.
set parameter id 'BSP' field <alv>-ebelp.
set parameter id 'BES' field <alv>-ebeln.
call transaction 'ME23N' and skip first screen.
when 'MBLNR'.
set parameter id 'MBN' field <alv>-mblnr.
set parameter id 'MJA' field <alv>-mjahr.
call transaction 'MB03' and skip first screen.
when 'MATNR'.
set parameter id 'MAT' field <alv>-matnr.
call transaction 'MM03' and skip first screen.
when 'LIFNR'.
set parameter id 'LIF' field <alv>-lifnr.
call transaction 'MK03'.
when 'KDAUF'.
set parameter id 'AUN' field <alv>-kdauf.
set parameter id 'AUP' field <alv>-kdpos.
call transaction 'VA03' and skip first screen.
when 'KDPOS'.
set parameter id 'AUN' field <alv>-kdauf.
set parameter id 'AUP' field <alv>-kdpos.
call transaction 'VA03' and skip first screen.
when 'WEMPF'.
clear f_vbak.
select single * into f_vbak from vbak
where vbeln eq <alv>-kdauf.
set parameter id 'KUN' field <alv>-wempf.
set parameter id 'VKO' field f_vbak-vkorg.
set parameter id 'VTW' field f_vbak-vtweg.
set parameter id 'SPA' field f_vbak-spart.
call transaction 'VD03' and skip first screen.
when 'KUNNR'.
clear f_vbak.
select single * into f_vbak from vbak
where vbeln eq <alv>-kdauf.
set parameter id 'KUN' field <alv>-kunnr.
set parameter id 'VKO' field f_vbak-vkorg.
set parameter id 'VTW' field f_vbak-vtweg.
set parameter id 'SPA' field f_vbak-spart.
call transaction 'VD03' and skip first screen.
endcase.
endif.
endif.
when '&NFO'.
data: ft_isetab type kkblo_seldis.
call function 'K_KKB_SELECTIONS_READ'
exporting
v_program = sy-repid
tables
ct_isetab = ft_isetab.
*
perform info_popup(saplkkbl) using ft_isetab[] space if found.
refresh ft_isetab[].
when '&XLSX'.
perform export_to_excel.
endcase.
endform. "user_command
*&---------------------------------------------------------------------*
*& Form authorization_check
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form authorization_check.
authority-check object 'S_TCODE'
id 'TCD' field 'ZMB51'.
if sy-subrc ne 0.
message e172(00) with 'ZMB51'.
* You are not authorized to use Transaction &
endif.
endform. "authorization_check
*&---------------------------------------------------------------------*
*& Form set_pf_status
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
form set_pf_status using rt_extab type slis_t_extab.
set pf-status 'STANDARD'.
if go_grid is initial.
call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
importing
e_grid = go_grid.
endif.
endform. "Set_pf_status
*&---------------------------------------------------------------------*
*& Form export_to_excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form export_to_excel.
data: f_filename type string.
* create zcl_excel_worksheet object
data: lo_excel type ref to zcl_excel,
lo_worksheet type ref to zcl_excel_worksheet.
data: l_path type string, " local dir
lv_workdir type string,
lv_file_separator type c.
data: lt_exfcat type zexcel_t_fieldcatalog.
data: lt_solix type solix_tab.
data: f_file type string.
data: f_path type string.
data: f_xlen type i.
create object lo_excel.
lo_worksheet = lo_excel->get_active_worksheet( ).
data: f_def_filename type string.
concatenate sy-datum '.xlsx' into f_def_filename.
cl_gui_frontend_services=>file_save_dialog(
exporting
* window_title = window_title
default_extension = 'XLSX'
default_file_name = f_def_filename
* with_encoding = with_encoding
* file_filter = file_filter
* initial_directory = initial_directory
prompt_on_overwrite = 'X'
changing
filename = f_file
path = f_path
fullpath = f_filename
* user_action = user_action
* file_encoding = file_encoding
exceptions
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
others = 4
).
if sy-subrc eq 0 and f_filename is not initial.
try.
lo_worksheet->bind_alv(
io_alv = go_grid " ALV or SAL object
it_table = gt_alv[] " internal table
* i_top = 1
* i_left = 1
i_table = space "format as table in Excel
).
catch zcx_excel.
endtry.
data: lo_excel_writer type ref to zif_excel_writer,
l_xstring type xstring.
"create excel writer object to get xstring
create object lo_excel_writer type zcl_excel_writer_2007.
*
l_xstring = lo_excel_writer->write_file( lo_excel ).
free: lo_excel, lo_worksheet, lo_excel_writer.
f_xlen = xstrlen( l_xstring ).
lt_solix = cl_bcs_convert=>xstring_to_solix( iv_xstring = l_xstring
).
clear l_xstring.
cl_gui_frontend_services=>gui_download(
exporting
bin_filesize = f_xlen
filename = f_filename
filetype = 'BIN'
* append = SPACE
* write_field_separator = SPACE
* header = '00'
* trunc_trailing_blanks = SPACE
* write_lf = 'X'
* col_select = SPACE
* col_select_mask = SPACE
* dat_mode = SPACE
* confirm_overwrite = SPACE
* no_auth_check = SPACE
* codepage = SPACE
* ignore_cerr = ABAP_TRUE
* replacement = '#'
* write_bom = SPACE
* trunc_trailing_blanks_eol = 'X'
* wk1_n_format = SPACE
* wk1_n_size = SPACE
* wk1_t_format = SPACE
* wk1_t_size = SPACE
* show_transfer_status = 'X'
* fieldnames = fieldnames
* write_lf_after_last_line = 'X'
* importing
* filelength = filelength
changing
data_tab = lt_solix
exceptions
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
others = 24
).
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
refresh lt_solix[].
endif.
endform. "EXPORT_TO_EXCEL