Who Changed My PO Quantity? Building a Quantity Change Audit Report with Change Documents in ABAP
Share

[[{“value”:”

Introduction

SAP records the answer in change documents tables CDHDR and CDPOS. The standard transactions, such as ME23N’s change view and report RSSCD100, show changes one document at a time. They do not give a consolidated, auditable, year-wide view. That view is what internal audit, procurement managers and controllers usually want.

In this blog I’ll walk through a small ABAP report, ZPO_QTY_CHANGE_REPORT, that:

  • picks all purchase orders created in a given year
  • reads every quantity EKPO Menge change made to those POs
  • shows old quantity, new quantity and the difference
  • shows who changed it user ID and full name, when, and with which transaction
  • groups the result by the quarter in which the PO was created
  • displays everything in a sortable, filterable ALV grid

You can adapt the same pattern to any other field, such as price, delivery date or plant.

Business scenario

Business question Where the report answers it

 

Which PO items had their quantity changed? PO number, item, description
What was the change? Old quantity, new quantity, difference
Who made the change and when? User ID, full name, date, time
Through which transaction? Transaction code (for example ME22N)
In which quarter was the PO created? Quarter (Q1 to Q4)

Typical users:  

internal audit, procurement managers, controllers and support teams investigating quantity disputes with vendors.

Technical background

Change documents in SAP

  • CDHDR is the change document header. It holds the object class, object ID, change number, user, date, time and transaction code.
  • CDPOS is the change document item. It holds the table name, table key, field name, change indicator, and the old and new values.
  • For purchase orders, the change document object class is EINKBELEG, and the object ID is the PO number.

Flow chart:

reddysekharpathipati_2-1794376758719.png

Solution design with small snippets

The report works in four steps.

  1. Get data. Select POs from EKKO for the chosen year, then read CDHDR for those POs (class EINKBELEG). Next read CDPOS filtered to TABNAME = EKPO, FNAME =MENGE and CHNGIND = U (update). Finally read EKPO for item descriptions.
  2. Process data. Link each CDPOS line back to its CDHDR header and decode the TABKEY. Convert the character values to quantities, calculate the difference and derive the quarter.
  3. Get full names. Join USR21 with ADRP to turn user IDs into readable names.
  4. Display. Show the result with CL_SALV_TABLE.

1. Decoding TABKEY

CDPOS-TABKEY is a concatenation of client, PO number and item number:

* TABKEY = CLIENT(3) + EBELN(10) + EBELP(5)
lv_ebeln = ls_pos-tabkey+3(10).
ls_out-ebelp = ls_pos-tabkey+13(5).

This traces every change line back to an exact PO item.

Snippet 2: Converting old and new values

VALUE OLD and VALUE NEW are stored as text, and negative values can have a trailing minus sign. A helper form moves the sign to the front before converting:

IF lv_txt+lv_len(1) = ‘-‘.
lv_txt = lv_txt(lv_len).
CONCATENATE ‘-‘ lv_txt INTO lv_txt.
ENDIF.

3: Difference and quarter

ls_out-diff_value = ls_out-old_value – ls_out-new_value.

A positive difference means the quantity was reduced. A negative one means it was increased. The quarter comes from the PO creation month through a simple CASE statement.

Performance

  • Every FOR ALL ENTRIES is protected by an emptiness check on the driver table.
  • Results are held in sorted tables with unique keys, so READ TABLE uses binary search.

5: ALV

CL_SALV_TABLE gives sorting, filtering, Excel export and layout saving with very little code.

Selection screen:

reddysekharpathipati_0-1794376463681.png

Complete source code

REPORT zpo_qty_change_report.

TABLES: ekko.

*———————————————————————*
* TYPES
*———————————————————————*
TYPES: BEGIN OF ty_out,
ebeln TYPE ekko-ebeln,
ebelp TYPE ekpo-ebelp,
txz01 TYPE ekpo-txz01,
aedat TYPE ekko-aedat,
udate TYPE cdhdr-udate,
utime TYPE cdhdr-utime,
tcode TYPE cdhdr-tcode,
old_value TYPE ekpo-menge,
new_value TYPE ekpo-menge,
diff_value TYPE ekpo-menge,
changed_by TYPE cdhdr-username,
fullname TYPE ad_namtext,
quarter TYPE char2,
END OF ty_out.

TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
aedat TYPE ekko-aedat,
END OF ty_ekko.

TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
txz01 TYPE ekpo-txz01,
END OF ty_ekpo.

TYPES: BEGIN OF ty_obj,
objectid TYPE cdhdr-objectid,
END OF ty_obj.

TYPES: BEGIN OF ty_hdr,
objectclas TYPE cdhdr-objectclas,
objectid TYPE cdhdr-objectid,
changenr TYPE cdhdr-changenr,
username TYPE cdhdr-username,
udate TYPE cdhdr-udate,
utime TYPE cdhdr-utime,
tcode TYPE cdhdr-tcode,
END OF ty_hdr.

TYPES: BEGIN OF ty_pos,
objectclas TYPE cdpos-objectclas,
objectid TYPE cdpos-objectid,
changenr TYPE cdpos-changenr,
tabname TYPE cdpos-tabname,
tabkey TYPE cdpos-tabkey,
fname TYPE cdpos-fname,
chngind TYPE cdpos-chngind,
value_new TYPE cdpos-value_new,
value_old TYPE cdpos-value_old,
END OF ty_pos.

TYPES: BEGIN OF ty_user,
bname TYPE usr21-bname,
fullname TYPE ad_namtext,
END OF ty_user.

*———————————————————————*
* DATA
*———————————————————————*
DATA: gt_out TYPE STANDARD TABLE OF ty_out,
gt_ekko TYPE SORTED TABLE OF ty_ekko
WITH UNIQUE KEY ebeln,
gt_ekpo TYPE SORTED TABLE OF ty_ekpo
WITH UNIQUE KEY ebeln ebelp,
gt_hdr TYPE SORTED TABLE OF ty_hdr
WITH UNIQUE KEY objectclas objectid changenr,
gt_pos TYPE STANDARD TABLE OF ty_pos,
gt_users TYPE SORTED TABLE OF ty_user
WITH UNIQUE KEY bname,
go_alv TYPE REF TO cl_salv_table.

*———————————————————————*
* SELECTION SCREEN
*———————————————————————*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.

PARAMETERS: p_year TYPE numc4 OBLIGATORY DEFAULT sy-datum(4).

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

SELECTION-SCREEN END OF BLOCK b1.

*———————————————————————*
* START
*———————————————————————*
START-OF-SELECTION.

PERFORM get_data.
PERFORM process_data.
PERFORM get_fullname.
PERFORM display_alv.

*———————————————————————*
* GET DATA
*———————————————————————*
FORM get_data.

DATA: lv_from TYPE sy-datum,
lv_to TYPE sy-datum,
lt_obj TYPE STANDARD TABLE OF ty_obj,
ls_obj TYPE ty_obj,
lt_hdr TYPE STANDARD TABLE OF ty_hdr,
ls_ekko TYPE ty_ekko.

CLEAR: gt_ekko, gt_ekpo, gt_hdr, gt_pos.

CONCATENATE p_year ‘0101’ INTO lv_from.
CONCATENATE p_year ‘1231’ INTO lv_to.

* PO HEADER – POs created in selected year
SELECT ebeln aedat
FROM ekko
INTO TABLE gt_ekko
WHERE ebeln IN s_ebeln
AND aedat BETWEEN lv_from AND lv_to.

IF gt_ekko IS INITIAL.
RETURN.
ENDIF.

* Prepare PO object IDs
LOOP AT gt_ekko INTO ls_ekko.

CLEAR ls_obj.
ls_obj-objectid = ls_ekko-ebeln.
APPEND ls_obj TO lt_obj.

ENDLOOP.

SORT lt_obj BY objectid.
DELETE ADJACENT DUPLICATES FROM lt_obj COMPARING objectid.

* CHANGE DOCUMENT HEADER – USER, DATE, TIME, TCODE
SELECT objectclas
objectid
changenr
username
udate
utime
tcode
FROM cdhdr
INTO TABLE lt_hdr
FOR ALL ENTRIES IN lt_obj
WHERE objectclas = ‘EINKBELEG’
AND objectid = lt_obj-objectid.

IF lt_hdr IS INITIAL.
RETURN.
ENDIF.

SORT lt_hdr BY objectclas objectid changenr.
gt_hdr = lt_hdr.

* CHANGE DOCUMENT ITEM – OLD AND NEW QUANTITY
SELECT objectclas
objectid
changenr
tabname
tabkey
fname
chngind
value_new
value_old
FROM cdpos
INTO TABLE gt_pos
FOR ALL ENTRIES IN lt_hdr
WHERE objectclas = lt_hdr-objectclas
AND objectid = lt_hdr-objectid
AND changenr = lt_hdr-changenr
AND tabname = ‘EKPO’
AND fname = ‘MENGE’
AND chngind = ‘U’.

IF gt_pos IS INITIAL.
RETURN.
ENDIF.

* PO ITEM DESCRIPTION
SELECT ebeln
ebelp
txz01
FROM ekpo
INTO TABLE gt_ekpo
FOR ALL ENTRIES IN gt_ekko
WHERE ebeln = gt_ekko-ebeln.

ENDFORM.

*———————————————————————*
* PROCESS DATA
*———————————————————————*
FORM process_data.

DATA: ls_pos TYPE ty_pos,
ls_hdr TYPE ty_hdr,
ls_ekko TYPE ty_ekko,
ls_ekpo TYPE ty_ekpo,
ls_out TYPE ty_out,
lv_ebeln TYPE ekko-ebeln,
lv_month TYPE n LENGTH 2.

CLEAR gt_out.

LOOP AT gt_pos INTO ls_pos.

CLEAR: ls_out, ls_hdr, ls_ekko, ls_ekpo.

* Read change document header
READ TABLE gt_hdr INTO ls_hdr
WITH TABLE KEY
objectclas = ls_pos-objectclas
objectid = ls_pos-objectid
changenr = ls_pos-changenr.

IF sy-subrc <> 0.
CONTINUE.
ENDIF.

* TABKEY = CLIENT(3) + EBELN(10) + EBELP(5)
lv_ebeln = ls_pos-tabkey+3(10).

ls_out-ebeln = lv_ebeln.
ls_out-ebelp = ls_pos-tabkey+13(5).

* PO HEADER
READ TABLE gt_ekko INTO ls_ekko
WITH TABLE KEY ebeln = lv_ebeln.

IF sy-subrc <> 0.
CONTINUE.
ENDIF.

ls_out-aedat = ls_ekko-aedat.

* PO ITEM DESCRIPTION
READ TABLE gt_ekpo INTO ls_ekpo
WITH TABLE KEY
ebeln = lv_ebeln
ebelp = ls_out-ebelp.

IF sy-subrc = 0.
ls_out-txz01 = ls_ekpo-txz01.
ENDIF.

* CHANGE USER DETAILS
ls_out-udate = ls_hdr-udate.
ls_out-utime = ls_hdr-utime.
ls_out-tcode = ls_hdr-tcode.
ls_out-changed_by = ls_hdr-username.

* OLD AND NEW QUANTITY
PERFORM conv_qty USING ls_pos-value_old
CHANGING ls_out-old_value.

PERFORM conv_qty USING ls_pos-value_new
CHANGING ls_out-new_value.

* DIFFERENCE
ls_out-diff_value = ls_out-old_value – ls_out-new_value.

* QUARTER BASED ON PO CREATED DATE
lv_month = ls_out-aedat+4(2).

CASE lv_month.
WHEN ’01’ OR ’02’ OR ’03’.
ls_out-quarter = ‘Q1’.

WHEN ’04’ OR ’05’ OR ’06’.
ls_out-quarter = ‘Q2’.

WHEN ’07’ OR ’08’ OR ’09’.
ls_out-quarter = ‘Q3’.

WHEN ’10’ OR ’11’ OR ’12’.
ls_out-quarter = ‘Q4’.
ENDCASE.

* APPEND OUTPUT
APPEND ls_out TO gt_out.

ENDLOOP.

SORT gt_out BY ebeln ebelp udate utime.

ENDFORM.

*———————————————————————*
* CONVERT CHANGE DOCUMENT QUANTITY
*———————————————————————*
FORM conv_qty USING iv_char TYPE cdpos-value_old
CHANGING cv_qty TYPE ekpo-menge.

DATA: lv_txt TYPE c LENGTH 30,
lv_len TYPE i.

CLEAR cv_qty.

lv_txt = iv_char.
CONDENSE lv_txt.

lv_len = strlen( lv_txt ).

IF lv_len = 0.
RETURN.
ENDIF.

* Trailing minus to leading minus
lv_len = lv_len – 1.

IF lv_txt+lv_len(1) = ‘-‘.

lv_txt = lv_txt(lv_len).

CONCATENATE ‘-‘ lv_txt INTO lv_txt.

ENDIF.

TRY.

cv_qty = lv_txt.

CATCH cx_sy_conversion_error.

CLEAR cv_qty.

ENDTRY.

ENDFORM.

*———————————————————————*
* GET USER FULL NAME
*———————————————————————*
FORM get_fullname.

DATA: lt_names TYPE STANDARD TABLE OF ty_user,
ls_user TYPE ty_user.

FIELD-SYMBOLS: <fs> TYPE ty_out.

CHECK gt_out IS NOT INITIAL.

SELECT u~bname,
a~name_text AS fullname
FROM usr21 AS u
INNER JOIN adrp AS a
ON a~persnumber = u~persnumber
INTO TABLE @LT_names
FOR ALL ENTRIES IN @gt_out
WHERE u~bname = @gt_out-changed_by
AND a~date_from = ‘00010101’
AND a~nation = @Space.

SORT lt_names BY bname.

DELETE ADJACENT DUPLICATES FROM lt_names
COMPARING bname.

gt_users = lt_names.

LOOP AT gt_out ASSIGNING <fs>.

READ TABLE gt_users INTO ls_user WITH TABLE KEY bname = <fs>-changed_by.

IF sy-subrc = 0.
<fs>-fullname = ls_user-fullname.
ENDIF.

ENDLOOP.

ENDFORM.

*———————————————————————*
* DISPLAY ALV
*———————————————————————*
FORM display_alv.

DATA: lo_cols TYPE REF TO cl_salv_columns_table,
lo_col TYPE REF TO cl_salv_column,
lx_err TYPE REF TO cx_root,
lv_msg TYPE string.

IF gt_out IS INITIAL.

MESSAGE ‘No quantity changes found for the selection’
TYPE ‘I’.

RETURN.

ENDIF.

TRY.

cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = gt_out ).

* ALV FUNCTIONS
go_alv->get_functions( )->set_all( abap_true ).

* ALV HEADER
go_alv->get_display_settings( )->set_list_header(
‘PO Quantity Change Log’ ).

* COLUMN SETTINGS
lo_cols = go_alv->get_columns( ).
lo_cols->set_optimize( abap_true ).

* PO NUMBER
lo_col = lo_cols->get_column( ‘EBELN’ ).
lo_col->set_long_text( ‘Purchasing Document’ ).

* ITEM
lo_col = lo_cols->get_column( ‘EBELP’ ).
lo_col->set_long_text( ‘PO Item’ ).

* DESCRIPTION
lo_col = lo_cols->get_column( ‘TXZ01’ ).
lo_col->set_long_text( ‘Item Description’ ).

* PO CREATED DATE
lo_col = lo_cols->get_column( ‘AEDAT’ ).
lo_col->set_long_text( ‘PO Created On’ ).

* CHANGE DATE
lo_col = lo_cols->get_column( ‘UDATE’ ).
lo_col->set_long_text( ‘Changed Date’ ).

* CHANGE TIME
lo_col = lo_cols->get_column( ‘UTIME’ ).
lo_col->set_long_text( ‘Changed Time’ ).

* TRANSACTION
lo_col = lo_cols->get_column( ‘TCODE’ ).
lo_col->set_long_text( ‘Transaction Code’ ).

* OLD QUANTITY
lo_col = lo_cols->get_column( ‘OLD_VALUE’ ).
lo_col->set_long_text( ‘Old Quantity’ ).

* NEW QUANTITY
lo_col = lo_cols->get_column( ‘NEW_VALUE’ ).
lo_col->set_long_text( ‘New Quantity’ ).

* DIFFERENCE
lo_col = lo_cols->get_column( ‘DIFF_VALUE’ ).
lo_col->set_long_text( ‘Difference (Old – New)’ ).

* USER ID
lo_col = lo_cols->get_column( ‘CHANGED_BY’ ).
lo_col->set_long_text( ‘Changed By (User ID)’ ).

* USER FULL NAME
lo_col = lo_cols->get_column( ‘FULLNAME’ ).
lo_col->set_long_text( ‘Changed By Full Name’ ).

* QUARTER
lo_col = lo_cols->get_column( ‘QUARTER’ ).
lo_col->set_long_text( ‘Quarter’ ).

* DISPLAY
go_alv->display( ).

CATCH cx_root INTO lx_err.

lv_msg = lx_err->get_text( ).
MESSAGE lv_msg TYPE ‘E’.

ENDTRY.

ENDFORM.

Sample output (screenshot)

Run the report for a year that has data, take a screenshot of the ALV grid, and insert it here. Hide or mask any real vendor or user names before publishing.

Caption suggestion: ALV output showing old quantity, new quantity, difference, user and quarter.

Sample output:

reddysekharpathipati_1-1794376526934.png

Solution review

  • Standard data only. It reads change documents, so no custom logging or modification is needed.
  • Simple structure. One FORM per step makes it easy to maintain.
  • Fast lookups. Sorted tables with unique keys avoid slow table scans.
  • Business-friendly output. Full names, proper quantities, difference and quarter instead of raw IDs and text.
  • Easy to reuse. Change the table and field name to track price, delivery date and more.

Conclusion

SAP already stores the complete history of who changed what on a purchase order. The challenge is turning that history into something usable. With fewer than 400 lines of ABAP, CDHDR, CDPOS, EKKO, EKPO and SALV, we built a quantity change log that answers the audit questions in one screen.

The same approach works for almost any change document object sales orders, materials, vendors and so on. Change the object class, table and field, and you have a new audit report.

I hope this helps you build your own change tracking reports. If you have improvements, such as a version that reads CDPOS first for better performance.

Thanks For Reading, Best Wishes
#ABAP Development   

 

 

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

By ali