REPORT zmmr003
.
TYPE-POOLS
:slis
.
TABLES
:mast
, mara
, stko
, makt
, afko
, afpo
, aufk
.
DATA
:stb
LIKE stpox
OCCURS
0
WITH
HEADER
LINE
.
DATA
:
BEGIN
OF matcat
OCCURS
0
.
INCLUDE
STRUCTURE cscmat
.
DATA
:
END
OF matcat
.
DATA
:
BEGIN
OF mats
.
INCLUDE
STRUCTURE cscmat
.
DATA
:
END
OF mats
.
DATA
:
BEGIN
OF selpool
.
INCLUDE
STRUCTURE cstmat
.
DATA
:
END
OF selpool
.
DATA
: dstst_flg
LIKE csdata
-xfeld
.
DATA
: layout
TYPE slis_layout_alv
,
gs_layout
TYPE slis_layout_alv
,
g_repid
LIKE sy
-repid
.
DATA
: wa_alv_field
TYPE slis_fieldcat_alv
,
"列描述内表,列清单
wa_alv_fieldcat
TYPE slis_t_fieldcat_alv
,
"定义内表
g_user_command
TYPE slis_formname
VALUE
'USER_COMMAND'
.
DATA
:
BEGIN
OF wa_data
,
werks
LIKE aufk
-werks
,
aufnr
LIKE afko
-aufnr
,
objnr
LIKE aufk
-objnr
,
plnbez
LIKE afko
-plnbez
,
gamng
LIKE afko
-gamng
,
END
OF wa_data
.
DATA
: it_data
LIKE wa_data
OCCURS
0
WITH
HEADER
LINE
.
DATA
: itab
TYPE
TABLE
OF zmmsf003
WITH
HEADER
LINE
.
DATA
: it_out
TYPE
TABLE
OF zmmsf003
WITH
HEADER
LINE
.
DATA
:it_objnr
TYPE
TABLE
OF jest
WITH
HEADER
LINE
.
DATA
:
BEGIN
OF wa_batch
,
werks
TYPE mchb
-werks
,
matnr
TYPE mchb
-matnr
,
lgort
TYPE mchb
-lgort
,
charg
TYPE mchb
-charg
,
clabs
TYPE mchb
-clabs
,
END
OF wa_batch
.
DATA
: it_batch
LIKE
TABLE
OF wa_batch
WITH
HEADER
LINE
.
SELECTION-SCREEN
BEGIN
OF
BLOCK block1
WITH
FRAME
TITLE
text
-
001
.
SELECT-OPTIONS
: s_werks
FOR aufk
-werks
.
SELECT-OPTIONS
: s_aufnr
FOR afko
-aufnr
.
SELECT-OPTIONS
:s_matnr
FOR afko
-plnbez
.
PARAMETERS
: s_auart
LIKE aufk
-auart
.
SELECTION-SCREEN
END
OF
BLOCK block1
.
INITIALIZATION
.
g_repid
= sy
-repid
.
START-OF-SELECTION
.
PERFORM getdata
.
PERFORM displaydata
.
PERFORM layout_init
CHANGING gs_layout
.
CALL
FUNCTION
'REUSE_ALV_GRID_DISPLAY'
"调用ALV 显示表单数据
EXPORTING
i_callback_program
= sy
-repid
"i_callback_top_of_page = 'TOP_OF_PAGE'
it_fieldcat
= wa_alv_fieldcat
is_layout
= gs_layout
i_callback_user_command
= g_user_command
TABLES
t_outtab
= it_out
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
.
*&---------------------------------------------------------------------*
*& Form getdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM getdata
.
SELECT
aufk
~werks
aufk
~aufnr
aufk
~objnr
afko
~plnbez
afko
~gamng
INTO CORRESPONDING
FIELDS
OF
TABLE it_data
FROM aufk
JOIN afko
ON aufk
~aufnr
= afko
~aufnr
WHERE aufk
~werks
IN s_werks
AND afko
~aufnr
IN s_aufnr
AND afko
~plnbez
IN s_matnr
AND auart
=
'PP01'
.
SORT it_data
BY aufnr plnbez
.
DELETE it_data
WHERE plnbez
IS
INITIAL
.
LOOP
AT it_data
INTO wa_data
.
SELECT *
INTO CORRESPONDING
FIELDS
OF
TABLE it_objnr
FROM jest
WHERE objnr
= wa_data
-objnr
AND stat
=
'I0002'
AND inact
=
''
.
IF it_objnr
IS
NOT
INITIAL
.
"工单已下达
SELECT
SINGLE lgort
INTO itab
-lgort
FROM afpo
WHERE aufnr
= wa_data
-aufnr
.
SELECT
SINGLE maktx
INTO itab
-maktx
FROM makt
WHERE matnr
= wa_data
-plnbez
.
CALL
FUNCTION
'CS_BOM_EXPL_MAT_V2'
EXPORTING
capid
=
'PP01'
"BOM 应用程序
datuv
= sy
-datum
"有效起始日
" emeng = p_emeng " 基本数量 Required quantity STKO-BMENG
mtnrv
= wa_data
-plnbez
"成品号或半成品号
stlan
=
'1'
"BOM 用途,1 代表生产
stlal
=
''
"可选BOM
mktls
=
'X'
mehrs
=
'X'
"是否多层展开,'X' 代表多层
rndkz
=
'1'
"Round off: ' '=always, '1'=never, '2'=only levels > 1 是否取整
werks
= wa_data
-werks
"工厂
IMPORTING
topmat
= selpool
"开始BOM 展开的物料显示
dstst
= dstst_flg
"BOM 帮助字段
TABLES
stb
= stb
matcat
= matcat
"下面含有组件的物料存放在该内表对应 STB-TTIDX 在类别表内的索引﹐标志直属哪个物料下的 BOM
EXCEPTIONS
alt_not_found
=
1
call_invalid
=
2
material_not_found
=
3
missing_authorization
=
4
no_bom_found
=
5
no_plant_data
=
6
no_suitable_bom_found
=
7
OTHERS
=
8
.
DELETE stb
WHERE mtart <>
'ROH'
.
SORT stb
BY idnrk
.
DATA
: s_idnrk
LIKE stpox
-idnrk
.
s_idnrk
=
''
.
IF stb[]
IS
NOT
INITIAL
.
LOOP
AT stb
.
DATA
: s_mmein
(
2
)
TYPE
c
.
IF s_idnrk <> stb
-idnrk
.
itab
-werks
= stb
-werks
.
itab
-idnrk
= stb
-idnrk
.
"组件
itab
-mngko
= stb
-mngko * wa_data
-gamng
.
"用量
IF stb
-mmein
=
'ST'
.
"ST单位变为PC
CALL
FUNCTION
'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input
= stb
-mmein
IMPORTING
output
= s_mmein
EXCEPTIONS
unit_not_found
=
1
OTHERS
=
2
.
itab
-mmein
= s_mmein
.
ELSE
.
itab
-mmein
= stb
-mmein
.
"基本计量单位
ENDIF
.
itab
-matnr
= wa_data
-plnbez
.
itab
-aufnr
= wa_data
-aufnr
.
APPEND itab
.
s_idnrk
= stb
-idnrk
.
ELSE
.
itab
-mngko
= itab
-mngko + stb
-mngko * wa_data
-gamng
.
sy
-tabix
= sy
-tabix
-
1
.
MODIFY itab
INDEX sy
-tabix
.
ENDIF
.
ENDLOOP
.
ENDIF
.
ENDIF
.
ENDLOOP
.
LOOP
AT itab
.
DATA
: sumqty
LIKE mchb
-clabs
.
sumqty
=
0
.
DATA
: mess
(
20
)
TYPE
c
.
SELECT
SINGLE
SUM
( clabs
)
INTO sumqty
FROM mchb
WHERE werks
= itab
-werks
AND matnr
= itab
-matnr
GROUP
BY werks matnr
.
" IF sumqty = 0.
" CONCATENATE '物料' itab-matnr '没有库存' INTO mess.
" MESSAGE:mess TYPE 'I'.
" ELSE.
IF sumqty >
0
.
SELECT matnr werks lgort charg clabs
INTO CORRESPONDING
FIELDS
OF
TABLE it_batch
FROM mchb
WHERE
matnr
= itab
-idnrk
AND werks
= itab
-werks
AND clabs >
0
.
SORT it_batch
BY werks matnr charg
ASCENDING
.
DATA
: sysl
LIKE afko
-gamng
, fpsl
LIKE afko
-gamng
, yfpsl
LIKE afko
-gamng
.
sysl
=
0
.
"剩余数量
fpsl
=
0
.
"分配数量
yfpsl
=
0
.
"已分配数量
LOOP
AT it_batch
.
IF yfpsl
=
0
AND itab
-mngko <= it_batch
-clabs
.
"该条记录第一次分配批次库存,第一个批次库存满足要求
fpsl
= itab
-menge
.
yfpsl
= yfpsl + fpsl
.
it_out
-werks
= itab
-werks
.
it_out
-aufnr
= itab
-aufnr
.
it_out
-matnr
= itab
-matnr
.
it_out
-lgort
= itab
-lgort
.
it_out
-maktx
= itab
-maktx
.
it_out
-idnrk
= itab
-idnrk
.
it_out
-mngko
= itab
-mngko
.
it_out
-mmein
= itab
-mmein
.
it_out
-charg
= it_batch
-charg
.
it_out
-menge
= fpsl
.
it_out
-umlgo
= it_batch
-lgort
.
APPEND it_out
.
EXIT
.
ELSEIF yfpsl
=
0
AND itab
-mngko >= it_batch
-clabs
.
"该条记录第一次分配库存,并且第一个批次库存不够需求量
fpsl
= it_batch
-clabs
.
yfpsl
= yfpsl + fpsl
.
it_out
-werks
= itab
-werks
.
it_out
-aufnr
= itab
-aufnr
.
it_out
-matnr
= itab
-matnr
.
it_out
-lgort
= itab
-lgort
.
it_out
-maktx
= itab
-maktx
.
it_out
-idnrk
= itab
-idnrk
.
it_out
-mngko
= itab
-mngko
.
it_out
-mmein
= itab
-mmein
.
it_out
-charg
= it_batch
-charg
.
it_out
-menge
= fpsl
.
it_out
-umlgo
= it_batch
-lgort
.
APPEND it_out
.
sysl
= itab
-mngko
- yfpsl
.
ELSEIF yfpsl <>
0
AND sysl <= it_batch
-clabs
.
"该条记录已经分配批次库存了,在这次批次库存分配中批次库存满足剩余需要的数量
fpsl
= sysl
.
yfpsl
= yfpsl + sysl
.
it_out
-werks
= itab
-werks
.
it_out
-aufnr
= itab
-aufnr
.
it_out
-matnr
= itab
-matnr
.
it_out
-lgort
= itab
-lgort
.
it_out
-maktx
= itab
-maktx
.
it_out
-idnrk
= itab
-idnrk
.
it_out
-mngko
= itab
-mngko
.
it_out
-mmein
= itab
-mmein
.
it_out
-charg
= it_batch
-charg
.
it_out
-menge
= fpsl
.
it_out
-umlgo
= it_batch
-lgort
.
APPEND it_out
.
EXIT
.
ELSE
.
"该条记录已经分配库存了,在这次分配中批次库存不满足需要的剩余数量,需要再次分配
fpsl
= it_batch
-clabs
.
yfpsl
= yfpsl + fpsl
.
it_out
-werks
= itab
-werks
.
it_out
-aufnr
= itab
-aufnr
.
it_out
-matnr
= itab
-matnr
.
it_out
-lgort
= itab
-lgort
.
it_out
-maktx
= itab
-maktx
.
it_out
-idnrk
= itab
-idnrk
.
it_out
-mngko
= itab
-mngko
.
it_out
-mmein
= itab
-mmein
.
it_out
-charg
= it_batch
-charg
.
it_out
-menge
= fpsl
.
it_out
-umlgo
= it_batch
-lgort
.
APPEND it_out
.
sysl
= itab
-mngko
- yfpsl
.
ENDIF
.
ENDLOOP
.
ENDIF
.
ENDLOOP
.
ENDFORM
.
"getdata
*&---------------------------------------------------------------------*
*& Form displaydata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM displaydata
.
PERFORM add_field
USING
'WERKS'
' 工厂'
'C111'
'X'
.
PERFORM add_field
USING
'MATNR'
' 成品物料号'
'C111'
'X'
.
PERFORM add_field
USING
'MAKTX'
' 物料描述'
'C111'
'X'
.
PERFORM add_field
USING
'AUFNR'
' 生产订单号'
'C111'
'X'
.
PERFORM add_field
USING
'LGORT'
' 仓库'
'C111'
'X'
.
PERFORM add_field
USING
'IDNRK'
' 物料组件'
'C111'
'X'
.
PERFORM add_field
USING
'MMEIN'
' 单位'
'C111'
'X'
.
PERFORM add_field
USING
'MNGKO'
' 组件需求数'
'C111'
'X'
.
PERFORM add_field
USING
'CHARG'
' 批次号'
'C111'
'X'
.
PERFORM add_field
USING
'MENGE'
' 分配批次数'
'C111'
'X'
.
PERFORM add_field
USING
'UMLGO'
' 仓位'
'C111'
'X'
.
ENDFORM
.
"displaydata
*&---------------------------------------------------------------------*
*& Form add_field
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->I_FIELDNAME text
* -->I_TEXT text
* -->I_NO text
* -->I_ZERO text
*----------------------------------------------------------------------*
FORM add_field
USING i_fieldname
TYPE slis_fieldname i_text
TYPE string i_no
TYPE
c i_zero
TYPE
c
.
CLEAR wa_alv_field
.
wa_alv_field
-fieldname
= i_fieldname
.
wa_alv_field
-tabname
=
'IT_OUT'
.
wa_alv_field
-ddictxt
=
'L'
.
"wa_alv_field-no_zero = i_zero.
wa_alv_field
-seltext_l
= i_text
.
APPEND wa_alv_field
TO wa_alv_fieldcat
.
ENDFORM
.
"add_field
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM user_command
USING r_ucomm
LIKE sy
-ucomm
rs_selfield
TYPE slis_selfield
.
CASE r_ucomm
.
WHEN
'&IC1'
.
" SAP STANDARD CODE FOR DOUBLE-CLICKING
IF rs_selfield
-sel_tab_field
=
'IT_OUT-MATNR'
.
SET
PARAMETER
ID
'MAT'
FIELD rs_selfield
-
value
.
CALL
TRANSACTION
'CS03'
AND
SKIP
FIRST
SCREEN
.
ENDIF
.
IF rs_selfield
-sel_tab_field
=
'IT_OUT-IDNRK'
.
SET
PARAMETER
ID
'MAT'
FIELD rs_selfield
-
value
.
CALL
TRANSACTION
'MM03'
AND
SKIP
FIRST
SCREEN
.
ENDIF
.
ENDCASE
.
ENDFORM
.
"user_command
*&---------------------------------------------------------------------*
*& Form layout_init
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RS_LAYOUT text
*----------------------------------------------------------------------*
FORM layout_init
CHANGING rs_layout
TYPE slis_layout_alv
.
"自动调整列宽
rs_layout
-colwidth_optimize
=
'X'
.
"rs_layout-box_fieldname = 'SEL'.
"rs_layout-zebra = 'X'.
"rs_layout-detail_popup = 'X'.
ENDFORM
.
"layout_init