custom/plugins/GbmedDocumentPaymenttarget/src/Core/Checkout/Document/DocumentTemplateRenderer.php line 95

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedDocumentPaymenttarget
  12.  * @copyright      Copyright (c) 2020, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\DocumentPaymenttarget\Core\Checkout\Document;
  18. use DateInterval;
  19. use DateTime;
  20. use DateTimeInterface;
  21. use Exception;
  22. use Gbmed\DocumentPaymenttarget\Core\Framework\Struct\StructDocument;
  23. use Gbmed\DocumentPaymenttarget\Installer\ConfigInstaller;
  24. use Shopware\Core\Checkout\Document\DocumentConfiguration;
  25. use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
  26. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  27. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  28. use Shopware\Core\System\SystemConfig\SystemConfigService;
  29. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  30. class DocumentTemplateRenderer implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var SystemConfigService
  34.      */
  35.     private $systemConfigService;
  36.     /**
  37.      * @var int
  38.      */
  39.     private $days;
  40.     /**
  41.      * @var array
  42.      */
  43.     private $payments = [];
  44.     /**
  45.      * @var array
  46.      */
  47.     private $documents = [];
  48.     /**
  49.      * @var string|null
  50.      */
  51.     private $paidtechnicalname;
  52.     /**
  53.      * @var array
  54.      */
  55.     private $epcDocuments = [];
  56.     /**
  57.      * @var array
  58.      */
  59.     private $epcPayments = [];
  60.     private $epcName;
  61.     private $epcIban;
  62.     private $epcBic;
  63.     /**
  64.      * Product constructor.
  65.      * @param SystemConfigService $systemConfigService
  66.      */
  67.     public function __construct(
  68.         SystemConfigService $systemConfigService
  69.     ) {
  70.         $this->systemConfigService $systemConfigService;
  71.     }
  72.     /**
  73.      * subscribe events
  74.      *
  75.      * @return array
  76.      */
  77.     public static function getSubscribedEvents(): array
  78.     {
  79.         return [
  80.             DocumentTemplateRendererParameterEvent::class => 'DocumentTemplateRendererParameter'
  81.         ];
  82.     }
  83.     /**
  84.      * add extensions into document parameters
  85.      *
  86.      * @param DocumentTemplateRendererParameterEvent $event
  87.      * @throws Exception
  88.      */
  89.     public function DocumentTemplateRendererParameter(DocumentTemplateRendererParameterEvent $event): void
  90.     {
  91.         $this->getConfig();
  92.         $params $event->getParameters();
  93.         $params['config'] = $this->getDocumentConfig($params);
  94.         if (!isset($params['order'], $params['config'], $params['config']['documentDate'], $params['config']['documentTypeId'])) {
  95.             return;
  96.         }
  97.         $paymentTarget = new DateTime($params['config']['documentDate']);
  98.         $paymentTarget->add(new DateInterval('P' $this->days 'D'));
  99.         $paidDate null;
  100.         /** @var OrderTransactionEntity $transaction */
  101.         $transaction $params['order']->getTransactions()->first();
  102.         $documentType $params['config']['documentTypeId'];
  103.         $displayEpc in_array($documentType$this->epcDocuments)
  104.             && in_array($transaction->getPaymentMethodId(), $this->epcPayments)
  105.             && $this->epcName
  106.             && $this->epcIban
  107.             && $this->epcBic;
  108.         if ($this->hasPaid($transaction->getStateMachineState())) {
  109.             $paidDate $transaction->getUpdatedAt() ?? $transaction->getCreatedAt();
  110.         }
  111.         if (($paidDate instanceof DateTimeInterface)
  112.             || !in_array($transaction->getPaymentMethodId(), $this->payments)
  113.             || !in_array($documentType$this->documents)
  114.         ) {
  115.             $paymentTarget null;
  116.         }
  117.         $event->addExtension(
  118.             'GbmedDocumentPaymenttarget',
  119.             $this->getStructDocument($paymentTarget$paidDate$displayEpc)
  120.         );
  121.     }
  122.     /**
  123.      * @param ?StateMachineStateEntity $state
  124.      * @return bool
  125.      */
  126.     private function hasPaid(?StateMachineStateEntity $state): bool
  127.     {
  128.         return $this->paidtechnicalname !== null
  129.             && ($state instanceof StateMachineStateEntity)
  130.             && $state->getId() === $this->paidtechnicalname;
  131.     }
  132.     /**
  133.      * return formated structure
  134.      *
  135.      * @param DateTimeInterface|null $paymentTarget
  136.      * @param DateTimeInterface|null $paidDate
  137.      * @param bool $displayEpc
  138.      * @return StructDocument
  139.      */
  140.     public function getStructDocument(
  141.         ?DateTimeInterface $paymentTarget null,
  142.         ?DateTimeInterface $paidDate null,
  143.         bool $displayEpc false
  144.     ): StructDocument {
  145.         $struct = new StructDocument();
  146.         $struct->setDays($this->days)
  147.             ->setPaymentTarget($paymentTarget)
  148.             ->setPaidDate($paidDate)
  149.             ->setDisplayEpc($displayEpc);
  150.         return $struct;
  151.     }
  152.     /**
  153.      * get plugin configurations
  154.      */
  155.     private function getConfig(): void
  156.     {
  157.         $this->days = (int)$this->systemConfigService->get(ConfigInstaller::getConfigKey('days'));
  158.         $this->payments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('payments'));
  159.         $this->documents = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('documents'));
  160.         $this->paidtechnicalname = (string)$this->systemConfigService->get(ConfigInstaller::getConfigKey('paidTechnicalName'));
  161.         $this->epcDocuments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('epcDocuments'));
  162.         $this->epcPayments = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('epcPaymentsDocument'));
  163.         $this->epcName $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcName'));
  164.         $this->epcIban $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcIban'));
  165.         $this->epcBic $this->systemConfigService->get(ConfigInstaller::getConfigKey('epcBic'));
  166.     }
  167.     /**
  168.      * if document config is instance of DocumentConfiguration we convert properties to array
  169.      *
  170.      * @param array $params
  171.      * @return array|null
  172.      */
  173.     private function getDocumentConfig(array $params): ?array
  174.     {
  175.         if (!isset($params['config'])) {
  176.             return null;
  177.         }
  178.         if (!($params['config'] instanceof DocumentConfiguration)) {
  179.             return $params['config'];
  180.         }
  181.         return $params['config']->getVars();
  182.     }
  183. }