custom/plugins/PickwareErpStarter/src/Product/PickwareProductInitializer.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareErpStarter\Product;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\DalBundle\Sql\SqlUuid;
  13. use Shopware\Core\Content\Product\ProductEvents;
  14. use Shopware\Core\Defaults;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class PickwareProductInitializer implements EventSubscriberInterface
  20. {
  21.     private Connection $db;
  22.     private ?EventDispatcherInterface $eventDispatcher;
  23.     /**
  24.      * @deprecated next-major `?EventDispatcherInterface $eventDispatcher` will be non-optional
  25.      */
  26.     public function __construct(Connection $db, ?EventDispatcherInterface $eventDispatcher null)
  27.     {
  28.         $this->db $db;
  29.         $this->eventDispatcher $eventDispatcher;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'productWritten',
  35.         ];
  36.     }
  37.     public function productWritten(EntityWrittenEvent $entityWrittenEvent): void
  38.     {
  39.         if ($entityWrittenEvent->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  40.             return;
  41.         }
  42.         $productIds = [];
  43.         foreach ($entityWrittenEvent->getWriteResults() as $writeResult) {
  44.             // $writeResult->getExistence() can be null, but we have no idea why and also not what this means.
  45.             $existence $writeResult->getExistence();
  46.             if (($existence === null && $writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT)
  47.                 || ($existence !== null && !$existence->exists())
  48.             ) {
  49.                 $productIds[] = $writeResult->getPrimaryKey();
  50.             }
  51.         }
  52.         $this->ensurePickwareProductsExist($productIdstrue);
  53.     }
  54.     public function ensurePickwareProductsExist(array $productIds, ?bool $productInserted false): void
  55.     {
  56.         if (!$this->eventDispatcher) {
  57.             return;
  58.         }
  59.         if (count($productIds) === 0) {
  60.             return;
  61.         }
  62.         $this->db->executeStatement(
  63.             'INSERT INTO `pickware_erp_pickware_product` (
  64.                 id,
  65.                 product_id,
  66.                 product_version_id,
  67.                 reserved_stock,
  68.                 stock_not_available_for_sale,
  69.                 incoming_stock,
  70.                 is_stock_management_disabled,
  71.                 created_at
  72.             ) SELECT
  73.                 ' SqlUuid::UUID_V4_GENERATION ',
  74.                 product.id,
  75.                 product.version_id,
  76.                 0,
  77.                 0,
  78.                 0,
  79.                 0,
  80.                 NOW(3)
  81.             FROM `product`
  82.             WHERE `product`.`id` IN (:ids) AND `product`.`version_id` = :liveVersionId
  83.             ON DUPLICATE KEY UPDATE `pickware_erp_pickware_product`.`id` = `pickware_erp_pickware_product`.`id`',
  84.             [
  85.                 'ids' => array_map('hex2bin'$productIds),
  86.                 'liveVersionId' => hex2bin(Defaults::LIVE_VERSION),
  87.             ],
  88.             [
  89.                 'ids' => Connection::PARAM_STR_ARRAY,
  90.             ],
  91.         );
  92.         if ($productInserted) {
  93.             // If a product gets cloned, the pickwareProduct extension gets cloned as well. We want that for certain
  94.             // values of the pickwareProduct, e.g. reorderPoint or isStockManagementDisabled. Other values are
  95.             // set to their default values by executing this query.
  96.             $this->db->executeStatement(
  97.                 'UPDATE `pickware_erp_pickware_product`
  98.             SET `pickware_erp_pickware_product`.`reserved_stock` = 0,
  99.                 `pickware_erp_pickware_product`.`incoming_stock` = 0,
  100.                 `pickware_erp_pickware_product`.`stock_not_available_for_sale` = 0
  101.             WHERE `pickware_erp_pickware_product`.`product_id` IN (:productIds)
  102.             AND `pickware_erp_pickware_product`.`product_version_id` = :liveVersionId;',
  103.                 [
  104.                     'productIds' => array_map('hex2bin'$productIds),
  105.                     'liveVersionId' => hex2bin(Defaults::LIVE_VERSION),
  106.                 ],
  107.                 [
  108.                     'productIds' => Connection::PARAM_STR_ARRAY,
  109.                 ],
  110.             );
  111.             $this->eventDispatcher->dispatch(new PickwareProductInsertedEvent($productIds));
  112.         }
  113.     }
  114.     public function ensurePickwareProductsExistForAllProducts(): void
  115.     {
  116.         $this->db->executeStatement(
  117.             'INSERT INTO `pickware_erp_pickware_product` (
  118.                 id,
  119.                 product_id,
  120.                 product_version_id,
  121.                 reserved_stock,
  122.                 stock_not_available_for_sale,
  123.                 incoming_stock,
  124.                 is_stock_management_disabled,
  125.                 created_at
  126.             ) SELECT
  127.                 ' SqlUuid::UUID_V4_GENERATION ',
  128.                 product.id,
  129.                 product.version_id,
  130.                 0,
  131.                 0,
  132.                 0,
  133.                 0,
  134.                 NOW(3)
  135.             FROM `product`
  136.             WHERE `product`.`version_id` = :liveVersionId
  137.             ON DUPLICATE KEY UPDATE `pickware_erp_pickware_product`.`id` = `pickware_erp_pickware_product`.`id`',
  138.             ['liveVersionId' => hex2bin(Defaults::LIVE_VERSION)],
  139.         );
  140.     }
  141. }