Sales Invoice Discount Table

i have this custom child table for discount in sales invoice. i need to count all of the discount and there are 2 type of discount which is amount and percentage, when i rearrange the position, the total need to be count again from the start.


this is the table. is anyone can help me with this ?