custom/plugins/EconsorCrowdfunding/src/Subscriber/CheckoutSubscriber.php line 133

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EconsorCrowdfunding\Subscriber;
  4. use EconsorCrowdfunding\Services\CrowdfundingMailService;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Routing\RouterInterface;
  17. /**
  18.  * Class CheckoutSubscriber
  19.  * @package EconsorCrowdfunding\Subscriber
  20.  */
  21. class CheckoutSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var Session
  25.      */
  26.     private $session;
  27.     /**
  28.      * @var EntityRepositoryInterface
  29.      */
  30.     private $campaignRepo;
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     private $orderRepo;
  35.     /**
  36.      * @var EntityRepositoryInterface
  37.      */
  38.     private $orderHistoryRepo;
  39.     /**
  40.      * @var RouterInterface
  41.      */
  42.     private $router;
  43.     /**
  44.      * @var EntityRepositoryInterface
  45.      */
  46.     private $goodieRepository;
  47.     /**
  48.      * @var EntityRepositoryInterface
  49.      */
  50.     private $orderBundleRepo;
  51.     /**
  52.      * @var EntityRepositoryInterface
  53.      */
  54.     private $bundleRepo;
  55.     /**
  56.      * @var CrowdfundingMailService
  57.      */
  58.     private $mailService;
  59.     /**
  60.      * @var SystemConfigService
  61.      */
  62.     private $configService;
  63.     /**
  64.      * @var SystemConfigService
  65.      */
  66.     private $ownSponsorRepository;
  67.     /**
  68.      * @var EntityRepositoryInterface
  69.      */
  70.     private $validationRepo;
  71.     /**
  72.      * @var EntityRepositoryInterface
  73.      */
  74.     private $shirtConfigRepo;
  75.     public static function getSubscribedEvents(): array
  76.     {
  77.         return [
  78.             KernelEvents::REQUEST => 'handleRequest'
  79.         ];
  80.     }
  81.     public function __construct(
  82.         Session $session,
  83.         EntityRepositoryInterface $campaignRepository,
  84.         EntityRepositoryInterface $orderRepository,
  85.         EntityRepositoryInterface $orderHistoryRepository,
  86.         EntityRepositoryInterface $goodieRepository,
  87.         RouterInterface $router,
  88.         EntityRepositoryInterface $tempBundles// TODO !!!!!!!!!!!!!!!!!!!!!!
  89.         EntityRepositoryInterface $orderBundles,
  90.         CrowdfundingMailService $mailService,
  91.         SystemConfigService $configService,
  92.         EntityRepositoryInterface $ownSponsor,
  93.         EntityRepositoryInterface $validation,
  94.         EntityRepositoryInterface $shirtConfig
  95.     ) {
  96.         $this->session $session;
  97.         $this->campaignRepo $campaignRepository;
  98.         $this->orderRepo $orderRepository;
  99.         $this->orderHistoryRepo $orderHistoryRepository;
  100.         $this->goodieRepository $goodieRepository;
  101.         $this->router $router;
  102.         $this->bundleRepo $tempBundles;
  103.         $this->orderBundleRepo $orderBundles;
  104.         $this->mailService $mailService;
  105.         $this->configService $configService;
  106.         $this->ownSponsorRepository $ownSponsor;
  107.         $this->validationRepo $validation;
  108.         $this->shirtConfigRepo $shirtConfig;
  109.     }
  110.     /**
  111.      * @param RequestEvent $event
  112.      */
  113.     public function handleRequest(RequestEvent $event)
  114.     {
  115.         $route $event->getRequest()->attributes->get('_route');
  116.         $oID $event->getRequest()->query->get('orderId');
  117.         if ('frontend.checkout.finish.page' === $route) {
  118.             if ($this->session->has('campaignID')) {
  119.                 $redirect $this->processCrowdfundingOrder($oID);
  120.                 $this->saveBundleData($oID);
  121.                 if($this->session->has('own-sponsor'))
  122.                 {
  123.                     $params $this->session->get('params');
  124.                     $c = new Criteria();
  125.                     $c->setIds([$this->session->get('campaignID')]);
  126.                     $campaign $this->campaignRepo->search($cContext::createDefaultContext())->getEntities()->first();
  127.                     $this->mailService->sendSponsor(
  128.                         $this->mailService->getMail($this->configService->get('EconsorCrowdfunding.config.campaignAwaitConfirmationSponsor')),
  129.                         $params['ownsponsorid'],
  130.                         $campaign->getSaleschannelid()
  131.                     );
  132.                     $this->campaignRepo->update([['id' => $campaign->getId(), 'status' => 6]], Context::createDefaultContext());
  133.                     $this->session->remove('own-sponsor');
  134.                     $this->session->remove('params');
  135.                 }
  136.                 $this->session->remove('campaignID');
  137.                 $this->session->remove('slug');
  138.                 $this->session->remove('sponsor');
  139.                 $event->setResponse($redirect);
  140.             }
  141.             //##########################################################################################################
  142.             if ($this->session->has('cartExtensions')) {
  143.                 if (null !== $this->session->get('cartExtensions')) {
  144.                     $this->processOrderExtensions(
  145.                         $oID,
  146.                         $this->session->get('cartExtensions')
  147.                     );
  148.                 }
  149.                 $this->session->remove('cartExtensions');
  150.             }
  151.             //##########################################################################################################
  152.             if ($this->session->has('shirtExtensions')) {
  153.                 $params $this->session->get('shirtExtensions');
  154.                 if($params)
  155.                 {
  156.                     $params $params[array_key_first($params)];
  157.                     $id $params['campaign'];
  158.                     $campaign $this->getCampaign($id);
  159.                     if (null !== $this->session->get('shirtExtensions')) {
  160.                         $this->processShirtExtensions(
  161.                             $oID,
  162.                             $this->session->get('shirtExtensions'),
  163.                             $campaign
  164.                         );
  165.                     }
  166.                 }
  167.                 $this->session->remove('shirtExtensions');
  168.             }
  169.             //##########################################################################################################
  170.             if ($this->session->has('sponsor-validate')) {
  171.                 $params $this->session->get('sponsor-validate');
  172.                 //delete validation flag
  173.                 $this->ownSponsorRepository->update(
  174.                     [
  175.                         [
  176.                             'id' => $params['sponsor'],
  177.                             'hash' => null,
  178.                             'confirmed' => true,
  179.                             'orderid' => $oID
  180.                         ]
  181.                     ],
  182.                     Context::createDefaultContext()
  183.                 );
  184.                 //change campaign status, substract from goal
  185.                 $c = new Criteria();
  186.                 $c->setIds([$params['campaign']]);
  187.                 $campaign $this->campaignRepo->search($cContext::createDefaultContext())->getEntities()->first();
  188.                 $c = new Criteria();
  189.                 $c->setIds([$params['sponsor']]);
  190.                 $sponsor $this->ownSponsorRepository->search($cContext::createDefaultContext())->getEntities(
  191.                 )->first();
  192.                 $this->campaignRepo->update(
  193.                     [
  194.                         [
  195.                             'id' => $params['campaign'],
  196.                             'fundinggoal' => ($campaign->getFundingGoal()) - (float)$params['amount'],
  197.                             'status' => 5
  198.                         ]
  199.                     ],
  200.                     Context::createDefaultContext()
  201.                 );
  202.                 //send mail to sponsor that he confirmed the campaign
  203.                 $this->mailService->sendCustomMail(
  204.                     $this->mailService->getMail(
  205.                         $this->configService->get('EconsorCrowdfunding.config.campaignSponsorConfirmed')
  206.                     ),
  207.                     false,
  208.                     [],
  209.                     [
  210.                         'email' => $sponsor->getEmail(),
  211.                         'firstname' => $sponsor->getName(),
  212.                         'lastname' => ''
  213.                     ],
  214.                     $campaign->getSaleschannelid()
  215.                 );
  216.                 $id Uuid::randomHex();
  217.                 $hash md5($id $sponsor->getEmail());
  218.                 //create submit page for trainer
  219.                 $this->validationRepo->create(
  220.                     [
  221.                         [
  222.                             'id' => $id,
  223.                             'hash' => $hash,
  224.                             'campaignid' => $params['campaign'],
  225.                             'values' => null
  226.                         ]
  227.                     ],
  228.                     Context::createDefaultContext()
  229.                 );
  230.                 //trainer mail, submit to admin there
  231.                 $this->mailService->send(
  232.                     $this->mailService->getMail(
  233.                         $this->configService->get(
  234.                             'EconsorCrowdfunding.config.campaignValidateCampaignTrainerAfterSponsor'
  235.                         )
  236.                     ),
  237.                     true,
  238.                     ['hash' => $hash],
  239.                     $campaign->getCustomer(),
  240.                     $campaign->getSaleschannelid()
  241.                 );
  242.             }
  243.         }
  244.     }
  245.     //##################################################################################################################
  246.     /**
  247.      * @param $oID
  248.      */
  249.     private function saveBundleData($oID)
  250.     {
  251.         $sessionId $this->session->get('sessionId');
  252.         $c = new Criteria();
  253.         $c->addFilter(new EqualsFilter('sessionId'$sessionId));
  254.         $tempBundle $this->bundleRepo->search($cContext::createDefaultContext())->getEntities();
  255.         $bundle $this->createBundle($tempBundle->first());
  256.         $this->orderBundleRepo->upsert(
  257.             [
  258.                 [
  259.                     'id' => Uuid::randomHex(),
  260.                     'orderid' => $oID,
  261.                     'bundlecontent' => $bundle
  262.                 ]
  263.             ],
  264.             Context::createDefaultContext()
  265.         );
  266.     }
  267.     /**
  268.      * @param $tempbundle
  269.      * @return array
  270.      */
  271.     private function createBundle($tempbundle): array
  272.     {
  273.         return [
  274.             'products' => [
  275.                 'shirtId' => $tempbundle->getShirtId(),
  276.                 'trousersId' => $tempbundle->getTrousersId(),
  277.                 'clipsId' => $tempbundle->getClipsId(),
  278.                 'keeperShirtId' => $tempbundle->getKeeperShirtId(),
  279.                 'keeperTrousersId' => $tempbundle->getKeeperTrousersId(),
  280.                 'keeperClipsId' => $tempbundle->getKeeperClipsId(),
  281.             ],
  282.             'configuration' => $tempbundle->getConfiguration(),
  283.             'clubData' => $tempbundle->getClubData()
  284.         ];
  285.     }
  286.     /**
  287.      * @param $orderID
  288.      * @param $extensions
  289.      */
  290.     private function processOrderExtensions($orderID$extensions)
  291.     {
  292.         $lineItems $this->getOrderLineItems($orderID);
  293.         $campaigns = [];
  294.         //dd($this->session);
  295.         foreach ($lineItems as $index => $item) {
  296.             $identifier $item->getIdentifier();
  297.             if (array_key_exists((string)$identifier$extensions)) {
  298.                 $campaigns[$extensions[$identifier]['campaignid']] = true;
  299.                 $this->processItem($extensions[$identifier], $orderID$index);
  300.                 $this->addToOrderHistory($extensions[$identifier]['campaignid'], $orderID$item->getId());
  301.             }
  302.         }
  303.         if ([] !== $campaigns) {
  304.             $this->processCampaigns($campaigns$orderID);
  305.         }
  306.     }
  307.     /**
  308.      * @param $orderID
  309.      * @param $extensions
  310.      * @param $campaign
  311.      */
  312.     private function processShirtExtensions($orderID$extensions$campaign)
  313.     {
  314.         $lineItems $this->getOrderLineItems($orderID);
  315.         $array = [];
  316.         foreach ($lineItems as $index => $item) {
  317.             $identifier $item->getProductId();
  318.             if (array_key_exists((string)$identifier$extensions)) {
  319.                 $array[$extensions[$identifier]['campaign']][$identifier] = $extensions[$identifier];
  320.                 $this->addAmountToGoal($campaign, [
  321.                     'amount' => $item->getTotalPrice()
  322.                 ]);
  323.                 $this->addShirtConfig($campaign$extensions[$identifier], $orderID$identifier);
  324.                 $this->addToOrderHistory($extensions[$identifier]['campaign'], $orderID$item->getId());
  325.             }
  326.         }
  327.         if ([] !== $array) {
  328.             $this->processCampaigns($array$orderID);
  329.         }
  330.     }
  331.     /**
  332.      * @param $campaign
  333.      * @param $params
  334.      * @param $orderID
  335.      * @param $product
  336.      */
  337.     private function addShirtConfig($campaign$params$orderID$product)
  338.     {
  339.         $this->shirtConfigRepo->upsert([[
  340.             'id' => Uuid::randomHex(),
  341.             'orderid' => $orderID,
  342.             'campaignid' => $campaign->getId(),
  343.             'productid' => $product,
  344.             'configuration' => $params
  345.         ]], Context::createDefaultContext());
  346.     }
  347.     /**
  348.      * @param $params
  349.      * @param $orderID
  350.      * @param $orderLineItemId
  351.      */
  352.     private function processItem($params$orderID$orderLineItemId)
  353.     {
  354.         $campaign $this->getCampaign($params['campaignid']);
  355.         $this->addAmountToGoal($campaign$params);
  356.         $this->addItemToHistory($params$orderID$orderLineItemId);
  357.         if (array_key_exists('goodieid'$params)) {
  358.             $goodie $this->getGoodie($params['goodieid']);
  359.             $this->updateGoodieStock($goodie$params);
  360.         }
  361.     }
  362.     /**
  363.      * @param $goodie
  364.      * @param $params
  365.      */
  366.     private function updateGoodieStock($goodie$params)
  367.     {
  368.         $this->goodieRepository->update(
  369.             [
  370.                 [
  371.                     'id' => $params['goodieid'],
  372.                     'stock' => $goodie->getStock() - 1
  373.                 ]
  374.             ],
  375.             Context::createDefaultContext()
  376.         );
  377.     }
  378.     /**
  379.      * @param $id
  380.      * @return mixed|null
  381.      */
  382.     private function getGoodie($id)
  383.     {
  384.         $c = new Criteria();
  385.         $c->setIds([$id]);
  386.         return $this->goodieRepository->search($cContext::createDefaultContext())->getEntities()->first();
  387.     }
  388.     /**
  389.      * @param $params
  390.      * @param $orderID
  391.      * @param $orderLineItemId
  392.      */
  393.     private function addItemToHistory($params$orderID$orderLineItemId)
  394.     {
  395.         $this->orderHistoryRepo->upsert(
  396.             [
  397.                 [
  398.                     'id' => Uuid::randomHex(),
  399.                     'campaignid' => $params['campaignid'],
  400.                     'orderid' => $orderID,
  401.                     'itemid' => $orderLineItemId
  402.                 ]
  403.             ],
  404.             Context::createDefaultContext()
  405.         );
  406.     }
  407.     /**
  408.      * @param $campaign
  409.      * @param $params
  410.      */
  411.     private function addAmountToGoal($campaign$params)
  412.     {
  413.         $this->campaignRepo->update(
  414.             [
  415.                 [
  416.                     'id' => $campaign->getId(),
  417.                     'fundinggoal' => ($campaign->getFundingGoal()) - (float)$params['amount']
  418.                 ]
  419.             ],
  420.             Context::createDefaultContext()
  421.         );
  422.     }
  423.     /**
  424.      * @param $orderID
  425.      * @return mixed
  426.      */
  427.     private function getOrderLineItems($orderID)
  428.     {
  429.         $c = new Criteria();
  430.         $c->setIds([$orderID]);
  431.         $c->addAssociation('lineItems');
  432.         return $this->orderRepo->search($cContext::createDefaultContext())->getEntities()->first()->getLineItems();
  433.     }
  434.     /**
  435.      * @param $id
  436.      * @return mixed|null
  437.      */
  438.     private function getCampaign($id)
  439.     {
  440.         $c = new Criteria();
  441.         $c->setIds([$id]);
  442.         return $this->campaignRepo->search($cContext::createDefaultContext())->getEntities()->first();
  443.     }
  444.     /**
  445.      * @param $campaigns
  446.      * @param $oID
  447.      */
  448.     private function processCampaigns($campaigns$oID)
  449.     {
  450.         foreach ($campaigns as $id => $dummy) {
  451.             $campaign $this->getCampaign($id);
  452.             $this->addOneSupporter($campaign);
  453.         }
  454.     }
  455.     /**
  456.      * @param $cID
  457.      * @param $oID
  458.      * @param $pID
  459.      */
  460.     private function addToOrderHistory($cID$oID$pID)
  461.     {
  462.         $c = new Criteria();
  463.         $c->addFilter(new EqualsFilter('campaignid'$cID));
  464.         $c->addFilter(new EqualsFilter('itemid'$pID));
  465.         $c->addFilter(new EqualsFilter('orderid'$oID));
  466.         $check $this->orderHistoryRepo->search($cContext::createDefaultContext())->getEntities()->first();
  467.         if(!$check)
  468.         {
  469.             $this->orderHistoryRepo->upsert(
  470.                 [
  471.                     [
  472.                         'id' => Uuid::randomHex(),
  473.                         'campaignid' => $cID,
  474.                         'itemid' => $pID,
  475.                         'orderid' => $oID
  476.                     ]
  477.                 ],
  478.                 Context::createDefaultContext()
  479.             );
  480.         }
  481.     }
  482.     /**
  483.      * @param $campaign
  484.      */
  485.     private function addOneSupporter($campaign)
  486.     {
  487.         $this->campaignRepo->update(
  488.             [
  489.                 [
  490.                     'id' => $campaign->getId(),
  491.                     'supporters' => $campaign->getSupporters() + 1
  492.                 ]
  493.             ],
  494.             Context::createDefaultContext()
  495.         );
  496.     }
  497.     //##################################################################################################################
  498.     /**
  499.      * @param $oID
  500.      * @return RedirectResponse
  501.      */
  502.     private function processCrowdfundingOrder($oID): RedirectResponse
  503.     {
  504.         $this->session->remove('fee');
  505.         $cID $this->session->get('campaignID');
  506.         $this->campaignRepo->update(
  507.             [
  508.                 [
  509.                     'id' => $cID,
  510.                     'confirmed' => false,
  511.                     'status' => 9,
  512.                     'initialorderid' => $oID
  513.                 ],
  514.             ],
  515.             Context::createDefaultContext()
  516.         );
  517.         $this->orderHistoryRepo->upsert(
  518.             [
  519.                 [
  520.                     'id' => Uuid::randomHex(),
  521.                     'campaignid' => $cID,
  522.                     'orderid' => $oID
  523.                 ]
  524.             ],
  525.             Context::createDefaultContext()
  526.         );
  527.         $slug $this->session->get('slug');
  528.         if(!$this->session->has('own-sponsor'))
  529.         {
  530.             $c = new Criteria();
  531.             $c->setIds([$cID]);
  532.             $campaign $this->campaignRepo->search($cContext::createDefaultContext())->getEntities()->first();
  533.             $this->mailService->sendAdmin(
  534.                 $this->mailService->getMail(
  535.                     $this->configService->get('EconsorCrowdfunding.config.campaignAwaitConfirmationAdmin')
  536.                 ),
  537.                 false,
  538.                 null,
  539.                 $campaign->getSaleschannelid()
  540.             );
  541.             $customer $campaign->getCustomer();
  542.             $this->mailService->send(
  543.                 $this->mailService->getMail(
  544.                     $this->configService->get('EconsorCrowdfunding.config.campaignAwaitConfirmationTrainer')
  545.                 ),
  546.                 true,
  547.                 [
  548.                     'slug' => $slug
  549.                 ],
  550.                 $customer,
  551.                 $campaign->getSaleschannelid()
  552.             );
  553.         }
  554.         return new RedirectResponse(
  555.             $this->router->generate('frontend.crowdfunding.campaign', ['slug' => $slug])
  556.         );
  557.     }
  558.     //##################################################################################################################
  559.     //##################################################################################################################
  560.     //##################################################################################################################
  561.     //##################################################################################################################
  562.     //##################################################################################################################
  563.     //##################################################################################################################
  564.     //##################################################################################################################
  565.     //##################################################################################################################
  566.     //##################################################################################################################
  567.     /**
  568.      * @param $oID
  569.      * @param $name
  570.      */
  571.     private function processOrder($oID$name)
  572.     {
  573.         $params $this->session->get($name);
  574.         $c = new Criteria();
  575.         $c->setIds([$params['campaignid']]);
  576.         $campaign $this->campaignRepo->search($cContext::createDefaultContext())->getEntities()->first();
  577.         $this->campaignRepo->update(
  578.             [
  579.                 [
  580.                     'id' => $campaign->getId(),
  581.                     'supporters' => $campaign->getSupporters() + 1,
  582.                     'fundinggoal' => ($campaign->getFundingGoal()) - (float)$params['amount']
  583.                 ]
  584.             ],
  585.             Context::createDefaultContext()
  586.         );
  587.         //update order history repo
  588.         $this->orderHistoryRepo->upsert(
  589.             [
  590.                 [
  591.                     'id' => Uuid::randomHex(),
  592.                     'campaignid' => $params['campaignid'],
  593.                     'orderid' => $oID
  594.                 ]
  595.             ],
  596.             Context::createDefaultContext()
  597.         );
  598.         $this->session->remove($name);
  599.     }
  600.     private function processGoodie()
  601.     {
  602.         $params $this->session->get('goodie');
  603.         $c = new Criteria();
  604.         $c->setIds([$params['goodieid']]);
  605.         $goodie $this->goodieRepository->search($cContext::createDefaultContext())->getEntities()->first();
  606.         $this->goodieRepository->update(
  607.             [
  608.                 [
  609.                     'id' => $params['goodieid'],
  610.                     'stock' => $goodie->getStock() - 1
  611.                 ]
  612.             ],
  613.             Context::createDefaultContext()
  614.         );
  615.     }
  616. }