custom/plugins/PremsIndividualOffer6/src/PremsIndividualOffer6.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * PremSoft
  4.  * Copyright © 2020 Premsoft - Sven Mittreiter
  5.  *
  6.  * @copyright  Copyright (c) 2020, premsoft - Sven Mittreiter (http://www.premsoft.de)
  7.  * @author     Sven Mittreiter <info@premsoft.de>
  8.  */
  9. namespace Prems\Plugin\PremsIndividualOffer6;
  10. use Prems\Plugin\PremsIndividualOffer6\Core\Lifecycle\InstallUninstall;
  11. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  12. use Shopware\Core\Checkout\Cart\Price\Struct\AbsolutePriceDefinition;
  13. use Shopware\Core\Checkout\Cart\Price\Struct\PercentagePriceDefinition;
  14. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  15. use Shopware\Core\Framework\Plugin;
  16. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  17. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  18. use Doctrine\DBAL\Connection;
  19. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  20. class PremsIndividualOffer6 extends Plugin
  21. {
  22.     public const OFFER_MODE_STATE 'prems_individual_offer_offer_mode_active';
  23.     public const OFFER_MODE_EXTENSION_NAME 'offer_mode_active';
  24.     public const OFFER_ENTITY_NAME 'prems_individual_offer';
  25.     public const ORDER_FROM_OFFER 'prems_from_offer';
  26.     public const ORDER_FROM_OFFER_ID 'prems_from_offer_id';
  27.     public function install(InstallContext $context): void
  28.     {
  29.         (new InstallUninstall(
  30.             $this->container->get('mail_template_type.repository'),
  31.             $this->container->get('mail_template.repository'),
  32.             __DIR__ '/Core/MailTemplates/',
  33.             $this->container->get(Connection::class),
  34.             $this->container->get('custom_field_set.repository'),
  35.         ))->install($context);
  36.         parent::install($context);
  37.     }
  38.     public function update(UpdateContext $context): void
  39.     {
  40.         parent::update($context);
  41.         // fix missing 'type' field in the offer tem price definition
  42.         if (\version_compare($context->getCurrentPluginVersion(), '3.4.0''>=')) {
  43.             $this->fixMissingTypeFieldInOfferItemPriceDefinition();
  44.         }
  45.         (new InstallUninstall(
  46.             $this->container->get('mail_template_type.repository'),
  47.             $this->container->get('mail_template.repository'),
  48.             __DIR__ '/Core/MailTemplates/',
  49.             $this->container->get(Connection::class),
  50.             $this->container->get('custom_field_set.repository'),
  51.         ))->update($context);
  52.     }
  53.     public function uninstall(UninstallContext $context): void
  54.     {
  55.         if ($context->keepUserData()) {
  56.             parent::uninstall($context);
  57.             return;
  58.         }
  59.         (new InstallUninstall(
  60.             $this->container->get('mail_template_type.repository'),
  61.             $this->container->get('mail_template.repository'),
  62.             __DIR__ '/Core/MailTemplates/',
  63.             $this->container->get(Connection::class),
  64.             $this->container->get('custom_field_set.repository'),
  65.         ))->uninstall($context);
  66.         $connection $this->container->get(Connection::class);
  67.         $connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_item`');
  68.         $connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_messages`');
  69.         $connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_comments`');
  70.         $connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer`');
  71.         $offerEntityName self::OFFER_ENTITY_NAME;
  72.         $numberRangeTypeId $connection->fetchOne("SELECT HEX(number_range_type.id) FROM `number_range_type` WHERE number_range_type.technical_name = '$offerEntityName'");
  73.         $numberRangeId $connection->fetchOne("SELECT HEX(number_range.id) FROM `number_range` WHERE HEX(number_range.type_id) = '$numberRangeTypeId'");
  74.         $connection->executeStatement("DELETE FROM `number_range` WHERE HEX(number_range.type_id) = '$numberRangeTypeId'");
  75.         $connection->executeStatement("DELETE FROM `number_range_type` WHERE HEX(number_range_type.id) = '$numberRangeTypeId'");
  76.         $connection->executeStatement("DELETE FROM `number_range_state` WHERE HEX(number_range_state.number_range_id) = '$numberRangeId'");
  77.         $connection->executeStatement("DELETE FROM `document_type` WHERE document_type.technical_name = '$offerEntityName'");
  78.         $connection->executeStatement("DELETE FROM `user_config` WHERE user_config.key = 'grid.setting.prems-individual-offer-item-grid'");
  79.         parent::uninstall($context);
  80.     }
  81.     /**
  82.      * Fix missing 'type' field in the offer tem price definition
  83.      *
  84.      * @return void
  85.      * @throws \JsonException
  86.      */
  87.     protected function fixMissingTypeFieldInOfferItemPriceDefinition(): void
  88.     {
  89.         $connection $this->container->get(Connection::class);
  90.         $items $connection->fetchAllAssociative("SELECT HEX(`id`) as id, `item_type`, `price_definition` FROM `prems_individual_offer_item` WHERE `price_definition` IS NOT NULL AND `price_definition` > ''");
  91.         foreach ($items as $item) {
  92.             $priceDefinition json_decode($item['price_definition'], true);
  93.             if (!empty($priceDefinition) && !array_key_exists('type'$priceDefinition)) {
  94.                 switch ($item['item_type']) {
  95.                     // product, custom and credit types
  96.                     case LineItem::PRODUCT_LINE_ITEM_TYPE:
  97.                     case LineItem::CUSTOM_LINE_ITEM_TYPE:
  98.                     case LineItem::CREDIT_LINE_ITEM_TYPE:
  99.                         // it is always the quantity type
  100.                         $type QuantityPriceDefinition::TYPE;
  101.                         break;
  102.                     // promotion type
  103.                     case LineItem::PROMOTION_LINE_ITEM_TYPE:
  104.                         // if price definition contains 'percentage' it is the percentage type
  105.                         if (array_key_exists('percentage'$priceDefinition)) {
  106.                             $type PercentagePriceDefinition::TYPE;
  107.                             break;
  108.                         }
  109.                         // it is the absolute type
  110.                         $type AbsolutePriceDefinition::TYPE;
  111.                         break;
  112.                     default:
  113.                         $type QuantityPriceDefinition::TYPE;
  114.                 }
  115.                 $priceDefinition = ['type' => $type] + $priceDefinition;
  116.             }
  117.             $id $item['id'];
  118.             $priceDefinition json_encode($priceDefinition\JSON_PRESERVE_ZERO_FRACTION \JSON_THROW_ON_ERROR);
  119.             $connection->executeStatement("UPDATE `prems_individual_offer_item` SET `price_definition` = '$priceDefinition' WHERE HEX(`id`) = '$id'");
  120.         }
  121.     }
  122. }