I need to calculate the cost price of the item at the point of sale by doing the following and using first in first out logic:
* Work out how many of the good receipt lines are still relevant based on the 'on hand quantity' at point of sale.
* Work out the average cost of the parts in stock based on the quantity of each batch remaining and the unit cost of those items.
i.e. * For the first sale there are 85 items in stock, these are fulfilled from the goods received on 2016/07/23 so the cost of sale is 25.37* 2 = 50.74
* For the second sale, there are 125 items in stock, 100 of these came from goods received on 2017/11/23, the remaining 25 come from the goods received on 2016/09/15. The cost price should be ((100*30.25) + (25*28.72))/125
..(3025+718)/125 = 29.944
Any help on this greatly appreciated. I've managed to use our stock table to get as far as this but I'm struggling with how to implement this logic.