Skip to content

Commit

Permalink
Merge pull request #651 from tinutac/master
Browse files Browse the repository at this point in the history
[Bugfix] Divide by zero error when migrating database schema on upgrade install initial setup #650
  • Loading branch information
Timo A. Hummel committed Apr 24, 2016
2 parents ddc47b0 + 161f04d commit 0919df2
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/PartKeepr/PartBundle/Entity/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ public function getProjectNames()

public function recomputeStockLevels()
{
$sum = 0;
$price = 0;
$currentStock = 0;
$avgPrice = 0;

$totalPartStockPrice = 0;
$lastPosEntryQuant = 0;
Expand All @@ -842,39 +842,35 @@ public function recomputeStockLevels()

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);
Expand Down

0 comments on commit 0919df2

Please sign in to comment.