Write a special log entry to DB when a user withdraws/add/move parts stock

This commit is contained in:
Jan Böhmer 2023-01-08 01:22:02 +01:00
parent b2157c93e3
commit 4c94d9c150
6 changed files with 256 additions and 10 deletions

View file

@ -2,11 +2,20 @@
namespace App\Services\Parts;
use App\Entity\LogSystem\PartStockChangedLogEntry;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Services\LogSystem\EventLogger;
class PartLotWithdrawAddHelper
final class PartLotWithdrawAddHelper
{
private $eventLogger;
public function __construct(EventLogger $eventLogger)
{
$this->eventLogger = $eventLogger;
}
/**
* Checks whether the given part can
* @param PartLot $partLot
@ -80,7 +89,11 @@ class PartLotWithdrawAddHelper
}
//Subtract the amount from the part lot
$partLot->setAmount($partLot->getAmount() - $amount);
$oldAmount = $partLot->getAmount();
$partLot->setAmount($oldAmount - $amount);
$event = PartStockChangedLogEntry::withdraw($partLot, $oldAmount, $partLot->getAmount(), $part->getAmountSum() , $comment);
$this->eventLogger->log($event);
return $partLot;
}
@ -111,8 +124,11 @@ class PartLotWithdrawAddHelper
throw new \RuntimeException("Cannot add to this part lot!");
}
//Subtract the amount from the part lot
$partLot->setAmount($partLot->getAmount() + $amount);
$oldAmount = $partLot->getAmount();
$partLot->setAmount($oldAmount + $amount);
$event = PartStockChangedLogEntry::add($partLot, $oldAmount, $partLot->getAmount(), $part->getAmountSum() , $comment);
$this->eventLogger->log($event);
return $partLot;
}
@ -154,9 +170,14 @@ class PartLotWithdrawAddHelper
throw new \RuntimeException('Not enough stock to withdraw!');
}
$oldOriginAmount = $origin->getAmount();
//Subtract the amount from the part lot
$origin->setAmount($origin->getAmount() - $amount);
//And add it to the target
$target->setAmount($target->getAmount() + $amount);
$event = PartStockChangedLogEntry::move($origin, $oldOriginAmount, $origin->getAmount(), $part->getAmountSum() , $comment, $target);
$this->eventLogger->log($event);
}
}