Tuesday, October 14, 2008

Query Code to Retrieve Sales Order Pricing

The following code pieces are adaptions of SAP code and can be used to retrieve line item pricing conditions for SD documents. A straightforward ABAP select statement can be used as the pricing condition is in KONV. Use the field KNUMV (Condition number) from Sales Order Header (VBAK table) and the field POSNR (Position Number) from Sales Order Detail(VBAP table) to link to the KONV table. e.g.

select single *  into konv from konv
         where knumv = vbak-knumv

             and  kposn = vbap-posnr
              and  kschl = 'PB00'.

Alternatively for complex scenarios you can use function modules to retrieve the data into a table and read the required values.
First define the DATA code section as follows:

* internal tables -------------------------------------------------*
data: tkomk like standard table of komk
with key key_uc
initial size 2
with header line.
data: begin of tkomp occurs 10.
include structure komp.
data: end of tkomp.
data: begin of tkomg occurs 10.
include structure komg.
data: end of tkomg.
data: begin of lt_komk occurs 1.
include structure komk.
data: end of lt_komk.
* internal table order conditions ---------------------------------*
data: da_xkomv like komv occurs 0 with header line.
data: da_xkonh like konh occurs 0 with header line.
data: da_xkonp like konp occurs 0 with header line.
data: da_xkonm like konm occurs 0 with header line.
data: da_xkonw like konw occurs 0 with header line.
data: da_xkomp like komp occurs 0 with header line.
data: ls_xkomp like komp.
* internal structure to read order conditions ---------------------*
data: da_comm_head_i like komk.
data: da_knumv_empty like order_view-header.


Next define your field for the specific pricing condition value, PB00 in this case and then apply the following code for the data retrieval,
* Do not refresh da_xkomv as Function Module 'PRICING_GET_CONDITIONS' will not re-read * * DB for previously read condition key field KNUMV
* See Comments in Program LV61AA11 and Form KONV_EINLESEN

refresh: da_xkomp, da_xkonh, da_xkonp,
da_xkonm, da_xkonw.
clear: da_xkomv,da_xkomp, da_xkonh, da_xkonp,
da_xkonm, da_xkonw.
da_comm_head_i-mandt = sy-mandt.
da_comm_head_i-kalsm = vbak-kalsm.
da_comm_head_i-kappl = 'V'.
da_comm_head_i-waerk = vbak-waerk.
da_comm_head_i-knumv = vbak-knumv.
da_comm_head_i-vbtyp = vbak-vbtyp.
if lt_komk-belnr ne vbak-vbeln.
call function 'SD_SALES_PRICING_INFORMATION'
EXPORTING
i_posnr = '00000'
i_read_doc = 'X'
i_vbeln = vbak-vbeln
IMPORTING
e_komk = tkomk
TABLES
ftkomk = lt_komk.
delete lt_komk where belnr ne vbak-vbeln.
endif.
da_comm_head_i-hwaer = tkomk-hwaer.
call function 'SD_SALES_PRICING_INFORMATION'
EXPORTING
i_posnr = vbap-posnr
i_vbeln = vbak-vbeln
IMPORTING
e_komp = tkomp
TABLES
ftkomk = lt_komk.
ls_xkomp = tkomp.
append ls_xkomp to da_xkomp.
call function 'PRICING_GET_CONDITIONS'
EXPORTING
comm_head_i = da_comm_head_i
TABLES
tkomv = da_xkomv
skomk = lt_komk
skomp = da_xkomp
skonh = da_xkonh
skonp = da_xkonp
skonm = da_xkonm
skonw = da_xkonw
EXCEPTIONS
error_message = 1
others = 2.
* Read da_xkomv for specific pricing condition e.g. PB00

read table da_xkomv with key
  knumv = da_comm_head_i-knumv
  kposn = vbap-posnr
  kschl = 'PB00'.
if sy-subrc = 0.
  move da_xkomv-kbetr to kbetr.
endif.
* retrieve other conditions the same way

No comments: