custom/plugins/NetiNextEasyCoupon/src/Subscriber/ExportSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  4. use NetInventors\NetiNextEasyCoupon\Core\Content\Condition\ConditionEntity;
  5. use NetInventors\NetiNextEasyCoupon\Core\Content\EasyCoupon\EasyCouponEntity;
  6. use NetInventors\NetiNextEasyCoupon\Core\Content\Transaction\TransactionCollection;
  7. use NetInventors\NetiNextEasyCoupon\Core\Content\Transaction\TransactionEntity;
  8. use NetInventors\NetiNextEasyCoupon\Service\Account\VoucherTransactionsService;
  9. use Psr\Log\LoggerInterface;
  10. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  11. use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeExportRecordEvent;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ExportSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $conditionRepository;
  25.     /**
  26.      * @var VoucherTransactionsService
  27.      */
  28.     private                 $voucherTransactionsService;
  29.     private LoggerInterface $logger;
  30.     /**
  31.      * ExportSubscriber constructor.
  32.      *
  33.      * @param EntityRepositoryInterface $conditionRepository
  34.      */
  35.     public function __construct(
  36.         EntityRepositoryInterface  $conditionRepository,
  37.         VoucherTransactionsService $voucherTransactionsService,
  38.         LoggerInterface            $logger
  39.     ) {
  40.         $this->conditionRepository        $conditionRepository;
  41.         $this->voucherTransactionsService $voucherTransactionsService;
  42.         $this->logger                     $logger;
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             ImportExportBeforeExportRecordEvent::class => 'beforeExport',
  48.         ];
  49.     }
  50.     public function beforeExport(ImportExportBeforeExportRecordEvent $event)
  51.     {
  52.         if ('neti_easy_coupon' !== $event->getConfig()->get('sourceEntity')) {
  53.             return;
  54.         }
  55.         $record          $event->getRecord();
  56.         $conditions      $this->getCouponConditions($record['id']);
  57.         $conditionsArray = [];
  58.         $virtualImport   = [];
  59.         $voucherTransactions $this->voucherTransactionsService->getTransactionsForVouchers([$record['id']]);
  60.         /** @var TransactionEntity|null $lastTransaction */
  61.         $lastTransaction $voucherTransactions->last();
  62.         if (null === $lastTransaction) {
  63.             $this->logger->error(
  64.                 'Export-Error - voucher has no transactions',
  65.                 [
  66.                     'easy_coupon_id' => $record['id'],
  67.                     'code'           => $record['code'],
  68.                 ]
  69.             );
  70.             return;
  71.         }
  72.         if (null !== $lastTransaction->getCustomerId() && null !== $lastTransaction->getOrderId()) {
  73.             $order $lastTransaction->getOrder();
  74.             if (null === $order) {
  75.                 $this->logger->error(
  76.                     'Export-Error - order of the transaction is deleted',
  77.                     [
  78.                         'easy_coupon_id' => $record['id'],
  79.                         'code'           => $record['code'],
  80.                     ]
  81.                 );
  82.             } else {
  83.                 $orderCustomer $order->getOrderCustomer();
  84.                 if ($orderCustomer instanceof OrderCustomerEntity) {
  85.                     $virtualImport['customerNumber'] = $orderCustomer->getCustomerNumber();
  86.                 } else {
  87.                     $this->logger->error(
  88.                         'Export-Error - customer of the order transaction could not be found',
  89.                         [
  90.                             'easy_coupon_id' => $record['id'],
  91.                             'code'           => $record['code'],
  92.                         ]
  93.                     );
  94.                 }
  95.             }
  96.         }
  97.         if ($conditions->getElements() !== []) {
  98.             /** @var ConditionEntity $condition */
  99.             foreach ($conditions->getElements() as $condition) {
  100.                 $conditionArray             = [];
  101.                 $conditionArray['id']       = $condition->getId();
  102.                 $conditionArray['couponId'] = $condition->getCouponId();
  103.                 $conditionArray['parentId'] = $condition->getParentId();
  104.                 $conditionArray['value']    = $condition->getValue();
  105.                 $conditionArray['position'] = $condition->getPosition();
  106.                 //getType crashes if the return value not a string, for some reason we also save a null value
  107.                 // there for a dirty solution is needed
  108.                 try {
  109.                     $conditionArray['type'] = $condition->getType();
  110.                 } catch (\TypeError $exception) {
  111.                     $conditionArray['type'] = null;
  112.                 }
  113.                 $conditionsArray[] = $conditionArray;
  114.             }
  115.         }
  116.         /** @var TransactionCollection $entities */
  117.         $entities $voucherTransactions->getEntities();
  118.         if (EasyCouponEntity::VALUE_TYPE_ABSOLUTE === (int) $record['value_type']) {
  119.             $record['value'] = round(round($entities->sum(), 3), 2);
  120.         }
  121.         $virtualImport['conditions'] = $conditionsArray;
  122.         $record['virtual_import']    = json_encode($virtualImport);
  123.         $event->setRecord($record);
  124.     }
  125.     /**
  126.      * @param string $couponId
  127.      *
  128.      * @return EntitySearchResult
  129.      */
  130.     private function getCouponConditions(string $couponId): EntitySearchResult
  131.     {
  132.         $criteria = new Criteria();
  133.         $criteria->addFilter(new EqualsFilter('couponId'$couponId));
  134.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  135.         return $this->conditionRepository->search($criteriaContext::createDefaultContext());
  136.     }
  137. }