<?php declare(strict_types=1);
namespace Fgits\AutoInvoice\Subscriber;
use Fgits\AutoInvoice\Service\ConditionChecker;
use Fgits\AutoInvoice\Service\CustomFields\OrderCustomFields;
use Fgits\AutoInvoice\Service\DB\Document as DBDocument;
use Fgits\AutoInvoice\Service\DB\Order as DBOrder;
use Fgits\AutoInvoice\Service\Document;
use Fgits\AutoInvoice\Service\DocumentCreator;
use Fgits\AutoInvoice\Service\FgitsLibrary\Mailer as FgitsMailer;
use Fgits\AutoInvoice\Service\FgitsLibrary\Utility;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Copyright (c) 2021. GOLLE IT.
*
* @author Andrey Grigorkin <andrey@golle-it.de>
*/
class MailSubscriber implements EventSubscriberInterface
{
private OrderCustomFields $orderCustomFields;
private DBDocument $dbDocument;
private DBOrder $dbOrder;
private ConditionChecker $conditionChecker;
private Document $document;
private DocumentCreator $documentCreator;
private FgitsMailer $mailer;
private Utility $utility;
private SessionInterface $session;
private SystemConfigService $systemConfigService;
private LoggerInterface $logger;
/**
* MailSubscriber constructor.
*
* @param OrderCustomFields $orderCustomFields
* @param DBDocument $dbDocument
* @param DBOrder $dbOrder
* @param ConditionChecker $conditionChecker
* @param Document $document
* @param DocumentCreator $documentCreator
* @param FgitsMailer $mailer
* @param Utility $utility
* @param SessionInterface $session
* @param SystemConfigService $systemConfigService
* @param LoggerInterface $logger
*/
public function __construct(
OrderCustomFields $orderCustomFields,
DBDocument $dbDocument,
DBOrder $dbOrder,
ConditionChecker $conditionChecker,
Document $document,
DocumentCreator $documentCreator,
FgitsMailer $mailer,
Utility $utility,
SessionInterface $session,
SystemConfigService $systemConfigService,
LoggerInterface $logger
) {
$this->orderCustomFields = $orderCustomFields;
$this->dbDocument = $dbDocument;
$this->dbOrder = $dbOrder;
$this->conditionChecker = $conditionChecker;
$this->document = $document;
$this->documentCreator = $documentCreator;
$this->mailer = $mailer;
$this->utility = $utility;
$this->session = $session;
$this->systemConfigService = $systemConfigService;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return[
MailBeforeSentEvent::class => 'onAfterCreateMessage'
];
}
/**
* Gets invoked when an email is ready to be sent.
*
* @param MailBeforeSentEvent $event
*
* @throws \Exception
*/
public function onAfterCreateMessage(MailBeforeSentEvent $event): void
{
$data = $event->getData();
$config = $this->systemConfigService->get('fgitsAutoinvoiceSW6.config', $data['salesChannelId']);
if ($config['active'] && !empty($config['sendCustomerEmail']) && !empty($config['attachEmail']) && isset($data['templateId'])) {
$mailTemplate = $this->mailer->getMailTemplateById($data['templateId'], ['mailTemplateType'], $event->getContext());
$mailTemplateType = $mailTemplate->getMailTemplateType();
if ($mailTemplateType->getTechnicalName() == $config['attachEmail']) {
$order = $this->dbOrder->getOrderById($this->session->get('fgits_autoinvoice_latest_order_id'), $this->utility->getOrderAssociations(), $event->getContext());
if (!$this->orderCustomFields->isCustomerNoInvoice($order) && $this->conditionChecker->shouldSendInvoice($order)) {
$this->documentCreator->createInvoice($order);
$invoice = $this->document->getInvoice($order, $event->getContext());
$event->getMessage()->attach($invoice['content'], $invoice['fileName'], $invoice['mimeType']);
$this->dbDocument->setSent($invoice['id'], $invoice['orderVersionId']);
$this->orderCustomFields->setOrderProcessed($order);
}
$this->orderCustomFields->setOrderConfirmationSent($order);
}
}
}
}