<?php
namespace B2bOffer;
use B2bOffer\Setup\FlowBuilderSetup;
use B2bOffer\Setup\PlatformMenuInstaller;
use Doctrine\DBAL\Connection;
use B2bOffer\Setup\CustomFieldInstaller;
use B2bOffer\Setup\DocumentTypeInstaller;
use B2bOffer\Setup\EventActionInstaller;
use B2bOffer\Setup\MailTemplateSetup;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class B2bOffer extends Plugin
{
private const NUMBER_RANGE_ID = '44ae3edc5358424fa2a59086c3825828';
private const NUMBER_TYPE_ID = '0eb345859fe7418ab417a305688adf7a';
public const DEFAULT_OPEN_STATUS_ID = '1f7fe3c343074c6b833a132aab4f17f3';
public const DEFAULT_REQUEST_STATUS_ID = '3730574503094101ac91bcedc3787195';
public const DEFAULT_CLOSED_STATUS_ID = '1607ba0a3ee444cfa86fc773157b82b5';
public const DEFAULT_DECLINED_STATUS_ID = '9c5cbe1f2d4c4785865f51203586ff25';
public const DEFAULT_ARCHIVED_STATUS_ID = '053abb1e7d9143bf8bb567957f2f975e';
public function install(InstallContext $context): void
{
$this->createNumberRange($context->getContext());
$this->setDefaultConfigValues();
(new MailTemplateSetup($this->container))->install();
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
(new DocumentTypeInstaller())->install($connection);
(new CustomFieldInstaller($this->container, $context->getContext()))->install();
if ($this->container->has('flow.repository')) {
(new FlowBuilderSetup($this->container, $context->getContext()))->install();
} else {
(new EventActionInstaller($this->container, $context->getContext()))->install();
}
(new PlatformMenuInstaller())->install($connection);
}
public function update(UpdateContext $context): void
{
$this->createNumberRange($context->getContext());
$this->setDefaultConfigValues();
(new MailTemplateSetup($this->container))->install();
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
(new DocumentTypeInstaller())->install($connection);
(new CustomFieldInstaller($this->container, $context->getContext()))->install();
if ($this->container->has('flow.repository')) {
(new FlowBuilderSetup($this->container, $context->getContext()))->install();
} else {
(new EventActionInstaller($this->container, $context->getContext()))->install();
}
(new PlatformMenuInstaller())->install($connection);
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->deleteNumberRange($context->getContext());
(new MailTemplateSetup($this->container))->uninstall();
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
(new DocumentTypeInstaller())->uninstall($connection);
(new CustomFieldInstaller($this->container, $context->getContext()))->uninstall();
(new EventActionInstaller($this->container, $context->getContext()))->uninstall();
if ($this->container->has('flow.repository')) {
(new FlowBuilderSetup($this->container, $context->getContext()))->uninstall();
}
$connection->executeStatement("
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `b2b_offer`;
DROP TABLE IF EXISTS `b2b_offer_item`;
DROP TABLE IF EXISTS `b2b_offer_address`;
DROP TABLE IF EXISTS `b2b_offer_customer`;
DROP TABLE IF EXISTS `b2b_offer_status`;
DROP TABLE IF EXISTS `b2b_offer_status_translation`;
SET FOREIGN_KEY_CHECKS = 1;
");
}
private function setDefaultConfigValues()
{
$config = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
if (empty($config->get('B2bOffer.config.defaultOpenStatusId'))) {
$config->set('B2bOffer.config.defaultOpenStatusId', self::DEFAULT_OPEN_STATUS_ID);
}
if (empty($config->get('B2bOffer.config.defaultCartStatusId'))) {
$config->set('B2bOffer.config.defaultCartStatusId', self::DEFAULT_REQUEST_STATUS_ID);
}
if (empty($config->get('B2bOffer.config.defaultClosedStatusId'))) {
$config->set('B2bOffer.config.defaultClosedStatusId', self::DEFAULT_CLOSED_STATUS_ID);
}
if (empty($config->get('B2bOffer.config.defaultValidUntil'))) {
$config->set('B2bOffer.config.defaultValidUntil', 30);
}
if (empty($config->get('B2bOffer.config.defaultInvalidOfferStatusId'))) {
$config->set('B2bOffer.config.defaultInvalidOfferStatusId', self::DEFAULT_DECLINED_STATUS_ID);
}
}
private function createNumberRange(Context $context)
{
/** @var EntityRepositoryInterface $numberRangeRepository */
$numberRangeRepository = $this->container->get('number_range.repository');
$numberRangeRepository->upsert([[
'id' => self::NUMBER_RANGE_ID,
'global' => true,
'pattern' => '{n}',
'start' => 1000,
'type' => [
'id' => self::NUMBER_TYPE_ID,
'global' => true,
'technicalName' => 'b2b_offer',
'translations' => [[
'typeName' => 'Offers',
'languageId' => Defaults::LANGUAGE_SYSTEM
]]
],
'translations' => [[
'name' => 'Offers',
'languageId' => Defaults::LANGUAGE_SYSTEM
]]
]], $context);
}
private function deleteNumberRange(Context $context)
{
/** @var EntityRepositoryInterface $numberRangeRepository */
$numberRangeRepository = $this->container->get('number_range.repository');
$numberRangeRepository->delete([['id' => self::NUMBER_RANGE_ID]], $context);
}
}