custom/plugins/GbmedDocumentBarcode/src/Core/Checkout/Document/DocumentTemplateRenderer.php line 101

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     GbmedDocumentBarcode
  12.  * @copyright      Copyright (c) 2019, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\DocumentBarcode\Core\Checkout\Document;
  18. use Gbmed\DocumentBarcode\Core\Framework\Struct\StructDocument;
  19. use Gbmed\DocumentBarcode\Installer\ConfigInstaller;
  20. use Gbmed\DocumentBarcode\Services\Logger;
  21. use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
  22. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  23. use Shopware\Core\Checkout\Order\OrderEntity;
  24. use Shopware\Core\Content\Product\ProductEntity;
  25. use Shopware\Core\System\SystemConfig\SystemConfigService;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Zend\Barcode\Barcode;
  28. use Zend\Stdlib\ErrorHandler;
  29. class DocumentTemplateRenderer implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * @var SystemConfigService
  33.      */
  34.     private $systemConfigService;
  35.     /**
  36.      * @var string
  37.      */
  38.     private $codeType;
  39.     /**
  40.      * @var string
  41.      */
  42.     private $useCode;
  43.     /**
  44.      * @var bool
  45.      */
  46.     private $drawText;
  47.     /**
  48.      * @var string
  49.      */
  50.     private $barcodeLayout;
  51.     /**
  52.      * @var string
  53.      */
  54.     private $barcodeImageWidth;
  55.     /**
  56.      * @var array
  57.      */
  58.     private $showBarcodeAt;
  59.     /**
  60.      * @var array
  61.      */
  62.     private $showBarcodeType;
  63.     /**
  64.      * @var Logger
  65.      */
  66.     private $logger;
  67.     /**
  68.      * Product constructor.
  69.      * @param SystemConfigService $systemConfigService
  70.      * @param Logger $logger
  71.      */
  72.     public function __construct(
  73.         SystemConfigService $systemConfigService,
  74.         Logger $logger
  75.     ) {
  76.         $this->systemConfigService $systemConfigService;
  77.         $this->logger $logger;
  78.     }
  79.     /**
  80.      * subscribe events
  81.      *
  82.      * @return array
  83.      */
  84.     public static function getSubscribedEvents(): array
  85.     {
  86.         return [
  87.             DocumentTemplateRendererParameterEvent::class => 'DocumentTemplateRendererParameter'
  88.         ];
  89.     }
  90.     /**
  91.      * add extensions into document parameters
  92.      *
  93.      * @param DocumentTemplateRendererParameterEvent $event
  94.      */
  95.     public function DocumentTemplateRendererParameter(DocumentTemplateRendererParameterEvent $event): void
  96.     {
  97.         $this->getConfig();
  98.         $params $event->getParameters();
  99.         if (!isset($params['order'])) {
  100.             return;
  101.         }
  102.         /** @var OrderEntity $order */
  103.         $order $params['order'];
  104.         $orderNumber $order->getOrderNumber();
  105.         $lineItems $order->getLineItems();
  106.         $customer $order->getOrderCustomer();
  107.         $customerNumber $customer->getCustomerNumber();
  108.         $documentNumber $params['config']['documentNumber'];
  109.         $documentName $params['config']['id'];
  110.         // lineItems
  111.         if ($this->show('lineItems'$documentName)) {
  112.             /** @var OrderLineItemEntity $lineItem */
  113.             foreach ($lineItems->getElements() as $index => $lineItem) {
  114.                 $lineItem->addExtension(
  115.                     'GbmedDocumentBarcode',
  116.                     $this->getStructDocument(
  117.                         $this->getBarcodeText($lineItem),
  118.                         $lineItem->getProduct()
  119.                     )
  120.                 );
  121.             }
  122.         }
  123.         // default settings
  124.         $event->addExtension(
  125.             'GbmedDocumentBarcode',
  126.             $this->getStructDocument()
  127.         );
  128.         // documentNumber
  129.         if ($this->show('documentNumber'$documentName)) {
  130.             $event->addExtension(
  131.                 'GbmedDocumentBarcodeDocumentNumber',
  132.                 $this->getStructDocument($documentNumber)
  133.             );
  134.         }
  135.         // customerNumber
  136.         if ($this->show('customerNumber'$documentName)) {
  137.             $event->addExtension(
  138.                 'GbmedDocumentBarcodeCustomerNumber',
  139.                 $this->getStructDocument($customerNumber)
  140.             );
  141.         }
  142.         // orderNumber
  143.         if ($this->show('orderNumber'$documentName)) {
  144.             $event->addExtension(
  145.                 'GbmedDocumentBarcodeOrderNumber',
  146.                 $this->getStructDocument($orderNumber)
  147.             );
  148.         }
  149.     }
  150.     /**
  151.      * return formated structure
  152.      *
  153.      * @param string|null $text
  154.      * @param ProductEntity|null $product
  155.      * @return StructDocument
  156.      */
  157.     public function getStructDocument($text null, ?ProductEntity $product null): StructDocument
  158.     {
  159.         $struct = new StructDocument();
  160.         $struct->setCodeType($this->codeType)
  161.             ->setUseCode($this->useCode)
  162.             ->setDrawText($this->drawText)
  163.             ->setBarcodeLayout($this->barcodeLayout)
  164.             ->setBarcodeImageWidth($this->barcodeImageWidth)
  165.             ->setShowBarcodeAt($this->showBarcodeAt);
  166.         if ($text) {
  167.             $struct->setImage(
  168.                 $this->draw(
  169.                     [
  170.                         'text' => $text,
  171.                         'drawText' => $this->drawText
  172.                     ],
  173.                     $this->codeType,
  174.                     $product
  175.                 )
  176.             );
  177.         }
  178.         return $struct;
  179.     }
  180.     /**
  181.      * @param OrderLineItemEntity $lineItem
  182.      *
  183.      * @return string|null
  184.      */
  185.     private function getBarcodeText(OrderLineItemEntity $lineItem)
  186.     {
  187.         /** @var ProductEntity $product */
  188.         $product $lineItem->getProduct();
  189.         if (!$product) {
  190.             return null;
  191.         }
  192.         $customFields $product->getCustomFields();
  193.         $text $this->useCode === 'eannumber' $product->getEan() : $product->getProductNumber();
  194.         if ($this->useCode === 'alternativebarcode') {
  195.             $text = !isset($customFields['gbmed_document_barcode_useCode']) || empty($customFields['gbmed_document_barcode_useCode'])
  196.                 ? null
  197.                 $customFields['gbmed_document_barcode_useCode'];
  198.         } else {
  199.             if ($this->useCode === 'alternativebarcode_eannumber') {
  200.                 $text $customFields['gbmed_document_barcode_useCode'] ?? null;
  201.                 if (!$text) {
  202.                     $text $product->getEan();
  203.                 }
  204.             } else {
  205.                 if ($this->useCode === 'alternativebarcode_productnumber') {
  206.                     $text $customFields['gbmed_document_barcode_useCode'] ?? null;
  207.                     if (!$text) {
  208.                         $text $product->getProductNumber();
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.         return $this->prepareText($text);
  214.     }
  215.     /**
  216.      * helper to prepare text
  217.      *
  218.      * @param string|null $text
  219.      * @return string|null
  220.      */
  221.     private function prepareText(?string $text null)
  222.     {
  223.         if ($text !== null && $this->codeType === 'ean13') {
  224.             $text substr($text012);
  225.         }
  226.         return $text;
  227.     }
  228.     /**
  229.      * helper to draw barcode
  230.      *
  231.      * @param array $renderer
  232.      * @param string $codeType
  233.      * @param ProductEntity|null $product
  234.      * @return string|null
  235.      * @noinspection PhpComposerExtensionStubsInspection
  236.      */
  237.     private function draw($renderer = [], $codeType 'code128', ?ProductEntity $product null): ?string
  238.     {
  239.         try {
  240.             /** @var \Zend\Barcode\Renderer\Image $barcode */
  241.             $barcode Barcode::factory($codeType'image'$renderer, []);
  242.             $image $barcode->drawOrThrowException();
  243.         } catch (\Exception $e) {
  244.             $this->logger->setProduct($product)
  245.                 ->error(
  246.                     $e->getMessage(),
  247.                     [
  248.                         'codeType' => $codeType,
  249.                         'text' => $renderer['text']
  250.                     ]
  251.                 );
  252.             $image null;
  253.         }
  254.         if ($image === null) {
  255.             return null;
  256.         }
  257.         ob_start();
  258.         imagepng($image);
  259.         $contents ob_get_clean();
  260.         return 'data:image/png;base64,' base64_encode($contents);
  261.     }
  262.     /**
  263.      * get plugin configurations
  264.      */
  265.     private function getConfig(): void
  266.     {
  267.         $this->codeType $this->systemConfigService->get(ConfigInstaller::getConfigKey('codeType'));
  268.         $this->useCode $this->systemConfigService->get(ConfigInstaller::getConfigKey('useCode'));
  269.         $this->drawText = (bool)$this->systemConfigService->get(ConfigInstaller::getConfigKey('drawText'));
  270.         $this->barcodeLayout $this->systemConfigService->get(ConfigInstaller::getConfigKey('barcodeLayout'));
  271.         $this->barcodeImageWidth $this->systemConfigService->get(ConfigInstaller::getConfigKey('barcodeImageWidth'));
  272.         $this->showBarcodeAt = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('showBarcodeAtDocumentBase'));
  273.         $this->showBarcodeType = (array)$this->systemConfigService->get(ConfigInstaller::getConfigKey('showBarcodeType'));
  274.     }
  275.     /**
  276.      * check if we can show barcode
  277.      *
  278.      * @param $type
  279.      * @param $at
  280.      * @return bool
  281.      */
  282.     private function show($type$at): bool
  283.     {
  284.         return (in_array($type$this->showBarcodeType) && in_array($at$this->showBarcodeAt));
  285.     }
  286. }