<?php declare(strict_types=1);
/**
* gb media
* All Rights Reserved.
*
* Unauthorized copying of this file, via any medium is strictly prohibited.
* The content of this file is proprietary and confidential.
*
* @category Shopware
* @package Shopware_Plugins
* @subpackage GbmedDocumentPaymenttarget
* @copyright Copyright (c) 2020, gb media
* @license proprietary
* @author Giuseppe Bottino
* @link http://www.gb-media.biz
*/
namespace Gbmed\DocumentPaymenttarget\Core\Checkout\Document;
use DateInterval;
use DateTime;
use DateTimeInterface;
use Exception;
use Gbmed\DocumentPaymenttarget\Core\Framework\Struct\StructDocument;
use Gbmed\DocumentPaymenttarget\Installer\ConfigInstaller;
use Shopware\Core\Checkout\Document\DocumentConfiguration;
use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DocumentTemplateRenderer implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var int
*/
private $days;
/**
* @var array
*/
private $payments = [];
/**
* @var array
*/
private $documents = [];
/**
* @var string|null
*/
private $paidtechnicalname;
/**
* @var array
*/
private $epcDocuments = [];
/**
* @var array
*/
private $epcPayments = [];
private $epcName;
private $epcIban;
private $epcBic;
/**
* Product constructor.
* @param SystemConfigService $systemConfigService
*/
public function __construct(
SystemConfigService $systemConfigService
) {
$this->systemConfigService = $systemConfigService;
}
/**
* subscribe events
*
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
DocumentTemplateRendererParameterEvent::class => 'DocumentTemplateRendererParameter'
];
}
/**
* add extensions into document parameters
*
* @param DocumentTemplateRendererParameterEvent $event
* @throws Exception
*/
public function DocumentTemplateRendererParameter(DocumentTemplateRendererParameterEvent $event): void
{
$this->getConfig();
$params = $event->getParameters();
$params['config'] = $this->getDocumentConfig($params);
if (!isset($params['order'], $params['config'], $params['config']['documentDate'], $params['config']['documentTypeId'])) {
return;
}
$paymentTarget = new DateTime($params['config']['documentDate']);
$paymentTarget->add(new DateInterval('P' . $this->days . 'D'));
$paidDate = null;
/** @var OrderTransactionEntity $transaction */
$transaction = $params['order']->getTransactions()->first();
$documentType = $params['config']['documentTypeId'];
$displayEpc = in_array($documentType, $this->epcDocuments)
&& in_array($transaction->getPaymentMethodId(), $this->epcPayments)
&& $this->epcName
&& $this->epcIban
&& $this->epcBic;
if ($this->hasPaid($transaction->getStateMachineState())) {
$paidDate = $transaction->getUpdatedAt() ?? $transaction->getCreatedAt();
}
if (($paidDate instanceof DateTimeInterface)
|| !in_array($transaction->getPaymentMethodId(), $this->payments)
|| !in_array($documentType, $this->documents)
) {
$paymentTarget = null;
}
$event->addExtension(
'GbmedDocumentPaymenttarget',
$this->getStructDocument($paymentTarget, $paidDate, $displayEpc)
);
}
/**
* @param ?StateMachineStateEntity $state
* @return bool
*/
private function hasPaid(?StateMachineStateEntity $state): bool
{
return $this->paidtechnicalname !== null
&& ($state instanceof StateMachineStateEntity)
&& $state->getId() === $this->paidtechnicalname;
}
/**
* return formated structure
*
* @param DateTimeInterface|null $paymentTarget
* @param DateTimeInterface|null $paidDate
* @param bool $displayEpc
* @return StructDocument
*/
public function getStructDocument(
?DateTimeInterface $paymentTarget = null,
?DateTimeInterface $paidDate = null,
bool $displayEpc = false
): StructDocument {
$struct = new StructDocument();
$struct->setDays($this->days)
->setPaymentTarget($paymentTarget)
->setPaidDate($paidDate)
->setDisplayEpc($displayEpc);
return $struct;
}
/**
* get plugin configurations
*/
private function getConfig(): void
{
$this->days = (int)$this->systemConfigService->get(ConfigInstaller::getConfigKey('days'));
$this->payments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('payments'));
$this->documents = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('documents'));
$this->paidtechnicalname = (string)$this->systemConfigService->get(ConfigInstaller::getConfigKey('paidTechnicalName'));
$this->epcDocuments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('epcDocuments'));
$this->epcPayments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('epcPaymentsDocument'));
$this->epcName = $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcName'));
$this->epcIban = $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcIban'));
$this->epcBic = $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcBic'));
}
/**
* if document config is instance of DocumentConfiguration we convert properties to array
*
* @param array $params
* @return array|null
*/
private function getDocumentConfig(array $params): ?array
{
if (!isset($params['config'])) {
return null;
}
if (!($params['config'] instanceof DocumentConfiguration)) {
return $params['config'];
}
return $params['config']->getVars();
}
}