Sub topic 15
Tracing documents and PO history
This chapter explains how SAP links the documents of one purchase and how to read that chain in the system and in code. It assumes you can create a purchase order (PO), post a goods receipt (GR) and an invoice receipt (IR), and read simple ABAP. Basic knowledge of debit and credit postings also helps.
Two views of the same chain
Every step in procure-to-pay creates its own document with its own number. SAP keeps them connected in two ways.
- PO history is stored per PO item in table
EKBE. It lists the follow-on documents that change the item's delivered or invoiced status. - Document flow is the wider chain of documents across applications. It starts at the requisition, continues through the PO, goods movement and invoice, and ends at the accounting and payment documents.
PO history answers "what has been delivered and invoiced for this item?" Document flow answers "which documents led to this one, and which came after it?"
Where each document lives
| Step | Business document | Main storage in S/4HANA | EKBE `VGABE` |
|---|---|---|---|
| Purchase order | PO header and item | `EKKO`, `EKPO` | not applicable |
| Goods receipt | Material document | `MATDOC` (`MKPF` and `MSEG` are compatibility views) | 1 |
| Invoice receipt | Logistics invoice | `RBKP`, `RSEG` | 2 |
| Subsequent debit or credit | Invoice document | `RBKP`, `RSEG` | 3 |
| Accounting | Journal entry | `BKPF`, `ACDOCA` | not in `EKBE` |
EKBE stores references to the material and invoice documents. It does not hold the accounting document. To go from a business document to its journal entry, use the object key. In BKPF, the fields AWTYP (reference procedure) and AWKEY (reference key) identify the source document. For a goods receipt, the key is the material document number plus the fiscal year.
Reading a PO history entry
Each EKBE row is one event on one item. These fields matter most:
EBELNandEBELP: the PO and item.VGABE: the transaction type, for example 1 for goods receipt and 2 for invoice receipt.BEWTP: a related event category that also separates deliveries (E) from invoices (Q).GJAHR,BELNR,BUZEI: fiscal year, document number and document item. The key is only unique with the year.SHKZG: debit or credit indicator.Sis a normal posting andHreverses or credits it.MENGE,DMBTR,WRBTR: quantity, amount in company code currency and amount in document currency.
The SHKZG rule is the most common trap. A reversed goods receipt does not delete the original row. It adds a second row with H. A plain sum of MENGE therefore overstates the delivered quantity.
REPORT z_po_history.
PARAMETERS p_ebeln TYPE ekko-ebeln OBLIGATORY.
TYPES: BEGIN OF ty_net,
ebelp TYPE ekbe-ebelp,
gr_qty TYPE ekbe-menge,
ir_qty TYPE ekbe-menge,
END OF ty_net.
DATA lt_net TYPE SORTED TABLE OF ty_net WITH UNIQUE KEY ebelp.
SELECT ebeln, ebelp, vgabe, gjahr, belnr, buzei, shkzg, menge
FROM ekbe
WHERE ebeln = @p_ebeln
ORDER BY ebelp, gjahr, belnr, buzei
INTO TABLE @DATA(lt_history).
LOOP AT lt_history INTO DATA(ls_hist).
" H marks a reversal or credit, so it reduces the quantity
DATA(lv_qty) = COND menge_d( WHEN ls_hist-shkzg = 'H'
THEN - ls_hist-menge
ELSE ls_hist-menge ).
ASSIGN lt_net[ ebelp = ls_hist-ebelp ] TO FIELD-SYMBOL(<ls_net>).
IF sy-subrc <> 0.
INSERT VALUE #( ebelp = ls_hist-ebelp ) INTO TABLE lt_net ASSIGNING <ls_net>.
ENDIF.
CASE ls_hist-vgabe.
WHEN '1'.
<ls_net>-gr_qty = <ls_net>-gr_qty + lv_qty.
WHEN '2'.
<ls_net>-ir_qty = <ls_net>-ir_qty + lv_qty.
ENDCASE.
ENDLOOP.
LOOP AT lt_net INTO DATA(ls_net).
WRITE: / ls_net-ebelp, 'GR net:', ls_net-gr_qty, 'IR net:', ls_net-ir_qty.
ENDLOOP.The report prints one line per item. If net received and net invoiced quantities differ, the item has an open GR/IR balance or a pending invoice.
Tracing in the user interface
In the PO transaction, open an item and choose the Purchase Order History tab. It lists goods receipts, invoice receipts and later debits or credits with posting date, quantity and amount. Selecting a row opens the underlying document. From a material document or invoice, you can open the accounting document to check the GR/IR posting.
In Fiori, the purchase order object pages show related documents and item history. Exact app names and layouts depend on your release, so check your launchpad.
Edge cases
- Partial deliveries and invoices. One item can have many
EKBErows, and their quantities rarely match one to one. A single invoice can also cover several goods receipts. - Reversals. Both the original and the reversal are kept, linked by the
SandHindicators and the reference fields. - Service items. Service entry sheets take the role of goods receipts, so history for service POs reads differently.
- Account assignment. For items with account assignment, history rows can repeat once per account assignment line.
- Archiving. After archiving, history is no longer in
EKBE. Reports that rely on it return nothing for old POs. - Local currency versus document currency. Compare amounts in the same currency. Exchange rate differences appear only in the accounting documents, not in
MENGE.
Exercise
A PO item has two EKBE rows with VGABE 1, both for 10 pieces. One has SHKZG = S and the other has SHKZG = H. How many pieces were delivered, and why?
Show answer
Net delivered quantity is 0. The H row is the reversal of the first goods receipt, so it cancels the quantity. Summing MENGE without considering SHKZG would wrongly give 20.
Exercise
Which fields in BKPF would you use to find the accounting document for a goods receipt, and what must you know about the material document to use them?
Show answer
Use AWTYP and AWKEY. The key is built from the material document number and its fiscal year, so you need both values.