<?php
namespace App\EventListener\Custom\Cyprian;
use App\Entity\Central\Client\Client;
use App\Entity\Client\PointOfSale\PointOfSaleMove;
use App\Entity\Client\Store\StoreCentral;
use App\Entity\Client\Store\StoreHeader;
use App\Entity\Client\Store\StoreMove;
use App\EventListener\GenericEvent;
use App\Model\ShellExec;
use App\Service\Client\Store\StoreStockManager;
use App\Service\ShellExecManager;
use Custom\Base\CyprianBase;
class PostListener
{
private $shellExecManager;
private $storeStockManager;
public function __construct(ShellExecManager $shellExecManager, StoreStockManager $storeStockManager)
{
$this->storeStockManager = $storeStockManager;
$this->shellExecManager = $shellExecManager;
}
public function postStoreHeaderCreate(GenericEvent $genericEvent)
{
/** @var StoreHeader $entity */
$entity = $genericEvent->getSubject();
$this->isClient($genericEvent->getAppManager()->getClient());
if (!$entity instanceof StoreHeader || $entity->getType() !== StoreHeader::STORE_HEADER_CUSTOMER_RECEIPT){
return;
}
// $shellExec = new ShellExec('cyprian:export:store_header', '--id='.$entity->getId().' --kernel=custom');
// $this->shellExecManager->runShellExec($shellExec, true);
}
public function postInventoryCreate(GenericEvent $genericEvent)
{
/** @var StoreMove $entity */
$entity = $genericEvent->getSubject();
$appManager = $genericEvent->getAppManager();
if($entity->getType() !== StoreMove::STORE_MOVE_INVENTORY || $entity->getNote() === 'helios'){
return;
}
$this->isClient($appManager->getClient());
$storeStock = $this->storeStockManager->getStoreStock($entity->getProduct());
$storeStock->setUserInventory($storeStock->getUserInventory() + $entity->getStoreInventory()->getQuantity());
$appManager->persist($storeStock);
}
public function postPointOfSaleMoveCreate(GenericEvent $genericEvent)
{
/** @var PointOfSaleMove $entity */
$entity = $genericEvent->getSubject();
if (!$entity instanceof PointOfSaleMove) {
return;
}
$this->isClient($genericEvent->getAppManager()->getClient());
$shellExec = new ShellExec('cyprian:export:point_of_sale_move', '--id='.$entity->getId().' --kernel=custom');
$this->shellExecManager->runShellExec($shellExec, true);
}
public function postStoreCentralCreate(GenericEvent $genericEvent)
{
/** @var StoreCentral $entity */
$entity = $genericEvent->getSubject();
if (!$entity instanceof StoreCentral) {
return;
}
$this->isClient($genericEvent->getAppManager()->getClient());
$shellExec = new ShellExec('cyprian:export:full_store_central', '--id='.$entity->getId().' --kernel=custom');
$this->shellExecManager->runShellExec($shellExec, true);
}
private function isClient($client)
{
if (!$client instanceof Client || $client->getCode() !== CyprianBase::getClientCode()){
throw new \Exception('Bad client');
}
}
}