Sub topic 7
Purchase Order History and Table EKBE
This chapter covers how SAP records what happened after a purchase order (PO) was created: goods receipts, invoice receipts and later debits or credits. It assumes you can read tables in SE16N, know the header and item structure of a PO (EKKO and EKPO), and can read basic ABAP SELECT syntax. You should also know the MM and FI roles in procurement.
Why purchasing has its own history
Sales documents store their links in VBFA. Purchasing does not. The PO history is kept in table EKBE, and it is keyed to the PO item. Each time a goods receipt, invoice receipt or subsequent adjustment refers to a PO item, SAP writes a row to EKBE.
In the GUI you see this data on the Purchase Order History tab at item level in ME23N. The tab is not a separate document. It is a view of the EKBE rows for the selected item.
This is a design difference to remember. In sales, the link table connects two documents. In purchasing, the PO item is the anchor and each follow-on document points back to it. To go from a goods receipt to an invoice receipt, you normally go through the PO item that both share.
What one EKBE row contains
Each row describes one follow-on posting against one PO item. The fields you will use most are listed below.
| Field | Meaning |
|---|---|
| EBELN | PO number |
| EBELP | PO item |
| ZEKKN | Account assignment sequence number, used for items with several account assignments |
| VGABE | Transaction or event type |
| GJAHR | Fiscal year of the follow-on document |
| BELNR | Number of the follow-on document |
| BUZEI | Item in the follow-on document |
| BEWTP | PO history category (for example E for goods receipt, Q for invoice receipt) |
| BWART | Movement type, filled for goods movements |
| MENGE | Quantity |
| DMBTR | Amount in local currency |
| SHKZG | Debit or credit indicator |
| XBLNR | Reference document number, such as a delivery note or vendor invoice number |
The key of EKBE is made of EBELN, EBELP, ZEKKN, VGABE, GJAHR, BELNR, BEWTP and BUZEI. Because BELNR is only unique within a fiscal year, GJAHR is always needed to identify a follow-on document.
The VGABE field
VGABE separates the kinds of event that appear in the history.
| VGABE | Event |
|---|---|
| 1 | Goods receipt |
| 2 | Invoice receipt |
| 3 | Subsequent debit or credit |
Other values exist for special cases, such as delivery costs and down payments. Treat the list above as the common core, and check the domain values of VGABE in your system for the rest.
The BELNR value means different things depending on VGABE. For VGABE 1 it is a material document number (table MKPF, MSEG). For VGABE 2 and 3 it is a logistics invoice verification document (table RBKP, RSEG). Following the link means choosing the right target table.
Debit and credit: reading quantities correctly
EKBE stores positive quantities and amounts. The sign comes from SHKZG. A value of S means debit and H means credit.
For a goods receipt, a normal receipt is a debit (S) and a return to the vendor or a reversal is a credit (H). If you add up MENGE without looking at SHKZG, you will count reversals as receipts and the total will be wrong.
For a correct delivered quantity, apply the sign yourself:
DATA lv_po TYPE ebeln VALUE '4500000123'.
SELECT ebelp,
SUM( CASE shkzg WHEN 'S' THEN menge ELSE 0 - menge END ) AS net_qty
FROM ekbe
WHERE ebeln = @lv_po
AND vgabe = '1'
GROUP BY ebelp
ORDER BY ebelp
INTO TABLE @DATA(lt_received).This statement uses CASE inside an aggregate, which is allowed in ABAP SQL on current releases (7.50 and later for the SQL expressions used here). On older releases, read the rows and sum them in a loop.
Reading the history with a query
The standard query for a PO reads EKBE by PO number and orders the result so related postings appear together.
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).The ORDER BY clause groups the rows by item, then by event type. Within each event type the rows follow the fiscal year and document number. The result shows, for each item, the goods receipts first, then the invoice receipts, then any later debits or credits.
How the chain looks in procurement
A complete simple chain has three links, all anchored on the PO item.
The arrows back to the PO item show that EKBE is read from the PO side. The history does not hold a direct pointer from the goods receipt to the invoice. Both are matched through the PO item, and invoice verification uses the history to decide what can still be invoiced.
Partial processing in the history
A PO item can be delivered and invoiced in parts. Each part creates its own EKBE row. Suppose item 10 orders 100 pieces:
- A goods receipt for 60 pieces creates a row with VGABE 1 and MENGE 60.
- A second goods receipt for 40 pieces creates another row with VGABE 1 and MENGE 40.
- An invoice for 100 pieces creates a row with VGABE 2 and MENGE 100.
The history shows three rows, and you cannot tell from the rows which receipt belongs to which invoice. The invoice covers the item as a whole. This differs from the sales side, where VBFA can link a specific delivery to a specific invoice.
Delivery costs and account assignment
Items with account assignment, for example a cost center, can have several distribution lines. ZEKKN identifies the line. If you query one item and see more rows than you expect, check ZEKKN before assuming a duplicate.
Planned delivery costs create their own history entries. They appear with a different VGABE and BEWTP, and they carry amounts but often no quantity. Filter on VGABE if your report must cover only goods and invoices.
Common errors
- Summing MENGE without SHKZG. Reversals and returns are stored as positive numbers with a credit indicator. Always apply the sign.
- Dropping GJAHR when joining. BELNR repeats across fiscal years. A join on BELNR alone can return the wrong document.
- Joining BELNR to one table for all rows. The target table depends on VGABE. Material documents and invoice documents live in different tables.
- Expecting invoice-to-receipt links. EKBE links each posting to the PO item, not to the other postings.
- Reading EKBE for the account assignment total. Use ZEKKN when item totals must match by distribution line.
Joining history to the follow-on document
To pull header data for the goods receipts, join EKBE to the material document header. The join must use both the document number and the year.
DATA lv_po TYPE ebeln VALUE '4500000123'.
SELECT e~ebelp,
e~belnr,
e~gjahr,
e~menge,
m~budat
FROM ekbe AS e
INNER JOIN mkpf AS m
ON m~mblnr = e~belnr
AND m~mjahr = e~gjahr
WHERE e~ebeln = @lv_po
AND e~vgabe = '1'
ORDER BY e~ebelp, e~gjahr, e~belnr
INTO TABLE @DATA(lt_gr_dates).The budat field is the posting date of the material document. For invoice rows, use the same pattern against RBKP, with BELNR matched to RBKP-BELNR and GJAHR to RBKP-GJAHR.
Exercises
Exercise
A PO item shows two EKBE rows with VGABE 1, MENGE 50 each. One has SHKZG S and the other has SHKZG H. How many pieces are in stock from this PO item, and why?
Show answer
Zero. The S row is a goods receipt of 50 pieces. The H row is a credit, such as a return or reversal, of 50 pieces. EKBE stores both quantities as positive numbers, so the net quantity is 50 minus 50.
Exercise
You join EKBE to RBKP on BELNR only and notice some invoices from earlier years in the result. What is the fix?
Show answer
Add GJAHR to the join condition (RBKP-GJAHR = EKBE-GJAHR). Document numbers restart each fiscal year, so BELNR alone is not unique. Also restrict the join to rows with VGABE 2 or 3, because VGABE 1 rows point to material documents.
Exercise
Why can you not read directly from EKBE which goods receipt a given invoice receipt belongs to?
Show answer
EKBE anchors each posting to the PO item. A goods receipt row and an invoice receipt row both reference the same EBELN and EBELP, but neither row points to the other. Invoice verification works against the totals on the item.