<?phpdeclare(strict_types=1);namespace NetInventors\NetiNextEasyCoupon\Subscriber;use NetInventors\NetiNextEasyCoupon\Core\Content\Condition\ConditionCollection;use NetInventors\NetiNextEasyCoupon\Core\Content\Transaction\TransactionCollection;use NetInventors\NetiNextEasyCoupon\Service\Account\VoucherTransactionsService;use NetInventors\NetiNextEasyCoupon\Service\ExportService;use Psr\Log\LoggerInterface;use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeExportRecordEvent;use Shopware\Core\Framework\Context;use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class ExportSubscriber implements EventSubscriberInterface{ /** * @var EntityRepositoryInterface */ private $conditionRepository; /** * @var VoucherTransactionsService */ private $voucherTransactionsService; private ExportService $exportService; /** * ExportSubscriber constructor. * * @param EntityRepositoryInterface $conditionRepository */ public function __construct( EntityRepositoryInterface $conditionRepository, VoucherTransactionsService $voucherTransactionsService, ExportService $exportService ) { $this->conditionRepository = $conditionRepository; $this->voucherTransactionsService = $voucherTransactionsService; $this->exportService = $exportService; } public static function getSubscribedEvents() { return [ ImportExportBeforeExportRecordEvent::class => 'beforeExport', ]; } public function beforeExport(ImportExportBeforeExportRecordEvent $event) { if ( 'neti_easy_coupon' !== $event->getConfig()->get('sourceEntity') || null !== $event->getConfig()->getMapping()->get('_error') ) { return; } $record = $event->getRecord(); /** @var ConditionCollection $conditions */ $conditions = $this->getCouponConditions($record['id'])->getEntities(); /** @var TransactionCollection $voucherTransactions */ $voucherTransactions = $this->voucherTransactionsService->getTransactionsForVouchers([$record['id']])->getEntities(); $record['virtual_import'] = \json_encode( $this->exportService->buildVirtualImport( $conditions, $voucherTransactions, $record ) ); $this->exportService->handleVoucherValues($record, $voucherTransactions); $event->setRecord($record); } /** * @param string $couponId * * @return EntitySearchResult */ private function getCouponConditions(string $couponId): EntitySearchResult { $criteria = new Criteria(); $criteria->addFilter(new EqualsFilter('couponId', $couponId)); $criteria->addSorting(new FieldSorting('createdAt', FieldSorting::ASCENDING)); return $this->conditionRepository->search($criteria, Context::createDefaultContext()); }}