How to calculate opening/closing balance of inventory items (products)

Kindly help please.

A file containing the shotscreen of the problem is attached. Please, view it for more insight. Thanks. 

On 7th Jun product available for sale is supposed to be the Balance on 2 Jun + Purchase on 7th Jun. And Balance on the 7th of Jun supposed to be Product available for sale - sale.

On 2 Jun product available for sale supposed to be  number purchased on 2 Jun + Balance on 1 Jun. And balance on 2 Jun supposed to be product available for sale on 2 Jun - sale of Jun 2.

On Jun 1 product available for sale supposed to be quantity in stock (begin balance)

NOTE: quantity in stock = 20 (this figure was added from the productDetail screen)

Please, kindly help me provide a solution to this challenge. Thanks

I'm sorry, I'm afraid I didn't get what you really need. Could you elaborate?