Bestellbestände lesen - Summe aller offenen Bestellmengen zu einem Material
Anwendung
Mit Transaktionscode MMBE können Bestellbestände für ein Material auf Werksebene abgelesen werden.
Entwicklung
Mit Funktionsbausteine
Man kann FuBa "MM_GET_PURCHASE_ORDER_QUANTITY" nutzen, um für jede Bestellposition die Bestellbestände in jede relevante Bestellung zu bekommen. Auch kann FuBa "MATERIAL_UNIT_CONVERSION" genutzt werden, um die Bestellmenge in die Basismengeneinheit zu konvertieren, um eine gesamtheitliche Summe für ein Material z.B. auf Werksebene zu bilden.
_get_po_header
private instance method _get_po_header importing iv_purchaseorder type vdm_purchaseorder iv_supplier type md_supplier iv_purorg type ekorg returning value(rv_po_header) type ekko
CLEAR rv_po_header.
READ TABLE _mt_po_head_buffer ASSIGNING FIELD-SYMBOL(<ls_po_head>)
"WITH KEY purchaseorder = iv_purchaseorder.
WITH KEY ebeln = iv_purchaseorder.
IF sy-subrc = 0.
rv_po_header = <ls_po_head>.
ELSE.
SELECT SINGLE * FROM ekko INTO @DATA(ls_po_header)
WHERE ebeln = @iv_purchaseorder
AND lifnr = @iv_supplier
AND ekorg = @iv_purorg.
IF sy-subrc = 0.
rv_po_header = ls_po_header.
APPEND ls_po_header TO _mt_po_head_buffer.
SORT _mt_po_head_buffer BY ebeln.
ELSE.
CLEAR rv_po_header. "not needed of course, just for clarity
ENDIF.
ENDIF.
_get_pur_qty
DATA: lt_purchaseorders TYPE TABLE OF ekko. "i_purchaseorderapi01.
DATA: lv_sum_pur_qty TYPE i_materialstock_2-matlwrhsstkqtyinmatlbaseunit.
LOOP AT _mt_articles ASSIGNING FIELD-SYMBOL(<ls_article>).
SELECT * FROM ekpo INTO TABLE @DATA(lt_ekpo)
WHERE matnr = @<ls_article>-article
AND werks = @<ls_article>-plant
AND bstyp = 'F'
AND loekz EQ @space
AND elikz EQ @space
AND wepos NE @space
AND retpo EQ @space.
IF sy-subrc <> 0.
"Maybe some protocol action here ;-)
CONTINUE.
ENDIF.
SORT lt_ekpo BY ebeln ebelp.
* get head entries for purchase orders from db or buffer
DATA: ls_po_item TYPE ekpo.
LOOP AT lt_ekpo INTO ls_po_item.
AT NEW ebeln.
DATA(ls_po_header) = _get_po_header( iv_purchaseorder = ls_po_item-ebeln "ls_po_item-purchaseorder
iv_supplier = <ls_article>-supplier
iv_purorg = <ls_article>-purchasingorganization ).
IF ls_po_header IS NOT INITIAL.
APPEND ls_po_header TO lt_purchaseorders.
ELSE.
DELETE lt_ekpo WHERE ebeln = ls_po_item-ebeln.
ENDIF.
ENDAT.
ENDLOOP.
* determine open quantity for each position in each purchase order
DATA: lt_ekko TYPE TABLE OF ekko.
DATA lt_positions TYPE TABLE OF purchwere.
LOOP AT lt_purchaseorders ASSIGNING FIELD-SYMBOL(<ls_po>).
REFRESH lt_ekko.
APPEND <ls_po> TO lt_ekko.
CALL FUNCTION 'MM_GET_PURCHASE_ORDER_QUANTITY'
EXPORTING
i_ebeln = <ls_po>-ebeln "<ls_po>-purchaseorder
TABLES
t_purchase_orders = lt_positions
t_ekko = lt_ekko
t_ekpo = lt_ekpo
EXCEPTIONS
invalid_purchase_order = 1
invalid_purchase_order_pos = 2
OTHERS = 3.
CHECK sy-subrc = 0.
LOOP AT lt_positions INTO DATA(ls_position).
IF ls_position-meins NE <ls_article>-baseunit.
* convert to base unit of measure
CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
EXPORTING
input = ls_position-owemg
kzmeinh = 'X'
matnr = <ls_article>-article
meinh = ls_position-meins
IMPORTING
meins = ls_position-meins
output = ls_position-owemg
EXCEPTIONS
conversion_not_found = 01
input_invalid = 02
material_not_found = 03
meinh_not_found = 04
meins_missing = 05
no_meinh = 06
output_invalid = 07
overflow = 08.
IF sy-subrc <> 0.
"do something which make sense in your case
ENDIF.
ENDIF.
lv_sum_pur_qty = lv_sum_pur_qty + ls_position-owemg.
ENDLOOP.
CLEAR lt_positions.
ENDLOOP.
CLEAR lv_sum_pur_qty, lt_purchaseorders.
ENDLOOP.
Dieses Wiki wird privat in Freizeit nach bestem Wissen und Gewissen gepflegt und dient primär als eigene Wissenssammlung für die tägliche Arbeit im SAP-ERP-Umfeld.
Alle dargestellten Softwareprodukte, auch in Form von Screenshots, sind Eigentum der SAP SE. Ihre Marken, vgl. Link, sind u.a.: SAP®, ABAP®, SAP Fiori®, SAP HANA®, SAP S/4HANA®, SAP S/4HANA® Cloud