Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Average Price of an Item
Message
From
30/09/1997 15:48:47
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
 
 
To
27/09/1997 00:33:13
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00051801
Message ID:
00052512
Views:
46
>items left in stock @ $5. Value = $450, 1000 items at $3 value = $3000. >Total >value = 3450. Avg price of issue is 3450/1090 = 3.165 > >As this goes on, the average price keeps on changing. If after 2 months, >somebody realizes that the operator had wrongly typed an extra ZERO and the >qty was 100 and price $3, this would change the price all the way down the >line. > >Any suggestions? Ah, if you want to recalculate retroactively, you'll need an index (temporary or not) by the item and date (so the buy/issue records come in proper order). Now
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
ends
This 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
   endif
This way we bury the roundoff error into the last issue and live happily ever after.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Reply
Map
View

Click here to load this message in the networking platform