cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

End routine logic not working with multiple record load via dtp.

joice_samuel1
Explorer
0 Likes
333

Hi Team,

We've written a end routine logic for calculating sales value %, sales quantity % & Average price. But if we execute the dtp with a single record this will give correct result. but for more records scenario this calculation not working.


For calculating sales value %, we need to take the sum of division wise net value sum based on bill num.
Also we need to take the sum of material group wise net value sum based on bill num.
Sales value % formula = ( material group wise net value sum / division wise net value sum ) * 100
above logic is used for sales quantity % also.
For Average price = sum of net price / count based on division

DATA LV_TOTALVALUE_MG TYPE DECIMALS 2,
       LV_TOTALQTY_MG TYPE I,
       LV_TOTALVALUE_DIV TYPE DECIMALS 2,
       LV_TOTALQTY_DIV TYPE I,
       LV_AVSELLPRICE_MG TYPE DECIMALS 2,
       LV_AVSELLPRICE_DIV TYPE DECIMALS 2,
       LV_CNT TYPE I.

TYPES: BEGIN OF ZSOTABLE,
BILL_NUM TYPE /BI0/OIBILL_NUM,
BILL_ITEM TYPE /BI0/OIBILL_ITEM,
ZZREGION TYPE /BI0/OIREGION,
ZZCOUNTRY TYPE /BI0/OICOUNTRY,
END OF ZSOTABLE.
TYPES: BEGIN OF ZSOTABLE1,
/BIC/ZZBILLNUM TYPE /BIC/OIZZBILLNUM,
* /BIC/ZZBILITM TYPE /BI0/OIBILL_ITEM,
PRICE_DATE TYPE /BI0/OIPRICE_DATE,
* NET_PRICE TYPE /BI0/OINET_PRICE,
END OF ZSOTABLE1.
TYPES: BEGIN OF ZSOTABLE2,
/BIC/ZZSTATE TYPE /BIC/OIZZSTATE,
ZZCOUNTRY TYPE /BIC/OIZZCOUNTRY,
ZZSTATCOD TYPE /BIC/OIZZSTATCOD,
/BIC/ZZREGION TYPE /BIC/OIZZREGION,

END OF ZSOTABLE2.
DATA : ZSORDERS TYPE STANDARD TABLE OF ZSOTABLE,
WA_ZSORDERS TYPE ZSOTABLE. " internal table for /BIC/AZ_D_BITM00

DATA : ZSORDERS1 TYPE STANDARD TABLE OF ZSOTABLE1,
WA_ZSORDERS1 TYPE ZSOTABLE1. "internal table for /BIC/AZSD_O1000


DATA : ZSORDERS2 TYPE STANDARD TABLE OF ZSOTABLE2,
WA_ZSORDERS2 TYPE ZSOTABLE2.
SELECT DOC_NUMBER
DOC_ITEM
REGION
COUNTRY
FROM /BIC/AZ_D_BITM00 INTO TABLE ZSORDERS FOR ALL
"From Billing Item (Granual Level) Lookup DSO
ENTRIES IN RESULT_PACKAGE WHERE
DOC_NUMBER = RESULT_PACKAGE-BILL_NUM AND
DOC_ITEM = RESULT_PACKAGE-BILL_ITEM.
SELECT /BIC/ZZBILLNUM
* /BIC/ZZBILITM
PRICE_DATE
* NET_PRICE
FROM /BIC/AZSD_O1000 INTO TABLE ZSORDERS1 FOR ALL
" From Loss of Sales Lookup DSO
ENTRIES IN RESULT_PACKAGE WHERE
/BIC/ZZBILLNUM = RESULT_PACKAGE-BILL_NUM.
SELECT /BIC/ZZSTATE
/BIC/ZZCOUNTRY
/BIC/ZZSTATCOD
/BIC/ZZREGION
FROM /BIC/AZSD_O1300 INTO TABLE ZSORDERS2.
SORT RESULT_PACKAGE BY BILL_NUM BILL_ITEM DIVISION MATL_GROUP.
DELETE RESULT_PACKAGE WHERE INV_QTY is INITIAL.
SORT ZSORDERS BY BILL_NUM BILL_ITEM ASCENDING.

SORT ZSORDERS1 BY /BIC/ZZBILLNUM ASCENDING.

SORT ZSORDERS2 BY ZZSTATCOD ASCENDING.
LOOP AT RESULT_PACKAGE INTO WA_PACKAGE.
READ TABLE ZSORDERS1 INTO WA_ZSORDERS1 WITH KEY
/BIC/ZZBILLNUM = WA_PACKAGE-BILL_NUM.
IF SY-SUBRC = 0.
ENDIF.

