<?php
declare(strict_types=1);
/**
* WBFK OG
* Copyright (c) 2024 WBFK OG
*
* @copyright Copyright (c) 2024 WBFK OG (https://wbfk.at)
* @author WBFK OG <info@wbfk.at>
*/
namespace Wbfk\DigitalInvoice\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtensionEntity;
use Wbfk\DigitalInvoice\Event\DocumentTypeEvent;
use Wbfk\DigitalInvoice\Service\Document\DocumentService;
class DocumentTypeSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly EntityRepositoryInterface $wbfkInvoiceDetailsOrderExtensionRepository,
) {
}
public static function getSubscribedEvents(): array
{
return [
DocumentService::DOCUMENT_TYPE_EVENT => 'DocumentTypeEvent',
];
}
public function DocumentTypeEvent(DocumentTypeEvent $event): void
{
$docType = $event->documentTypeName;
if (in_array($docType, ['storno', 'invoice', 'credit_note'])) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderId', $event->orderId));
/** @var WbfkInvoiceDetailsOrderExtensionEntity $orderInvoiceExt */
$orderInvoiceExt = $this->wbfkInvoiceDetailsOrderExtensionRepository->search($criteria, $event->context)->first();
$fileType = $orderInvoiceExt?->getInvoiceType();
if ($fileType && $fileType === WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_X_INVOICE) {
$event->fileType = WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_X_INVOICE;
}
}
}
}