partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 0919df20c5b360e23bd2e7c741c2e3f802af40bc
parent ddc47b03a64b9365f7acd1c541152bd01e5a6db6
Author: Timo A. Hummel <felicitus@felicitus.org>
Date:   Sun, 24 Apr 2016 22:32:05 +0200

Merge pull request #651 from tinutac/master

[Bugfix] Divide by zero error when migrating database schema on upgrade install initial setup #650
Diffstat:
Msrc/PartKeepr/PartBundle/Entity/Part.php | 60++++++++++++++++++++++++++++--------------------------------
1 file changed, 28 insertions(+), 32 deletions(-)

diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php @@ -832,8 +832,8 @@ class Part extends BaseEntity public function recomputeStockLevels() { - $sum = 0; - $price = 0; + $currentStock = 0; + $avgPrice = 0; $totalPartStockPrice = 0; $lastPosEntryQuant = 0; @@ -842,39 +842,35 @@ class Part extends BaseEntity foreach ($this->getStockLevels() as $stockLevel) { - $sum += $stockLevel->getStockLevel(); - - if ($stockLevel->getStockLevel() > 0) { - - $lastPosEntryQuant = $stockLevel->getStockLevel(); - $lastPosEntryPrice = $stockLevel->getPrice(); - $totalPartStockPrice += $lastPosEntryPrice * ($lastPosEntryQuant + $negativeStock); - $price = $totalPartStockPrice / $sum; - } - else { - if ($sum <= 0) { - $price = 0; - $totalPartStockPrice = 0; - $negativeStock = $sum; - } - else { - $negativeStock = 0; - if ($sum < $lastPosEntryQuant){ - $totalPartStockPrice = $sum * $lastPosEntryPrice; - $price = $totalPartStockPrice / $sum; - } - else { - $totalPartStockPrice += $stockLevel->getStockLevel() * $price; - $price = $totalPartStockPrice / $sum; - } - } - } + $currentStock += $stockLevel->getStockLevel(); + + if ($currentStock <= 0) { + $avgPrice = 0; + $totalPartStockPrice = 0; + $negativeStock = $currentStock; + } else { + if ($stockLevel->getStockLevel() > 0) { + $lastPosEntryQuant = $stockLevel->getStockLevel(); + $lastPosEntryPrice = $stockLevel->getPrice(); + $totalPartStockPrice += $lastPosEntryPrice * ($lastPosEntryQuant + $negativeStock); + $avgPrice = $totalPartStockPrice / $currentStock; + } else { + if ($currentStock < $lastPosEntryQuant) { + $totalPartStockPrice = $currentStock * $lastPosEntryPrice; + $avgPrice = $totalPartStockPrice / $currentStock; + } else { + $totalPartStockPrice += $stockLevel->getStockLevel() * $avgPrice; + $avgPrice = $totalPartStockPrice / $currentStock; + } + $negativeStock = 0; + } + } } - $this->setStockLevel($sum); - $this->setAveragePrice($price); + $this->setStockLevel($currentStock); + $this->setAveragePrice($avgPrice); - if ($sum < $this->getMinStockLevel()) { + if ($currentStock < $this->getMinStockLevel()) { $this->setLowStock(true); } else { $this->setLowStock(false);