public function recomputeStockLevels()
{
$currentStock = 0;
$avgPrice = 0;
$totalPartStockPrice = 0;
$lastPosEntryQuant = 0;
$lastPosEntryPrice = 0;
$negativeStock = 0;
foreach ($this->getStockLevels() as $stockLevel) {
$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($currentStock);
$this->setAveragePrice($avgPrice);
if ($currentStock < $this->getMinStockLevel()) {
$this->setLowStock(true);
} else {
$this->setLowStock(false);
}
}
/** * Updates the stock level for a specific part * * @param Part $part The part to update * @param OnFlushEventArgs $eventArgs The event arguments */ protected function updateStockLevel(Part $part, OnFlushEventArgs $eventArgs) { $entityManager = $eventArgs->getEntityManager(); $part->recomputeStockLevels(); $entityManager->getUnitOfWork()->recomputeSingleEntityChangeSet($entityManager->getClassMetadata(get_class($part)), $part); }