custom/plugins/TwigelBonusProgram/src/Subscriber/OrderDelete.php line 70

Open in your IDE?
  1. <?php
  2. namespace Twigel\BonusProgram\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Twigel\BonusProgram\Core\Content\TwigelOrderStatus\TwigelOrderStatusEntity;
  14. use Twigel\BonusProgram\Service\TwigelCustomerOrderService;
  15. class OrderDelete implements EventSubscriberInterface
  16. {
  17.     /** @var EntityRepository */
  18.     private $orderStatusRepository;
  19.     /** @var EntityRepository */
  20.     private $orderRepository;
  21.     /** @var EntityRepository */
  22.     private $customerRepository;
  23.     /** @var SystemConfigService */
  24.     private $config;
  25.     /** @var EntityRepository */
  26.     private $orderCustomerRepository;
  27.     /** @var EntityRepository */
  28.     private $customerAccountRepository;
  29.     /** @var EntityRepository */
  30.     private $twigelStatusRepository;
  31.     public function __construct(
  32.         EntityRepository $orderStatusRepository,
  33.         EntityRepository $orderRepository,
  34.         EntityRepository $customerRepository,
  35.         SystemConfigService $config,
  36.         EntityRepository $orderCustomerRepository,
  37.         EntityRepository $customerAccountRepository,
  38.         EntityRepository $twigelStatusRepository
  39.     )
  40.     {
  41.         $this->orderStatusRepository $orderStatusRepository;
  42.         $this->orderRepository $orderRepository;
  43.         $this->customerRepository $customerRepository;
  44.         $this->config $config;
  45.         $this->orderCustomerRepository $orderCustomerRepository;
  46.         $this->customerAccountRepository $customerAccountRepository;
  47.         $this->twigelStatusRepository $twigelStatusRepository;
  48.     }
  49.     /**
  50.      * @inheritDoc
  51.      */
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             OrderEvents::ORDER_DELETED_EVENT => 'deleteOrder'
  56.         ];
  57.     }
  58.     /**
  59.      * On order delete check customer turnOver and set group for customer
  60.      * @param EntityWrittenEvent $event
  61.      */
  62.     public function deleteOrder(EntityWrittenEvent $event)
  63.     {
  64.         $criteria = new Criteria();
  65.         $criteria->addFilter(new EqualsFilter('orderId'null));
  66.         $orderStatus $this->orderStatusRepository->search($criteriaContext::createDefaultContext())->getElements();
  67.         /** @var TwigelOrderStatusEntity $item */
  68.         foreach ($orderStatus as $item) {
  69.             $customerId $item->getCustomerId();
  70.             $status self::getCustomerTurnOver($customerId);
  71.             self::checkCustomerGroup($status$customerId);
  72.             self::checkCustomerAccount($customerId$item->getBonusAmount());
  73.             $this->orderStatusRepository->delete([
  74.                 [
  75.                     'id' => $item->getId()
  76.                 ]
  77.             ], Context::createDefaultContext());
  78.         }
  79.     }
  80.     /**
  81.      * Return customer status based on his turnOver
  82.      * @param $customerId
  83.      * @return string
  84.      */
  85.     private function getCustomerTurnOver($customerId): string
  86.     {
  87.         $service = new TwigelCustomerOrderService($this->orderCustomerRepository);
  88.         return $service->countTurnOver($customerId);
  89.     }
  90.     private function checkCustomerGroup($status$customerId)
  91.     {
  92.         $criteria = new Criteria([$customerId]);
  93.         /** @var CustomerEntity $customer */
  94.         $customer $this->customerRepository->search($criteriaContext::createDefaultContext())->first();
  95.         if ($status){
  96.             $statusGroupId $this->config->get("TwigelBonusProgram")['config'][TwigelCustomerOrderService::CONFIG_CUSTOMER_GROUPS[$status]];
  97.         }else{
  98.             $statusGroupId Defaults::FALLBACK_CUSTOMER_GROUP;
  99.         }
  100.         if ($statusGroupId !== $customer->getGroupId()) {
  101.             $this->customerRepository->update([
  102.                 [
  103.                     'id' => $customerId,
  104.                     'groupId' => $statusGroupId
  105.                 ]
  106.             ], Context::createDefaultContext());
  107.         }
  108.     }
  109.     private function checkCustomerAccount($customerId$bonusAmount)
  110.     {
  111.         $criteria = new Criteria();
  112.         $criteria->addFilter(new EqualsFilter('customerId'$customerId));
  113.         $customerAccount $this->customerAccountRepository->search($criteriaContext::createDefaultContext())->first();
  114.         if (!$customerAccount) {
  115.             return null;
  116.         }
  117.         $status self::getCustomerStatusId($customerIdContext::createDefaultContext());
  118.         $update = [
  119.             'id' => $customerAccount->getId(),
  120.             'bonusAmount' => $customerAccount->getBonusAmount() - (int)$bonusAmount
  121.         ];
  122.         if ($status){
  123.             $update['statusId'] = $status->getId();
  124.         }else{
  125.             $update['statusId'] = null;
  126.         }
  127.         $this->customerAccountRepository->update([$update], Context::createDefaultContext());
  128.     }
  129.     private function getCustomerStatusId($customerIdContext $context)
  130.     {
  131.         $status self::getCustomerStatus($customerId);
  132.         if ($status['turnOver']) {
  133.             $customerGroupId $this->config->get('TwigelBonusProgram')['config'][TwigelCustomerOrderService::CONFIG_CUSTOMER_GROUPS[$status['turnOver']]];
  134.             $statusCriteria = new Criteria();
  135.             $statusCriteria->addFilter(new EqualsFilter('customerGroupId'$customerGroupId));
  136.             return $this->twigelStatusRepository->search($statusCriteria$context)->first();
  137.         } else {
  138.             return null;
  139.         }
  140.     }
  141.     private function getCustomerStatus($customerId)
  142.     {
  143.         $service = new TwigelCustomerOrderService($this->orderCustomerRepository);
  144.         return [
  145.             'turnOver' => $service->countTurnOver($customerId),
  146.             'amountOfOrders' => $service->countOrdersOfCustomer($customerId)
  147.         ];
  148.     }
  149. }