一个将MCRE的物料消耗,CKM3的下层差异接受和CO03的实际成本加工在一个报表的代码
************************************************************************
* Program Title: 产成实际成本组件明细表 *
* 对本期与基期的产品单位成本按成本组件原材料、包装物、半成品、直接人工、
*制造费用及制造费用-闲置设备分解到下一阶
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Data Table List: *
* mlit:物料分类帐凭证: 项目 *
* mlhd:物料总帐凭证:标题 *
* mlpp:物料分类帐凭证:记帐期间和数量 *
* mlcr:物料分类帐凭证:货币和值 *
* mlcrf:物料分类帐文件:字段组(货币) *
* s026:
* Refrence Table List: *
*----------------------------------------------------------------------*
* Modification Log: *
************************************************************************
* Date Programmer Correction Number DesingDoc Number *
************************************************************************
REPORT zcor0009 LINE-SIZE 100 MESSAGE-ID zdev.
*----------------------------------------------------------------------*
* TYPES OR TYPE-POOLS *
*----------------------------------------------------------------------*
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
*TABLES *
*----------------------------------------------------------------------*
TABLES:s026,coss,mara.
*----------------------------------------------------------------------*
* GLOBAL VARIABLES *
*----------------------------------------------------------------------*
*系统公用变量,调用程序名及屏幕编号
DATA: g_repid LIKE sy-repid,
g_dynnum LIKE sy-dynnr.
DATA: g_return_rc LIKE sy-subrc,
g_dates LIKE sy-datum,
g_datee LIKE sy-datum.
*用于提高查询速度见的空查询条件
RANGES: r_werks FOR ekpo-werks,
r_vrsio FOR mcs0-vrsio,
r_spmon FOR mcs0-spmon,
r_sptag FOR s026-sptag,
r_spwoc FOR mcs0-spwoc,
r_mcomp FOR s026-mcomp,
r_aufnr FOR s026-aufnr,
r_spbup FOR mcs0-spbup.
*返回期间单据
DATA: BEGIN OF gt_s000 OCCURS 100.
INCLUDE STRUCTURE s026.
DATA: farbe.
DATA: END OF gt_s000.
*可用物料(用于计算差异)
DATA: BEGIN OF gt_matnr OCCURS 100,
werks LIKE s026-werks,
matnr LIKE s026-matnr,
END OF gt_matnr.
*物料制造数量(用于计算差异)
DATA: BEGIN OF gt_matnr_q OCCURS 100,
matnr LIKE s026-matnr,
menge LIKE mlppf-menge,
END OF gt_matnr_q.
*生产订单->对象 (用于计算非材料消耗)
DATA: BEGIN OF gt_objnr OCCURS 100,
objnr LIKE coep-objnr,
END OF gt_objnr.
*结果内表
DATA: BEGIN OF gt_result OCCURS 0 ,
type(2), "成本类型
desc(8), "成本类型描述
mcomp LIKE s026-mcomp, "组件
mcompdesc LIKE makt-maktx, "物料描叙
basme LIKE s026-basme, "单位
req_q LIKE s026-bdmng, "需求数量
withdrawn_q LIKE s026-bdmng, "领料数量
withdrawn_m LIKE s026-enwrt, "标准值
differ_m LIKE s026-enwrt, "差异值
fact_m LIKE s026-enwrt, "实际值
END OF gt_result.
*global value of alv display
DATA:
gt_sort TYPE slis_t_sortinfo_alv,
gt_fieldcat TYPE slis_t_fieldcat_alv,
gt_event TYPE slis_alv_event OCCURS 0 WITH HEADER LINE, "Grid输出表头事件内表
gt_head TYPE slis_t_listheader. "Grid输出表头
DATA: BEGIN OF bdcdata OCCURS 10.
INCLUDE STRUCTURE bdcdata.
DATA: END OF bdcdata.
*----------------------------------------------------------------------*
* PARAMETERS AND SELECT-OPTIONS *
*----------------------------------------------------------------------*
*选择条件:工厂/物料/生产订单号/期间
PARAMETERS: p_werks LIKE s026-werks OBLIGATORY
MEMORY ID wrk MODIF ID sc.
SELECT-OPTIONS: s_matnr FOR s026-matnr MEMORY ID mat
MATCHCODE OBJECT mat1
MODIF ID sc.
*SELECT-OPTIONS: s_aufnr FOR s026-aufnr MEMORY ID auf MODIF ID auf
* MATCHCODE OBJECT orde.
*本期起始/截止期间,基期起始
SELECTION-SCREEN BEGIN OF BLOCK sc_1 WITH FRAME TITLE text-001.
PARAMETERS: p_mons TYPE spmon OBLIGATORY MEMORY ID pos
MODIF ID sc.
PARAMETERS: p_mone TYPE spmon OBLIGATORY MEMORY ID poe MODIF ID sc.
SELECTION-SCREEN END OF BLOCK sc_1.
***********************************************************************
*INITIALIZATION.
***********************************************************************
INITIALIZATION.
g_repid = sy-repid.
g_dynnum = sy-dynnr.
GET PARAMETER ID 'POS' FIELD p_mons.
IF p_mons IS INITIAL.
SET PARAMETER ID 'POS' FIELD sy-datum+0(6).
ENDIF.
GET PARAMETER ID 'POE' FIELD p_mone.
IF p_mone IS INITIAL.
SET PARAMETER ID 'POE' FIELD sy-datum+0(6).
ENDIF.
***********************************************************************
*AT SELECTION-SCREEN OUTPUT
***********************************************************************
AT SELECTION-SCREEN OUTPUT.
***********************************************************************
* START-OF-SELECTION
***********************************************************************
START-OF-SELECTION.
**计算报表日期本期相关变量
IF p_mons NE space AND p_mone NE space.
CONCATENATE p_mons '01' INTO r_sptag-low.
CONCATENATE p_mone '01' INTO g_dates.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = g_dates
IMPORTING
last_day_of_month = g_datee.
MOVE g_datee+0(8) TO r_sptag-high.
r_sptag-option = 'BT'.
r_sptag-sign = 'I'.
APPEND r_sptag.
ENDIF.
*判断用否可以运算
CALL FUNCTION 'Z_AUTH_CPU'
IMPORTING
z_return_error = g_return_rc.
IF g_return_rc > 0.
MESSAGE e100 WITH '服务器达到最大会话数,请稍后再试'.
EXIT.
ENDIF.
**权限控制
r_werks-sign = 'I'.
r_werks-option = 'EQ'.
r_werks-low = p_werks.
APPEND r_werks.
CALL FUNCTION 'Z_AUTH_WERKS'
IMPORTING
z_return_code = g_return_rc
TABLES
z_ittab = r_werks
z_outtab = r_werks.
IF g_return_rc NE 0.
EXIT.
ENDIF.
*计算数据
PERFORM select_data.
***********************************************************************
* END-OF-SELECTION
***********************************************************************
END-OF-SELECTION.
*设置显示格式
PERFORM f_fieldcat_init.
*
PERFORM event_top_of_page TABLES gt_event.
*ALV显示报表
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
i_callback_user_command = 'USER_COMMAND'
it_fieldcat = gt_fieldcat[]
it_sort = gt_sort[]
it_events = gt_event[]
TABLES
t_outtab = gt_result.
***********************************************************************
* Form Name : select_data
* Form Description:
*提取数据
*----------------------------------------------------------------------
*参数说明
*----------------------------------------------------------------------
*修改说明
*----------------------------------------------------------------------
FORM select_data .
RANGES: lr_kstar FOR coep-kstar,
lr_gjahr FOR coep-gjahr,
lr_perio FOR coep-perio,
lr_parob1 FOR coep-parob1,
lr_objnr FOR coep-objnr.
data: l_oldmatnr like mara-matnr.
DATA: BEGIN OF lt_mlit OCCURS 100,
belnr LIKE mlit-belnr,
kjahr LIKE mlit-kjahr,
matnr LIKE mlit-matnr,
* posnr LIKE mlit-posnr,
END OF lt_mlit.
DATA: BEGIN OF lt_mlit2 OCCURS 100,
belnr LIKE mlit-belnr,
kjahr LIKE mlit-kjahr,
END OF lt_mlit2.
DATA: BEGIN OF lt_mlit3 OCCURS 100,
belnr LIKE mlit-belnr,
kjahr LIKE mlit-kjahr,
END OF lt_mlit3.
DATA: BEGIN OF lt_mlhd OCCURS 100,
belnr LIKE mlit-belnr,
kjahr LIKE mlit-kjahr,
END OF lt_mlhd.
DATA: it_mlhd LIKE mlhd OCCURS 0 WITH HEADER LINE.
DATA:l_kstar LIKE coep-kstar,l_money LIKE coep-wogbtr.
SELECT * FROM s026 INTO TABLE gt_s000
WHERE ssour = space
AND vrsio IN r_vrsio
AND spmon IN r_spmon
AND sptag IN r_sptag
AND spwoc IN r_spwoc
AND spbup IN r_spbup
AND werks = p_werks
AND matnr IN s_matnr
AND mcomp IN r_mcomp
AND aufnr IN r_aufnr.
LOOP AT gt_s000.
gt_result-type = '02'. "直接材料
gt_result-mcomp = gt_s000-mcomp.
* gt_result-basme = gt_s000-basme.
gt_result-req_q = gt_s000-bdmng.
gt_result-withdrawn_q = gt_s000-enmng.
gt_result-withdrawn_m = gt_s000-enwrt.
COLLECT gt_result.
MOVE gt_s000-matnr TO gt_matnr-matnr .
MOVE gt_s000-werks TO gt_matnr-werks .
APPEND gt_matnr.
CONCATENATE 'OR' gt_s000-aufnr INTO gt_objnr-objnr .
APPEND gt_objnr.
CLEAR gt_result.
CLEAR gt_matnr.
CLEAR gt_objnr.
ENDLOOP.
*计算差异
SORT gt_matnr BY werks matnr.
DELETE ADJACENT DUPLICATES FROM gt_matnr.
IF NOT gt_matnr[] IS INITIAL.
SELECT mlit~belnr mlit~kjahr mlit~matnr mlppf~menge
INTO (lt_mlit-belnr,lt_mlit-kjahr,gt_matnr_q-matnr,gt_matnr_q-menge)
FROM mlit
INNER JOIN mlpp
ON mlit~mandt = mlpp~mandt AND mlit~belnr = mlpp~belnr
AND mlit~kjahr = mlpp~kjahr AND mlit~posnr = mlpp~posnr
INNER JOIN mlppf
ON mlit~mandt = mlppf~mandt AND mlit~belnr = mlppf~belnr
AND mlit~kjahr = mlppf~kjahr AND mlit~posnr = mlppf~posnr
* INNER JOIN mlcrf
* ON mlit~mandt = mlcrf~mandt AND mlit~belnr = mlcrf~belnr
* AND mlit~kjahr = mlcrf~kjahr AND mlit~posnr = mlcrf~posnr
FOR ALL entries IN gt_matnr
WHERE mlit~bwkey = gt_matnr-werks AND mlit~matnr = gt_matnr-matnr
AND mlit~psart = 'MO'
AND mlpp~budat IN r_sptag
AND ( mlit~xabrerr = space OR mlit~xabrerr IS NULL )
* and mlcrf~FELDG = 'ZUMO'
.
lt_mlit-matnr = gt_matnr_q-matnr.
APPEND lt_mlit.
APPEND gt_matnr_q.
CLEAR gt_matnr_q.
CLEAR lt_mlit.
ENDSELECT.
SORT lt_mlit BY matnr belnr kjahr .
DELETE ADJACENT DUPLICATES FROM lt_mlit.
IF NOT lt_mlit[] IS INITIAL.
l_oldmatnr = space.
LOOP AT lt_mlit.
if l_oldmatnr = space.
l_oldmatnr = lt_mlit-matnr.
endif.
IF lt_mlit-matnr NE l_oldmatnr.
SELECT *
INTO TABLE it_mlhd
FROM mlhd
FOR ALL ENTRIES IN lt_mlit2
WHERE mlhd~belnr = lt_mlit2-belnr AND mlhd~kjahr = lt_mlit2-kjahr
AND mlhd~vgart = 'MS'.
SORT it_mlhd DESCENDING BY cpudt cputm.
READ TABLE it_mlhd INDEX 1.
IF sy-subrc = 0."取最后一次的差异调整
lt_mlit3-belnr = it_mlhd-belnr.
lt_mlit3-kjahr = it_mlhd-kjahr.
append lt_mlit3.
ENDIF.
refresh lt_mlit2.
l_oldmatnr = lt_mlit-matnr.
ENDIF.
lt_mlit2-belnr = lt_mlit-belnr.
lt_mlit2-kjahr = lt_mlit-kjahr.
append lt_mlit2.
ENDLOOP.
if l_oldmatnr ne space.
SELECT *
INTO TABLE it_mlhd
FROM mlhd
FOR ALL ENTRIES IN lt_mlit2
WHERE mlhd~belnr = lt_mlit2-belnr AND mlhd~kjahr = lt_mlit2-kjahr
AND mlhd~vgart = 'MS'.
SORT it_mlhd DESCENDING BY cpudt cputm.
READ TABLE it_mlhd INDEX 1.
IF sy-subrc = 0."取最后一次的差异调整
lt_mlit3-belnr = it_mlhd-belnr.
lt_mlit3-kjahr = it_mlhd-kjahr.
append lt_mlit3.
ENDIF.
endif.
SORT lt_mlit3 BY belnr kjahr .
SELECT mlit~matnr mlcrf~prdif
INTO (gt_result-mcomp,gt_result-differ_m)
FROM mlit
INNER JOIN mlcrf
ON mlit~mandt = mlcrf~mandt AND mlit~belnr = mlcrf~belnr
AND mlit~kjahr = mlcrf~kjahr AND mlit~posnr = mlcrf~posnr
FOR ALL entries IN lt_mlit3
WHERE mlit~belnr = lt_mlit3-belnr AND mlit~kjahr = lt_mlit3-kjahr
AND mlit~psart = 'MI'.
gt_result-type = '02'. "直接材料差异
COLLECT gt_result.
CLEAR gt_result.
ENDSELECT.
ENDIF.
ENDIF.
SORT gt_objnr BY objnr.
DELETE ADJACENT DUPLICATES FROM gt_objnr.
*取制造成本
IF NOT gt_objnr[] IS INITIAL.
lr_kstar-option = 'EQ'.
lr_kstar-sign = 'I'.
lr_kstar-low = 'DEP-ACT'.
APPEND lr_kstar.
lr_kstar-low = 'OVH-ACT'.
APPEND lr_kstar.
lr_kstar-low = 'LAB-ACT'.
APPEND lr_kstar.
lr_objnr-option = 'EQ'.
lr_objnr-sign = 'I'.
LOOP AT gt_objnr.
lr_objnr-low = gt_objnr-objnr.
APPEND lr_objnr.
ENDLOOP.
SELECT * INTO coss
FROM coss
WHERE objnr IN lr_objnr
AND coss~kstar IN lr_kstar
AND wrttp = '04'.
CASE coss-kstar.
WHEN 'LAB-ACT'.
gt_result-mcomp = '直接人工'.
gt_result-type = '03'.
WHEN 'OVH-ACT'.
gt_result-mcomp = '制造费用'.
gt_result-type = '03'.
WHEN 'DEP-ACT'.
gt_result-mcomp = '其他设备折旧'.
gt_result-type = '03'.
ENDCASE.
gt_result-withdrawn_m = coss-wkg001 + coss-wkg002 + coss-wkg003 +
coss-wkg004 + coss-wkg005 + coss-wkg006 +
coss-wkg007 + coss-wkg008 + coss-wkg009 +
coss-wkg010 + coss-wkg011 + coss-wkg012 +
coss-wkg013 + coss-wkg014 + coss-wkg015 +
coss-wkg016 .
COLLECT gt_result.
CLEAR gt_result.
ENDSELECT.
ENDIF.
LOOP AT gt_result.
CASE gt_result-type.
WHEN '02'.
SELECT SINGLE maktx INTO (gt_result-mcompdesc) FROM makt
WHERE matnr = gt_result-mcomp AND spras = 1.
SELECT SINGLE meins INTO (gt_result-basme) FROM mara
WHERE matnr = gt_result-mcomp.
gt_result-desc = '直接材料'.
gt_result-desc = '直接材料'.
WHEN '03'.
gt_result-desc = '制造费用'.
ENDCASE.
gt_result-fact_m = gt_result-withdrawn_m + gt_result-differ_m.
MODIFY gt_result.
ENDLOOP.
ENDFORM. " select_data
************************************************************************
*&Form Name : event_top_of_page
*----------------------------------------------------------------------*
* Function Description: *
* 设置表输出表头事件
*----------------------------------------------------------------------*
*参数说明 *
*----------------------------------------------------------------------*
*-->ex_gt_eventb 事件内表
*----------------------------------------------------------------------*
* Date Programmer Description *
* YYYY/MM/DD XXXXXXXX *
************************************************************************
FORM event_top_of_page TABLES ex_gt_event TYPE slis_t_event.
REFRESH ex_gt_event.
CLEAR ex_gt_event.
ex_gt_event-name = slis_ev_top_of_page.
ex_gt_event-form = 'WRITE_TOP_OF_PAGE'.
APPEND ex_gt_event.
ENDFORM. "event_top_of_page
************************************************************************
*&Form Name : write_top_of_page
*----------------------------------------------------------------------*
* Function Description: *
* 设置明细表表头
*----------------------------------------------------------------------*
*参数说明 *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Date Programmer Description *
* YYYY/MM/DD XXXXXXXX *
************************************************************************
FORM write_top_of_page.
DATA: wa_hline TYPE slis_listheader,
l_menge(15) TYPE c,
l_maktx LIKE makt-maktx,
l_matnr LIKE makt-matnr.
REFRESH gt_head.
CLEAR: wa_hline.
DATA: l_count TYPE i.
wa_hline-typ = 'H'.
wa_hline-info = '成品实际成本组件明细分析'.
APPEND wa_hline TO gt_head.
wa_hline-typ = 'S'.
wa_hline-key = '统计时间'.
CONCATENATE g_dates+0(8) '-' g_datee+0(8) INTO wa_hline-info.
APPEND wa_hline TO gt_head.
wa_hline-typ = 'S'.
wa_hline-key = '工 厂'.
wa_hline-info = p_werks.
APPEND wa_hline TO gt_head.
*
SELECT COUNT( * ) INTO (l_count) FROM mara WHERE matnr IN s_matnr.
IF l_count = 1.
wa_hline-typ = 'S'.
wa_hline-key = '物 料'.
SELECT SINGLE matnr maktx INTO (l_matnr,l_maktx) FROM makt
WHERE matnr IN s_matnr AND spras = 1.
READ TABLE gt_matnr_q.
l_menge = gt_matnr_q-menge.
CONCATENATE l_matnr ':' l_maktx '-数量:' l_menge INTO wa_hline-info.
APPEND wa_hline TO gt_head.
ENDIF.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = gt_head.
ENDFORM. "write_top_of_page
************************************************************************
*&Form Name : user_command
*----------------------------------------------------------------------*
* Function Description: *
* 对汇总表双击显示收货明细
*----------------------------------------------------------------------*
*参数说明
*-->r_ucomm: PAI触发的功能代码
*-->rs_selfield: 同Grid相关联性质结构
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Date Programmer Description *
* YYYY/MM/DD XXXXXXXX *
************************************************************************
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA l_matnr LIKE mara-matnr.
CASE r_ucomm.
WHEN '&IC1'. "doubleclick
READ TABLE gt_result INDEX rs_selfield-tabindex.
IF sy-subrc = 0.
SELECT SINGLE * FROM mara WHERE matnr = gt_result-mcomp.
IF sy-subrc = 0.
SET PARAMETER ID 'WRK' FIELD p_werks.
SET PARAMETER ID 'MAT' FIELD gt_result-mcomp.
SET PARAMETER ID 'POS' FIELD p_mons.
SET PARAMETER ID 'POE' FIELD p_mone.
CALL TRANSACTION 'ZCOR0009' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
CLEAR r_ucomm.
ENDCASE.
ENDFORM. "USER_COMMAND_SUM
***********************************************************************
* Form Name : f_fieldcat_init
* Form Description:
*设置ALV输出报表的显示字段、排序字段及其相关属性 *
*----------------------------------------------------------------------
*参数说明
*----------------------------------------------------------------------
*修改说明
*----------------------------------------------------------------------
FORM f_fieldcat_init .
DATA: wa_fieldcat TYPE slis_fieldcat_alv,
wa_sort TYPE slis_sortinfo_alv.
*显示字段
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'TYPE'.
wa_fieldcat-seltext_s = '序号'.
wa_fieldcat-key = 'X'.
wa_fieldcat-no_out = 'X'.
wa_fieldcat-fix_column = 'X'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'DESC'.
wa_fieldcat-seltext_s = '费用类型'.
wa_fieldcat-key = 'X'.
wa_fieldcat-fix_column = 'X'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MCOMP'.
wa_fieldcat-seltext_l = '组件'.
wa_fieldcat-key = 'X'.
wa_fieldcat-outputlen = '20'.
wa_fieldcat-fix_column = 'X'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'MCOMPDESC'.
wa_fieldcat-seltext_l = '组件描叙'.
wa_fieldcat-outputlen = '30'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'BASME'.
wa_fieldcat-seltext_l = '单位'.
wa_fieldcat-outputlen = '3'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'REQ_Q'.
wa_fieldcat-seltext_l = '需求数量'.
wa_fieldcat-no_zero = 'X'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-outputlen = '15'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'WITHDRAWN_Q'.
wa_fieldcat-seltext_l = '领料数量'.
wa_fieldcat-no_zero = 'X'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-outputlen = '15'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'WITHDRAWN_M'.
wa_fieldcat-seltext_l = '标准值'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-no_zero = 'X'.
wa_fieldcat-outputlen = '18'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'DIFFER_M'.
wa_fieldcat-seltext_l = '下层差异'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-no_zero = 'X'.
wa_fieldcat-outputlen = '18'.
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FACT_M'.
wa_fieldcat-seltext_l = '实际值'.
wa_fieldcat-no_zero = 'X'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-outputlen = '18'.
APPEND wa_fieldcat TO gt_fieldcat.
*排序字段(品牌/可比性/分类)
CLEAR wa_sort.
wa_sort-fieldname = 'DESC'.
wa_sort-spos = 1.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO gt_sort.
ENDFORM. "f_fieldcat_init