* pool of form routines
include zmppn001.
* Define BDC Table Structure
data: begin of itab_bdc_tab occurs 0.
include structure bdcdata.
data: end of itab_bdc_tab.
* Input record layout of Leagcy File
data: begin of itab_xcel occurs 0,
matnr(18) type c,
werks(4) type c,
alnag(2) type c,
verid(4) type c,
text1(40) type c,
bstmi like mkal-bstmi,
bstma like mkal-bstma,
adatu(10) type c,
bdatu(10) type c,
stlal(2) type c,
stlan(1) type c,
serkz(1) type c,
mdv01(8) type c,
elpro(4) type c,
alort(4) type c,
end of itab_xcel.
data: begin of lt_pp04_cache occurs 0,
matnr like itab_xcel-matnr,
werks like itab_xcel-werks,
alnag like itab_xcel-alnag,
plnnr like mapl-plnnr,
arbpl like crhd-arbpl,
ktext like crtx-ktext,
end of lt_pp04_cache.
data: v_ssnnr(4) type n,
v_lines_in_xcel like sy-tabix,
v_ssnname like apqi-groupid,
v_trans_in_ssn type i,
* wa_xcel LIKE itab_xcel,
l_tabix like sy-tabix,
v_matnr like rc27m-matnr,
v_plnnr like mapl-plnnr,
v_plnal like mapl-plnal,
v_tcode like sy-tcode value 'C223',
v_plnty like plas-plnty value 'R',
v_objty like crhd-objty value 'A',
v_plpo_steus like plpo-steus value 'PP04',
v_verwe like crhd-verwe value '0007'.
* Parameters
selection-screen: skip 3.
selection-screen: begin of block 1 with frame.
*
parameters: p_name like rlgrap-filename
default 'C:\My Documents\InputFile.txt'
obligatory,
* bdc session name prefix
p_bdcpfx(6) default 'ZPVCRT'
obligatory,
* number for transction per BDC session
p_trnssn type i
default 2000 obligatory,
* retain the BDC session after successfull execution
p_keep like apqi-qerase
default 'X',
* user who will be executing BDC session
p_uname like apqi-userid
default sy-uname
obligatory.
*
selection-screen: end of block 1.
*
********************************************************
********************************************************
*
* possible entry list (F4 dropdown) for input file name
at selection-screen on value-request for p_name.
*-SELECT FILE FROM USERS LOCAL PC
call function 'WS_FILENAME_GET'
exporting
* DEF_FILENAME = ' '
def_path = 'C:\Temp\'
mask = ',*.*,*.*.'
mode = 'O'
title = 'Select File '(007)
importing
filename = p_name
* RC =
exceptions
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
others = 5.
if sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
*
********************************************************
********************************************************
*
* begin the show :-)
*
start-of-selection.
* read data from input file
perform transfer_xcel_to_itab.
*
*
loop at itab_xcel.
* hang on to xcel line num
l_tabix = sy-tabix.
* each line in the xcel file marks begining of new prod.version defn
* if num-of-trnas-in-session = 0, create new BDC session
if v_trans_in_ssn is initial.
perform bdc_session_open.
endif.
* begin new bdc script for rtg create trans
* fill in bdc-data for prod.version maintenance screens
perform bdc_build_script.
* insert the bdc script as a BDC transaction
perform bdc_submit_transaction.
* keep track of how many BDC transactions were inserted in the BDC
* session
add 1 to v_trans_in_ssn.
* if the user-specified num of trans in BDC session is reached OR
* if end of input file is reached, close the BDC session
if v_trans_in_ssn = p_trnssn or
l_tabix = v_lines_in_xcel.
perform bdc_session_close.
clear v_trans_in_ssn.
endif.
endloop.
top-of-page.
call function 'Z_HEADER'
* EXPORTING
* FLEX_TEXT1 =
* FLEX_TEXT2 =
* FLEX_TEXT3 =
.
*---------------------------------------------------------------------*
* FORM TRANSFER_XCEL_TO_ITAB *
*---------------------------------------------------------------------*
* Transfer Xcel Spreadsheet to SAP Internal Table *
*---------------------------------------------------------------------*
form transfer_xcel_to_itab.
*
* Read the tab-delimited file into itab
call function 'WS_UPLOAD'
exporting
filename = p_name
filetype = 'DAT'
* IMPORTING
* filelength = flength
tables
data_tab = itab_xcel
exceptions
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_table_width = 4
invalid_type = 5
no_batch = 6
unknown_error = 7
others = 8.
*
if sy-subrc = 0.
* sort the data
sort itab_xcel by matnr werks.
clear v_lines_in_xcel.
* if no data in the file - error out
describe table itab_xcel lines v_lines_in_xcel.
if v_lines_in_xcel is initial.
write: / 'No data in input file'.
stop.
endif.
else.
* if file upload failed - error out
write: / 'Error reading input file'.
stop.
endif.
endform.
*---------------------------------------------------------------------*
* FORM BDC_SESSION_OPEN *
*---------------------------------------------------------------------*
* Open BDC Session *
*---------------------------------------------------------------------*
form bdc_session_open.
* create bdc session name = prefix-from-selectn-screen + nnnn
add 1 to v_ssnnr.
concatenate p_bdcpfx v_ssnnr into v_ssnname.
* open new bdc session
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
group = v_ssnname
keep = p_keep
user = p_uname
exceptions
client_invalid = 1
destination_invalid = 2
group_invalid = 3
group_is_locked = 4
holddate_invalid = 5
internal_error = 6
queue_error = 7
running = 8
system_lock_error = 9
user_invalid = 10
others = 11.
endform.
*---------------------------------------------------------------------*
* FORM BDC_BUILD_SCRIPT *
*---------------------------------------------------------------------*
* Build BDC *
*---------------------------------------------------------------------*
form bdc_build_script.
data: l_arbpl like crhd-arbpl,
l_text1 like mkal-text1,
l_mdv01 like mkal-mdv01,
l_mapl like mapl.
* clear bdc-data itab - begin of new bdc transaction
clear itab_bdc_tab.
refresh itab_bdc_tab.
*
* read material cross reference tables to determine sap part#
clear : v_matnr, v_plnnr, v_plnal.
perform read_matnr_cross_ref using itab_xcel-matnr
itab_xcel-werks
changing v_matnr.
*
* determine the version description to use
if itab_xcel-text1 is initial.
l_text1 = itab_xcel-verid.
else.
l_text1 = itab_xcel-text1.
endif.
* determine the routing group# and group ctr# to use
perform read_routing .
*
* determine the production line to use
if itab_xcel-mdv01 is initial.
* if not provided in the file then:
* prod line = work ctr on the last PP04 op of the rtg determined above
perform read_wc_on_last_pp04 using v_plnnr v_plnal
changing l_mdv01.
* NOTE: when executing the above form\routine, if v_plnnr is initial
* or v_plnal is initial, THEN l_mdv01 will automatically be
* returned blank (ie initial)
else.
l_mdv01 = itab_xcel-mdv01.
endif.
*
* build bdc script
perform bdc_build_script_record
*
* fill in initial screen
using: 'X' 'SAPLCMFV' '1000',
' ' 'BDC_OKCODE' '=ENTE',
' ' 'MKAL-WERKS' itab_xcel-werks,
' ' 'MKAL-MATNR' v_matnr,
' ' 'MKAL_ADMIN-DISPO' space,
' ' 'MKAL-PLNNR' space,
' ' 'MKAL_ADMIN-STTAG' space,
' ' 'MKAL-PLNNG' space,
' ' 'MKAL-MDV01' space,
' ' 'MKAL-PLNNM' space,
* click create button on initial screen and go to detail screen
'X' 'SAPLCMFV' '1000',
' ' 'BDC_OKCODE' '=CREA',
* fill in the detail screen and go back to initial screen
'X' 'SAPLCMFV' '2000',
' ' 'BDC_OKCODE' '=CLOS',
' ' 'MKAL_EXPAND-MATNR' v_matnr,
' ' 'MKAL_EXPAND-VERID' itab_xcel-verid,
' ' 'MKAL_EXPAND-TEXT1' l_text1,
' ' 'MKAL_EXPAND-BSTMI' itab_xcel-bstmi,
' ' 'MKAL_EXPAND-BSTMA' itab_xcel-bstma,
' ' 'MKAL_EXPAND-ADATU' itab_xcel-adatu,
' ' 'MKAL_EXPAND-BDATU' itab_xcel-bdatu,
' ' 'MKAL_EXPAND-PLTYG' v_plnty,
' ' 'MKAL_EXPAND-PLNNG' v_plnnr,
' ' 'MKAL_EXPAND-ALNAG' v_plnal,
' ' 'MKAL_EXPAND-STLAL' itab_xcel-stlal,
' ' 'MKAL_EXPAND-STLAN' itab_xcel-stlan,
' ' 'MKAL_EXPAND-SERKZ' itab_xcel-serkz,
' ' 'MKAL_EXPAND-MDV01' l_mdv01,
' ' 'MKAL_EXPAND-ELPRO' itab_xcel-elpro,
' ' 'MKAL_EXPAND-ALORT' itab_xcel-alort,
* save the production version from initial screen
'X' 'SAPLCMFV' '1000',
' ' 'BDC_OKCODE' '=SAVE'.
*
endform.
*---------------------------------------------------------------------*
* FORM BDC_SUBMIT_TRANSACTION *
*---------------------------------------------------------------------*
* Submit BDC Session *
*---------------------------------------------------------------------*
form bdc_submit_transaction.
** Load BDC script as a trqansction in BDC session
call function 'BDC_INSERT'
exporting
tcode = v_tcode
tables
dynprotab = itab_bdc_tab
exceptions
internal_error = 01
not_open = 02
queue_error = 03
tcode_invalid = 04.
endform.
*---------------------------------------------------------------------*
* FORM BDC_BUILD_SCRIPT_RECORD *
*---------------------------------------------------------------------*
form bdc_build_script_record using dynbegin name value.
clear itab_bdc_tab.
if dynbegin = 'X'.
move: name to itab_bdc_tab-program,
value to itab_bdc_tab-dynpro,
'X' to itab_bdc_tab-dynbegin.
else.
move: name to itab_bdc_tab-fnam,
value to itab_bdc_tab-fval.
shift itab_bdc_tab-fval left deleting leading space.
endif.
append itab_bdc_tab.
endform.
*---------------------------------------------------------------------*
* FORM BDC_SESSION_CLOSE *
*---------------------------------------------------------------------*
* Close BDC Session *
*---------------------------------------------------------------------*
form bdc_session_close.
* close the session
call function 'BDC_CLOSE_GROUP'
exceptions
not_open = 1
queue_error = 2
others = 3.
skip 2.
if sy-subrc ne 0.
write: / 'Error Closing BDC Session ' , 'RETURN CODE: ', sy-subrc.
else.
write : / 'Session created:', v_ssnname,
50 '# of transactions:', v_trans_in_ssn.
endif.
endform.
*&---------------------------------------------------------------------*
*& Form read_routing_cache
*&---------------------------------------------------------------------*
*FORM read_routing_cache USING pi_matnr
* pi_werks
* pi_alnag
* pi_verid
* pi_mdv01.
*
* DATA: BEGIN OF lt_plpo OCCURS 0,
* vornr LIKE plpo-vornr,
* objty LIKE crhd-objty,
* objid LIKE crhd-objid,
* arbpl LIKE crhd-arbpl,
* END OF lt_plpo,
* l_mapl_plnnr LIKE mapl-plnnr.
*
** determine the routing group#
* CLEAR lt_pp04_cache.
** chk if its in the cache first, if not then get it from MAPL table
** and put it in the cache
* READ TABLE lt_pp04_cache WITH KEY matnr = pi_matnr
* werks = pi_werks
* alnag = pi_alnag.
* IF sy-subrc = 0.
** do nothing - lt_pp04_cache header line has rtg#
* ELSE.
** get the routing group # from MAPL
* SELECT plnnr INTO l_mapl_plnnr
* FROM mapl UP TO 1 ROWS
* WHERE matnr = pi_matnr AND
* werks = pi_werks AND
* plnty = 'R' AND
* plnal = pi_alnag AND
* loekz = space.
* ENDSELECT.
** put it in the cache internal table
* IF NOT l_mapl_plnnr IS INITIAL.
* lt_pp04_cache-matnr = pi_matnr.
* lt_pp04_cache-werks = pi_werks.
* lt_pp04_cache-alnag = pi_alnag.
* lt_pp04_cache-plnnr = l_mapl_plnnr.
* APPEND lt_pp04_cache.
* ENDIF.
* ENDIF.
*
** if the rtg# was determined AND
** -- the work center was not determined yet AND
** -- work center was really needed for this line in the input file
** then
** -- read the work center from last PP04 operation on the routing
** -- update the cache accordingly
*
*
* IF NOT lt_pp04_cache-plnnr IS INITIAL AND
* lt_pp04_cache-arbpl IS INITIAL AND
* ( pi_verid IS INITIAL OR
* pi_mdv01 IS INITIAL ).
*
** read the last PP04 operation
* CLEAR lt_plpo.
* REFRESH lt_plpo.
* SELECT vornr e~objty e~objid e~arbpl
* INTO CORRESPONDING FIELDS OF TABLE lt_plpo
* FROM plas AS b
* INNER JOIN plpo AS c
* ON b~plnty = c~plnty AND
* b~plnnr = c~plnnr AND
* b~zaehl = c~zaehl
* INNER JOIN crhd AS e
* ON c~arbid = e~objid
* WHERE b~plnty = v_plnty AND
* b~plnnr = lt_pp04_cache-plnnr AND
* b~plnal = lt_pp04_cache-alnag AND
* c~loekz = space AND
* c~steus = v_plpo_steus AND
* e~objty = v_objty AND
* e~werks = lt_pp04_cache-werks AND
* e~verwe = v_verwe.
* SORT lt_plpo BY vornr DESCENDING.
* READ TABLE lt_plpo INDEX 1.
**
* IF NOT lt_plpo-arbpl IS INITIAL.
* lt_pp04_cache-arbpl = lt_plpo-arbpl.
** read work center description
* SELECT SINGLE ktext INTO lt_pp04_cache-ktext
* FROM crtx WHERE objty = lt_plpo-objty AND
* objid = lt_plpo-objid AND
* spras = sy-langu.
** the following read will get the index of the correct record to be
** updated in the cache
* READ TABLE lt_pp04_cache
* WITH KEY matnr = pi_matnr
* werks = pi_werks
* alnag = pi_alnag.
* MODIFY lt_pp04_cache
* INDEX sy-tabix
* TRANSPORTING arbpl ktext.
* ENDIF.
* ENDIF.
*
*ENDFORM. " read_last_pp04_operation_cache
*&---------------------------------------------------------------------*
*& Form read_routing
*&---------------------------------------------------------------------*
form read_routing.
data: begin of lt_mapl occurs 0,
plnnr like mapl-plnnr,
plnal like mapl-plnal,
end of lt_mapl,
l_arbpl like crhd-arbpl.
* get all the rtg# and grp ctr# from MAPL
select plnnr plnal
into corresponding fields of table lt_mapl
from mapl
where matnr = v_matnr and
werks = itab_xcel-werks and
plnty = v_plnty and "Rate Routing
loekz = space. "with del flag = OFF
sort lt_mapl by plnal.
if not itab_xcel-verid is initial.
* if the verid=0001 then use the 1st good rtg-grp# and grp-ctr#
if itab_xcel-verid = '0001'.
read table lt_mapl index 1.
v_plnnr = lt_mapl-plnnr.
v_plnal = lt_mapl-plnal.
else.
* if the verid<>0001 then use the rtg-grp# and grp-ctr# of the routing
* whose work center on the last PP04 operation matches the given verid
loop at lt_mapl.
clear l_arbpl.
* get the work center from the last PP04 operation
perform read_wc_on_last_pp04 using lt_mapl-plnnr
lt_mapl-plnal
changing l_arbpl.
*
if itab_xcel-verid = l_arbpl.
v_plnnr = lt_mapl-plnnr.
v_plnal = lt_mapl-plnal.
exit.
endif.
endloop.
endif.
else.
* do nothing
endif.
* For version IDs that are other then '0000' or 'ZWIP' :--
if itab_xcel-verid NE '0000' and
itab_xcel-verid NE 'ZWIP'.
* if routing group# or group counter was not determined, make the
* valid-to date 99/99/9999 so that the BDC, on execution, errors out.
if v_plnnr is initial or
v_plnal is initial.
itab_xcel-bdatu = '99/99/9999'.
endif.
endif.
** determine the routing group#
* CLEAR lt_pp04_cache.
** chk if its in the cache first, if not then get it from MAPL table
** and put it in the cache
* READ TABLE lt_pp04_cache WITH KEY matnr = pi_matnr
* werks = pi_werks
* alnag = pi_alnag.
* IF sy-subrc = 0.
** do nothing - lt_pp04_cache header line has rtg#
* ELSE.
** get the routing group # from MAPL
** put it in the cache internal table
* IF NOT l_mapl_plnnr IS INITIAL.
* lt_pp04_cache-matnr = pi_matnr.
* lt_pp04_cache-werks = pi_werks.
* lt_pp04_cache-alnag = pi_alnag.
* lt_pp04_cache-plnnr = l_mapl_plnnr.
* APPEND lt_pp04_cache.
* ENDIF.
* ENDIF.
*
** if the rtg# was determined AND
** -- the work center was not determined yet AND
** -- work center was really needed for this line in the input file
** then
** -- read the work center from last PP04 operation on the routing
** -- update the cache accordingly
*
*
* IF NOT lt_pp04_cache-plnnr IS INITIAL AND
* lt_pp04_cache-arbpl IS INITIAL AND
* ( pi_verid IS INITIAL OR
* pi_mdv01 IS INITIAL ).
*
** read the last PP04 operation
* CLEAR lt_plpo.
* REFRESH lt_plpo.
* SELECT vornr e~objty e~objid e~arbpl
* INTO CORRESPONDING FIELDS OF TABLE lt_plpo
* FROM plas AS b
* INNER JOIN plpo AS c
* ON b~plnty = c~plnty AND
* b~plnnr = c~plnnr AND
* b~zaehl = c~zaehl
* INNER JOIN crhd AS e
* ON c~arbid = e~objid
* WHERE b~plnty = v_plnty AND
* b~plnnr = lt_pp04_cache-plnnr AND
* b~plnal = lt_pp04_cache-alnag AND
* c~loekz = space AND
* c~steus = v_plpo_steus AND
* e~objty = v_objty AND
* e~werks = lt_pp04_cache-werks AND
* e~verwe = v_verwe.
* SORT lt_plpo BY vornr DESCENDING.
* READ TABLE lt_plpo INDEX 1.
**
* IF NOT lt_plpo-arbpl IS INITIAL.
* lt_pp04_cache-arbpl = lt_plpo-arbpl.
** read work center description
* SELECT SINGLE ktext INTO lt_pp04_cache-ktext
* FROM crtx WHERE objty = lt_plpo-objty AND
* objid = lt_plpo-objid AND
* spras = sy-langu.
** the following read will get the index of the correct record to be
** updated in the cache
* READ TABLE lt_pp04_cache
* WITH KEY matnr = pi_matnr
* werks = pi_werks
* alnag = pi_alnag.
* MODIFY lt_pp04_cache
* INDEX sy-tabix
* TRANSPORTING arbpl ktext.
* ENDIF.
* ENDIF.
endform. " read_last_pp04_operation_cache
*&---------------------------------------------------------------------*
*& Form read_wc_on_last_pp04
*&---------------------------------------------------------------------*
form read_wc_on_last_pp04 using pi_plnnr
pi_plnal
changing pe_arbpl.
data: begin of lt_plpo occurs 0,
vornr like plpo-vornr,
objty like crhd-objty,
objid like crhd-objid,
arbpl like crhd-arbpl,
end of lt_plpo.
* get all the PP04 operations for the given rtg# & grp-ctr#
select vornr e~objty e~objid e~arbpl
into corresponding fields of table lt_plpo
from plas as b
inner join plpo as c
on b~plnty = c~plnty and
b~plnnr = c~plnnr and
b~zaehl = c~zaehl
inner join crhd as e
on c~arbid = e~objid
where b~plnty = v_plnty and "Rate Routing
b~plnnr = pi_plnnr and
b~plnal = pi_plnal and
c~loekz = space and "Oper Del Flag = OFF
c~steus = v_plpo_steus and "PP04
e~objty = v_objty. "WC Obj Type = 'A'
* read the last operation
sort lt_plpo by vornr descending.
read table lt_plpo index 1.
pe_arbpl = lt_plpo-arbpl.
endform.
RELATED LINKS
BDC 1
BDC 2
BDC 9
BDC SAMPLE CODE XD 01