Posted: Sat Sep 13, 2008 6:53 pm Post subject: Sales Discount Report
Code:
REPORT z_sales_discount.
TYPES: BEGIN OF s_all,
vkorg TYPE vkorg, "sales organization
fkart TYPE fkart, "billing type
fkdat TYPE fkdat, "billing date
b_vbeln TYPE vbeln_vf, "billing document
kunag TYPE kunag, "sold-to party
spart TYPE spart, "division
gsber TYPE gsber, "business area
bstnk TYPE bstnk, "po munber
aubel TYPE vbeln_va, "sales document
matnr TYPE matnr, "material number
kwert TYPE kwert, "gross value
kbetr TYPE kbetr, "rate(condition amount or percentage)
knumv TYPE knumv, "number of document condition
kposn TYPE kposn, "condition item number
kschl TYPE kscha, "condition type
b_posnr TYPE posnr_vf, "billing item
waerk TYPE waerk, "sd document currency
kurrf TYPE kurrf, "exchange rate_accntg
name1 TYPE name1_gp, "name 1
kunnr TYPE name1, "customer name 1
arktx TYPE arktx, "short text for sales order item
fkimg TYPE fkimg, "actual billed quantity
vrkme TYPE vrkme, "sales unit
netwr TYPE netwr_fp, "net value of the billing item in document currency
s_vbeln TYPE vbeln_va, "sales document
ra01_discount TYPE kbetr, "ra01%discount
ra01_dis_amt TYPE kwert, "ra01 discount amount
ra00_discount TYPE kbetr, "ra00%discount
ra00_dis_amt TYPE kwert, "ra00 discount amount
s_kwmeng TYPE kwmeng, "sales order total quantity
s_kwert TYPE kwert, "sales order total gross value
END OF s_all.
TYPES: BEGIN OF s_konv,
kwert TYPE kwert, "gross value
kbetr TYPE kbetr, "rate(condition amount or percentage)
knumv TYPE knumv, "number of document condition
kposn TYPE kposn, "condition item number
kschl TYPE kscha, "condition type
END OF s_konv.
TYPES: BEGIN OF s_kna1,
name1 TYPE name1_gp, "name 1
kunnr TYPE name1, "customer name 1
kunag TYPE kunag, "sold-to party
END OF s_kna1.
TYPES: BEGIN OF s_vbap,
kwmeng TYPE kwmeng, "cumulative order quantity in sales units
abgru TYPE abgru_va, "reason for rejection of quotations and sales orders
s_vbeln TYPE vbeln_va, "sales document
END OF s_vbap.
TYPE-POOLS: slis. "ALV Declarations
TYPES: t_all TYPE STANDARD TABLE OF s_all,
t_konv TYPE STANDARD TABLE OF s_konv,
t_kna1 TYPE STANDARD TABLE OF s_kna1,
t_vbap TYPE STANDARD TABLE OF s_vbap.
DATA: it_all TYPE t_all,
it_konv TYPE t_konv,
it_kna1 TYPE t_kna1,
it_vbap TYPE t_vbap,
it_total TYPE t_all,
it_total_out TYPE t_all.
DATA: wa_all TYPE LINE OF t_all,
wa_konv TYPE LINE OF t_konv,
wa_kna1 TYPE LINE OF t_kna1,
wa_vbap TYPE LINE OF t_vbap,
wa_total TYPE LINE OF t_all,
wa_total_out TYPE LINE OF t_all.
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
gd_repid LIKE sy-repid.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.
SELECT-OPTIONS: x_vkorg FOR wa_all-vkorg, "sales organization
x_fkart FOR wa_all-fkart MATCHCODE OBJECT h_tvfk, "billing type
x_fkdat FOR wa_all-fkdat, "billing date
x_vbeln FOR wa_all-b_vbeln MATCHCODE OBJECT f4_vbrk, "billing document
x_kunag FOR wa_all-kunag MATCHCODE OBJECT debi. "sold-to party
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-001.
PARAMETERS x_check AS CHECKBOX.
SELECT-OPTIONS: x_spart FOR wa_all-spart, "division
x_gsber FOR wa_all-gsber MATCHCODE OBJECT h_tgsb, "business area
x_matnr FOR wa_all-matnr, "material
x_bstnk FOR wa_all-bstnk, "po munber
x_aubel FOR wa_all-aubel. "sales document
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN END OF BLOCK b1.
*&---------------------------------------------------------------------*
*& Form data_select
*&---------------------------------------------------------------------*
* perform data selecting
*----------------------------------------------------------------------*
FORM data_select.
PERFORM select_from_vbrk_vbrp_vbak.
PERFORM select_from_konv.
PERFORM select_from_kna1.
PERFORM select_from_vbap.
ENDFORM. "data_select
*&---------------------------------------------------------------------*
*& Form data_processing
*&---------------------------------------------------------------------*
* perform data processing
*----------------------------------------------------------------------*
FORM data_processing.
PERFORM get_gross_dis_amt.
PERFORM get_total_gross.
PERFORM get_total.
ENDFORM. "data_processing
*&---------------------------------------------------------------------*
*& Form data_display
*&---------------------------------------------------------------------*
* perform data displaying
*----------------------------------------------------------------------*
FORM data_display.
IF x_check = 'X'.
PERFORM build_fieldcatalog.
PERFORM display_alv_report USING it_all.
ELSE.
PERFORM build_fieldcatalog.
PERFORM display_alv_report USING it_total_out.
ENDIF.
ENDFORM. "data_display
*&---------------------------------------------------------------------*
*& Form select_from_vbrk_vbrp_vbak
*&---------------------------------------------------------------------*
* 1~select data from table vbrk/vbrp/vbak into it_all.
* 2~when billing type is 's1',add negative to net value
*----------------------------------------------------------------------*
FORM select_from_vbrk_vbrp_vbak.
SELECT vbrk~vkorg
vbrk~fkart
vbrk~fkdat
vbrk~vbeln AS b_vbeln
vbrk~kunag
vbrp~spart
vbrp~matnr
vbrp~gsber
vbrp~aubel
vbak~bstnk
vbrk~knumv
vbrp~posnr AS b_posnr
vbrk~waerk
vbrk~kurrf
vbrp~matnr
vbrp~arktx
vbrp~fkimg
vbrp~vrkme
vbrp~netwr
INTO CORRESPONDING FIELDS OF TABLE it_all
FROM vbrp
JOIN vbak ON vbak~vbeln = vbrp~aubel
JOIN vbrk ON vbrk~vbeln = vbrp~vbeln
WHERE vbrk~vkorg IN x_vkorg
AND vbrk~vkorg NOT IN ('4802','4803')
AND vbrk~fkart IN x_fkart
AND vbrk~fkart NOT IN ('ZMF8','ZPF8')
AND vbrk~fkdat IN x_fkdat
AND vbrk~vbeln IN x_vbeln
AND vbrk~kunag IN x_kunag
AND vbrp~spart IN x_spart
AND vbrp~matnr IN x_matnr
AND vbrp~gsber IN x_gsber
AND vbrp~aubel IN x_aubel.
SORT it_all BY b_vbeln kunag.
IF sy-subrc <> 0.
MESSAGE i001(zjenni_msg).
ENDIF.
LOOP AT it_all INTO wa_all.
IF wa_all-fkart = 'S1'.
wa_all-netwr = -1 * wa_all-netwr.
MODIFY it_all FROM wa_all TRANSPORTING netwr.
ENDIF.
ENDLOOP.
CLEAR wa_all.
ENDFORM. "select_from_vbrk_vbrp_vbak
*&---------------------------------------------------------------------*
*& Form select_from_konv
*&---------------------------------------------------------------------*
* select data from table konv into it_konv
*----------------------------------------------------------------------*
FORM select_from_konv.
IF it_all[] IS NOT INITIAL.
SELECT kwert
kbetr
knumv
kposn
kschl
FROM konv
INTO CORRESPONDING FIELDS OF TABLE it_konv
FOR ALL ENTRIES IN it_all
WHERE knumv = it_all-knumv
AND kposn = it_all-b_posnr
AND kschl IN ('PR00','RA00','RA01').
ENDIF.
SORT it_konv BY knumv kposn.
IF sy-subrc <> 0.
MESSAGE i001(zjenni_msg).
ENDIF.
ENDFORM. "select_from_konv
*&---------------------------------------------------------------------*
*& Form select_from_kna1
*&---------------------------------------------------------------------*
* select data from table kna1 into it_all
*----------------------------------------------------------------------*
FORM select_from_kna1.
IF it_all[] IS NOT INITIAL.
SELECT name1
kunnr
FROM kna1
INTO CORRESPONDING FIELDS OF TABLE it_kna1
FOR ALL ENTRIES IN it_all
WHERE kunnr = it_all-kunag.
ENDIF.
SORT it_kna1 BY kunnr.
IF sy-subrc <> 0.
MESSAGE i001(zjenni_msg).
ENDIF.
LOOP AT it_all INTO wa_all.
READ TABLE it_kna1 INTO wa_kna1
WITH KEY kunnr = wa_all-kunag BINARY SEARCH.
wa_all-name1 = wa_kna1-name1.
MODIFY it_all FROM wa_all TRANSPORTING name1.
ENDLOOP.
CLEAR wa_all.
ENDFORM. "select_from_kna1
*&---------------------------------------------------------------------*
*& Form select_from_vbap
*&---------------------------------------------------------------------*
* 1~select data from table vbap
* 2~get sales order total quantity
*----------------------------------------------------------------------*
FORM select_from_vbap.
DATA: it_temp TYPE t_vbap,
wa_temp TYPE LINE OF t_vbap.
IF it_all[] IS NOT INITIAL.
SELECT kwmeng
abgru
posnr
vbeln AS s_vbeln
FROM vbap
INTO CORRESPONDING FIELDS OF TABLE it_vbap
FOR ALL ENTRIES IN it_all
WHERE vbeln = it_all-aubel
AND posnr = it_all-b_posnr
AND abgru = ''.
ENDIF.
SORT it_vbap BY kwmeng.
IF sy-subrc <> 0.
MESSAGE i001(zjenni_msg).
ENDIF.
LOOP AT it_vbap INTO wa_vbap.
COLLECT wa_vbap INTO it_temp.
ENDLOOP.
CLEAR wa_vbap.
LOOP AT it_all INTO wa_all.
READ TABLE it_temp INTO wa_temp
WITH KEY s_vbeln = wa_all-aubel BINARY SEARCH.
wa_all-s_kwmeng = wa_temp-kwmeng.
MODIFY it_all FROM wa_all TRANSPORTING s_kwmeng.
ENDLOOP.
CLEAR wa_all.
ENDFORM. "select_from_vbap
*&---------------------------------------------------------------------*
*& Form get_gross_dis_amt
*&---------------------------------------------------------------------*
* get particular gross value, discount,discount amount
*----------------------------------------------------------------------*
FORM get_gross_dis_amt.
DATA: it_temp TYPE t_konv,
wa_temp TYPE LINE OF t_konv.
LOOP AT it_all INTO wa_all.
READ TABLE it_konv INTO wa_konv
WITH KEY knumv = wa_all-knumv
kposn = wa_all-b_posnr
BINARY SEARCH.
IF wa_konv-kschl = 'PR00'.
wa_all-kwert = wa_konv-kwert.
MODIFY it_all FROM wa_all TRANSPORTING kwert.
ENDIF.
IF wa_konv-kschl = 'RA01'.
wa_all-ra01_discount = wa_konv-kbetr / 10.
wa_all-ra01_dis_amt = wa_konv-kwert.
MODIFY it_all FROM wa_all TRANSPORTING ra01_discount.
MODIFY it_all FROM wa_all TRANSPORTING ra01_dis_amt.
ENDIF.
IF wa_konv-kschl = 'RA00'.
wa_all-ra01_discount = wa_konv-kbetr / 10.
wa_all-ra01_dis_amt = wa_konv-kwert.
MODIFY it_all FROM wa_all TRANSPORTING ra00_discount.
MODIFY it_all FROM wa_all TRANSPORTING ra00_dis_amt.
ENDIF.
CLEAR wa_konv.
ENDLOOP.
CLEAR wa_all.
ENDFORM. "get_gross_dis_amt
*&---------------------------------------------------------------------*
*& Form get_total_gross
*&---------------------------------------------------------------------*
* 1~get total gross value
* 2~when billing type is 's1',add negative to gross value and total gross
*----------------------------------------------------------------------*
FORM get_total_gross.
DATA: it_temp TYPE t_konv,
wa_temp TYPE LINE OF t_konv,
it_konv_temp TYPE t_konv,
wa_konv_temp TYPE LINE OF t_konv.
IF it_all[] IS NOT INITIAL.
SELECT kwert
knumv
kschl
FROM konv
INTO CORRESPONDING FIELDS OF TABLE it_konv_temp
FOR ALL ENTRIES IN it_all
WHERE knumv = it_all-knumv
AND kschl = 'PR00'.
ENDIF.
LOOP AT it_konv_temp INTO wa_konv_temp.
COLLECT wa_konv_temp INTO it_temp.
ENDLOOP.
CLEAR wa_konv_temp.
LOOP AT it_temp INTO wa_temp.
* write: at / wa_temp-kwert,
* '@'.
ENDLOOP.
LOOP AT it_all INTO wa_all.
READ TABLE it_temp INTO wa_temp
WITH KEY knumv = wa_all-knumv
BINARY SEARCH.
wa_all-s_kwert = wa_temp-kwert.
MODIFY it_all FROM wa_all TRANSPORTING s_kwert.
ENDLOOP.
CLEAR wa_all.
LOOP AT it_all INTO wa_all.
IF wa_all-fkart = 'S1'.
wa_all-kwert = -1 * wa_all-kwert.
wa_all-s_kwert = -1 * wa_all-s_kwert.
MODIFY it_all FROM wa_all TRANSPORTING kwert s_kwert.
ENDIF.
ENDLOOP.
CLEAR wa_all.
ENDFORM. "get_total_gross
*&---------------------------------------------------------------------*
*& Form get_total
*&---------------------------------------------------------------------*
* get all the total value into it_total_out
*----------------------------------------------------------------------*
FORM get_total.
DATA: it_total_temp TYPE t_all,
wa_total_temp TYPE LINE OF t_all.
LOOP AT it_total INTO wa_total.
COLLECT wa_total INTO it_total_out.
ENDLOOP.
CLEAR wa_total.
LOOP AT it_total_out INTO wa_total_out.
READ TABLE it_all INTO wa_all
WITH KEY b_vbeln = wa_total_out-b_vbeln BINARY SEARCH.
wa_total_out-kurrf = wa_all-kurrf.
wa_total_out-kwert = wa_all-s_kwert.
MODIFY it_total_out FROM wa_total_out TRANSPORTING kurrf kwert netwr.
ENDLOOP.
CLEAR wa_total_out.
ENDFORM. "get_total
*&---------------------------------------------------------------------*
*& Form build_fieldcatalog
*&---------------------------------------------------------------------*
* form fieldcatalog for both items and totals
*----------------------------------------------------------------------*
FORM build_fieldcatalog.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.