<?php
namespace App\EventListener\Custom\Labas;
use App\Entity\Central\Client\Client;
use App\Entity\Client\PointOfSale\PointOfSaleMove;
use App\Entity\Client\Store\StoreHeader;
use App\EventListener\GenericEvent;
use App\Exception\ValidationException;
use App\Model\ShellExec;
use App\Service\AppManager;
use App\Service\ShellExecManager;
use App\Utils\ClientUtils;
use Eshop\Entity\Customer\CustomerOrder;
use Symfony\Contracts\Translation\TranslatorInterface;
class PostListener
{
private ShellExecManager $shellExecManager;
private AppManager $appManager;
private TranslatorInterface $translator;
public function __construct(ShellExecManager $shellExecManager, AppManager $appManager, TranslatorInterface $translator)
{
$this->translator = $translator;
$this->appManager = $appManager;
$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;
}
$this->runCommand('export_paragon', $entity->getId());
}
public function postPointOfSaleMoveCreate(GenericEvent $genericEvent)
{
/** @var PointOfSaleMove $entity */
$entity = $genericEvent->getSubject();
$this->isClient($genericEvent->getAppManager()->getClient());
if (!$entity instanceof PointOfSaleMove) {
return;
}
$this->runCommand('export_point_of_sale_move', $entity->getId());
}
public function postEshopCustomerOrder(GenericEvent $genericEvent)
{
/** @var CustomerOrder $entity */
$entity = $genericEvent->getSubject();
$this->isClient($genericEvent->getAppManager()->getClient());
if (!$entity instanceof CustomerOrder) {
return;
}
if ($entity->getState() !== CustomerOrder::STATE_CONFIRMED || $entity->getFinishedAt() === null) {
return;
}
$invalidCoupons = [];
foreach ($entity->getDiscountCoupons() as $discountCoupon) {
if($discountCoupon->isValid() === false) {
$invalidCoupons[] = $this->translator->trans('discount_coupon.can_not_be_used', ['%code%' => $discountCoupon->getCode()], 'validators');
continue;
}
$discountCoupon->setAlreadyUsed($discountCoupon->getAlreadyUsed() + 1);
$this->appManager->persist($discountCoupon);
}
if (count($invalidCoupons) > 0) {
throw new ValidationException(['discountCoupons' => $invalidCoupons]);
}
if ($entity->getDiscountCoupons()->count() > 0) {
$this->appManager->flush();
}
$this->runCommand('export_eshop_order', $entity->getId());
$this->runCommand('eshop:email_customer_order', $entity->getId());
sleep(10);
$this->changeCustomerOrderState($genericEvent);
}
public function changeCustomerOrderState(GenericEvent $genericEvent)
{
$entity = $genericEvent->getSubject();
$this->isClient($genericEvent->getAppManager()->getClient());
if (!$entity instanceof CustomerOrder) {
return;
}
$this->runCommand('eshop:firebase_customer_order', $entity->getId());
}
private function isClient($client)
{
if (!$client instanceof Client || $client->getCode() !== ClientUtils::LABAS) {
throw new \Exception('Bad client');
}
}
private function runCommand(string $labasCommand, int $entityId, bool $onBackground = true)
{
$shellExec = new ShellExec('labas:' . $labasCommand, $entityId . ' --kernel=labas');
$this->shellExecManager->runShellExec($shellExec, $onBackground);
}
}