@notrinos

yes I just checked the history of this file. I found that earlier it was

```
if ($qty > 0 && ($qoh != -$qty))
$avg_cost = ($avg_cost*($qoh+$qty_delayed)+$unit_cost*$qty_new)/($qoh+$qty);
```

And I think this was perfect because this was taking the Weighted Average of the Item Produced

Later this was changed to

```
if ($qty > 0 && ($qoh != -$qty)) {
if ($qoh == 0 && $avg_cost != 0)
$avg_cost = ($avg_cost + ($unit_cost*$qty_new)/$qty)/2;
else
$avg_cost = ($avg_cost*($qoh+$qty_delayed)+$unit_cost*$qty_new)/($qoh+$qty);
}
```

Here I am unable to understand that why we need to take Simple Average for condition

`if ($qoh == 0 && $avg_cost != 0)`

Assume following transactions

1. Item A was produced qty=300 with cost allocated after WO process = 50

2. Item A was sold qty = 300 @ 200 price

3. Item A was again produced qty = 400 and cost allocated after WO Process = 60

Now as per your revised condition the Average Material Cost calculation is taking the weightage of Cost 50 (Existing Avg Cost)

hence New $avg_cost will be calculated as 55

While the Old working will calculate the Weighted Average, that will Nullify the Weightage of 50 (Existing Avg Cost) and New $avg_cost will be calculated as 60, that is required.

www.boxygen.pk