seek item_lc sum_ln=0 qty_ln=0 AvgPrice_ln=0 scan while item=item_lc if buy qty_ln=qty_ln+qty sum_ln=sum_ln+qty*price if qty_ln#0 AvgPrice_ln=sum_ln/qty_ln else sum_ln=0 qty_ln=0 endif else repl avgprice with AvgPrice_ln qty_ln=qty_ln-qty sum_ln=sum_ln-qty*price endif endsThis is how we usually do it here. Now there are some glitches to this (not as much the routine itself, but the average price method). You see that if ever the quantity on stock reaches zero, we assume its value is also zero; this may not be really the case - the average price has its roundoff error, and keeping sufficient number of decimals may not be quite good for all the cases. Take an example of a warehouse storing cars and corn, and have corn's price by the kilogram. The number to store the prices of both should be at least Currency type, if not Double. A way out of this is to store the qty*price too (in the Issue type records), so at least when you total (for reporting) you get the properly multiplied qty*avgprice, with much less roundoff error. Another thing to watch at, is that at the Issue side, if we issue the last of stock, there should be no remainder of value. The trick is to avoid calculating the average price for the issue, but to calculate the proportional value of it, and then just calculate the average price. Then the last lines read like this:
else repl value with qty*sum_ln/qty_ln; avgprice with value/qty qty_ln=qty_ln-qty if qty_ln=0 repl value with sum_ln; avgprice with value/qty sum_ln=0 endif endifThis way we bury the roundoff error into the last issue and live happily ever after.