diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php index d53644782..4bae020db 100644 --- a/src/PartKeepr/PartBundle/Entity/Part.php +++ b/src/PartKeepr/PartBundle/Entity/Part.php @@ -823,24 +823,41 @@ public function recomputeStockLevels() { $sum = 0; $price = 0; + + $totalPartStockPrice = 0; + $lastPosEntryQuant = 0; + $lastPosEntryPrice = 0; foreach ($this->getStockLevels() as $stockLevel) { - - if ($stockLevel->getStockLevel() < 0) { - $this->setRemovals(true); - } else { - $price += $stockLevel->getPrice() * $stockLevel->getStockLevel(); - } + $sum += $stockLevel->getStockLevel(); - } - - if ($sum > 0) { - $this->setAveragePrice($price / $sum); - } else { - $this->setAveragePrice(0); - } + + if ($stockLevel->getStockLevel() > 0) { + + $lastPosEntryQuant = $stockLevel->getStockLevel(); + $lastPosEntryPrice = $stockLevel->getPrice(); + $totalPartStockPrice += $lastPosEntryPrice * $lastPosEntryQuant; + $price = $totalPartStockPrice / $sum; + } + else { + if ($sum < 0) { + $price = 0; + } + else { + if ($sum < $lastPosEntryQuant){ + $totalPartStockPrice = $sum * $lastPosEntryPrice; + $price = $totalPartStockPrice / $sum; + } + else { + $totalPartStockPrice += $stockLevel->getStockLevel() * $price; + $price = $totalPartStockPrice / $sum; + } + } + } + } $this->setStockLevel($sum); + $this->setAveragePrice($price); if ($sum < $this->getMinStockLevel()) { $this->setLowStock(true);