10-01-2008 9:10 AM
i am creating one report which will give data of BOM component.
i am using MAST STKO and STPO,
but while debugging i found there are more than one line item are coming in STKO(BOM header ) table itself,
and STPO is not having alternative BOM,
so can any body tell how to get correct data from STPO.
regards]
Chandramani
10-01-2008 12:09 PM
The easiest way to look up BOM data is with this function module "CS_BOM_EXPL_MAT_V2".
However I have looked up data with the tables as well. It's a lot of code, and it may change. We have had some changes to it. We may have more changes. It's easy to miss something. And it was written a while ago. Meaning it has tables with headers... etc...
Good Luck!
Michelle
The table passed to the function module looks like this:
I_MATNR LIKE ZSTRUCT_MATNR Structure that contains only the material number
O_ITEM LIKE ZSTRUCT_BOM BOM structure used with Z_BOM_EXPLODE_ONE_LEVEL
Here's the code:
FUNCTION z_bom_explode_one_level.
*"----
""Local interface:
*" IMPORTING
*" REFERENCE(EFFECTIVE_DATE) LIKE SY-DATUM
*" REFERENCE(INCLUDE_FIXED) TYPE ZCHAR1 DEFAULT ' '
*" REFERENCE(INCLUDE_FUTURE) TYPE ZCHAR1 DEFAULT ' '
*" TABLES
*" I_MATNR STRUCTURE ZSTRUCT_MATNR
*" O_ITEM STRUCTURE ZSTRUCT_BOM
*" EXCEPTIONS
*" NO_MATERIALS_SENT
*" NO_BOM
*" NO_BOM_ITEMS
*"----
Task # Date Prgmr Description
D46K905338 add field definition for field
i_include-fixed. This allows
the calling program to determine
whether or not STPO records
where FMENG(fixed qty)
field not blank are deleted
from internal table. If
include_fixed parm = ' ',
the fixed qty records are
deleted. If include_fixed parm
= 'X', those fixed qty records
are retained.
*D46K905424 Add sort to i_mast
*D46K907122 clear a couple internal tables
to handle situation where this
function is called > 1 time
during the run of the calling
program
*D46K916409 Remove all deleted records from
I_Stas
*D46K921679 change FM to include future
effective materials.
************************************************************************
IF i_matnr[] IS INITIAL.
RAISE no_materials_sent.
ENDIF.
i_matnr1[] = i_matnr[].
i_effective_date = effective_date.
i_include_fixed = include_fixed.
i_include_future = include_future. "D46K916409
CLEAR i_mkal. "D46K907122
REFRESH i_mkal. "D46K907122
CLEAR i_stas2. "D46K907122
REFRESH i_stas2. "D46K907122
PERFORM load_bom_tables.
PERFORM get_item_table.
IF NOT i_include_future IS INITIAL. "D46K921679
PERFORM add_future_effective_materials. "D46K921679
ENDIF. "D46K921679
SORT i_item BY matnr werks. "D46K921679
o_item[] = i_item[].
ENDFUNCTION.
----
FORM load_bom_tables *
----
Pull the information from the DB tables. *
----
FORM load_bom_tables.
SELECT matnr werks stlan stlnr stlal FROM mast INTO TABLE i_mast
FOR ALL ENTRIES IN i_matnr1
WHERE matnr = i_matnr1-matnr.
LOOP AT i_mast.
IF i_mast-stlan <> '1'.
DELETE i_mast.
ENDIF.
ENDLOOP.
IF i_mast[] IS INITIAL.
RAISE no_bom.
ENDIF.
Load BOM header information.
SELECT stlty stlnr stlal stkoz bmeng FROM stko INTO TABLE i_stko
FOR ALL ENTRIES IN i_mast
WHERE stlty = c_m AND
stlnr = i_mast-stlnr AND
stlal = i_mast-stlal.
SORT i_stko BY stlnr stlal.
Load Version table with the lowest version number.
SELECT matnr werks verid plnty plnnr stlal alnal "D46K921679
INTO TABLE i_mkal2
FROM mkal
FOR ALL ENTRIES IN i_matnr1
WHERE matnr = i_matnr1-matnr.
SORT i_mkal2 BY matnr verid.
GET MKAL DATA WITH LOWEST VERSION ID (VERID)
LOOP AT i_mkal2.
IF i_mkal2-matnr <> i_mkal-matnr.
MOVE-CORRESPONDING i_mkal2 TO i_mkal.
APPEND i_mkal.
ENDIF.
ENDLOOP.
SORT i_matnr1 BY matnr.
SORT i_mkal.
SELECT stlty stlnr stlkn stpoz menge datuv idnrk postp fmeng
potx1 lkenz
FROM stpo INTO TABLE i_stpo
FOR ALL ENTRIES IN i_mast
WHERE stlnr = i_mast-stlnr.
DELETE i_stpo WHERE lkenz = 'X'.
SORT i_stpo BY stlnr stlkn.
SELECT stlty stlnr stlal stlkn stasz lkenz datuv
FROM stas INTO TABLE i_stas
FOR ALL ENTRIES IN i_mast
WHERE stlnr = i_mast-stlnr.
Pull only the valid records from i_stas... (Records not deleted that
fall into the effective date.
SORT i_stas BY stlnr stlal stlkn stasz datuv. "D46K921679
CLEAR: sav_stlnr, sav_stlal, sav_stlkn. "D46K921679
LOOP AT i_stas.
stas_tabix = sy-tabix - 1. "D46K921679
IF sav_stlnr = i_stas-stlnr AND "D46K921679
sav_stlal = i_stas-stlal AND "D46K921679
sav_stlkn = i_stas-stlkn. "D46K921679
one_day_back = i_stas-datuv - 1. "D46K921679
READ TABLE i_stas INTO wa_stas INDEX stas_tabix. "D46K921679
IF sy-subrc = 0. "D46K921679
wa_stas-enddt = one_day_back. "D46K921679
MODIFY i_stas FROM wa_stas INDEX stas_tabix. "D46K921679
ENDIF. "D46K921679
ELSE. "D46K921679
sav_stlnr = i_stas-stlnr. "D46K921679
sav_stlal = i_stas-stlal. "D46K921679
sav_stlkn = i_stas-stlkn. "D46K921679
i_stas-enddt = '99991231'. "D46K921679
MODIFY i_stas INDEX sy-tabix. "D46K921679
ENDIF. "D46K921679
READ TABLE i_stas2 WITH KEY stlnr = i_stas-stlnr
stlal = i_stas-stlal
stlkn = i_stas-stlkn
BINARY SEARCH.
IF sy-subrc <> 0.
i_stas2 = i_stas.
IF i_stas-datuv <= i_effective_date.
IF sy-tabix > 0.
INSERT i_stas2 INDEX sy-tabix.
ELSE.
INSERT i_stas2 INDEX 1.
ENDIF.
ENDIF.
ELSE.
IF i_stas-datuv <= i_effective_date AND
i_stas-datuv > i_stas2-datuv.
i_stas2 = i_stas.
MODIFY i_stas2 INDEX sy-tabix.
ENDIF.
Added for duplicate records one is deleted one isn't.
IF i_stas-datuv <= i_effective_date AND
i_stas-datuv = i_stas2-datuv AND
i_stas-lkenz = 'X'. "D46K916409
i_stas2 = i_stas. "D46K916409
MODIFY i_stas2 INDEX sy-tabix. "D46K916409
ENDIF. "D46K916409
IF NOT i_include_future IS INITIAL. "D46K921679
IF i_stas-datuv > i_effective_date. "D46K921679
wa_stas-lkenz = ' '. "D46K921679
ELSE.
wa_stas-lkenz = i_stas-lkenz. "D46K921679
ENDIF. "D46K921679
MODIFY i_stas2 FROM wa_stas INDEX sy-tabix. "D46K921679
ENDIF. "D46K921679
ENDIF.
ENDLOOP.
DELETE i_stas2 WHERE lkenz = 'X'.
SORT i_stas2 BY stlnr stlal stlkn. "D46K921679
IF i_stpo[] IS INITIAL.
RAISE no_bom_items.
ENDIF.
SELECT matnr mtart matkl FROM mara INTO TABLE i_mara
FOR ALL ENTRIES IN i_stpo
WHERE matnr = i_stpo-idnrk.
SORT i_mara BY matnr.
SORT i_stas BY stlty stlnr stlkn. "D46K921679
SORT i_mast BY matnr werks stlal. "D46K921679
IF i_include_fixed = ' '. "D46K905338
DELETE i_stpo WHERE fmeng <> ' '.
ENDIF. "D46K905338
ENDFORM.
----
FORM get_item_table *
----
Main processing... Get the information for the table that *
will be returned by the function module. *
----
FORM get_item_table.
REFRESH: i_item.
CLEAR: i_item.
LOOP AT i_matnr1.
READ TABLE i_mkal WITH KEY matnr = i_matnr1-matnr
BINARY SEARCH.
IF sy-subrc = 0.
PERFORM read_mast_using_alt_bom.
ELSE.
PERFORM read_mast.
ENDIF.
ENDLOOP.
ENDFORM.
----
FORM read_mast_using_alt_bom *
----
read the BOM header table MAST with the alt BOM *
----
FORM read_mast_using_alt_bom.
READ TABLE i_mast WITH KEY matnr = i_matnr1-matnr
werks = i_mkal-werks "D46K921679
stlal = i_mkal-stlal BINARY SEARCH.
IF sy-subrc <> 0.
PERFORM read_mast.
ELSE.
LOOP AT i_mast FROM sy-tabix.
IF i_mast-matnr <> i_matnr1-matnr OR
i_mast-werks <> i_mkal-werks OR "D46K921679
i_mast-stlal <> i_mkal-stlal.
EXIT.
ENDIF.
READ TABLE i_stko WITH KEY stlnr = i_mast-stlnr
stlal = i_mast-stlal
BINARY SEARCH.
IF sy-subrc <> 0.
i_stko-bmeng = 0.
ENDIF.
READ TABLE i_stas2 WITH KEY stlnr = i_mast-stlnr
stlal = i_mast-stlal
BINARY SEARCH.
LOOP AT i_stas2 FROM sy-tabix.
IF i_stas2-stlnr <> i_mast-stlnr OR
i_stas2-stlal <> i_mast-stlal.
EXIT.
ENDIF.
PERFORM read_stpo.
ENDLOOP.
ENDLOOP.
ENDIF.
ENDFORM.
----
FORM read_mast *
----
read the BOM without the alternative BOM. *
----
FORM read_mast.
stas2_found = 'N'. "D46K921679
READ TABLE i_mast WITH KEY matnr = i_matnr1-matnr
BINARY SEARCH.
IF sy-subrc = 0.
LOOP AT i_mast FROM sy-tabix.
IF i_mast-matnr <> i_matnr1-matnr.
EXIT.
ENDIF.
READ TABLE i_stko WITH KEY stlnr = i_mast-stlnr
stlal = i_mast-stlal "D46K921679
BINARY SEARCH.
IF sy-subrc <> 0.
i_stko-bmeng = 0.
ENDIF.
READ TABLE i_stas2 WITH KEY stlnr = i_mast-stlnr
stlal = i_mast-stlal "D46K921679
BINARY SEARCH.
LOOP AT i_stas2 FROM sy-tabix.
IF i_stas2-stlnr <> i_mast-stlnr OR "D46K921679
i_stas2-stlal <> i_mast-stlal. "D46K921679
EXIT.
ENDIF.
stas2_found = 'Y'. "D46K921679
PERFORM read_stpo.
ENDLOOP.
IF stas2_found = 'Y'. "D46K921679
EXIT. "D46K921679
ENDIF. "D46K921679
ENDLOOP.
ENDIF.
ENDFORM.
----
FORM read_stpo *
----
Read the item level tables using information pulled from stas *
to get the valid records. Fill the internal table being sent *
back in the function module. *
----
FORM read_stpo.
CLEAR: i_item.
READ TABLE i_stpo WITH KEY stlnr = i_stas2-stlnr
stlkn = i_stas2-stlkn
BINARY SEARCH.
LOOP AT i_stpo FROM sy-tabix.
IF i_stpo-stlnr <> i_stas2-stlnr OR
i_stpo-stlkn <> i_stas2-stlkn.
EXIT.
ENDIF.
IF sy-subrc = 0.
READ TABLE i_mara WITH KEY matnr = i_stpo-idnrk
BINARY SEARCH.
IF sy-subrc = 0.
i_item-matkl = i_mara-matkl.
i_item-mtart = i_mara-mtart.
ENDIF.
IF i_stko-bmeng <> 0.
i_item-menge = i_stpo-menge / i_stko-bmeng.
ENDIF.
i_item-postp = i_stpo-postp.
i_item-potx1 = i_stpo-potx1.
i_item-matnr = i_mast-matnr.
i_item-werks = i_mast-werks.
i_item-idnrk = i_stpo-idnrk.
i_item-begin_date = i_stas2-datuv. "D46K921679
i_item-end_date = i_stas2-enddt. "D46K921679
i_item-active = 'X'. "D46K921679
APPEND i_item.
ENDIF.
ENDLOOP.
ENDFORM.
*----
*
*----
FORM add_future_effective_materials.
SORT i_mast BY stlnr stlal. "D46K921679
LOOP AT i_stas. "D46K921679
READ TABLE i_mast WITH KEY "D46K921679
stlnr = i_stas-stlnr "D46K921679
stlal = i_stas-stlal BINARY SEARCH. "D46K921679
IF sy-subrc = 0.
IF i_stas-lkenz <> 'X'. "D46K921679
READ TABLE i_stko WITH KEY stlnr = i_stas-stlnr "D46K921679
stlal = i_stas-stlal "D46K921679
BINARY SEARCH. "D46K921679
IF sy-subrc <> 0. "D46K921679
i_stko-bmeng = 0. "D46K921679
ENDIF. "D46K921679
READ TABLE i_stas2 WITH KEY "D46K921679
stlnr = i_stas-stlnr "D46K921679
stlal = i_stas-stlal "D46K921679
stlkn = i_stas-stlkn BINARY SEARCH. "D46K921679
IF sy-subrc = 0. "D46K921679
IF i_stas-datuv > i_stas2-datuv AND "D46K921679
i_stas2-datuv > i_effective_date. "D46K921679
PERFORM read_stpo_for_future_materials. "D46K921679
ENDIF. "D46K921679
ELSE. "D46K921679
IF i_stas-datuv > i_effective_date. "D46K921679
PERFORM read_stpo_for_future_materials. "D46K921679
ENDIF. "D46K921679
ENDIF. "D46K921679
ENDIF. "D46K921679
ENDIF. "D46K921679
ENDLOOP. "D46K921679
ENDFORM.
*----
get any materials that are effective startig tomorrow
*----
FORM read_stpo_for_future_materials.
CLEAR: i_item. "D46K921679
READ TABLE i_stpo WITH KEY stlnr = i_stas-stlnr "D46K921679
stlkn = i_stas-stlkn "D46K921679
BINARY SEARCH. "D46K921679
LOOP AT i_stpo FROM sy-tabix. "D46K921679
IF i_stpo-stlnr <> i_stas-stlnr OR "D46K921679
i_stpo-stlkn <> i_stas-stlkn. "D46K921679
EXIT. "D46K921679
ENDIF. "D46K921679
IF sy-subrc = 0. "D46K921679
READ TABLE i_mara WITH KEY matnr = i_stpo-idnrk "D46K921679
BINARY SEARCH. "D46K921679
IF sy-subrc = 0. "D46K921679
i_item-matkl = i_mara-matkl. "D46K921679
i_item-mtart = i_mara-mtart. "D46K921679
ENDIF. "D46K921679
IF i_stko-bmeng <> 0. "D46K921679
i_item-menge = i_stpo-menge / i_stko-bmeng. "D46K921679
ENDIF. "D46K921679
i_item-postp = i_stpo-postp. "D46K921679
i_item-potx1 = i_stpo-potx1. "D46K921679
i_item-matnr = i_mast-matnr. "D46K921679
i_item-werks = i_mast-werks. "D46K921679
i_item-idnrk = i_stpo-idnrk. "D46K921679
i_item-begin_date = i_stas-datuv. "D46K921679
APPEND i_item. "D46K921679
ENDIF. "D46K921679
ENDLOOP. "D46K921679
ENDFORM.
10-03-2008 5:52 PM
>
> The easiest way to look up BOM data is with this function module "CS_BOM_EXPL_MAT_V2".
>
> However I have looked up data with the tables as well. It's a lot of code, and it may change. We have had some changes to it. We may have more changes. It's easy to miss something.
And exactly for that reason: stick to the standard SAP function.
10-03-2008 1:18 PM
Hi check the code below for a similar requirement.
&----
*& Report ZPPPRM_1087_BOM
*&
&----
*&
*&
&----
report zppprm_1087_bom.
include zbom_data_declarartion.
include zbom_selection_screen.
include zform_used.
*initialization
initialization.
prm1 = 'SELECTION'.
prm2 = 'Materials'.
&----
START-OF-SELECTION
&----
start-of-selection.
Get the data from data base tablees
perform get_data.
*Format the final data
perform final_data.
*Display the final data
perform display_final.
&----
*& Include ZBOM_DATA_DECLARARTION
&----
************************************************************************
T Y P E - P O O L S
************************************************************************
tables: mara, "Material Master
mast, "Material to BOM Linkage
stpo. "BOM item
type-pools: slis .
types: begin of ty_mara,
matnr type matnr,
mtart type mtart,
end of ty_mara.
types: begin of ty_mast,
matnr type matnr,
werks type werks_d,
stlan type stlan,
stlnr type stnum,
stlal type stalt,
end of ty_mast.
types: begin of ty_header,
matnr type matnr,
werks type werks_d,
end of ty_header.
types: begin of ty_stpo,
stlty type stlty,
stlnr type stnum,
stlkn type stlkn,
stpoz type cim_count,
idnrk type idnrk,
meins type kmpme,
menge type kmpmg,
preis type cprei,
peinh type peinh,
end of ty_stpo.
types: begin of ty_bom,
matnr type matnr,
werks type werks_d,
idnrk type idnrk,
meins type kmpme,
menge type kmpmg,
end of ty_bom.
Internal tables used in the code
data: it_stpo type standard table of ty_stpo,
it_mara1 type standard table of ty_mara,
it_mara2 type standard table of ty_mara,
it_mast type standard table of ty_mast,
it_bom type standard table of ty_bom,
it_header type standard table of ty_header.
Work Areas used in the code
data: wa_mast type ty_mast,
wa_stpo type ty_stpo,
wa_mara type ty_mara,
wa_bom type ty_bom,
wa_header type ty_header.
data: v_matnr type mara-matnr.
data: col_pos type i.
data: sortcat type slis_t_sortinfo_alv,
sortcat_ln type slis_sortinfo_alv,
fcat type slis_t_fieldcat_alv,
s_fcat type slis_fieldcat_alv.
data: obj_hieralv type ref to cl_salv_hierseq_table.
data: it_binding type salv_t_hierseq_binding,
wa_binding type salv_s_hierseq_binding.
&----
*& Include ZBOM_SELECTION_SCREEN
&----
*-Declaration of selection screen forthe program-*
selection-screen begin of block blk1 with frame title prm1.
selection-screen begin of line.
selection-screen comment (10) prm2 for field so_matnr.
Selection Option.
select-options : so_matnr for v_matnr.
selection-screen end of line.
selection-screen end of block blk1.
&----
*& Include ZFORM_USED
&----
&----
*& Form GET_DATA
&----
text
----
form get_data .
*LOADING FINISHED MATERIALS INTO AN INTERNAL TABLE
select matnr
mtart
into table it_mara1
from mara
where matnr in so_matnr
and mtart = 'FERT'.
if sy-subrc = 0.
sort it_mara1 by matnr.
endif.
*LOADING CORRESPONDING FROM Material to BOM LINK TABLE
if it_mara1 is not initial.
select matnr
werks
stlan
stlnr
stlal
into table it_mast
from mast
for all entries in it_mara1
where matnr = it_mara1-matnr .
if sy-subrc = 0.
sort it_mast by stlnr.
loop at it_mast into wa_mast.
move-corresponding wa_mast to wa_header.
*
append wa_header to it_header.
endloop.
endif.
endif.
*LOADING CORRESPONDING BOM COMPONENTS FRON BOM ITEM TABLE
if it_mast is not initial.
select stlty
stlnr
stlkn
stpoz
idnrk
meins
menge
preis
peinh
from stpo
into table it_stpo
for all entries in it_mast
where stlnr = it_mast-stlnr.
if sy-subrc eq 0.
sort it_stpo by stlnr.
endif.
endif.
*LOAD SEMIFINISHED MATERIAL
select matnr
mtart
from mara
into table it_mara2
where mtart = 'HALB'.
if sy-subrc eq 0.
sort it_mara2 by matnr.
endif.
endform. " GET_SELECT
&----
*& Form FINAL_data
&----
text
----
form final_data .
loop at it_stpo into wa_stpo.
clear wa_mara.
read table it_mara2 into wa_mara
with key matnr = wa_stpo-idnrk
binary search.
if sy-subrc ne 0.
continue.
else.
read table it_mast into wa_mast
with key stlnr = wa_stpo-stlnr
binary search.
if sy-subrc eq 0.
clear wa_bom.
wa_bom-matnr = wa_mast-matnr.
wa_bom-werks = wa_mast-werks.
wa_bom-idnrk = wa_stpo-idnrk.
wa_bom-meins = wa_stpo-meins.
wa_bom-menge = wa_stpo-menge.
collect wa_bom into it_bom.
move-corresponding wa_mast to wa_header.
append wa_header to it_header.
endif.
endif.
endloop.
sort it_header by matnr werks.
sort it_bom by matnr werks idnrk.
delete adjacent duplicates from it_header comparing matnr werks.
endform. " FINAL_data
&----
*& Form display_summary
&----
text
----
form display_final.
wa_binding-master = 'MATNR'.
wa_binding-slave = 'MATNR'.
append wa_binding to it_binding.
*Process of binding the header and BOM info in hirarchial form
try.
call method cl_salv_hierseq_table=>factory
exporting
t_binding_level1_level2 = it_binding
importing
r_hierseq = obj_hieralv
changing
t_table_level1 = it_header
t_table_level2 = it_bom.
catch cx_salv_data_error .
catch cx_salv_not_found .
endtry.
*to dispay the info
call method obj_hieralv->display.
endform.
Hope this helps you,
Murthy.
10-03-2008 6:28 PM
Hi Kumar
If you are looking at following FM
CS_BOM_EXPLOSION General BOM explosion
CS_BOM_EXPLOSION_EQUI BOM explosion - initial screen: equipment
CS_BOM_EXPLOSION_MAT BOM explosion (old version); as of 3.0, use CS_BOM_EXPL_MAT_V2
CS_BOM_EXPL_EQU_V2 BOM explosion for equipment
CS_BOM_EXPL_KND_V1 BOM explosion for material
CS_BOM_EXPL_MAT_V2 BOM explosion for material
with regards
naveen
10-06-2008 6:57 AM
hi,
STLTY,STLNR,STLAL and STKOZ are key field in both table.so, you can find the records in table STPO by using these field.STLTY is BOM category to find BOM area like SD,MM/PP etc. you try to use this field. it will be better.
10-21-2008 8:06 AM