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

Monday, October 13, 2008

How to add extra address fields to Query

If you to want add some of the extra customer address fields such as, Street 2 and Street 3, to a query you will find that they are not part of the standard table KNA1 but are located in the address table that is linked to via the address number field KNA1-ADRNR.
To fetch the values you can make use of the standard function module ADDR_GET_COMPLETE and follow the process set out below to enhance you infoset.
First add the type pool definition used by the function module to the DATA code section plus the data definitions used for the retrieval part.

* Declare transfer structure

* Include type szadr.

type-pools: szadr.

* Data definitions

data: addr1_complete type szadr_addr1_complete.

data: wa_addr1_lin type szadr_addr1_line.



Next define an additional field for the data element.












Then define the source code to retrieve the values for the data element.

* Specify parameters
*break-point.
clear: cust_addr_street2, cust_addr_street3, cust_addr_street3.
*addrnumber = kna1-adrnr.
call function 'ADDR_GET_COMPLETE'
exporting addrnumber = kna1-adrnr
importing addr1_complete = addr1_complete
exceptions parameter_error = 1
address_not_exist = 2
internal_error = 3.
* You can access and process the data read by LOOPing over the tables
* in ADDR1_COMPLETE.
* The subtable ADDR1_TAB has the type structure SZADR_ADDR1_LINE,
* so you must access the fields with ADDR1_COMPLETE-DATA-.
loop at addr1_complete-addr1_tab into wa_addr1_lin.
move wa_addr1_lin-data-str_suppl1 to cust_addr_street2.
move wa_addr1_lin-data-str_suppl2 to cust_addr_street3.
move wa_addr1_lin-data-str_suppl3 to cust_addr_street4.
endloop.

Finally Save and Generate the infoset after assigning the new data element to a report group. The field will then be available for use in the Query definition.