需求:SAP在没有采用批次的情况下,取寄售库龄信息
前提条件:遵循先进先出逻辑
报表效果如下图:
程序设计:
1、 定义后台作业,每天从供应商特殊库存表mkol中取数据,并保存到表ztmm_mkol表中,
因为,物料在期间内没有货物移动的情况下,供应商特殊库存历史表mkolh是不更新的,
为了保证能取到历史期间的库存历史,所以定义后台作业并保存数据到ztmm_mkol
2、 查询ztmm_mkol
3、 查询mkpf,mseg里101移动类型的数据,并根据凭证过账日期,计算出每条物料凭证所属的库龄
4、 Mseg得出的库龄数据小于等于ztmm_mkol里的历史库存时,将mseg数量作为第N个月的库龄库存,否则,将ztmm_mkol里的历史库存作为第N个月的库龄库存
逻辑代码:
perform frm_get_data.
perform frm_deal_data.
form frm_get_data .
data:
l_budat_b type budat,
l_budat_e type budat,
l_budat_e1 type budat.
data:
lt_mat like table of lt_mkolh with header line.
* l_budat_e = p_lfgja && p_lfmon && '01'.
concatenate p_lfgja p_lfmon '01' into l_budat_e1.
call function 'LAST_DAY_OF_MONTHS'
exporting
day_in = l_budat_e1 "输入日期
importing
last_day_of_month = l_budat_e "返回最后一天日期:20160131
exceptions
day_in_no_date = 1
others = 2.
"l_budat_e = l_budat_e - 1.
call function 'CCM_GO_BACK_MONTHS'
exporting
currdate = l_budat_e "输入日期
backmonths = 36 "过去月数
importing
newdate = l_budat_b. "返回日期
select
matnr
werks
* lgort
lifnr
lfgja
lfmon
sum( slabs ) as slabs
into corresponding fields of table lt_mkolh
from ztmm_mkol_n
where werks in s_werks
and matnr in s_matnr
and lifnr in s_lifnr
and lfgja = p_lfgja
and lfmon = p_lfmon
and sobkz = 'K'
group by matnr werks lifnr lfgja lfmon.
if lt_mkolh[] is not initial.
select
b~mblnr
b~mjahr
b~zeile
b~matnr
b~werks
* b~bwart
* b~sobkz
b~lifnr
b~menge
a~budat
into corresponding fields of table lt_mseg
from mkpf as a
inner join mseg as b on a~mblnr = b~mblnr and a~mjahr = b~mjahr
where b~werks in s_werks
and b~matnr in s_matnr
and b~lifnr in s_lifnr
and a~budat >= l_budat_b
and a~budat <= l_budat_e
and b~sobkz = 'K'
and b~bwart = '101'.
endif.
loop at lt_mseg.
call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
exporting
i_date_from = lt_mseg-budat
i_date_to = l_budat_e
importing
e_months = lt_mseg-month_diff.
if lt_mseg-month_diff = 0.
lt_mseg-month_diff = 1.
endif.
modify lt_mseg transporting month_diff.
move-corresponding lt_mseg to lt_mseg_t.
collect lt_mseg_t.
endloop.
lt_mat[] = lt_mkolh[].
sort lt_mat by matnr werks.
delete adjacent duplicates from lt_mat comparing matnr werks.
if lt_mat[] is not initial.
select
matnr
bwkey
kalnr
into corresponding fields of table lt_ckmlhd
from ckmlhd
for all entries in lt_mat[]
where matnr = lt_mat-matnr
and bwkey = lt_mat-werks.
endif.
sort lt_ckmlhd by kalnr.
if lt_ckmlhd[] is not initial.
select
kalnr
bdatj
poper
peinh
vprsv
stprs
pvprs
into corresponding fields of table lt_ckmlcr
from ckmlcr
for all entries in lt_ckmlhd[]
where kalnr = lt_ckmlhd-kalnr
and bdatj = p_lfgja
and poper = p_lfmon.
endif.
sort lt_ckmlcr by kalnr.
endform. " frm_get_data
form frm_deal_data .
data l_menge like mseg-menge.
data l_slabs like mkolh-slabs. "库存数量
data l_price type ck_stprs_1.
data l_month_diff type i.
data l_exit type c.
data ls_ckmlcr like line of lt_ckmlcr.
sort lt_mseg_t by matnr werks lifnr month_diff.
* sort lt_mseg_t by month_diff.
sort lt_ckmlhd by matnr bwkey.
sort lt_ckmlcr by kalnr.
loop at lt_mkolh.
clear: l_menge,l_slabs,l_price,l_exit.
clear: lt_ckmlhd ,lt_ckmlcr,ls_ckmlcr.
select single name1 into lt_mkolh-lifnr_name
from lfa1
where lifnr = lt_mkolh-lifnr.
select single maktx into lt_mkolh-maktx
from makt
where matnr = lt_mkolh-matnr and spras = '1'.
read table lt_ckmlhd with key matnr = lt_mkolh-matnr bwkey = lt_mkolh-werks binary search.
if sy-subrc = 0.
read table lt_ckmlcr into ls_ckmlcr with key kalnr = lt_ckmlhd-kalnr binary search.
if sy-subrc = 0.
else.
select single
kalnr
bdatj
poper
peinh
vprsv
stprs
pvprs
into corresponding fields of ls_ckmlcr
from ckmlcr
where kalnr = lt_ckmlhd-kalnr.
endif.
case ls_ckmlcr-vprsv.
when 'S'.
l_price = ls_ckmlcr-stprs / ls_ckmlcr-peinh.
when 'V'.
l_price = ls_ckmlcr-pvprs / ls_ckmlcr-peinh.
endcase.
lt_mkolh-vprsv = ls_ckmlcr-vprsv. "价格控制
endif.
loop at lt_mseg_t where matnr = lt_mkolh-matnr and werks = lt_mkolh-werks and lifnr = lt_mkolh-lifnr.
clear l_month_diff.
l_month_diff = lt_mseg_t-month_diff.
if l_month_diff = 1 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs1 = lt_mseg_t-menge.
else.
lt_mkolh-slabs1 = lt_mkolh-slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs1.
modify lt_mkolh transporting slabs1 .
endif.
if l_month_diff = 2 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs2 = lt_mseg_t-menge.
else.
lt_mkolh-slabs2 = lt_mkolh-slabs - l_slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs2.
modify lt_mkolh transporting slabs2 .
endif.
if l_month_diff = 3 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs3 = lt_mseg_t-menge.
else.
lt_mkolh-slabs3 = lt_mkolh-slabs - l_slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs3.
modify lt_mkolh transporting slabs3 .
endif.
if l_month_diff = 4 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs4 = lt_mseg_t-menge.
else.
lt_mkolh-slabs4 = lt_mkolh-slabs - l_slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs4.
modify lt_mkolh transporting slabs4.
endif.
if l_month_diff = 5 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs5 = lt_mseg_t-menge.
else.
lt_mkolh-slabs5 = lt_mkolh-slabs - l_slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs5.
modify lt_mkolh transporting slabs5.
endif.
if l_month_diff = 6 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs6 = lt_mseg_t-menge.
else.
lt_mkolh-slabs6 = lt_mkolh-slabs - l_slabs.
l_exit = 'X'.
endif.
l_slabs = l_slabs + lt_mkolh-slabs6.
modify lt_mkolh transporting slabs6.
endif.
if l_month_diff >= 7 and l_month_diff <= 12 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs7 = lt_mkolh-slabs7 + lt_mseg_t-menge.
l_slabs = l_slabs + lt_mseg_t-menge.
else.
lt_mkolh-slabs7 = lt_mkolh-slabs - lt_mkolh-slabs1 - lt_mkolh-slabs2 - lt_mkolh-slabs3 -
lt_mkolh-slabs4 - lt_mkolh-slabs5 - lt_mkolh-slabs6.
l_exit = 'X'.
endif.
modify lt_mkolh transporting slabs7.
endif.
if l_month_diff >= 13 and l_month_diff <= 24 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs8 = lt_mkolh-slabs8 + lt_mseg_t-menge.
l_slabs = l_slabs + lt_mseg_t-menge.
else.
lt_mkolh-slabs8 = lt_mkolh-slabs - lt_mkolh-slabs1 - lt_mkolh-slabs2 - lt_mkolh-slabs3 -
lt_mkolh-slabs4 - lt_mkolh-slabs5 - lt_mkolh-slabs6 - lt_mkolh-slabs7.
l_exit = 'X'.
endif.
modify lt_mkolh transporting slabs8 .
endif.
if l_month_diff >= 25 and l_month_diff <= 36 and l_exit ne 'X'.
l_menge = l_menge + lt_mseg_t-menge.
if l_menge <= lt_mkolh-slabs.
lt_mkolh-slabs9 = lt_mkolh-slabs9 + lt_mseg_t-menge.
l_slabs = l_slabs + lt_mseg_t-menge.
else.
lt_mkolh-slabs9 = lt_mkolh-slabs - lt_mkolh-slabs1 - lt_mkolh-slabs2 - lt_mkolh-slabs3 -
lt_mkolh-slabs4 - lt_mkolh-slabs5 - lt_mkolh-slabs6 - lt_mkolh-slabs7 - lt_mkolh-slabs8.
l_exit = 'X'.
endif.
modify lt_mkolh transporting slabs9.
endif.
if l_exit = 'X'.
exit.
endif.
endloop.
lt_mkolh-slabs10 = lt_mkolh-slabs - lt_mkolh-slabs1 - lt_mkolh-slabs2 - lt_mkolh-slabs3 - lt_mkolh-slabs4
- lt_mkolh-slabs5 - lt_mkolh-slabs6 - lt_mkolh-slabs7 - lt_mkolh-slabs8 - lt_mkolh-slabs9.
if lt_mkolh-slabs10 < 0.
lt_mkolh-slabs10 = 0.
endif.
if lt_mkolh-slabs1 > 0.
lt_mkolh-zprice1 = lt_mkolh-slabs1 * l_price.
endif.
if lt_mkolh-slabs2 > 0.
lt_mkolh-zprice2 = lt_mkolh-slabs2 * l_price.
endif.
if lt_mkolh-slabs3 > 0.
lt_mkolh-zprice3 = lt_mkolh-slabs3 * l_price.
endif.
if lt_mkolh-slabs4 > 0.
lt_mkolh-zprice4 = lt_mkolh-slabs4 * l_price.
endif.
if lt_mkolh-slabs5 > 0.
lt_mkolh-zprice5 = lt_mkolh-slabs5 * l_price.
endif.
if lt_mkolh-slabs6 > 0.
lt_mkolh-zprice6 = lt_mkolh-slabs6 * l_price.
endif.
if lt_mkolh-slabs7 > 0.
lt_mkolh-zprice7 = lt_mkolh-slabs7 * l_price.
endif.
if lt_mkolh-slabs8 > 0.
lt_mkolh-zprice8 = lt_mkolh-slabs8 * l_price.
endif.
if lt_mkolh-slabs9 > 0.
lt_mkolh-zprice9 = lt_mkolh-slabs9 * l_price.
endif.
if lt_mkolh-slabs10 > 0.
lt_mkolh-zprice10 = lt_mkolh-slabs10 * l_price.
endif.
lt_mkolh-zprice = lt_mkolh-slabs * l_price.
call function 'CONVERSION_EXIT_MATN1_OUTPUT'
exporting
input = lt_mkolh-matnr
importing
output = lt_mkolh-matnr.
modify lt_mkolh transporting matnr slabs10 zprice zprice1 zprice2 zprice3 zprice4 zprice5 zprice6 zprice7 zprice8 zprice9 zprice10 lifnr_name maktx vprsv.
endloop.
endform. " frm_deal_data