<?php
declare(strict_types=1);
namespace K3n\Logistics;
use Shopware\Core\Defaults;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\StateMachine\StateMachineEntity;
class K3nLogistics extends Plugin
{
private const STATE_MACHINE_STATE_MISSING_DOCUMENTS = 'missing_documents';
private const STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID = '52430dcb6e53bb3b4d0bc71729a9f367';
private const STATE_MACHINE_TRANSITION_OPEN_TO_MISSING_DOCUMENTS_ID = '1379e36c314356ff1083f6e7f4b4bb27';
private const STATE_MACHINE_TRANSITION_IN_PROGRESS_TO_MISSING_DOCUMENTS_ID = '0c0952754a26683fc6f116a6c7e67624';
private const STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_COMPLETED_ID = '90286a9972c90767b4056d87a9e6bf9e';
private const STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_OPEN_ID = 'b85bc7625590a591782bcff6f9ea2516';
public function install(InstallContext $installContext): void
{
$this->createLogisticsRole($installContext->getContext());
$this->createOrderStatus($installContext->getContext());
}
private function createLogisticsRole(Context $context)
{
/**
* @var EntityRepositoryInterface
*/
$aclRoleRepository = $this->container->get('acl_role.repository');
$aclLogisticsRole = [
'id' => '1a1b40f6a91c4c2abe61c410361b923b',
'name' => 'Logistics',
'description' => 'As a Logistics User You can see all the orders and You can change the status of the order to done / missing documents',
'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"
]
];
$aclRoleRepository->upsert([$aclLogisticsRole], $context);
}
private function createOrderStatus(Context $context)
{
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
$stateMachineTransitionRepository = $this->container->get('state_machine_transition.repository');
// 'missing_documents' state
$stateMachineMissingDocumentsState = $stateMachineStateRepository->search(new Criteria([self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_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_IN_PROGRESS_ID = $this->getOrderStateMachineStateId($context, $STATE_MACHINE_ORDER_ID, 'in_progress');
$STATE_MACHINE_STATE_COMPLETED_ID = $this->getOrderStateMachineStateId($context, $STATE_MACHINE_ORDER_ID, 'completed');
if (count($stateMachineMissingDocumentsState) == 0) {
$stateMachineStateRepository->create([
[
'id' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID,
'technicalName' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS,
'name' => 'Missing documents',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'translations' => [
$GERMAN_LANGUAGE_ID => [
'name' => 'Fehlende Unterlagen'
],
$ENGLISH_LANGUAGE_ID => [
'name' => 'Missing documents'
],
$POLISH_LANGUAGE_ID => [
'name' => 'BrakujÄ…ce dokumenty'
]
]
],
], $context);
}
$stateMachineOpenToMissingDocumentTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_OPEN_TO_MISSING_DOCUMENTS_ID]), $context)->getElements();
if (count($stateMachineOpenToMissingDocumentTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_OPEN_TO_MISSING_DOCUMENTS_ID,
'actionName' => 'open_to_missing_document',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'toStateId' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
$stateMachineInProgressToMissingDocumentTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_IN_PROGRESS_TO_MISSING_DOCUMENTS_ID]), $context)->getElements();
if (count($stateMachineInProgressToMissingDocumentTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_IN_PROGRESS_TO_MISSING_DOCUMENTS_ID,
'actionName' => 'in_progress_to_missing_document',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => $STATE_MACHINE_STATE_IN_PROGRESS_ID,
'toStateId' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
$stateMachineMissingDocumentsToCompletedTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_COMPLETED_ID]), $context)->getElements();
if (count($stateMachineMissingDocumentsToCompletedTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_COMPLETED_ID,
'actionName' => 'missing_documents_to_completed',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID,
'toStateId' => $STATE_MACHINE_STATE_COMPLETED_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
$stateMachineMissingDocumentsToOpenTransition = $stateMachineTransitionRepository->search(new Criteria([self::STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_OPEN_ID]), $context)->getElements();
if (count($stateMachineMissingDocumentsToOpenTransition) == 0) {
$stateMachineTransitionRepository->create([
[
'id' => self::STATE_MACHINE_TRANSITION_MISSING_DOCUMENTS_TO_OPEN_ID,
'actionName' => 'missing_documents_to_open',
'stateMachineId' => $STATE_MACHINE_ORDER_ID,
'fromStateId' => self::STATE_MACHINE_STATE_MISSING_DOCUMENTS_ID,
'toStateId' => $STATE_MACHINE_STATE_OPEN_ID,
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
], $context);
}
}
private function deleteLogisticsRole(Context $context)
{
/**
* @var EntityRepositoryInterface $repository
*/
$aclRoleRepository = $this->container->get('acl_role.repository');
$aclRoleRepository->delete([['id' => '1a1b40f6a91c4c2abe61c410361b923b']], $context);
}
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 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 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();
}
}