Change the Delivery qty during the creation of the Delivery.
Program : MV50AFZ1
FORM userexit_save_document_prepare.
…….
…..
...
DATA: l_so_stock LIKE bapicm61v-wkbst.
DATA:BEGIN OF it_vbfa OCCURS 0,
vbelv LIKE vbfa-vbelv,
posnv LIKE vbfa-posnv,
vbeln LIKE vbfa-vbeln,
posnn LIKE vbfa-posnn,
END OF it_vbfa.
DATA: BEGIN OF it_vbuk OCCURS 0,
vbeln LIKE vbfa-vbeln,
END OF it_vbuk.
DATA: BEGIN OF it_lips OCCURS 0,
vbeln LIKE lips-vbeln,
posnr LIKE lips-posnr,
lfimg LIKE lips-lfimg,
END OF it_lips.
DATA: lv_subrc LIKE sy-subrc,
l_index LIKE sy-subrc.
DATA: yxwmdve LIKE bapiwmdve OCCURS 5 WITH HEADER LINE,
yxwmdvs LIKE bapiwmdvs OCCURS 5 WITH HEADER LINE.
DATA atp_qty LIKE yxwmdve-com_qty.
*Check the sales Order stock
IF likp-lfart = 'ZZLF' AND likp-vkorg = 'ZAPP'
AND t180-trtyp = 'H'.
LOOP AT xlips WHERE pstyv = 'ZZ02'.
l_index = sy-tabix.
CLEAR : atp_qty, l_so_stock.
REFRESH :yxwmdvs[],yxwmdve[].
yxwmdvs-req_qty = xlips-lfimg.
APPEND yxwmdvs.
*Check the sales order stock for that item.
CALL FUNCTION 'BAPI_MATERIAL_AVAILABILITY'
EXPORTING
plant = xlips-werks
stge_loc = xlips-lgort
material = xlips-matnr
doc_number = xlips-vgbel " Sales Order
itm_number = xlips-vgpos " Sales Item
unit = xlips-meins
stock_ind = 'E' " Sales Order Stock
check_rule = 'BE' " Mile stone orders
IMPORTING
av_qty_plt = l_so_stock
TABLES
wmdvsx = yxwmdvs
wmdvex = yxwmdve.
IF sy-subrc = 0 AND l_so_stock GT 0 .
atp_qty = l_so_stock.
* get all the Delivery Orders for this Item.
REFRESH: it_vbfa[],it_vbuk[],it_lips[].
* Compute the remaining ATP Qty.If there is no PGi for a Delv , that qty will still be shown inn
* the Function module. so reduce that qty from the atp qty.
* get all delivery for the selected orders.
SELECT vbelv posnv vbeln posnn INTO TABLE it_vbfa FROM vbfa
WHERE vbelv = xlips-vgbel AND posnv = xlips-vgpos
AND vbtyp_n = 'J' AND vbtyp_v = 'C'.
*Check the status of the Goods Movement (PGI)
IF NOT it_vbfa[] IS INITIAL.
SELECT vbeln FROM vbuk INTO TABLE it_vbuk
FOR ALL ENTRIES IN it_vbfa
WHERE vbeln = it_vbfa-vbeln AND
wbstk NE 'C'.
ENDIF.
* Collect deliveries which doesnt have PGI Doc.
LOOP AT it_vbfa.
READ TABLE it_vbuk WITH KEY vbeln = it_vbfa-vbeln.
IF sy-subrc NE 0.
DELETE it_vbfa WHERE vbeln = it_vbfa-vbeln .
ENDIF.
ENDLOOP.
*Get the Delv Qty
IF NOT it_vbfa[] IS INITIAL.
SELECT vbeln posnr lfimg INTO TABLE it_lips FROM lips
FOR ALL ENTRIES IN it_vbfa
WHERE vbeln = it_vbfa-vbeln AND
posnr = it_vbfa-posnn.
ENDIF.
* Now ATP qty = ATP QTY - Delv Qty(with NO PGI).
LOOP AT it_lips.
atp_qty = atp_qty - it_lips-lfimg.
ENDLOOP.
* If the Delivery Qty is greater than the ATP qty
* then change the Delivery qty to Available QTY.
IF xlips-lfimg GT atp_qty
OR xlips-lfimg IS INITIAL AND atp_qty GT 0.
MOVE atp_qty TO xlips-lgmng.
MOVE atp_qty TO xlips-lfimg.
MOVE atp_qty TO xlips-lgmng_flo.
MOVE atp_qty TO xlips-lfimg_flo.
MODIFY xlips INDEX l_index .
ENDIF.
ENDIF.
ENDLOOP." xlips.
ENDFORM. "USEREXIT_SAVE_DOCUMENT_PREPARE
DATA:BEGIN OF it_vbfa OCCURS 0,
vbelv LIKE vbfa-vbelv,
posnv LIKE vbfa-posnv,
vbeln LIKE vbfa-vbeln,
posnn LIKE vbfa-posnn,
END OF it_vbfa.
DATA: BEGIN OF it_vbuk OCCURS 0,
vbeln LIKE vbfa-vbeln,
END OF it_vbuk.
DATA: BEGIN OF it_lips OCCURS 0,
vbeln LIKE lips-vbeln,
posnr LIKE lips-posnr,
lfimg LIKE lips-lfimg,
END OF it_lips.
DATA: lv_subrc LIKE sy-subrc,
l_index LIKE sy-subrc.
DATA: yxwmdve LIKE bapiwmdve OCCURS 5 WITH HEADER LINE,
yxwmdvs LIKE bapiwmdvs OCCURS 5 WITH HEADER LINE.
DATA atp_qty LIKE yxwmdve-com_qty.
*Check the sales Order stock
IF likp-lfart = 'ZZLF' AND likp-vkorg = 'ZAPP'
AND t180-trtyp = 'H'.
LOOP AT xlips WHERE pstyv = 'ZZ02'.
l_index = sy-tabix.
CLEAR : atp_qty, l_so_stock.
REFRESH :yxwmdvs[],yxwmdve[].
yxwmdvs-req_qty = xlips-lfimg.
APPEND yxwmdvs.
*Check the sales order stock for that item.
CALL FUNCTION 'BAPI_MATERIAL_AVAILABILITY'
EXPORTING
plant = xlips-werks
stge_loc = xlips-lgort
material = xlips-matnr
doc_number = xlips-vgbel " Sales Order
itm_number = xlips-vgpos " Sales Item
unit = xlips-meins
stock_ind = 'E' " Sales Order Stock
check_rule = 'BE' " Mile stone orders
IMPORTING
av_qty_plt = l_so_stock
TABLES
wmdvsx = yxwmdvs
wmdvex = yxwmdve.
IF sy-subrc = 0 AND l_so_stock GT 0 .
atp_qty = l_so_stock.
* get all the Delivery Orders for this Item.
REFRESH: it_vbfa[],it_vbuk[],it_lips[].
* Compute the remaining ATP Qty.If there is no PGi for a Delv , that qty will still be shown inn
* the Function module. so reduce that qty from the atp qty.
* get all delivery for the selected orders.
SELECT vbelv posnv vbeln posnn INTO TABLE it_vbfa FROM vbfa
WHERE vbelv = xlips-vgbel AND posnv = xlips-vgpos
AND vbtyp_n = 'J' AND vbtyp_v = 'C'.
*Check the status of the Goods Movement (PGI)
IF NOT it_vbfa[] IS INITIAL.
SELECT vbeln FROM vbuk INTO TABLE it_vbuk
FOR ALL ENTRIES IN it_vbfa
WHERE vbeln = it_vbfa-vbeln AND
wbstk NE 'C'.
ENDIF.
* Collect deliveries which doesnt have PGI Doc.
LOOP AT it_vbfa.
READ TABLE it_vbuk WITH KEY vbeln = it_vbfa-vbeln.
IF sy-subrc NE 0.
DELETE it_vbfa WHERE vbeln = it_vbfa-vbeln .
ENDIF.
ENDLOOP.
*Get the Delv Qty
IF NOT it_vbfa[] IS INITIAL.
SELECT vbeln posnr lfimg INTO TABLE it_lips FROM lips
FOR ALL ENTRIES IN it_vbfa
WHERE vbeln = it_vbfa-vbeln AND
posnr = it_vbfa-posnn.
ENDIF.
* Now ATP qty = ATP QTY - Delv Qty(with NO PGI).
LOOP AT it_lips.
atp_qty = atp_qty - it_lips-lfimg.
ENDLOOP.
* If the Delivery Qty is greater than the ATP qty
* then change the Delivery qty to Available QTY.
IF xlips-lfimg GT atp_qty
OR xlips-lfimg IS INITIAL AND atp_qty GT 0.
MOVE atp_qty TO xlips-lgmng.
MOVE atp_qty TO xlips-lfimg.
MOVE atp_qty TO xlips-lgmng_flo.
MOVE atp_qty TO xlips-lfimg_flo.
MODIFY xlips INDEX l_index .
ENDIF.
ENDIF.
ENDLOOP." xlips.
ENDFORM. "USEREXIT_SAVE_DOCUMENT_PREPARE
No comments:
Post a Comment