FUNCTION Z_GET_PERIOD_WSTOCK_3.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(MATNR) LIKE MSKU-MATNR "物料编号
*" REFERENCE(WERKS) LIKE MSKU-WERKS "工厂
*" REFERENCE(KUNNR) LIKE MSKU-KUNNR "购货方编号
*" REFERENCE(LFGJA) LIKE MSKU-LFGJA "会计年度
*" REFERENCE(LFMON) LIKE MSKU-LFMON "会计期间
*" EXPORTING
*" REFERENCE(CURR_STOCK) LIKE MSKU-KULAB "期末库存
*"----------------------------------------------------------------------
data: cyear(4),cmonth(2),currmonth(6),mskUmonth(6).
data: h_tab like mskUH occurs 0 with header line.
TABLES: MSKU.
cyear = lfgja. cmonth = lfmon.
concatenate cyear cmonth into currmonth.
*----------------------------------------------------------------
select single * INTO MSKU from MSKU where KUNNR = KUNNR AND matnr =
matnr and werks = werks.
if sy-subrc = 0.
cyear = mskU-lfgja. cmonth = mskU-lfmon.
concatenate cyear cmonth into mskUmonth.
if mskUmonth > currmonth.
*-----本期期末库存已经存入MSKUH
select * into table h_tab
from mskUh where KUNNR = KUNNR
AND matnr = matnr
and werks = werks
and ( ( lfgja = lfgja and
lfmon >= lfmon ) or lfgja > lfgja )
order by lfgja ascending lfmon ascending .
if sy-subrc = 0.
loop at h_tab.
cyear = h_tab-lfgja. cmonth = h_tab-lfmon.
concatenate cyear cmonth into mskUmonth.
if mskUmonth >= currmonth.
CURR_STOCK = h_tab-KULAB + h_tab-KUINS.
exit.
endif.
endloop.
endif.
else.
*-----本期期末库存还未存入MSKUH
CURR_STOCK = mskU-KULAB + mskU-KUINS.
endif.
endif.
ENDFUNCTION.