<?php declare(strict_types=1);
namespace Albis\AlbisLease;
use Albis\AlbisLease\Service\AlbisPayment;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionCollection;
use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionEntity;
use Shopware\Core\Content\Rule\RuleEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\BusinessEventInterface;
use Shopware\Core\Framework\Event\BusinessEventRegistry;
use Shopware\Core\Framework\Event\EventAction\Aggregate\EventActionRule\EventActionRuleDefinition;
use Shopware\Core\Framework\Event\EventAction\EventActionCollection;
use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Shopware\Core\Framework\Uuid\Uuid;
class AlbisLease extends Plugin
{
private $paymentMethodId = null;
private $mailTemplateTypeId = null;
private $mailTemplateId = null;
private $ruleId = null;
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->paymentMethodId = Uuid::randomHex();
$this->mailTemplateTypeId = Uuid::randomHex();
$this->mailTemplateId = Uuid::randomHex();
$this->ruleId = Uuid::randomHex();
$this->createAlbisProductGroups($installContext->getContext());
$this->createAlbisPaymentMethod($installContext->getContext());
$this->createAlbisMailTemplate($installContext->getContext());
$this->createAlbisPaymentRule($installContext->getContext());
$this->createAlbisBusinessEvent($installContext->getContext());
}
public function uninstall(UninstallContext $context): void
{
// Only set the payment method to inactive when uninstalling. Removing the payment method would
// cause data consistency issues, since the payment method might have been used in several orders
$this->setPaymentMethodIsActive(false, $context->getContext());
//Keep UserData? Then do nothing here
if ($context->keepUserData()) {
return;
}
$this->deleteAlbisPaymentMethod($context);
$this->deleteAlbisMailTemplate($context);
$this->deleteAlbisPaymentRule($context);
$this->deleteAlbisBusinessEvent($context);
}
public function activate(ActivateContext $context): void
{
$this->setPaymentMethodIsActive(true, $context->getContext());
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
$this->setPaymentMethodIsActive(false, $context->getContext());
parent::deactivate($context);
}
private function setPaymentMethodIsActive(bool $active, Context $context): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentMethodId = $this->getPaymentMethodId();
// Payment does not even exist, so nothing to (de-)activate here
if (!$paymentMethodId) {
return;
}
$paymentMethod = [
'id' => $paymentMethodId,
'active' => $active,
];
$paymentRepository->update([$paymentMethod], $context);
}
private function getPaymentMethodId(): ?string
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
// Fetch ID for update
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier', AlbisPayment::class));
$paymentIds = $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext());
if ($paymentIds->getTotal() === 0) {
return null;
}
return $paymentIds->getIds()[0];
}
private function createAlbisProductGroups(Context $context):void
{
}
private function createAlbisPaymentMethod(Context $context): void
{
$paymentMethodExists = $this->getPaymentMethodId();
// Payment method exists already, no need to continue here
if ($paymentMethodExists) {
return;
}
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->create([[
'id' => $this->paymentMethodId,
'handlerIdentifier' => AlbisPayment::class,
'name' => 'Albis Leasing',
'description' => 'Leasing über Albis lease',
'pluginId' => $pluginId,
'active' => true,
'afterOrderEnabled' => false
]], $context);
}
private function createAlbisMailTemplate(Context $context): void
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateType = [
[
'id' => $this->mailTemplateTypeId,
'name' => 'Albis Leasingantrag übermittelt',
'technicalName' => 'albis_contract_submit',
'availableEntities' => [
'product' => 'product',
'salesChannel' => 'sales_channel',
'order' => 'order'
]
]
];
$mailTemplate = [
[
'id' => $this->mailTemplateId,
'mailTemplateTypeId' => $this->mailTemplateTypeId,
'subject' => [
'en-GB' => 'We have received your Leasing application',
'de-DE' => 'Wir haben Ihren Leasingantrag erhalten'
],
'description' => 'Leasingantrag wurde nach einer Bestellung eingereicht',
'senderName' => '{{ salesChannel.name }}',
'contentPlain' => file_get_contents(__DIR__ . '/Resources/snippet/mail/contract_submit/plain.txt'),
'contentHtml' => file_get_contents(__DIR__ . '/Resources/snippet/mail/contract_submit/html.txt')
]
];
try {
$mailTemplateTypeRepository->create($mailTemplateType, $context);
$mailTemplateRepository->create($mailTemplate, $context);
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
private function createAlbisPaymentRule(Context $context): void
{
$ruleRepository = $this->container->get('rule.repository');
$ruleRepository->create([[
'id' => $this->ruleId,
'name' => 'Bestellungen mit der Albis Leasing Zahlungsart',
'priority' => 0,
'description' => 'Alle Bestellungen, die mit der Albis Leasing Zahhlungsart übermittelt wurden. 2',
'conditions' => [
[
'type' => 'paymentMethod',
'value' => [
'operator' => '=',
'paymentMethodIds' => [
'60c601c37b0040b0ada60d3e4a168e46'
]
]
]
]
]], $context);
}
private function createAlbisBusinessEvent(Context $context): void
{
$eventActionRepository = $this->container->get('event_action.repository');
$eventActionRepository->create([[
'eventName' => 'albis.leasing.application.placed',
'actionName' => 'action.mail.send',
'title' => 'Albis Leasingantrag wurde eingereicht',
'description' => 'Albis',
'active' => true,
'config' => [
'mail_template_id' => $this->mailTemplateId,
'mail_template_type_id' => $this->mailTemplateTypeId
],
// 'rules' => [
// [
// 'id' => $this->ruleId
// ],
// ],
]], $context);
}
private function deleteAlbisPaymentMethod(UninstallContext $uninstallContext)
{
$paymentMethodId = $this->getPaymentMethodId();
// Payment method doesnt exists, no need to continue here
if (!$paymentMethodId) {
return;
}
// @Bug-961215 - Wait for shopware 6 plugin payment method deletion by api
// /** @var EntityRepositoryInterface $paymentRepository */
// $paymentRepository = $this->container->get('payment_method.repository');
//
// $paymentRepository->delete([
// [
// 'id' => $paymentMethodId
// ]
// ], $uninstallContext->getContext());
}
private function deleteAlbisMailTemplate(UninstallContext $uninstallContext)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var MailTemplateTypeEntity $myCustomMailTemplateType */
$myCustomMailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('technicalName', 'albis_contract_submit')),
$uninstallContext->getContext()
)->first();
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())),
$uninstallContext->getContext()
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
$mailTemplateRepository->delete($ids, $uninstallContext->getContext());
$mailTemplateTypeRepository->delete([
[
'id' => $myCustomMailTemplateType->getId()
]
], $uninstallContext->getContext());
}
private function deleteAlbisPaymentRule(UninstallContext $context): void
{
$ruleRepository = $this->container->get('rule.repository');
/** @var RuleEntity $paymentRule */
$paymentRule = $ruleRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', 'Bestellungen mit der Albis Leasing Zahlungsart')),
$context->getContext()
)->first();
// Rule doesnt exists, no need to continue here
if(!$paymentRule) {
return;
}
$ruleRepository->delete([
[
'id' => $paymentRule->getId()
]
], $context->getContext());
}
private function deleteAlbisBusinessEvent(UninstallContext $context): void
{
$eventActionRepository = $this->container->get('event_action.repository');
/** @var EventActionEntity $paymentRule */
$eventAction = $eventActionRepository->search(
(new Criteria())->addFilter(new EqualsFilter('title', 'Albis Leasingantrag wurde eingereicht')),
$context->getContext()
)->first();
// Rule doesnt exists, no need to continue here
if(!$eventAction) {
return;
}
$eventActionRepository->delete([
[
'id' => $eventAction->getId()
]
], $context->getContext());
}
}