MODIFY RESULT_PACKAGE from WA_PACKAGE.

CLEAR WA_PACKAGE.

ENDLOOP.
CLEAR IT_RESULT_PACKAGE[].
IT_RESULT_PACKAGE[] = RESULT_PACKAGE[].

""Repeat the SORT
SORT RESULT_PACKAGE BY BILL_NUM BILL_ITEM DIVISION MATL_GROUP.
SORT IT_RESULT_PACKAGE BY BILL_NUM BILL_ITEM DIVISION MATL_GROUP.
CLEAR WA_PACKAGE.
LOOP AT RESULT_PACKAGE INTO WA_PACKAGE.
READ TABLE ZSORDERS INTO WA_ZSORDERS WITH KEY
BILL_NUM = WA_PACKAGE-BILL_NUM
BILL_ITEM = WA_PACKAGE-BILL_ITEM.
IF SY-SUBRC = 0.
WA_PACKAGE-/BIC/ZZSTATCOD =
WA_ZSORDERS-ZZREGION.
WA_PACKAGE-/BIC/ZZCOUNTRY =
WA_ZSORDERS-ZZCOUNTRY.

ENDIF.

READ TABLE ZSORDERS2 INTO WA_ZSORDERS2 WITH KEY
ZZSTATCOD = WA_PACKAGE-/BIC/ZZSTATCOD
ZZCOUNTRY = WA_PACKAGE-/BIC/ZZCOUNTRY.
IF SY-SUBRC = 0.
ENDIF.
LOOP AT IT_RESULT_PACKAGE INTO WA_RESULT.
IF WA_RESULT-DIVISION = WA_PACKAGE-DIVISION AND
WA_RESULT-MATL_GROUP = WA_PACKAGE-MATL_GROUP.

""For Material Grp wise Total
LV_TOTALQTY_MG = LV_TOTALQTY_MG + WA_RESULT-INV_QTY.
LV_TOTALVALUE_MG = LV_TOTALVALUE_MG + WA_RESULT-NET_VALUE.

""For Division wise Total
LV_TOTALQTY_DIV = LV_TOTALQTY_DIV + WA_RESULT-INV_QTY.
LV_TOTALVALUE_DIV = LV_TOTALVALUE_DIV + WA_RESULT-NET_VALUE.

LV_CNT = LV_CNT + 1.
LV_AVSELLPRICE_MG = LV_AVSELLPRICE_MG + WA_RESULT-NET_PRICE.

ELSE.

IF WA_RESULT-DIVISION = WA_PACKAGE-DIVISION.

LV_TOTALQTY_DIV = LV_TOTALQTY_DIV + WA_RESULT-INV_QTY.
LV_TOTALVALUE_DIV = LV_TOTALVALUE_DIV + WA_RESULT-NET_VALUE.

ENDIF.

ENDIF.

CLEAR : WA_RESULT.

ENDLOOP.
IF LV_TOTALQTY_DIV NE 0.
WA_PACKAGE-/BIC/ZSALESQTY = ( LV_TOTALQTY_MG / LV_TOTALQTY_DIV ) * 100.

ENDIF.
IF LV_TOTALVALUE_DIV NE 0.
WA_PACKAGE-/BIC/ZSALESVAL = ( LV_TOTALVALUE_MG / LV_TOTALVALUE_DIV ) *
100.

ENDIF.
IF LV_CNT NE 0.
WA_PACKAGE-/BIC/ZAVGPRICE = ( LV_AVSELLPRICE_MG / LV_CNT ).

ENDIF.

MODIFY RESULT_PACKAGE FROM WA_PACKAGE.

CLEAR: LV_TOTALQTY_MG,
LV_TOTALQTY_DIV,
LV_TOTALVALUE_MG,
LV_TOTALVALUE_DIV,
LV_AVSELLPRICE_MG,
LV_CNT,
WA_PACKAGE.

ENDLOOP.

DELETE RESULT_PACKAGE WHERE BILL_NUM IS INITIAL.

Accepted Solutions (0)

Answers (1)

Answers (1)

TanweerZaki
Discoverer
0 Likes

You can easily debug and find the underlying reason. Seems with multiple record conditions are failing. I advise to run the DTP with multiple record and debug the code. You can set the watch point with desired variable value and then debug. I am sure this will make you understand where the code is failing.