custom/plugins/KzphSimplenote/src/Subscriber/OrderSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kzph\Simplenote\Subscriber;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Doctrine\DBAL\Connection;
  12. class OrderSubscriber implements EventSubscriberInterface
  13. {
  14.     private $kzphSimplenoteRepository;
  15.     private $requestStack;
  16.     private $systemConfigService;
  17.     private $connection;
  18.     private $config;
  19.     public function __construct(
  20.         EntityRepository $kzphSimplenoteRepository,
  21.         RequestStack $requestStack,
  22.         Connection $connection,
  23.         ?SystemConfigService $systemConfigService null
  24.     ) {
  25.         $this->kzphSimplenoteRepository $kzphSimplenoteRepository;
  26.         $this->requestStack $requestStack;
  27.         $this->connection $connection;
  28.         $this->systemConfigService $systemConfigService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  34.         ];
  35.     }
  36.     public function onOrderWritten(EntityWrittenEvent $entityWrittenEvent): void
  37.     {
  38.         $this->_userCommentAction($entityWrittenEvent);
  39.         $this->_replicateAction($entityWrittenEvent);
  40.     }
  41.     private function _userCommentAction($entityWrittenEvent)
  42.     {
  43.         try {
  44.             $config $this->systemConfigService->get('KzphSimplenote.config');
  45.             $this->config $config;
  46.             if (($this->config['showNotefieldInCheckout'] ?? false) === false)
  47.                 return;
  48.             $writeResults $entityWrittenEvent->getWriteResults();
  49.             $request $this->requestStack->getCurrentRequest();
  50.             foreach ($writeResults as $writeResult) {
  51.                 $payload $writeResult->getPayload();
  52.                 if (($payload['id'] ?? false)) {
  53.                     if (($request->get('kzphSimpleNote') ?? false)) {
  54.                         $this->kzphSimplenoteRepository->upsert([
  55.                             [
  56.                                 'id' => $payload['id'],
  57.                                 'entityId' => $payload['id'],
  58.                                 'entityType' => 'order',
  59.                                 'username' => 'System (Frontend)',
  60.                                 'note' => $request->get('kzphSimpleNote'),
  61.                                 'showDesktop' => 1,
  62.                                 'showMessage' => 0,
  63.                                 'replicateInOrder' => 0
  64.                             ],
  65.                         ], $entityWrittenEvent->getContext());
  66.                         return;
  67.                     }
  68.                 }
  69.             }
  70.         } catch (\Exception $e) {
  71.         }
  72.     }
  73.     private function _replicateAction($entityWrittenEvent)
  74.     {
  75.         try {
  76.             $config $this->systemConfigService->get('KzphSimplenote.config');
  77.             $this->config $config;
  78.             $writeResults $entityWrittenEvent->getWriteResults();
  79.             foreach ($writeResults as $writeResult) {
  80.                 if($writeResult->getEntityName() != 'order')
  81.                     continue;
  82.                 if($writeResult->getOperation() != 'insert')
  83.                     continue;
  84.                 $payload $writeResult->getPayload();
  85.                 if($payload['versionId'] != '0fa91ce3e96a4bc2be4bd9ce752c3425')
  86.                     continue;
  87.                 if (($payload['id'] ?? false)) {
  88.                     foreach ($this->_findAllReplicateMessages($payload['id']) as $oneMessage) {
  89.                         $this->kzphSimplenoteRepository->upsert([
  90.                             [
  91.                                 'id' => Uuid::randomHex(),
  92.                                 'entityId' => $payload['id'],
  93.                                 'entityType' => 'order',
  94.                                 'username' => 'System (Backend)',
  95.                                 'note' => $oneMessage,
  96.                                 'showDesktop' => 0,
  97.                                 'showMessage' => 0,
  98.                                 'replicateInOrder' => 0
  99.                             ],
  100.                         ], $entityWrittenEvent->getContext());
  101.                     }
  102.                     return;
  103.                 }
  104.             }
  105.         } catch (\Exception $e) {
  106.         }
  107.     }
  108.     private function _findAllReplicateMessages($orderId)
  109.     {
  110.         $sqlQuery 'SELECT IF(CC.replicate_in_order = 1, CC.note, "") AS customerNote, IF(LIC.replicate_in_order = 1, LIC.note, "") AS lineItemNote FROM `order` A
  111.         LEFT JOIN `order_customer` B ON A.id = B.order_id AND A.version_id = UNHEX("0FA91CE3E96A4BC2BE4BD9CE752C3425")
  112.         LEFT JOIN `order_line_item` C ON A.id = C.order_id AND C.version_id = A.version_id
  113.         LEFT JOIN `kzph_simplenote` CC ON CC.entity_id = B.customer_id
  114.         LEFT JOIN `kzph_simplenote` LIC ON LIC.entity_id = C.product_id
  115.         WHERE (CC.replicate_in_order = 1 OR LIC.replicate_in_order = 1) AND A.id = :orderId';
  116.         $sqlResult $this->connection->executeQuery($sqlQuery, ['orderId' => hex2bin($orderId)]);
  117.         $notes $sqlResult->fetchAllAssociative();
  118.         $noteStack = array();
  119.         foreach ($notes as $oneNote) {
  120.             if ($oneNote['customerNote'])
  121.                 $noteStack[md5($oneNote['customerNote'])] = $oneNote['customerNote'];
  122.             if ($oneNote['lineItemNote'])
  123.                 $noteStack[md5($oneNote['lineItemNote'])] = $oneNote['lineItemNote'];
  124.         }
  125.         return $noteStack;
  126.     }
  127. }