<?php
declare(strict_types=1);
namespace K3n\Merchant;
use Shopware\Core\Defaults;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\StateMachine\StateMachineEntity;
use Shopware\Core\Framework\Uuid\Uuid;
class K3nMerchant extends Plugin
{
private const STATE_MACHINE_STATE_ACCEPTED = 'accepted';
private const STATE_MACHINE_STATE_ACCEPTED_ID = '6ae733b8491047c9bac615e399105a7e';
private const STATE_MACHINE_STATE_REJECTED = 'rejected';
private const STATE_MACHINE_STATE_REJECTED_ID = '3d17146ff9a24e9a809b7e75ac39df82';
private const STATE_MACHINE_STATE_PURCHASE = 'purchase';
private const STATE_MACHINE_STATE_PURCHASE_ID = 'ef4c30a065535c27e2f5818ff550e0d8';
private const STATE_MACHINE_TRANSITION_ACCEPTED_ID = '6fd97d792b549594df4fb55b617c4f9b';
private const STATE_MACHINE_TRANSITION_REJECTED_ID = 'd51c3cc92d88cbf02f32408cbbee97bc';
private const STATE_MACHINE_TRANSITION_CANCEL_REJECTED_ID = '1d45e961e8fedf3dd1ed953818804f7c';
private const STATE_MACHINE_TRANSITION_PURCHASE_ID = 'c13a25d781e8ceb3309626c1a50bc868';
private const STATE_MACHINE_TRANSITION_OPEN_PURCHASED_ID = 'ab2f9d882cdea7dd4f25cb9e97c14eb1';
private const STATE_MACHINE_TRANSITION_REJECT_ACCEPTED_ID = 'baef4894edd1d48807d24f53517b8294';
private const STATE_MACHINE_TRANSITION_OPEN_DONE_ID = 'cf20a65c00fa9f274791243e4939324a';
public function install(InstallContext $installContext): void
{
$context = $installContext->getContext();
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
$stateMachineTransitionRepository = $this->container->get('state_machine_transition.repository');
// 'accepted' state
$stateMachineAcceptedState = $stateMachineStateRepository->search(new Criteria([self::STATE_MACHINE_STATE_ACCEPTED_ID]), $context)->getElements();
$STATE_MACHINE_ORDER_ID = $this->getOrderStateMachineId($context);
$GERMAN_LANGUAGE_ID = $this->getLanguageId($context, 'de-DE');
$ENGLISH_LANGUAGE_ID = $this->getLanguageId($context, 'en-GB');
$POLISH_LANGUAGE_ID = $this->getLanguageId($context, 'pl-PL');
$STATE_MACHINE_STATE_OPEN_ID = $this->getOrderStateMachineStateId($context, $STATE_MACHINE_ORDER_ID, 'open');
$STATE_MACHINE_STATE_CANCELLED_ID = $this->getOrderStateMachineStateId($context, $STATE_MACHINE_ORDER_ID, 'cancelled');
$STATE_MACHINE_STATE_DONE_ID = $this->getOrderStateMachineStateId($context, $STATE_MACHINE_ORDER_ID, 'completed');
if (count($stateMachineAcceptedState) == 0) {
$stateMachineStateRepository->create([
[
'id' => self::STATE_MACHINE_STATE_ACCEPTED_ID,
'technicalName' => self::STATE_MACHINE_STATE_ACCEPTED,
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'translations' => [
$GERMAN_LANGUAGE_ID => [
'name' => 'Akzeptiert'
],
$ENGLISH_LANGUAGE_ID => [
'name' => 'Accepted'
],
$POLISH_LANGUAGE_ID => [
'name' => 'Przyjęty'
]
]
],
], $context);
}
$stateMachineAcceptedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_ACCEPTED_ID]), $context)->getElements();
if (count($stateMachineAcceptedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_ACCEPTED_ID,
'actionName' => 'accepted',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'toStateId' => self::STATE_MACHINE_STATE_ACCEPTED_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
// 'rejected' state
$stateMachineRejectedState = $stateMachineStateRepository->search(new Criteria([self::STATE_MACHINE_STATE_REJECTED_ID]), $context)->getElements();
if (count($stateMachineRejectedState) == 0) {
$stateMachineStateRepository->create([
[
'id' => self::STATE_MACHINE_STATE_REJECTED_ID,
'technicalName' => self::STATE_MACHINE_STATE_REJECTED,
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'translations' => [
$GERMAN_LANGUAGE_ID => [
'name' => 'Abgelehnt'
],
$ENGLISH_LANGUAGE_ID => [
'name' => 'Rejected'
],
$POLISH_LANGUAGE_ID => [
'name' => 'Odrzucony'
],
]
],
], $context);
}
$stateMachineRejectedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_REJECTED_ID]), $context)->getElements();
if (count($stateMachineRejectedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_REJECTED_ID,
'actionName' => 'rejected',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'toStateId' => self::STATE_MACHINE_STATE_REJECTED_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
$stateMachineCancelRejectedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_CANCEL_REJECTED_ID]), $context)->getElements();
if (count($stateMachineCancelRejectedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_CANCEL_REJECTED_ID,
'actionName' => 'cancel_rejected',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_REJECTED_ID,
'toStateId' => $STATE_MACHINE_STATE_CANCELLED_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
// 'purchase' state
$stateMachinePurchaseState = $stateMachineStateRepository->search(new Criteria([self::STATE_MACHINE_STATE_PURCHASE_ID]), $context)->getElements();
if (count($stateMachinePurchaseState) == 0) {
$stateMachineStateRepository->create([
[
'id' => self::STATE_MACHINE_STATE_PURCHASE_ID,
'technicalName' => self::STATE_MACHINE_STATE_PURCHASE,
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'translations' => [
$GERMAN_LANGUAGE_ID => [
'name' => 'Kauf'
],
$ENGLISH_LANGUAGE_ID => [
'name' => 'Purchase'
],
$POLISH_LANGUAGE_ID => [
'name' => 'Zakup'
],
]
],
], $context);
}
$stateMachinePurchaseTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_PURCHASE_ID]), $context)->getElements();
if (count($stateMachinePurchaseTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_PURCHASE_ID,
'actionName' => 'purchase',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_ACCEPTED_ID,
'toStateId' => self::STATE_MACHINE_STATE_PURCHASE_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
$stateMachineOpenPurchasedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_OPEN_PURCHASED_ID]), $context)->getElements();
if (count($stateMachineOpenPurchasedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_OPEN_PURCHASED_ID,
'actionName' => 'open_purchased',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_PURCHASE_ID,
'toStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
// transition from accepted to rejected
$stateMachineRejectAcceptedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_REJECT_ACCEPTED_ID]), $context)->getElements();
if (count($stateMachineRejectAcceptedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_REJECT_ACCEPTED_ID,
'actionName' => 'reject_accepted',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_ACCEPTED_ID,
'toStateId' => self::STATE_MACHINE_STATE_REJECTED_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
// transition from open to done
$stateMachineOpenDoneTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_OPEN_DONE_ID]), $context)->getElements();
if (count($stateMachineOpenDoneTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_OPEN_DONE_ID,
'actionName' => 'reject_accepted',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'toStateId' => $STATE_MACHINE_STATE_DONE_ID ,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
}
private function getOrderStateMachineId(Context $context): string
{
/**
* @var EntityRepositoryInterface $stateMachineRepository
*/
$stateMachineRepository = $this->container->get('state_machine.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('technicalName', 'order.state'));
/**
* @var StateMachineEntity $orderStateMachine
*/
$orderStateMachine = $stateMachineRepository->search($criteria, $context)->first();
return $orderStateMachine->getId();
}
private function getLanguageId(Context $context, string $languageCode): ?string
{
/**
* @var EntityRepositoryInterface $languageRepository
*/
$languageRepository = $this->container->get('language.repository');
$criteria = new Criteria();
$criteria->addAssociation('locale');
$criteria->addFilter(new EqualsFilter('locale.code', $languageCode));
/**
* @var LanguageEntity $language
*/
$language = $languageRepository->search($criteria, $context)->first();
if ($language) {
return $language->getId();
}
return null;
}
private function getOrderStateMachineStateId(Context $context, string $orderStateMachineId, string $technicalName): string
{
/**
* @var EntityRepositoryInterface $stateMachineStateRepository
*/
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('stateMachineId', $orderStateMachineId));
$criteria->addFilter(new EqualsFilter('technicalName', $technicalName));
/**
* @var StateMachineStateEntity $stateMachineState
*/
$stateMachineState = $stateMachineStateRepository->search($criteria, $context)->first();
return $stateMachineState->getId();
}
public function update(InstallContext $installContext): void
{
$this->updateRetailerRole($installContext->getContext());
}
private function updateRetailerRole(Context $context)
{
/**
* @var EntityRepositoryInterface
*/
$aclRoleRepository = $this->container->get('acl_role.repository');
$aclRetailerRole = [
'id' => 'a390397f72be4b4ab1f8d61e08849108',
'name' => 'Retailer',
'description' => 'As a Retailer you can see your own orders.',
'privileges' => [
"country:read",
"country_state:read",
"currency:read",
"custom_field:read",
"custom_field_set:read",
"custom_field_set_relation:read",
"customer:read",
"customer_address:read",
"document:create",
"document:read",
"document:update",
"document_type:read",
"iwv_extra_order_file:create",
"iwv_extra_order_file:delete",
"iwv_extra_order_file:read",
"iwv_extra_order_file:update",
"language:read",
"locale:read",
"log_entry:create",
"mail_template:read",
"mail_template_sales_channel:create",
"mail_template_sales_channel:read",
"mail_template_type:read",
"message_queue_stats:read",
"order.editor",
"order.viewer",
"order:delete",
"order:read",
"order:update",
"order_address:create",
"order_address:read",
"order_address:update",
"order_customer:read",
"order_customer:update",
"order_delivery:read",
"order_delivery:update",
"order_line_item:create",
"order_line_item:read",
"order_line_item:update",
"order_tag:create",
"order_tag:delete",
"order_tag:read",
"order_tag:update",
"order_transaction:read",
"payment_method:read",
"product:read",
"product_visibility:read",
"property_group:read",
"property_group_option:read",
"sales_channel:read",
"salutation:read",
"shipping_method:read",
"state_machine:read",
"state_machine_history:create",
"state_machine_history:read",
"state_machine_state:read",
"state_machine_transition:read",
"swag_language_pack_language:read",
"tag:create",
"tag:read",
"user:read",
"user_config:create",
"user_config:read",
"user_config:update",
"vio_representative_agent:read",
"customer_group:read",
"acl_role:read", // Result in error: Access to entity "acl_role" are not allowed in scope "user"
"version:delete"
]
];
$aclRoleRepository->upsert([$aclRetailerRole], $context);
}
}