Sub topic 7
Purchase order history and table EKBE
This chapter assumes you know the header and item structure of a purchase order (tables EKKO and EKPO), can open a table in SE16N, and can read a basic ABAP SELECT. It covers how SAP records follow-on documents of a purchase order and how to read that record.
Why purchasing has its own history
Sales documents store their links in VBFA. Purchasing does not use VBFA. Each purchase order item keeps its follow-on documents in table EKBE, called the purchasing document history. In the GUI you see it on the Purchase Order History tab of an item in ME23N.
EKBE holds one row for each posting that refers to a PO item. A goods receipt creates a row. An invoice receipt creates another. The links are therefore stored per PO item, not per PO header.
Key fields
| Field | Meaning |
|---|---|
| EBELN, EBELP | Purchase order number and item |
| ZEKKN | Account assignment sequence number |
| VGABE | Transaction type of the history record |
| GJAHR, BELNR, BUZEI | Fiscal year, document number and item of the follow-on document |
| MENGE | Quantity of the posting |
| SHKZG | Debit or credit indicator, which shows reversals |
The most useful field is VGABE:
- 1 is a goods receipt.
- 2 is an invoice receipt.
- 3 is a subsequent debit or credit.
BELNR does not mean the same thing for every VGABE value. For a goods receipt it is a material document number. For an invoice receipt it is a logistics invoice verification document number. Both are combined with GJAHR, so always use the year in your join or filter.
Reading the history with ABAP
The following query lists the history of one PO in a stable order.
DATA lv_po TYPE ebeln VALUE '4500000123'.
SELECT ebeln, ebelp, vgabe, gjahr, belnr, buzei, menge
FROM ekbe
WHERE ebeln = @lv_po
ORDER BY ebelp, vgabe, gjahr, belnr, buzei
INTO TABLE @DATA(lt_history).Each row in lt_history is one follow-on posting. Rows with the same EBELP belong to one item. Sorting by VGABE places goods receipts before invoice receipts inside each item.
A worked example
Suppose PO 4500000123 has item 10 for 100 pieces. The history looks like this.
| EBELP | VGABE | BELNR | MENGE |
|---|---|---|---|
| 00010 | 1 | 5000000456 | 60 |
| 00010 | 1 | 5000000457 | 40 |
| 00010 | 2 | 5105600789 | 100 |
The item was delivered in two goods receipts (60 and 40 pieces). A single invoice then covered the full 100 pieces. Adding MENGE per VGABE gives the received quantity and the invoiced quantity. If they differ, the item is only partly received or invoiced.
Common errors
- Ignoring SHKZG. A reversal or return posts a credit row (SHKZG = H) with its own quantity. If you sum MENGE without checking SHKZG, the totals are too high.
- Filtering without GJAHR. The same BELNR can exist in different fiscal years.
- Looking for goods receipts in VBFA. Purchasing links are not in VBFA.
- Expecting a row before posting. EKBE only holds posted documents. A PO with no history has no goods receipt or invoice yet.
Exercise
A PO item ordered 50 pieces. EKBE shows two VGABE 1 rows with MENGE 50 each. One has SHKZG = S and one has SHKZG = H. How many pieces are currently received, and why?
Show answer
Zero pieces are currently received. The row with SHKZG = S is the goods receipt of 50 pieces. The row with SHKZG = H is a credit, such as a reversal or return, that cancels it. Summing MENGE without the indicator would wrongly give 100.