<?php declare(strict_types=1);
/**
* PremSoft
* Copyright © 2020 Premsoft - Sven Mittreiter
*
* @copyright Copyright (c) 2020, premsoft - Sven Mittreiter (http://www.premsoft.de)
* @author Sven Mittreiter <info@premsoft.de>
*/
namespace Prems\Plugin\PremsIndividualOffer6;
use Prems\Plugin\PremsIndividualOffer6\Core\Lifecycle\InstallUninstall;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\AbsolutePriceDefinition;
use Shopware\Core\Checkout\Cart\Price\Struct\PercentagePriceDefinition;
use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class PremsIndividualOffer6 extends Plugin
{
public const OFFER_MODE_STATE = 'prems_individual_offer_offer_mode_active';
public const OFFER_MODE_EXTENSION_NAME = 'offer_mode_active';
public const OFFER_ENTITY_NAME = 'prems_individual_offer';
public const ORDER_FROM_OFFER = 'prems_from_offer';
public const ORDER_FROM_OFFER_ID = 'prems_from_offer_id';
public function install(InstallContext $context): void
{
(new InstallUninstall(
$this->container->get('mail_template_type.repository'),
$this->container->get('mail_template.repository'),
__DIR__ . '/Core/MailTemplates/',
$this->container->get(Connection::class),
$this->container->get('custom_field_set.repository'),
))->install($context);
parent::install($context);
}
public function update(UpdateContext $context): void
{
parent::update($context);
// fix missing 'type' field in the offer tem price definition
if (\version_compare($context->getCurrentPluginVersion(), '3.4.0', '>=')) {
$this->fixMissingTypeFieldInOfferItemPriceDefinition();
}
(new InstallUninstall(
$this->container->get('mail_template_type.repository'),
$this->container->get('mail_template.repository'),
__DIR__ . '/Core/MailTemplates/',
$this->container->get(Connection::class),
$this->container->get('custom_field_set.repository'),
))->update($context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
parent::uninstall($context);
return;
}
(new InstallUninstall(
$this->container->get('mail_template_type.repository'),
$this->container->get('mail_template.repository'),
__DIR__ . '/Core/MailTemplates/',
$this->container->get(Connection::class),
$this->container->get('custom_field_set.repository'),
))->uninstall($context);
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_item`');
$connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_messages`');
$connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer_comments`');
$connection->executeStatement('DROP TABLE IF EXISTS `prems_individual_offer`');
$offerEntityName = self::OFFER_ENTITY_NAME;
$numberRangeTypeId = $connection->fetchOne("SELECT HEX(number_range_type.id) FROM `number_range_type` WHERE number_range_type.technical_name = '$offerEntityName'");
$numberRangeId = $connection->fetchOne("SELECT HEX(number_range.id) FROM `number_range` WHERE HEX(number_range.type_id) = '$numberRangeTypeId'");
$connection->executeStatement("DELETE FROM `number_range` WHERE HEX(number_range.type_id) = '$numberRangeTypeId'");
$connection->executeStatement("DELETE FROM `number_range_type` WHERE HEX(number_range_type.id) = '$numberRangeTypeId'");
$connection->executeStatement("DELETE FROM `number_range_state` WHERE HEX(number_range_state.number_range_id) = '$numberRangeId'");
$connection->executeStatement("DELETE FROM `document_type` WHERE document_type.technical_name = '$offerEntityName'");
$connection->executeStatement("DELETE FROM `user_config` WHERE user_config.key = 'grid.setting.prems-individual-offer-item-grid'");
parent::uninstall($context);
}
/**
* Fix missing 'type' field in the offer tem price definition
*
* @return void
* @throws \JsonException
*/
protected function fixMissingTypeFieldInOfferItemPriceDefinition(): void
{
$connection = $this->container->get(Connection::class);
$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` > ''");
foreach ($items as $item) {
$priceDefinition = json_decode($item['price_definition'], true);
if (!empty($priceDefinition) && !array_key_exists('type', $priceDefinition)) {
switch ($item['item_type']) {
// product, custom and credit types
case LineItem::PRODUCT_LINE_ITEM_TYPE:
case LineItem::CUSTOM_LINE_ITEM_TYPE:
case LineItem::CREDIT_LINE_ITEM_TYPE:
// it is always the quantity type
$type = QuantityPriceDefinition::TYPE;
break;
// promotion type
case LineItem::PROMOTION_LINE_ITEM_TYPE:
// if price definition contains 'percentage' it is the percentage type
if (array_key_exists('percentage', $priceDefinition)) {
$type = PercentagePriceDefinition::TYPE;
break;
}
// it is the absolute type
$type = AbsolutePriceDefinition::TYPE;
break;
default:
$type = QuantityPriceDefinition::TYPE;
}
$priceDefinition = ['type' => $type] + $priceDefinition;
}
$id = $item['id'];
$priceDefinition = json_encode($priceDefinition, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR);
$connection->executeStatement("UPDATE `prems_individual_offer_item` SET `price_definition` = '$priceDefinition' WHERE HEX(`id`) = '$id'");
}
}
}