FUNCTION zle_pda_available_qty.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_WERKS) TYPE WERKS_D
*" REFERENCE(I_MATNR) TYPE MATNR
*" TABLES
*" O_TAB STRUCTURE ZLES0060 OPTIONAL
*"----------------------------------------------------------------------
CHECK i_werks >< '' AND i_matnr >< ''.
*" 가용재고 추출용......................
DATA: l_atp LIKE bapicm61v-wkbst.
DATA: it_wmdvex TYPE bapiwmdve OCCURS 0 WITH HEADER LINE,
it_wmdvsx TYPE bapiwmdvs OCCURS 0 WITH HEADER LINE.
DATA: l_tabix LIKE sy-tabix.
DATA: g_qty1 TYPE cdseq.
DATA: g_qty2 TYPE cdseq.
CLEAR it_zles0060.
REFRESH it_zles0060.
*" 현재재고 추출.......................
SELECT mard~matnr AS matnr
mard~labst AS stock_qty
makt~maktx AS maktx
mara~meins AS meins
APPENDING CORRESPONDING FIELDS OF TABLE it_zles0060
FROM mard INNER JOIN mara
ON mard~mandt = mara~mandt
AND mard~matnr = mara~matnr
INNER JOIN makt
ON mard~mandt = makt~mandt
AND mard~matnr = makt~matnr
AND makt~spras = sy-langu
WHERE mard~werks = i_werks
AND mard~lgort = '2000'
AND mard~matnr = i_matnr
AND mard~labst > 0.
CHECK NOT it_zles0060[] IS INITIAL.
*" 가용재고 추출.......................
CLEAR : it_wmdvsx, it_wmdvex, l_atp.
REFRESH : it_wmdvsx, it_wmdvex.
LOOP AT it_zles0060.
l_tabix = sy-tabix.
CLEAR l_atp.
CALL FUNCTION 'BAPI_MATERIAL_AVAILABILITY'
EXPORTING
plant = i_werks " 플랜트
material = it_zles0060-matnr " 자재코드
unit = it_zles0060-meins " 단위
check_rule = 'RP'
stge_loc = '2000' " 저장위치
IMPORTING
av_qty_plt = l_atp
TABLES
wmdvsx = it_wmdvsx
wmdvex = it_wmdvex.
IF sy-subrc IS INITIAL.
READ TABLE it_wmdvex INDEX 1.
IF sy-subrc IS INITIAL.
it_zles0060-avail_qty = it_wmdvex-com_qty. " 가용재고
ENDIF.
ENDIF.
MODIFY it_zles0060 INDEX l_tabix.
ENDLOOP.
*" 최종 테이블로 MOVE......................
LOOP AT it_zles0060.
MOVE-CORRESPONDING it_zles0060 TO o_tab.
CLEAR : g_qty1, g_qty2.
o_tab-avail_qty = g_qty1 = it_zles0060-avail_qty.
o_tab-stock_qty = g_qty2 = it_zles0060-stock_qty.
APPEND o_tab.
CLEAR: o_tab, it_zles0060.
ENDLOOP.
FREE it_zles0060.
ENDFUNCTION.