src/App/EventListener/Custom/Labas/PostListener.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Custom\Labas;
  3. use App\Entity\Central\Client\Client;
  4. use App\Entity\Client\PointOfSale\PointOfSaleMove;
  5. use App\Entity\Client\Store\StoreHeader;
  6. use App\EventListener\GenericEvent;
  7. use App\Exception\ValidationException;
  8. use App\Model\ShellExec;
  9. use App\Service\AppManager;
  10. use App\Service\ShellExecManager;
  11. use App\Utils\ClientUtils;
  12. use Eshop\Entity\Customer\CustomerOrder;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class PostListener
  15. {
  16.     private ShellExecManager $shellExecManager;
  17.     private AppManager $appManager;
  18.     private TranslatorInterface $translator;
  19.     public function __construct(ShellExecManager $shellExecManagerAppManager $appManagerTranslatorInterface $translator)
  20.     {
  21.         $this->translator $translator;
  22.         $this->appManager $appManager;
  23.         $this->shellExecManager $shellExecManager;
  24.     }
  25.     public function postStoreHeaderCreate(GenericEvent $genericEvent)
  26.     {
  27.         /** @var StoreHeader $entity */
  28.         $entity $genericEvent->getSubject();
  29.         $this->isClient($genericEvent->getAppManager()->getClient());
  30.         if (!$entity instanceof StoreHeader || $entity->getType() !== StoreHeader::STORE_HEADER_CUSTOMER_RECEIPT) {
  31.             return;
  32.         }
  33.         $this->runCommand('export_paragon'$entity->getId());
  34.     }
  35.     public function postPointOfSaleMoveCreate(GenericEvent $genericEvent)
  36.     {
  37.         /** @var PointOfSaleMove $entity */
  38.         $entity $genericEvent->getSubject();
  39.         $this->isClient($genericEvent->getAppManager()->getClient());
  40.         if (!$entity instanceof PointOfSaleMove) {
  41.             return;
  42.         }
  43.         $this->runCommand('export_point_of_sale_move'$entity->getId());
  44.     }
  45.     public function postEshopCustomerOrder(GenericEvent $genericEvent)
  46.     {
  47.         /** @var CustomerOrder $entity */
  48.         $entity $genericEvent->getSubject();
  49.         $this->isClient($genericEvent->getAppManager()->getClient());
  50.         if (!$entity instanceof CustomerOrder) {
  51.             return;
  52.         }
  53.         if ($entity->getState() !== CustomerOrder::STATE_CONFIRMED || $entity->getFinishedAt() === null) {
  54.             return;
  55.         }
  56.         $invalidCoupons = [];
  57.         foreach ($entity->getDiscountCoupons() as $discountCoupon) {
  58.             if($discountCoupon->isValid() === false) {
  59.                 $invalidCoupons[] = $this->translator->trans('discount_coupon.can_not_be_used', ['%code%' => $discountCoupon->getCode()], 'validators');
  60.                 continue;
  61.             }
  62.             $discountCoupon->setAlreadyUsed($discountCoupon->getAlreadyUsed() + 1);
  63.             $this->appManager->persist($discountCoupon);
  64.         }
  65.         if (count($invalidCoupons) > 0) {
  66.             throw new ValidationException(['discountCoupons' => $invalidCoupons]);
  67.         }
  68.         if ($entity->getDiscountCoupons()->count() > 0) {
  69.             $this->appManager->flush();
  70.         }
  71.         $this->runCommand('export_eshop_order'$entity->getId());
  72.         $this->runCommand('eshop:email_customer_order'$entity->getId());
  73.         sleep(10);
  74.         $this->changeCustomerOrderState($genericEvent);
  75.     }
  76.     public function changeCustomerOrderState(GenericEvent $genericEvent)
  77.     {
  78.         $entity $genericEvent->getSubject();
  79.         $this->isClient($genericEvent->getAppManager()->getClient());
  80.         if (!$entity instanceof CustomerOrder) {
  81.             return;
  82.         }
  83.         $this->runCommand('eshop:firebase_customer_order'$entity->getId());
  84.     }
  85.     private function isClient($client)
  86.     {
  87.         if (!$client instanceof Client || $client->getCode() !== ClientUtils::LABAS) {
  88.             throw new \Exception('Bad client');
  89.         }
  90.     }
  91.     private function runCommand(string $labasCommandint $entityIdbool $onBackground true)
  92.     {
  93.         $shellExec = new ShellExec('labas:' $labasCommand$entityId ' --kernel=labas');
  94.         $this->shellExecManager->runShellExec($shellExec$onBackground);
  95.     }
  96. }