<?php
namespace CioPointsFlow\Subscriber;
use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
use CioMasterdata\Service\MasterdataService;
use CioPointsFlow\Service\AccountsCalculationService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AccountPageSubscriber implements EventSubscriberInterface
{
private MasterdataService $masterdataService;
private AccountsCalculationService $accountsCalculationService;
public function __construct(MasterdataService $masterdataService, AccountsCalculationService $accountsCalculationService)
{
$this->masterdataService = $masterdataService;
$this->accountsCalculationService = $accountsCalculationService;
}
public static function getSubscribedEvents()
{
return [
StorefrontRenderEvent::class => 'onStorefrontRenderEvent',
CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent'
];
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
$customer = $event->getSalesChannelContext()->getCustomer();
if ($customer) {
if (key_exists('page', $event->getParameters()) && $event->getParameters()['page'] instanceof AccountOverviewPage) {
if ($this->masterdataService->getMasterdatasForCustomer($customer) instanceof EntitySearchResult) {
$event->setParameter('masterdata', $this->masterdataService->getMasterdatasForCustomer($customer, true)->first());
}
$event->setParameter('childBudgets', $this->accountsCalculationService->getChildBudgets());
$event->setParameter('totalAccountAmount', $this->accountsCalculationService->calculateTotalAccount($event->getContext()));
$event->setParameter('statusAccountAmount', $this->accountsCalculationService->calculateStatusAccountIncludingChildBudgets($event->getContext()));
$event->setParameter('useableAccountAmount', $this->accountsCalculationService->calculateUsableAccount($event->getSalesChannelContext()->getContext(), $event->getSalesChannelContext()->getCustomer()));
}
}
}
public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
{
// add all in this plugin used customer acl roles
$event->addRoles([
[
'title' => 'BUDGET_EXPIRE_CALCULATE',
'description' => 'Kunde kann Punkte die aktuell in einem ausgewählten Zeitraum verfallen würden ermitteln und ausgeben lassen.'
],
[
'title' => 'BUDGET_EXPIRE_EXECUTE_CALCULATED',
'description' => 'Kunde kann Punkteverfall ausführen.'
]
]);
}
}