<?php
namespace App\EventListener\Client\Store;
use App\Entity\Client\Store\StoreHeader;
use App\Entity\Client\Store\StoreMove;
use App\EventListener\GenericEvent;
use App\Service\AppManager;
use App\Service\Client\Store\StoreHeaderManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class StoreMoveListener
{
private $appManager;
private $storeHeaderManager;
public function __construct(StoreHeaderManager $storeHeaderManager, AppManager $appManager)
{
$this->appManager = $appManager;
$this->storeHeaderManager = $storeHeaderManager;
}
public function addMove(GenericEvent $genericEvent)
{
$storeMove = $genericEvent->getSubject();
if (!$storeMove instanceof StoreMove) {
return;
}
$storeHeader = $storeMove->getHeader();
if (!$storeHeader instanceof StoreHeader) {
return;
}
if (!$storeHeader->isTemporary()){
$genericEvent->setResponse(new JsonResponse(json_encode(['message' => 'Header is locked', 'errorCode' => '1001']), 403, [], true));
$genericEvent->stopPropagation();
return;
}
$allowedTypes = [StoreHeader::STORE_HEADER_WAREHOUSE_RECEIPT, StoreHeader::STORE_HEADER_WAREHOUSE_RECEIPT_RETURN, StoreHeader::STORE_HEADER_STORE_TRANSFER];
if (!in_array($storeHeader->getType(), $allowedTypes)) {
return;
}
$haveProductInMoves = $storeHeader->getMoves()->filter(function (StoreMove $existStoreMove) use ($storeMove) {
return $existStoreMove->getProduct()->getId() === $storeMove->getProduct()->getId() && $existStoreMove->getId() !== $storeMove->getId();
});
foreach ($haveProductInMoves as $haveProductInMove) {
$storeHeader->removeMove($haveProductInMove);
}
$this->appManager->eventPersist($storeHeader, AppManager::UPDATE, 'move_add');
$genericEvent->setResponse(new Response($storeMove->getId(), 201));
$genericEvent->stopPropagation();
}
public function removeMove(GenericEvent $genericEvent)
{
$storeMove = $genericEvent->getSubject();
if (!$storeMove instanceof StoreMove) {
return;
}
$storeHeader = $storeMove->getHeader();
if (!$storeHeader instanceof StoreHeader) {
return;
}
$this->isLocked($storeHeader, $genericEvent);
if ($genericEvent->isPropagationStopped()){
return;
}
if(!StoreHeaderManager::isWarehouseType($storeHeader->getType())){
return;
}
$haveProductInMoves = $storeHeader->getMoves()->filter(function (StoreMove $existStoreMove) use ($storeMove) {
return $existStoreMove->getProduct()->getId() === $storeMove->getProduct()->getId() && $existStoreMove->getId() !== $storeMove->getId();
});
foreach ($haveProductInMoves as $haveProductInMove) {
$storeHeader->removeMove($haveProductInMove);
}
$this->appManager->eventPersist($storeHeader, AppManager::UPDATE, 'move_delete');
$genericEvent->setResponse(new Response(null, 204));
$genericEvent->stopPropagation();
}
private function isLocked(StoreHeader $storeHeader, GenericEvent $genericEvent)
{
if (!$storeHeader->isTemporary()){
$genericEvent->setResponse(new JsonResponse(json_encode(['message' => 'Header is locked', 'errorCode' => '1001']), 403, [], true));
$genericEvent->stopPropagation();
}
}
}