<?php
declare(strict_types=1);
namespace Vio\B2BWorkflow\Core\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use VioB2BLogin\Core\Services\EmployeeService;
use VioB2BLogin\Entity\Employee\EmployeeEntity;
use VioB2BLogin\VioB2BLogin;
use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutCustomerOrderRequestPlacedEvent;
use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutEmployeeOrderRequestPlacedEvent;
use Vio\B2BWorkflow\Core\ContextExtension;
use Vio\B2BWorkflow\Services\StateService;
class CartSubscriber implements EventSubscriberInterface
{
private EmployeeService $employeeService;
private StateService $stateService;
private EventDispatcherInterface $eventDispatcher;
private EntityRepositoryInterface $employeeRepository;
public function __construct(
EmployeeService $employeeService,
StateService $stateService,
EventDispatcherInterface $eventDispatcher,
EntityRepositoryInterface $employeeRepository
)
{
$this->employeeService = $employeeService;
$this->stateService = $stateService;
$this->eventDispatcher = $eventDispatcher;
$this->employeeRepository = $employeeRepository;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'onCartConverted',
CheckoutOrderPlacedEvent::class => ['onOrderPlaced', 999]
];
}
public function onCartConverted(CartConvertedEvent $event): void
{
$cart = $event->getCart();
$convertedCart = $event->getConvertedCart();
$salesChannelContext = $event->getSalesChannelContext();
$employee = $this->employeeService->getEmployeeByContext($salesChannelContext);
// check rights
if ($employee
&& $cart->hasExtensionOfType(ContextExtension::KEY, ContextExtension::class)) {
$convertedCart['stateId'] = $this->stateService->getApprovalState($event->getContext())->getId();
foreach ($convertedCart['transactions'] as &$transaction) {
$transaction['stateId'] = $this->stateService->getPresetState($event->getContext())->getId();
}
unset($transaction);
$extension = (new ContextExtension())->setActive(true);
$event->getContext()->addExtension(ContextExtension::KEY, $extension);
}
$event->setConvertedCart($convertedCart);
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
if ($order->getStateId() === $this->stateService->getApprovalState($event->getContext())->getId()) {
// when the order has an employee
$orderCustomFields = $event->getOrder()->getCustomFields();
if (is_array($orderCustomFields)
&& array_key_exists(VioB2BLogin::EMPLOYEE_FIELD, $orderCustomFields)) {
// load Employee
$employeeId = $orderCustomFields[VioB2BLogin::EMPLOYEE_FIELD];
$criteria = new Criteria([$employeeId]);
$criteria->addAssociation('customer');
$employee = $this->employeeRepository->search($criteria, $event->getContext())->first();
if ($employee instanceof EmployeeEntity) {
$this->eventDispatcher->dispatch(new CheckoutEmployeeOrderRequestPlacedEvent(
$event->getContext(),
$event->getOrder(),
$employee,
$event->getSalesChannelId()
));
$this->eventDispatcher->dispatch(new CheckoutCustomerOrderRequestPlacedEvent(
$event->getContext(),
$event->getOrder(),
$employee,
$event->getSalesChannelId()
));
}
}
$event->stopPropagation();
}
}
}