生產入庫增強
CMOD MBCF0002
EXIT EXIT_SAPMM07M_001
INCLUDE ZXMBCU02.
*&---------------------------------------------------------------------*
*& 包含 ZXMBCU02
*&---------------------------------------------------------------------*
DATA:lv_zxh TYPE zppt013-zxh.
DATA:p_zzmjh TYPE zmmt002-zzmjh.
DATA:lv_str1 TYPE zmmt002-zzmjh.
DATA:lv_str2 TYPE zmmt002-zzmjh.
DATA:lv_str3 TYPE zmmt002-zzmjh.
DATA:lv_str4 TYPE zmmt002-zzmjh.
DATA:lv_str5 TYPE zmmt002-zzmjh.
DATA:p_anln1 TYPE anla-anln1.
DATA:auart TYPE auart.
DATA:l_bdmng TYPE bdmng, "需求數
l_enmng TYPE enmng. "提貨數
DATA:kostl LIKE usr21-kostl.
DATA:g_matnr LIKE mara-matnr.
DATA:zyxrk TYPE menge_d.
*data:kostl like usr21-kostl.
DATA: l_sgtxt LIKE mseg-sgtxt.
e_sgtxt = i_mseg-sgtxt.
"控制CO11N不能做計劃外投料
IF sy-tcode = 'CO11N'
AND i_mseg-rsnum IS INITIAL.
MESSAGE e003(zhele) WITH i_mseg-zeile i_mseg-matnr.
ENDIF.
"控制CO11N不能做計劃外投料
"控制素材收貨時必須輸入模具號
CLEAR:auart.
CLEAR kostl.
SELECT SINGLE kostl INTO kostl FROM usr21 WHERE bname = sy-uname.
IF kostl = ''.
SELECT SINGLE auart FROM aufk INTO auart
WHERE aufnr = i_mseg-aufnr.
IF sy-subrc = 0.
IF ( sy-tcode = 'MIGO'
OR sy-tcode = 'MIGO_GO' )
AND i_mseg-bwart = '101'
AND ( auart = 'LX03' OR auart = 'LX21' OR auart = 'LX22' OR auart = 'LX23' ).
SELECT SINGLE zzmjh INTO p_zzmjh FROM zmmt002 WHERE matnr = i_mseg-matnr .
CONDENSE p_zzmjh NO-GAPS.
SPLIT p_zzmjh AT '/' INTO lv_str1 lv_str2 lv_str3 lv_str4 lv_str5.
* lxf start
* IF i_mseg-sgtxt = lv_str1 OR i_mseg-sgtxt = lv_str2 OR i_mseg-sgtxt = lv_str3 OR i_mseg-sgtxt = lv_str4 OR i_mseg-sgtxt = lv_str5.
* SELECT SINGLE anln1 INTO p_anln1 FROM anla WHERE anlkl = '18010000' AND sernr = i_mseg-sgtxt.
* IF p_anln1 = ''.
* MESSAGE '模具編號沒有建立資產編號' TYPE 'E'.
* EXIT.
* ENDIF.
* ELSE .
* MESSAGE '沒有在物料主資料裡維護相應的模具號' TYPE 'E'.
* EXIT.
* ENDIF.
* lxf end
* SELECT sernr INTO TABLE @DATA(gt_anla)
* FROM anla
* WHERE anlkl = '18010000'
* AND sernr <> ''.
* SHIFT i_mseg-sgtxt LEFT DELETING LEADING space.
* SELECT SINGLE zxh INTO lv_zxh FROM zppt013 WHERE matnr = i_mseg-matnr AND zzmjh = i_mseg-sgtxt AND loekz = ''.
* IF lv_zxh = ''.
* MESSAGE '請輸入正確的模具編號' TYPE 'E'.
* EXIT.
* ENDIF.
ENDIF .
* READ TABLE gt_anla INTO DATA(gs_anla)
* WITH KEY sernr = i_mseg-sgtxt.
* IF sy-subrc <> 0.
* MESSAGE '請輸入正確的模具編號' TYPE 'E'.
* EXIT.
* ENDIF.
* ENDIF.
ENDIF.
IF ( sy-tcode = 'MIGO'
OR sy-tcode = 'MIGO_GO' )
AND i_mseg-bwart = '102'
AND ( auart = 'LX03' OR auart = 'LX21' OR auart = 'LX22' OR auart = 'LX23' OR auart = 'LX31' OR auart = 'LX32' OR auart = 'LX33' ).
CLEAR l_sgtxt.
SELECT SINGLE sgtxt INTO l_sgtxt FROM mseg WHERE mblnr = i_mseg-smbln.
IF l_sgtxt IS NOT INITIAL.
i_mseg-sgtxt = l_sgtxt.
ENDIF.
ENDIF.
IF sy-mandt EQ '555' OR sy-mandt EQ '800' . "只有和樂控制
IF ( sy-tcode = 'MIGO'OR sy-tcode = 'MIGO_GO' )
AND i_mseg-bwart = '101'
AND ( auart = 'LX01' OR auart = 'LX02' OR auart = 'LX04' OR auart = 'LX10' OR auart = 'LX11' ).
IF i_mseg-ablad = ''.
MESSAGE '請輸入責任人' TYPE 'E'.
ENDIF.
IF i_mseg-sgtxt = ''.
MESSAGE '請輸入備註文字' TYPE 'E'.
ENDIF.
SHIFT i_mseg-sgtxt LEFT DELETING LEADING space.
IF i_mseg-sgtxt+0(1) = '1' OR i_mseg-sgtxt+0(1) = '2' .
ELSE.
MESSAGE '請輸入備註文字 1 或 2' TYPE 'E'.
ENDIF .
ENDIF.
ENDIF .
ENDIF.
"控制素材收貨時必須輸入模具號
"控制必須先報工再入庫
IF sy-mandt EQ '555' OR sy-mandt EQ '800' . "只有和樂控
IF sy-tcode = 'MIGO' OR sy-tcode = 'MIGO_GO'.
IF i_mseg-bwart EQ '101' AND ( auart = 'LX01' OR auart = 'LX02' OR auart = 'LX03' OR auart = 'LX04'
OR auart = 'LX07' OR auart = 'LX10' OR auart = 'LX11'
OR auart = 'LX21' OR auart = 'LX22' OR auart = 'LX23'
OR auart = 'LX31' OR auart = 'LX32' OR auart = 'LX33'
OR auart = 'LX24' OR auart = 'LX34' ).
SELECT aufnr,SUM( lmnga ) AS lmnga INTO TABLE @DATA(gt_afru) FROM afru
WHERE aufnr = @i_mseg-aufnr
AND stzhl = ''
AND stokz = ''
GROUP BY aufnr.
SORT gt_afru BY aufnr.
SELECT aufnr,wemng INTO TABLE @DATA(gt_afpo) FROM afpo WHERE aufnr = @i_mseg-aufnr GROUP BY aufnr,wemng.
SORT gt_afpo BY aufnr.
READ TABLE gt_afru INTO DATA(gs_afru) WITH KEY aufnr = i_mseg-aufnr.
* IF sy-subrc EQ 0.
READ TABLE gt_afpo INTO DATA(gs_afpo) WITH KEY aufnr = i_mseg-aufnr.
* IF sy-subrc EQ 0.
IF gs_afru-lmnga - gs_afpo-wemng - i_mseg-erfmg < 0 .
MESSAGE '入庫數量大於已報工數量' TYPE 'E'.
EXIT.
ENDIF.
* ENDIF.
* ENDIF.
ENDIF.
ENDIF.
ENDIF .
"ERFMG錄入數量
"控制不允許超領料
IF sy-tcode = 'MIGO' OR sy-tcode = 'MIGO_GI' OR sy-tcode = 'ZMIGO_01' OR sy-tcode = 'ZMIGO_02' .
IF i_mseg-bwart EQ '261' AND i_mseg-rsnum NE ''.
SELECT SINGLE bdmng enmng INTO (l_bdmng,l_enmng) FROM resb WHERE rsnum = i_mseg-rsnum AND rspos = i_mseg-rspos AND rsart = i_mseg-rsart.
l_bdmng = i_mseg-menge - ( l_bdmng - l_enmng ).
IF l_bdmng > 0.
MESSAGE e000(zhele) WITH i_mseg-matnr '超過可領料數量' l_bdmng i_mseg-meins.
ENDIF.
ENDIF.
ENDIF.
"控制不允許超領料
"控制拆卸單副產品入庫數+報廢數不能大於需求數
IF sy-tcode = 'MIGO' OR sy-tcode = 'MIGO_GI' OR sy-tcode = 'ZMIGO_01' OR sy-tcode = 'ZMIGO_02' .
IF i_mseg-bwart = '531' AND ( auart = 'LX05' OR auart = 'LX09' ).
DATA:BEGIN OF gs_aufm,
aufnr LIKE aufm-aufnr,
matnr LIKE aufm-matnr,
menge LIKE aufm-menge,
* shkzg LIKE aufm-shkzg,
END OF gs_aufm.
DATA:gt_aufm LIKE TABLE OF gs_aufm,
lt_aufm LIKE TABLE OF gs_aufm,
ls_aufm LIKE gs_aufm.
DATA:BEGIN OF gs_zmmt003i,
aufnr LIKE zmmt003i-aufnr,
matnr LIKE zmmt003i-matnr,
zbfsl LIKE zmmt003i-zbfsl,
END OF gs_zmmt003i.
DATA:gt_zmmt003i LIKE TABLE OF gs_zmmt003i.
DATA:BEGIN OF ws_resb,
aufnr LIKE resb-aufnr,
matnr LIKE resb-matnr,
bdmng LIKE resb-bdmng,
END OF ws_resb.
DATA wt_resb LIKE TABLE OF ws_resb.
CLEAR:gs_aufm,ls_aufm,gs_zmmt003i,ws_resb.
SELECT aufnr matnr SUM( menge ) AS menge INTO TABLE gt_aufm FROM aufm WHERE aufnr = i_mseg-aufnr AND matnr = i_mseg-matnr AND bwart = '531' GROUP BY aufnr matnr.
SORT gt_aufm BY aufnr matnr.
SELECT aufnr matnr SUM( menge ) AS menge INTO TABLE lt_aufm FROM aufm WHERE aufnr = i_mseg-aufnr AND matnr = i_mseg-matnr AND bwart = '532' GROUP BY aufnr matnr.
SORT lt_aufm BY aufnr matnr.
SELECT aufnr matnr SUM( zbfsl ) AS zbfsl INTO TABLE gt_zmmt003i FROM zmmt003i WHERE aufnr = i_mseg-aufnr AND matnr = i_mseg-matnr AND loekz = '' GROUP BY aufnr matnr.
SORT gt_zmmt003i BY aufnr matnr.
SELECT aufnr matnr bdmng INTO TABLE wt_resb FROM resb WHERE aufnr = i_mseg-aufnr AND matnr = i_mseg-matnr AND xloek = '' GROUP BY aufnr matnr bdmng.
SORT wt_resb BY aufnr matnr.
READ TABLE gt_aufm INTO gs_aufm WITH KEY aufnr = i_mseg-aufnr matnr = i_mseg-matnr BINARY SEARCH.
READ TABLE lt_aufm INTO ls_aufm WITH KEY aufnr = i_mseg-aufnr matnr = i_mseg-matnr BINARY SEARCH.
READ TABLE gt_zmmt003i INTO gs_zmmt003i WITH KEY aufnr = i_mseg-aufnr matnr = i_mseg-matnr BINARY SEARCH.
READ TABLE wt_resb INTO ws_resb WITH KEY aufnr = i_mseg-aufnr matnr = i_mseg-matnr BINARY SEARCH.
IF i_mseg-erfmg + gs_aufm-menge - ls_aufm-menge + gs_zmmt003i-zbfsl - ws_resb-bdmng > 0.
MESSAGE '入庫數與報廢數之和大於需求數' TYPE 'E'.
ENDIF.
CLEAR:gs_aufm,ls_aufm,gs_zmmt003i,ws_resb.
ENDIF.
ENDIF.
"控制拆卸單副產品入庫數+報廢數不能大於需求數
"控制配套入庫
IF sy-mandt EQ '555' OR sy-mandt EQ '800' . "只有和樂控制
IF i_mseg-bwart = '101' AND
( auart = 'LX01'
OR auart = 'LX02'
* OR auart = 'LX03'
OR auart = 'LX04'
OR auart = 'LX07'
OR auart = 'LX10'
OR auart = 'LX11' ).
DATA:lliao LIKE aufm-menge, "對應元件的領料數量
tabix TYPE sy-tabix,
psmng LIKE afpo-psmng,
dwyl LIKE aufm-menge, "單位用量
yxrksl LIKE aufm-menge, "元件允許入庫的數量
wemng LIKE afpo-wemng.
* select aufnr,matnr,sum( bdmng ) AS bdmng,sum( ausch ) AS ausch "需求數/報廢率
* INTO TABLE @DATA(gt_resb)
* FROM resb
* WHERE aufnr = @i_mseg-aufnr
* and bwart = '261'
* AND xloek = ''
* and bdmng <> 0
* GROUP BY aufnr,matnr.
* SORT gt_resb BY aufnr matnr.
* DELETE ADJACENT DUPLICATES FROM gt_resb.
*
* SELECT aufnr,matnr,bwart,SUM( menge ) AS menge "提貨數量
* INTO TABLE @DATA(gt_aufm)
* FROM aufm
* WHERE bwart IN ('261','262')
* AND aufnr = @i_mseg-aufnr
* GROUP BY aufnr,bwart,matnr.
* SORT gt_aufm BY aufnr matnr bwart.
* DELETE ADJACENT DUPLICATES FROM gt_aufm.
*
** SELECT aufnr,matnr,SUM( zbfsl ) AS zbfsl,SUM( zwssl ) AS zwssl "報廢數量、無實物數
** INTO TABLE @DATA(gt_zmmt003i)
** FROM zmmt003i
** WHERE aufnr = @i_mseg-aufnr
** AND loekz = ''
** GROUP BY aufnr,matnr.
** SORT gt_zmmt003i BY aufnr matnr.
** DELETE ADJACENT DUPLICATES FROM gt_zmmt003i.
*
* CLEAR:yxrksl.
*
* READ TABLE gt_resb INTO DATA(wa_resb)
* INDEX 1.
* IF sy-subrc = 0.
* yxrksl = wa_resb-bdmng.
* ENDIF.
** yxrksl = 999999999.
*
* CLEAR:wa_resb.
* LOOP AT gt_resb INTO wa_resb.
*
* READ TABLE gt_aufm INTO DATA(wa_aufm)
* WITH KEY aufnr = wa_resb-aufnr
* matnr = wa_resb-matnr
* bwart = '261'
* BINARY SEARCH.
* IF sy-subrc = 0.
* lliao = lliao + wa_aufm-menge.
* ENDIF.
* CLEAR:wa_aufm.
*
* READ TABLE gt_aufm INTO wa_aufm
* WITH KEY aufnr = wa_resb-aufnr
* matnr = wa_resb-matnr
* bwart = '262'
* BINARY SEARCH.
* IF sy-subrc = 0.
* lliao = lliao - wa_aufm-menge.
* ENDIF.
* CLEAR:wa_aufm.
*
** READ TABLE gt_zmmt003i INTO DATA(wa_zmmt003i)
** WITH KEY aufnr = wa_resb-aufnr
** matnr = wa_resb-matnr
** BINARY SEARCH.
** IF sy-subrc = 0.
** lliao = lliao - wa_zmmt003i-zbfsl - wa_zmmt003i-zwssl.
** ENDIF.
*
* CLEAR:psmng.
* SELECT SINGLE psmng "訂單數
* INTO psmng
* FROM afpo
* WHERE aufnr = wa_resb-aufnr.
*
* IF psmng <> 0
* AND wa_resb-bdmng <> 0
* AND ( 1 + wa_resb-ausch ) <> 0.
* CLEAR dwyl.
* dwyl = ( lliao / wa_resb-bdmng ) * psmng." / ( 1 + ( wa_resb-ausch / 100 ) ).
* ENDIF.
*
* IF dwyl <> 0.
* IF dwyl < yxrksl.
* yxrksl = ceil( dwyl ).
* g_matnr = wa_resb-matnr.
* ENDIF.
* ENDIF.
*
* CLEAR:lliao,dwyl.
*
* ENDLOOP.
*
* SELECT SINGLE wemng
* INTO wemng
* FROM afpo
* WHERE aufnr = i_mseg-aufnr.
* CLEAR kostl.
* SELECT SINGLE kostl INTO kostl FROM usr21 WHERE bname = sy-uname.
* IF kostl = ''.
* IF ( wemng + i_mseg-menge ) > yxrksl.
* lliao = wemng + i_mseg-menge - yxrksl.
* MESSAGE '訂單收貨數量大於允許入庫的的數量' && g_matnr && lliao && i_mseg-meins TYPE 'E'.
* ENDIF.
* ENDIF.
* CLEAR g_matnr.
*ENDIF.
"控制配套入庫
*
SELECT resb~aufnr,resb~matnr, resb~bdmng ,resb~enmng,resb~ausch "需求數量/提貨數量
INTO TABLE @DATA(gt_resb)
FROM resb INNER JOIN mara ON resb~matnr = mara~matnr
WHERE resb~aufnr = @i_mseg-aufnr
AND resb~bwart = '261'
AND resb~xloek = ''
AND resb~bdmng <> 0
AND mara~mtart <> 'Z011'
AND postp = 'L'
GROUP BY resb~aufnr,resb~matnr, resb~bdmng ,resb~enmng,resb~ausch.
SORT gt_resb BY aufnr matnr.
DELETE ADJACENT DUPLICATES FROM gt_resb.
CLEAR:yxrksl.
yxrksl = 99999999.
LOOP AT gt_resb INTO DATA(wa_resb).
lliao = wa_resb-enmng.
IF lliao = 0.
yxrksl = 0.
g_matnr = wa_resb-matnr.
EXIT.
ENDIF.
SELECT SINGLE psmng "訂單數
INTO psmng
FROM afpo
WHERE aufnr = wa_resb-aufnr.
IF psmng <> 0
AND wa_resb-bdmng <> 0.
CLEAR dwyl.
dwyl = ( lliao / wa_resb-bdmng ) * psmng.
ENDIF.
IF dwyl <> 0.
IF dwyl < yxrksl.
yxrksl = ceil( dwyl ).
g_matnr = wa_resb-matnr.
ENDIF.
ELSE.
yxrksl = 0.
g_matnr = wa_resb-matnr.
EXIT.
ENDIF.
CLEAR:lliao,dwyl.
ENDLOOP.
SELECT SINGLE wemng
INTO wemng
FROM afpo
WHERE aufnr = i_mseg-aufnr.
CLEAR kostl.
SELECT SINGLE kostl INTO kostl FROM usr21 WHERE bname = sy-uname.
IF kostl = ''.
IF ( wemng + i_mseg-menge ) > yxrksl.
lliao = wemng + i_mseg-menge - yxrksl.
MESSAGE '訂單收貨數量大於允許入庫的的數量' && g_matnr && lliao && i_mseg-meins TYPE 'E'.
ENDIF.
ENDIF.
CLEAR g_matnr.
ENDIF.
ENDIF .
*
*"控制委外訂單配套入庫
*IF ( sy-tcode = 'MIGO' OR sy-tcode = 'MIGO_GR' )
* AND i_mseg-bwart = '101'
* AND i_mseg-ebeln+0(2) = '42'.
*
* DATA:llsl_01 LIKE aufm-menge, "對應元件的領料數量
* llsl_02 LIKE aufm-menge,
* zllsl LIKE aufm-menge,
** tab TYPE sy-tabix,
* menge LIKE ekpo-menge,
* menge_01 LIKE ekpo-menge,
* menge_02 LIKE ekpo-menge,
* zdwyl LIKE mseg-menge, "單位用量
* yxrks LIKE ekpo-menge. "元件允許入庫的數量
*
* DATA:BEGIN OF ls_resb,
* ebeln LIKE resb-ebeln,
* ebelp LIKE resb-ebelp,
* matnr LIKE resb-matnr,
* bdmng LIKE resb-bdmng,
* enmng LIKE resb-enmng,
* ausch LIKE resb-ausch,
* END OF ls_resb.
*
* DATA:lt_resb LIKE TABLE OF ls_resb.
*
* SELECT ebeln ebelp matnr bdmng enmng ausch "需求數量/提貨數量
* INTO TABLE lt_resb
* FROM resb "INNER JOIN mara ON resb~matnr = mara~matnr
* WHERE ebeln = i_mseg-ebeln
* AND ebelp = i_mseg-ebelp
* AND xloek = ''
* AND bdmng <> 0.
*
* CLEAR:yxrks, g_matnr.
* yxrks = 99999999.
* LOOP AT lt_resb INTO ls_resb.
* CLEAR:menge,zllsl,llsl_01,llsl_02.
* SELECT SINGLE SUM( menge ) AS menge INTO llsl_01 FROM mseg
* WHERE wempf = i_mseg-ebeln
* AND ablad = i_mseg-ebelp
* AND matnr = ls_resb-matnr
* AND bwart = '541'
* AND shkzg = 'H'.
*
* SELECT SINGLE SUM( menge ) AS menge INTO llsl_02 FROM mseg
* WHERE ebeln = i_mseg-ebeln
* AND wempf = i_mseg-ebelp
* AND ablad = ls_resb-matnr
* AND bwart = '542'
* AND shkzg = 'H'.
*
* zllsl = llsl_01 - llsl_02.
*
* IF zllsl = 0.
* yxrksl = 0.
* g_matnr = ls_resb-matnr.
* EXIT.
* ENDIF.
*
* SELECT SINGLE menge "訂單數
* INTO menge
* FROM ekpo
* WHERE ebeln = ls_resb-ebeln AND ebelp = ls_resb-ebelp.
*
* IF menge <> 0
* AND ls_resb-bdmng <> 0.
* CLEAR zdwyl.
* zdwyl = ( zllsl / ls_resb-bdmng ) * menge.
* ENDIF.
*
* IF zdwyl <> 0.
* IF zdwyl < yxrks.
* yxrks = ceil( zdwyl ).
* g_matnr = ls_resb-matnr.
* ENDIF.
* ELSE.
* yxrks = 0.
* g_matnr = ls_resb-matnr.
* EXIT.
* ENDIF.
*
* CLEAR:zllsl,zdwyl,menge.
*
* ENDLOOP.
*
* SELECT SINGLE SUM( menge ) AS menge
* INTO menge_01
* FROM mseg
* WHERE ebeln = i_mseg-ebeln
* AND ebelp = i_mseg-ebelp
* AND bwart = '101'
* AND shkzg = 'S'.
*
* SELECT SINGLE SUM( menge ) AS menge
* INTO menge_02
* FROM mseg
* WHERE ebeln = i_mseg-ebeln
* AND ebelp = i_mseg-ebelp
* AND bwart = '102'
* AND shkzg = 'H'.
*
* CLEAR kostl.
* SELECT SINGLE kostl INTO kostl FROM usr21 WHERE bname = sy-uname.
* IF kostl = ''.
* IF ( menge_01 - menge_02 + i_mseg-menge ) > yxrksl.
* zllsl = menge_01 - menge_02 + i_mseg-menge - yxrks.
* MESSAGE '收貨數量大於允許入庫的的數量' && g_matnr && zllsl && i_mseg-meins TYPE 'E'.
* ENDIF.
* ENDIF.
* CLEAR g_matnr.
*ENDIF.