<?php
namespace CioApprovalThreshold\Subscriber;
use CioApprovalThreshold\Definition\Customer\CustomerApprovalThresholdEntity;
use CioApprovalThreshold\Definition\Order\OrderApprovalThresholdEntity;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderPage;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutConfirmSubscriber implements EventSubscriberInterface
{
private EntityRepository $customerRepository;
public function __construct(EntityRepository $customerRepository)
{
$this->customerRepository = $customerRepository;
}
public static function getSubscribedEvents()
{
return [
StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
];
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
$possibleReleaseCustomer = [];
$defaultReleaseCustomer = null;
$customer = $event->getSalesChannelContext()->getCustomer();
$freigeber = [];
if ($customer instanceof CustomerEntity) {
if (is_array($event->getParameters()) && key_exists('page', $event->getParameters())) {
$page = $event->getParameters()['page'];
if($page instanceof AccountOrderPage) {
foreach($page->getOrders()->getElements() as $orderId => $order) {
$freigeberCustomer = null;
/** @var OrderApprovalThresholdEntity $orderApprovalThresholdEntity */
$orderApprovalThresholdEntity = $order->getExtension('approvalThresholdExtension');
if($orderApprovalThresholdEntity instanceof OrderApprovalThresholdEntity) {
$freigeberId = $orderApprovalThresholdEntity->getSelectedReleaseCustomerId();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $freigeberId));
$freigeberCustomer = $this->customerRepository->search($criteria, $event->getContext())->first();
}
if ($freigeberCustomer instanceof CustomerEntity) {
$freigeber[$orderId] = $freigeberCustomer->getFirstName() . ' ' . $freigeberCustomer->getLastName();
} else {
$freigeber[$orderId] = '';
}
}
}
if ($page instanceof CheckoutConfirmPage) {
$totalPrice = $page->getCart()->getPrice()->getTotalPrice();
$criteria = new Criteria();
$criteria->addFilter(new RangeFilter('approvalThresholdExtension.releaseThreshold', [
RangeFilter::GTE => $totalPrice
]));
$criteria->addSorting(new FieldSorting('lastName', FieldSorting::ASCENDING), new FieldSorting('firstName', FieldSorting::ASCENDING, true));
$possibleReleaseCustomer = $this->customerRepository->search($criteria, $event->getContext())->getElements();
/** @var CustomerApprovalThresholdEntity|null $customerThreshold */
$customerThreshold = $event->getSalesChannelContext()->getCustomer()->getExtension('approvalThresholdExtension');
$defaultReleaseCustomer = $customerThreshold?->getDefaultReleaseCustomerId();
}
}
}
$event->setParameter('freigeber', $freigeber);
$event->setParameter('cioPossibleReleaseCustomer', $possibleReleaseCustomer);
$event->setParameter('cioDefaultReleaseCustomer', $defaultReleaseCustomer);
}
}