Selecting Condition Record Value (VK12) for a Sales Order [SAP]

The goal is to compare a sales order item condition value (for example PR00) to VK12 / VK13 value. As you know there are multiple levels in VK12 condition records. When a condition is used the value of condition is calculated based on these condition records. But since each condition uses different fields and all these fields are maintainable and new condition records can be added, the deciding which VK12 condition record will be used is not simple as SELECT from this table, then this table…

We are approaching to cutover phase in our SAP ECC Upgrade project. There is a custom program which compares sales orders condition value and condition records values, and reports if there is difference. A standard function module SALES_PRICE_DIRECTLY_READ had been used in 4.6C for selection VK12 condition record value by giving a sales order item’s information. But it seems that the function module was changes a lot in ECC 6.0 and it doesn’t work in ECC 6.0 by giving the same parameters. So I needed to figure out another way to do that!

I have come up a new custom function module to calculate VK12 condition record value. Here is the usage of the function module.

CALL FUNCTION ‘Z_PCC_GET_PRICING_CONDITION’
EXPORTING
iv_condition_type                 = lv_kschl
iv_validity_date                    = <fs_komv>-kdatu
is_communication_header       = ls_komk
is_communication_item          = ls_komp
IMPORTING
et_condition_item              = lt_konp
EXCEPTIONS
condition_type_not_found       = 1
condition_access_seq_not_found = 2
condition_record_not_found     = 3
OTHERS                         = 4.

You just need to five condition type, validity date of condition record, communication header and communication item data of a sales order, then the function module will return condition item table which has the value in KBETR field. The value must be same as condition analysis value from VA03 transaction.

To fill out communication header and communication item for a sales order, you can use this method:

CLEAR ls_komk.
MOVE-CORRESPONDING <fs_vbak> TO ls_komk.
MOVE-CORRESPONDING <fs_vbkd> TO ls_komk.

CLEAR ls_komp.
MOVE-CORRESPONDING <fs_vbap> TO ls_komp.

IF ls_komp-pmatn IS INITIAL.
ls_komp-pmatn = ls_komp-matnr.
ENDIF.

And here is the Z_PCC_GET_PRICING_CONDITION function module code:

FUNCTION z_pcc_get_pricing_condition.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(IV_CONDITION_TYPE) TYPE  KSCHL
*”     VALUE(IV_VALIDITY_DATE) TYPE  SY-DATUM DEFAULT SY-DATUM
*”     VALUE(IS_COMMUNICATION_HEADER) TYPE  KOMK
*”     VALUE(IS_COMMUNICATION_ITEM) TYPE  KOMP
*”  EXPORTING
*”     VALUE(ET_CONDITION_ACCESS) TYPE  COND_A000_T
*”     VALUE(ES_CONDITION_HEADER) TYPE  KONH
*”     VALUE(ET_CONDITION_ITEM) TYPE  KONP_T
*”  EXCEPTIONS
*”      CONDITION_TYPE_NOT_FOUND
*”      CONDITION_ACCESS_SEQ_NOT_FOUND
*”      CONDITION_RECORD_NOT_FOUND
*”———————————————————————-

DATA:
ls_t685 TYPE t685,
lt_t682i TYPE STANDARD TABLE OF t682i,
ls_t682i TYPE t682i,
ls_koprt TYPE koprt,
lt_kondtab TYPE STANDARD TABLE OF a000,
ls_kondtab TYPE a000.

* Get Condition Type info
CLEAR ls_t685.
SELECT SINGLE *
FROM t685
INTO ls_t685
WHERE kvewe = gc_kvewe_pricing AND
kappl = gc_kappl_sales AND
kschl = iv_condition_type.
IF NOT sy-subrc IS INITIAL.
MESSAGE e021
WITH iv_condition_type
gc_kvewe_pricing
gc_kappl_sales
RAISING condition_type_not_found.
ENDIF.

* Get Condition Access Sequence info
CLEAR lt_t682i[].
SELECT *
FROM t682i
INTO TABLE lt_t682i
WHERE kvewe = gc_kvewe_pricing AND
kappl = gc_kappl_sales AND
kozgf = ls_t685-kozgf.
IF NOT sy-subrc IS INITIAL.
MESSAGE e022
WITH ls_t685-kozgf
gc_kvewe_pricing
gc_kappl_sales
RAISING condition_access_seq_not_found.
ENDIF.
* Find the proper access sequence step
SORT lt_t682i BY kolnr.
LOOP AT lt_t682i INTO ls_t682i.
CLEAR lt_kondtab[].
CALL FUNCTION ‘SD_COND_ACCESS’
EXPORTING
application          = gc_kappl_sales
condition_type       = iv_condition_type
date                 = iv_validity_date
header_comm_area     = is_communication_header
position_comm_area   = is_communication_item
t682i_i              = ls_t682i
koprt_i              = ls_koprt “required parameter, even is blank
no_mem_import        = ‘X’
TABLES
condition_records    = lt_kondtab
EXCEPTIONS
field_is_initial     = 8
not_read_unqualified = 2
read_but_not_found   = 4.
CHECK sy-subrc IS INITIAL.
IF LINES( lt_kondtab ) > 0.
EXIT.
ENDIF.

ENDLOOP.

IF NOT LINES( lt_kondtab ) > 0.
MESSAGE e023 WITH iv_condition_type
RAISING condition_record_not_found.
ELSE.
*   Get condition access info
et_condition_access[] = lt_kondtab[].

*   Assumption: There would be only one record!
CLEAR ls_kondtab.
READ TABLE lt_kondtab
INTO ls_kondtab INDEX 1.

*   Get condition header
SELECT SINGLE *
INTO es_condition_header
FROM konh
WHERE knumh = ls_kondtab-knumh.

*   Get condition items
SELECT * INTO TABLE et_condition_item
FROM konp
WHERE knumh = ls_kondtab-knumh.
ENDIF.

ENDFUNCTION.

References:

Tags: , , , ,

One Response to “Selecting Condition Record Value (VK12) for a Sales Order [SAP]”

  1. Tuncay Karaca Says:

    Let me add this new line for filling communication header and item parameters of the function module.

    MOVE-CORRESPONDING TO ls_komk.

    So it is latest code:

    CLEAR ls_komk.
    MOVE-CORRESPONDING TO ls_komk. < ---
    MOVE-CORRESPONDING TO ls_komk.
    MOVE-CORRESPONDING TO ls_komk.

    CLEAR ls_komp.
    MOVE-CORRESPONDING TO ls_komp.

    IF ls_komp-pmatn IS INITIAL.
    ls_komp-pmatn = ls_komp-matnr.
    ENDIF.

Leave a Reply