Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Need help regarding calculation..

Former Member
0 Likes
1,016

Dear Gurus I Need help regarding calculation..

I have made a smartform for T-code vf02.

There is a Billing Document For Ex 3900000008

item  	Description	Billed Quantity	               Net Value	            Material
10	        BS2P-20	        0.00	                0.00	                     BS2P-20
11	        BS2P-20	        1,000.00	        180,000.00	     BS2P-20
12	        BS2P-20	        1,000.00	        180,000.00	     BS2P-20
20	        BS2P-20	        0.00	                0.00	                     BS2P-20
21	        BS2P-20	        1,500.00	        270,000.00 	     BS2P-20
22	        BS2P-20	        1,500.00	        270,000.00	     BS2P-20

I want to write a code that

If the first line item containing a billed quantity 0 then add billed quantity of line item 11 and 12 and like wise.

below is my code. But im having problem in that please help me out

LOOP AT itab_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.
    IF  vbrk_tab-fkimg NE 0.
      " MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      " MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      " MODIFY itab_tab.
      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.
        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.
      ENDLOOP.
      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.

    ELSEIF vbrk_tab-fkimg EQ 0.
      LOOP AT itab_tab.
        READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
        READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.
        LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

          vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
          itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.

        ENDLOOP.
        MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
        MOVE vbrk_tab-arktx  TO itab_tab-arktx .
        MODIFY itab_tab.

      ENDLOOP.
    ENDIF.
    DELETE ADJACENT DUPLICATES FROM itab_tab COMPARING fkimg.

  ENDLOOP.

Regards

Saad Nisar.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
986

Hi,

Thats right. Its not advisable to loop at a itab with the loop on the same itab. Just use this,


LOOP AT itab_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.

    IF  vbrk_tab-fkimg EQ 0.

      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.

      ENDLOOP.

      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.
ENDLOOP.

Regards,

Vik

10 REPLIES 10
Read only

Former Member
0 Likes
986

Hi,

First Append all data in internal table and then go for calculation, as you r doing.

Calculate all fields in single loop. insted of sub loop in main loop.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
986

This logic might help you.

use control break statements

loop at itab into wa.

at new line-item.

clear qty.

clear flag.

if wa-quantity = 0.

flag = 'T'.

else.

flag = 'F'.

endif.

endat.

if flag =;'T'

qty = qty + wa-quantity.

endif.

endloop.

Read only

Former Member
0 Likes
986

Hi

I am not quite clear with what you have been trying to do when fkimg <> 0..

However, ur code..

READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.

READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.

LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.

itab_tab-kwert = vbrk_tab-fkimg * itab_tab-kbetr.

ENDLOOP.

MOVE vbrk_tab-fkimg TO itab_tab-fkimg.

MOVE vbrk_tab-arktx TO itab_tab-arktx .

MODIFY itab_tab.

should itself be sufficient to provide u with te sum total of qty of all other lines with same aupos. I dont think u need to have the LOOP on ur itab again..

It is never advisable to delete or append the table in the LOOP on the same table.

Revert in more detail, if we can discuss further.

Read only

0 Likes
986

vbrk_tab-fkimg eq 0 means that the bill quantity is 0

actually i want to add those values of billed quantity which contains batch split.

regards

Saad Nisar

Read only

Former Member
0 Likes
986

vbrk_tab-fkimg eq 0 means that the bill quantity is 0

actually i want to add those values of billed quantity which contains batch split.

regards

Saad Nisar

Read only

0 Likes
986

Hi,

In case of fkimg NE 0 also u hav written the logic to add up qty..

I think u may just want to do nothin in that case and write the logic in part 2 when fkimg eq 0 without the nested loop on ur itab again.. and it shud work..

Revert with results.

Read only

Former Member
0 Likes
987

Hi,

Thats right. Its not advisable to loop at a itab with the loop on the same itab. Just use this,


LOOP AT itab_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.

    IF  vbrk_tab-fkimg EQ 0.

      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.

      ENDLOOP.

      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.
ENDLOOP.

Regards,

Vik

Read only

0 Likes
986

i want to show that result too when fkimg is not equal to 0.

LOOP AT itab_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.

    IF  vbrk_tab-fkimg EQ 0.
      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.

      ENDLOOP.
      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
    modify itab_tab.
endif.

  ENDLOOP.

If i use this code it works properly when the fkimg is eq to 0

but when the fkimg is not equal to 0 it do nothing..

if i write this code

LOOP AT itab_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.

    IF  vbrk_tab-fkimg EQ 0.
      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.

        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.

      ENDLOOP.
      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .

    ENDIF.

    IF vbrk_tab-fkimg NE 0.
      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.
    ENDIF.
 "   DELETE ADJACENT DUPLICATES FROM itab_tab COMPARING fkimg.

  ENDLOOP.

it doubles my entries when fkimg is not equal to 0. which i dont want to do.

Read only

0 Likes
986

Hi,

The try something like this,


LOOP AT itab_tab.
clear: vbrk-tab, vbrp_tab.
    READ TABLE vbrk_tab WITH KEY posnr = itab_tab-posnr.
if sy-subrc = 0.
    READ TABLE vbrp_tab WITH KEY posnr = itab_tab-posnr.
 if sy-subrc = 0.
    IF  vbrk_tab-fkimg EQ 0.
      LOOP AT vbrp_tab WHERE aupos EQ vbrk_tab-aupos.
 
        vbrk_tab-fkimg = vbrp_tab-fkimg + vbrk_tab-fkimg.
        itab_tab-kwert =   vbrk_tab-fkimg * itab_tab-kbetr.
 
      ENDLOOP.
      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.
 else.

      MOVE vbrk_tab-fkimg TO  itab_tab-fkimg.
      MOVE vbrk_tab-arktx  TO itab_tab-arktx .
      MODIFY itab_tab.
    ENDIF.
  endif.
endif.
  ENDLOOP.

Regards,

Vikranth

Read only

Former Member
0 Likes
986

Thank You All for your support..

Thanks vikred ..

I got my result i was getting all the values so i wrote

delete itab_tab where size eq ''.

and i got what i was requiring it.

thanks again.