I have following condition
buying 3 pen gives 1 free pencil
buying 5 pen gives 1 free rubber
buying 10 pen gives 1 free chocolate


so if some one buys 19 pen then his free items will be

1 chocolate due to 10 pen
1 rubber due to remaining (19-10) pen
1 pencil due to remaining (9-5) pen

I have objects of sales_details where trnqty is the quantity purchased how to calculate the free items using drl