Program :ZSDR010
T-Code: ZSD010
Download Requirements Plan Data
1、 download出和MD63一样格式的excel 以便用户操作
2、 用于zsd011,可以添加物料,也可以添加column,但添加column时格式要注意,和download出来的Column一样
*&---------------------------------------------------------------------*
*& Progarm : ZSDR011 Author : Jimmy Wong
*& Created : 21 Nov 2012 App : SD
*& Title : Download Requirements Plan Data
*& Description : Download Requirements Plan Data
*&---------------------------------------------------------------------*
*& Version Author Date description
*& Jimmy 21 Nov 2012 the first version
*& the last update time 2012.12.03 17:00
*&---------------------------------------------------------------------*
report zsdr010 no standard page heading.
*&---------------------------------------------------------------------*
* database table
*&---------------------------------------------------------------------*
tables : rlgrap,pbim,pbed,cm60a.
*&---------------------------------------------------------------------*
* internal table & variables
*&---------------------------------------------------------------------*
data:begin of itab occurs 0,
matnr like pbim-matnr,
werks like pbim-werks,
bedae like pbim-bedae,
versb like pbim-versb,
pbdnr like pbim-pbdnr,
entlu like pbed-entlu,
prgbz like tprg-prgbz,
pdatu like pbed-pdatu,
meins like pbed-meins,
plnmg like pbed-plnmg,
pspel like pbim-pspel,
posid like prps-posid,
end of itab.
data:gt_itab like itab occurs 0 with header line.
data:begin of it_tprg occurs 20,
prgbz like tprg-prgbz,
prgrs like tprg-prgrs,
end of it_tprg.
data: begin of it_prps occurs 0,
pspnr like prps-pspnr,
posid like prps-posid,
end of it_prps.
data:begin of it_field occurs 0,
pdatu like pbed-pdatu,
prgbz like tprg-prgbz,
entlu like pbed-entlu,
fdnam type c length 20,
fdfield type c length 20,
end of it_field.
field-symbols: <gt_output> type standard table,
<wa_output>,
<wa_field>.
data: it_structure type lvc_t_fcat,
wa_structure type lvc_s_fcat,
gt_output type ref to data,
wa_new_line type ref to data .
data: gv_file type string.
data:lv_year type i,
wa_year(4) type c,
lv_week like scal-week,
lv_tabix like sy-tabix,
wa_week type c length 10.
data: begin of it_pbdnr occurs 0,
pbdnr like pbim-pbdnr,
end of it_pbdnr .
*----------------------------------------------------------------------*
* Parameter & Select-Options *
*----------------------------------------------------------------------*
selection-screen begin of block 1 with frame title text-001.
parameters: p_pbdnr like pbim-pbdnr obligatory ,"default 'ELTEK',
p_werks like pbim-werks obligatory default '8101'.
select-options:s_versb for pbim-versb obligatory no intervals ,
s_pdatu for pbed-pdatu obligatory ,
s_matnr for pbim-matnr.
parameters p_down type rlgrap-filename default 'C:\temp\for pir download\'.
selection-screen end of block 1.
initialization.
s_pdatu-sign = 'I'.
s_pdatu-option = 'BT'.
s_pdatu-low = sy-datum.
lv_year = sy-datum+0(4) + 1.
wa_year = lv_year .
concatenate wa_year '12' '31' into s_pdatu-high.
append s_pdatu.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
at selection-screen on value-request for p_down.
call function 'WS_FILENAME_GET'
exporting
def_path = 'C:\'
mask = ',Excel Files,*.xls,All Files,*.*.'(101)
mode = 'O'
title = 'Select file'(100)
importing
filename = p_down
exceptions
selection_cancel = 1
selection_error = 2
others = 3.
if sy-subrc ne 0.
" .
endif.
at selection-screen on value-request for p_pbdnr.
perform get_pbdnr changing p_pbdnr.
*&---------------------------------------------------------------------*
* start of process
*&---------------------------------------------------------------------*
start-of-selection.
perform get_data.
if itab[] is initial .
message 'No Record Found' type 'I'.
exit.
endif.
perform get_field.
perform create_structure.
perform create_dynamic_table.
perform add_header.
perform process_data.
perform download_data.
end-of-selection.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_data .
select a~pbdnr a~werks a~versb a~matnr a~bedae
b~entlu b~pdatu b~plnmg b~meins a~pspel
into corresponding fields of table itab
from pbim as a
inner join pbed as b on a~bdzei = b~bdzei
where a~pbdnr = p_pbdnr
and a~werks = p_werks
and a~versb in s_versb
and a~bedae = 'VSFB'
and b~pdatu in s_pdatu
and a~matnr in s_matnr.
if itab[] is not initial.
select pspnr posid
into corresponding fields of table it_prps
from prps
for all entries in itab
where pspnr = itab-pspel.
sort it_prps by pspnr posid .
select prgbz prgrs
into corresponding fields of table it_tprg
from tprg
where spras = sy-langu.
sort it_tprg by prgrs prgbz.
endif.
endform. " GET_DATA
*&---------------------------------------------------------------------*
*& Form CREATE_STRUCTURE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form create_structure .
data:lv_total type i,
lv_i type i,
lv_j type i,
lv_len type i,
lv_c type c length 6,
lv_field type c length 20.
define create_stru.
wa_structure-fieldname = &1.
wa_structure-col_pos = &2.
wa_structure-inttype = &3.
wa_structure-intlen = &4.
append wa_structure to it_structure.
end-of-definition.
create_stru 'PBDNR' '1' 'C' '20' .
create_stru 'MATNR' '2' 'C' '20' .
create_stru 'WERKS' '3' 'C' '20' .
create_stru 'VERSB' '4' 'C' '20' .
create_stru 'POSID' '5' 'C' '20' .
create_stru 'MEINS' '6' 'C' '20' .
describe table it_field lines lv_total .
lv_j = 7.
lv_i = 0.
loop at it_field .
lv_tabix = sy-tabix.
lv_c = lv_tabix.
condense lv_c.
lv_len = strlen( lv_c ).
if lv_len = 1.
concatenate '0' lv_c into lv_c.
endif.
concatenate 'FIELD' lv_c into lv_field.
it_field-fdfield = lv_field.
create_stru lv_field lv_j 'C' '20' .
modify it_field index lv_tabix.
add 1 to lv_j.
endloop.
endform. " CREATE_STRUCTURE
*&---------------------------------------------------------------------*
*& Form CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form create_dynamic_table .
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_structure
importing
ep_table = gt_output.
assign gt_output->* to <gt_output>.
endform. " CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& Form GET_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_field .
loop at itab.
it_field-entlu = itab-entlu.
it_field-pdatu = itab-pdatu.
read table it_tprg with key prgrs = it_field-entlu binary search.
if sy-subrc eq 0.
it_field-prgbz = it_tprg-prgbz .
endif.
if it_field-entlu = '2'.
call function 'GET_WEEK_INFO_BASED_ON_DATE'
exporting
date = it_field-pdatu
importing
week = lv_week
* monday =
* sunday =
.
concatenate lv_week+4(2) lv_week+0(4) into wa_week separated by '/'.
else.
concatenate it_field-pdatu+4(2) it_field-pdatu+0(4) into wa_week separated by '/'.
endif.
concatenate it_field-prgbz wa_week into it_field-fdnam separated by space.
collect it_field.
"WBS
read table it_prps with key pspnr = itab-pspel binary search.
if sy-subrc eq 0.
gt_itab-posid = it_prps-posid .
endif.
gt_itab-matnr = itab-matnr.
gt_itab-werks = itab-werks.
gt_itab-versb = itab-versb.
gt_itab-pbdnr = itab-pbdnr.
gt_itab-meins = itab-meins.
collect gt_itab.
clear:it_field,lv_week,wa_week,gt_itab.
endloop.
sort it_field by pdatu.
endform. " GET_FIELD
*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form process_data .
sort gt_itab by matnr versb pbdnr werks .
loop at gt_itab.
loop at it_structure into wa_structure.
assign component wa_structure-fieldname of structure <wa_output> to <wa_field>.
if wa_structure-fieldname ='PBDNR' .
<wa_field> = gt_itab-pbdnr.
elseif wa_structure-fieldname ='MATNR' .
<wa_field> = gt_itab-matnr.
perform tranfer_material using <wa_field>
changing <wa_field>.
elseif wa_structure-fieldname ='WERKS' .
<wa_field> = gt_itab-werks.
elseif wa_structure-fieldname ='VERSB' .
<wa_field> = gt_itab-versb.
elseif wa_structure-fieldname ='POSID' .
<wa_field> = gt_itab-posid.
elseif wa_structure-fieldname ='MEINS' .
<wa_field> = gt_itab-meins.
else.
read table it_field with key fdfield = wa_structure-fieldname.
if sy-subrc eq 0.
read table itab with key matnr = gt_itab-matnr werks = gt_itab-werks
versb = gt_itab-versb pbdnr = gt_itab-pbdnr
pdatu = it_field-pdatu entlu = it_field-entlu.
if sy-subrc eq 0.
<wa_field> = itab-plnmg.
endif.
endif.
endif.
endloop.
append <wa_output> to <gt_output>.
clear:<wa_field>,<wa_output> .
endloop.
endform. " PROCESS_DATA
*&---------------------------------------------------------------------*
*& Form ADD_HEADER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form add_header .
create data wa_new_line like line of <gt_output>.
assign wa_new_line->* to <wa_output>.
loop at it_structure into wa_structure.
assign component wa_structure-fieldname of structure <wa_output> to <wa_field>.
if wa_structure-fieldname ='PBDNR' .
<wa_field> = 'Reqmst Plan'.
elseif wa_structure-fieldname ='MATNR' .
<wa_field> = 'Material'.
elseif wa_structure-fieldname ='WERKS' .
<wa_field> = 'Plant'.
elseif wa_structure-fieldname ='VERSB' .
<wa_field> = 'Version'.
elseif wa_structure-fieldname ='POSID' .
<wa_field> = 'WBS Element'.
elseif wa_structure-fieldname ='MEINS' .
<wa_field> = 'Base Unit'.
else.
read table it_field with key fdfield = wa_structure-fieldname.
if sy-subrc eq 0.
<wa_field> = it_field-fdnam.
endif.
endif.
endloop.
append <wa_output> to <gt_output>.
clear:<wa_field>,<wa_output> .
endform. " ADD_HEADER
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form download_data .
perform get_file.
call function 'GUI_DOWNLOAD'
exporting
filename = gv_file
filetype = 'DAT'
codepage = '4103'
replacement = '#'
write_field_separator = 'X'
write_bom = 'X'
tables
data_tab = <gt_output>.
if sy-subrc eq 0.
* --- show document in the XLS Reader ---
cl_gui_frontend_services=>execute( exporting document = gv_file
exceptions cntl_error = 1
error_no_gui = 2
bad_parameter = 3
file_not_found = 4
path_not_found = 5
file_extension_unknown = 6
error_execute_failed = 7
synchronous_failed = 8
not_supported_by_gui = 9
others = 10 ).
endif.
endform. " DOWNLOAD_DATA
*&---------------------------------------------------------------------*
*& Form get_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form get_file .
data: wa_key(30) type c.
data: lv_i type i.
data: lv_c(1) type c.
data: wa_date(8) type c.
data: wa_time like sy-uzeit.
data: lv_char type c.
wa_date = sy-datum.
wa_time = sy-uzeit.
concatenate 'ReqmtsPlan' wa_date wa_time into wa_key.
concatenate wa_key '.xls' into wa_key.
lv_i = strlen( p_down ).
lv_i = lv_i - 1 .
if lv_i > 1.
lv_c = p_down+lv_i(1).
endif.
lv_char = '\' .
if lv_c eq lv_char.
concatenate p_down wa_key into gv_file.
else.
concatenate p_down lv_char wa_key into gv_file.
endif.
endform. " GET_FILE
*&---------------------------------------------------------------------*
*& Form TRANFER_MATERIAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_ITAB_MATNR text
* <--P_ITAB_MATNR_C text
*----------------------------------------------------------------------*
form tranfer_material using p_old
changing p_new .
call function 'CONVERSION_EXIT_MATN1_OUTPUT'
exporting
input = p_old
importing
output = p_new.
endform. "tranfer_material
*&---------------------------------------------------------------------*
*& Form GET_PBDNR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_PBDNR text
*----------------------------------------------------------------------*
form get_pbdnr changing p_p_pbdnr.
data: begin of tab_pbdnr occurs 0.
include structure cm60h.
data:end of tab_pbdnr.
data: begin of felder occurs 10.
include structure help_value.
data: end of felder.
data: begin of xdynfields occurs 10.
include structure dynpread.
data: end of xdynfields.
data: xflag(1) type c value 'X'.
data: l_index like sy-tabix.
data: l_pbdnr like am60x-pbdnr,
epbdnr like am60x-pbdnr,
mpbdnr like am60x-pbdnr.
clear felder. refresh felder.
*- Nummer (Pick-Up)
felder-tabname = 'CM60H'.
felder-fieldname = 'PBDNR'.
felder-selectflag = xflag.
append felder.
clear felder.
*- Material
felder-tabname = 'CM60H'.
felder-fieldname = 'MATNR'.
append felder.
*- Bedarfsart (Pick-Up)
felder-tabname = 'CM60H'.
felder-fieldname = 'BERID'.
append felder.
*- Werk
felder-tabname = 'CM60H'.
felder-fieldname = 'WERKS'.
append felder.
*- Bedarfsart (Pick-Up)
felder-tabname = 'CM60H'.
felder-fieldname = 'BEDAE'.
append felder.
*- Version
felder-tabname = 'CM60H'.
felder-fieldname = 'VERSB'.
append felder.
call function 'REQUIREMENT_NUMBER'
exporting
icm60a = cm60a
tables
tab_pbdnr = tab_pbdnr.
*--> Sort Result "HW 665025
sort tab_pbdnr by pbdnr matnr werks berid versb bedae.
call function 'HELP_VALUES_GET_NO_DD_NAME'
exporting
display = ' '
selectfield = 'P_PBDNR'
* TITEL = ' '
importing
ind = l_index
tables
fields = felder
full_table = tab_pbdnr
exceptions
full_table_empty = 1
no_tablestructure_given = 2
no_tablefields_in_dictionary = 3
more_then_one_selectfield = 4
no_selectfield = 5
others = 6.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
read table tab_pbdnr index l_index.
if sy-subrc eq 0.
p_p_pbdnr = tab_pbdnr-pbdnr.
endif.
endform. " GET_PBDNR