app/Plugin/Coupon4/Event.php line 338

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\Coupon4;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\Order;
  15. use Eccube\Event\TemplateEvent;
  16. use Eccube\Entity\OrderItem;
  17. use Customize\Repository\CustomerCouponRepository;
  18. use Eccube\Repository\OrderRepository;
  19. use Plugin\Coupon4\Service\CouponService;
  20. use Plugin\Coupon4\Entity\Coupon;
  21. use Plugin\Coupon4\Entity\CouponOrder;
  22. use Plugin\Coupon4\Repository\CouponOrderRepository;
  23. use Plugin\Coupon4\Repository\CouponRepository;
  24. use Eccube\Entity\Master\OrderItemType;
  25. use Eccube\Entity\Master\TaxDisplayType;
  26. use Eccube\Entity\Master\TaxType;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\DependencyInjection\ContainerInterface;
  30. use Symfony\Component\HttpFoundation\RedirectResponse;
  31. use Symfony\Component\HttpFoundation\RequestStack;
  32. /**
  33.  * Class Event.
  34.  */
  35. class Event implements EventSubscriberInterface
  36. {
  37.     /**
  38.      * @var CouponOrderRepository
  39.      */
  40.     private $couponOrderRepository;
  41.     /**
  42.      * @var EntityManagerInterface
  43.      */
  44.     private $entityManager;
  45.     /**
  46.      * @var CouponRepository
  47.      */
  48.     private $couponRepository;
  49.     /**
  50.      * @var OrderRepository
  51.      */
  52.     private $orderRepository;
  53.     /**
  54.      * @var CustomerCouponRepository
  55.      */
  56.     private $customerCouponRepository;
  57.     /**
  58.      * @var \Twig_Environment
  59.      */
  60.     private $twig;
  61.     /**
  62.      * @var CouponService
  63.      */
  64.     private $couponService;
  65.     protected $container;
  66.     protected $router;
  67.     private $requestStack;
  68.     /**
  69.      * Event constructor.
  70.      *
  71.      * @param CouponOrderRepository $couponOrderRepository
  72.      * @param EntityManagerInterface $entityManager
  73.      * @param CouponService $couponService
  74.      * @param CouponRepository $couponRepository
  75.      * @param OrderRepository $orderRepository
  76.      * @param CustomerCouponRepository $customerCouponRepository
  77.      * @param \Twig_Environment $twig
  78.      */
  79.     public function __construct(CouponOrderRepository $couponOrderRepository
  80.                                 EntityManagerInterface $entityManager
  81.                                 CouponService $couponService,
  82.                                 CouponRepository $couponRepository
  83.                                 OrderRepository $orderRepository
  84.                                 CustomerCouponRepository $customerCouponRepository
  85.                                 \Twig_Environment $twig,
  86.                                 ContainerInterface $container,
  87.                                 RequestStack $requestStack)
  88.     {
  89.         $this->couponOrderRepository $couponOrderRepository;
  90.         $this->entityManager $entityManager;
  91.         $this->couponService $couponService;
  92.         $this->couponRepository $couponRepository;
  93.         $this->orderRepository $orderRepository;
  94.         $this->customerCouponRepository $customerCouponRepository;
  95.         $this->twig $twig;
  96.         $this->container $container;
  97.         $this->router $this->container->get('router');
  98.         $this->requestStack $requestStack;
  99.     }
  100.     /**
  101.      * Todo: admin.order.delete.complete has been deleted.
  102.      *
  103.      * @return array
  104.      */
  105.     public static function getSubscribedEvents()
  106.     {
  107.         return [
  108.             'Shopping/index.twig' => 'index',
  109.             'Shopping/confirm.twig' => 'confirm',
  110.             'Mypage/history.twig' => 'onRenderMypageHistory',
  111.             '@admin/Order/edit.twig' => 'onRenderAdminOrderEdit',
  112.         ];
  113.     }
  114.     /**
  115.      * @param TemplateEvent $event
  116.      */
  117.     public function index(TemplateEvent $event)
  118.     {
  119.         $parameters $event->getParameters();
  120.         // 登録がない、レンダリングをしない
  121.         /** @var Order $Order */
  122.         $Order $parameters['Order'];
  123.         /** @var Customer $customer */
  124.         $Customer $parameters['Customer'];
  125.         $CustomerCouponList null;
  126.         $form null;
  127.         $parameters['RefreshPage'] = "N";
  128.         if(is_object($Customer)){
  129.             if(!is_object($Customer->getChainStore())){
  130.                 //個人クーポン情報取得
  131.                 $CustomerCouponList $this->customerCouponRepository->findActiveCouponList($Customer);
  132.                 $couponCd null;
  133.                 $maxdiscount 0;
  134.                 if(is_array($CustomerCouponList) && sizeof($CustomerCouponList) > 0){
  135.                     foreach($CustomerCouponList as $CustomerCoupon){
  136.                         $Coupon $CustomerCoupon->getCoupon();
  137.                         $discount 0;
  138.                         //計算
  139.                         $couponProducts $this->couponService->existsCouponProduct($Coupon$Order);
  140.                         if(sizeof($couponProducts) != 0){
  141.                             // 値引き額を取得
  142.                             $discount $this->couponService->recalcOrder($Coupon$couponProducts);
  143.                             $lowerLimit $Coupon->getCouponLowerLimit();
  144.                             $checkLowerLimit $this->couponService->isLowerLimitCoupon($couponProducts$lowerLimit);
  145.                             if (!$checkLowerLimit) {
  146.                                 $discount 0;
  147.                                 $message trans('plugin_coupon.front.shopping.lowerlimit', ['lowerLimit' => number_format($lowerLimit)]);
  148.                                 $Coupon->setCouponRealType("hidden");
  149.                                 $Coupon->setCouponRealMessage($message);
  150.                             }
  151.                         }
  152.                 
  153.                         $checkCouponUseTime $this->couponRepository->checkCouponUseTime($Coupon->getCouponCd());
  154.                         if (!$checkCouponUseTime) {
  155.                             $discount 0;
  156.                             $Coupon->setCouponRealType("hidden");
  157.                         }
  158.                         if ($Coupon->getReuse() != 'Y') {
  159.                             $couponUsedOrNot $this->couponService->checkCouponUsedOrNot($Coupon->getCouponCd(), $Customer);
  160.                             if ($Coupon && $couponUsedOrNot) {
  161.                                 // 既に存在している
  162.                                 $discount 0;
  163.                                 $Coupon->setCouponRealType("hidden");
  164.                             }
  165.                         }
  166.                         $Coupon->setOrderDiscountPrice($discount);
  167.                         if($maxdiscount $discount && $discount != 0){
  168.                             $maxdiscount $discount;
  169.                             $couponCd $Coupon->getCouponCd();
  170.                         }
  171.                     }
  172.                     if($Order->getUseCoupon() == null){
  173.                         if($couponCd != null){
  174.                             $this->applyCoupon($Order$Customer$couponCd);
  175.                             $CouponOrder $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
  176.                             $this->addCouponDiscountItem($Order$CouponOrder);
  177.                             //$Order = $this->orderRepository->findOneBy(["id" => $Order->getId()]);
  178.                             //$parameters['Order'] = $Order;
  179.                             //$url = $this->router->generate('shopping');
  180.                             //$response = new RedirectResponse($url);
  181.                             //$event->setResponse($response);
  182.                             //$parameters['RefreshPage'] = "Y";
  183.                         }
  184.                     }
  185.                 }else{
  186.                     //Cookie クーポン情報取得 ( New Member )
  187.                     $form $this->applyCookieCoupon($Order$Customer$event);
  188.                 }
  189.             }else{
  190.                 $this->couponService->removeCouponOrder($Order);
  191.             }
  192.         }else{
  193.             //Cookie クーポン情報取得 ( Guest )
  194.             $form $this->applyCookieCoupon($Order$Customer$event);
  195.         }
  196.         $parameters['CustomerCouponList'] = $CustomerCouponList;
  197.         $event->setParameters($parameters);
  198.         return $this->confirm($event);
  199.     }
  200.     private function applyCookieCoupon($Order$Customer$event){
  201.         $request $this->requestStack->getCurrentRequest();
  202.         $couponIdCookie $request->cookies->get('couponIdCookie');
  203.         
  204.         if( $Order->getManualCoupon() == "Y" ){
  205.             return;
  206.         }
  207.         if($Order->getUseCoupon() != "N" && $couponIdCookie != null){
  208.             $this->couponService->removeCouponOrder($Order);
  209.             $Coupon $this->couponRepository->findActiveCoupon($couponIdCookie);
  210.             if($Coupon != null){
  211.                 //計算
  212.                 $couponProducts $this->couponService->existsCouponProduct($Coupon$Order);
  213.                 
  214.                 if(sizeof($couponProducts) != 0){
  215.                     // 値引き額を取得
  216.                     $discount $this->couponService->recalcOrder($Coupon$couponProducts);
  217.                     if($discount 0){
  218.                        $this->applyCoupon($Order$Customer$couponIdCookie);
  219.                        $CouponOrder $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
  220.                        $this->addCouponDiscountItem($Order$CouponOrder);
  221.                     }
  222.                 }
  223.             }
  224.         }else{
  225.             if($couponIdCookie != null){
  226.                 $CouponOrder $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
  227.                 if(is_object($CouponOrder)){
  228.                     $Coupon $this->couponRepository->findActiveCoupon($CouponOrder->getCouponCd());
  229.                     if(!is_object($Coupon)){
  230.                         $this->couponService->removeCouponOrder($Order);
  231.                     }
  232.                 }
  233.             }
  234.         }
  235.                         
  236.     }
  237.     /**
  238.      * @param TemplateEvent $event
  239.      */
  240.     public function confirm(TemplateEvent $event)
  241.     {
  242.         $parameters $event->getParameters();
  243.         // 登録がない、レンダリングをしない
  244.         /** @var Order $Order */
  245.         $Order $parameters['Order'];
  246.         /** @var Customer $customer */
  247.         $Customer $parameters['Customer'];
  248.         // クーポンが未入力でクーポン情報が存在すればクーポン情報を削除
  249.         $CouponOrder $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
  250.         $parameters['CouponOrder'] = $CouponOrder;
  251.         $event->setParameters($parameters);
  252.         if (strpos($event->getView(), 'index.twig') !== false) {
  253.             $event->addSnippet('@Coupon4/default/coupon_shopping_item.twig');
  254.         } else {
  255.             $event->addSnippet('@Coupon4/default/coupon_shopping_item_confirm.twig');
  256.         }
  257.     }
  258.     /**
  259.      * Hook point add coupon information to mypage history.
  260.      *
  261.      * @param TemplateEvent $event
  262.      */
  263.     public function onRenderMypageHistory(TemplateEvent $event)
  264.     {
  265.         log_info('Coupon trigger onRenderMypageHistory start');
  266.         $parameters $event->getParameters();
  267.         if (is_null($parameters['Order'])) {
  268.             return;
  269.         }
  270.         $Order $parameters['Order'];
  271.         // クーポン受注情報を取得する
  272.         $CouponOrder $this->couponOrderRepository->findOneBy([
  273.             'order_id' => $Order->getId(),
  274.         ]);
  275.         if (is_null($CouponOrder)) {
  276.             return;
  277.         }
  278.         // set parameter for twig files
  279.         $parameters['coupon_cd'] = $CouponOrder->getCouponCd();
  280.         $parameters['coupon_name'] = $CouponOrder->getCouponName();
  281.         $event->setParameters($parameters);
  282.         $event->addSnippet('@Coupon4/default/mypage_history_coupon.twig');
  283.         log_info('Coupon trigger onRenderMypageHistory finish');
  284.     }
  285.     /**
  286.      * [order/{id}/edit]表示の時のEvent Fork.
  287.      * クーポン関連項目を追加する.
  288.      *
  289.      * @param TemplateEvent $event
  290.      */
  291.     public function onRenderAdminOrderEdit(TemplateEvent $event)
  292.     {
  293.         log_info('Coupon trigger onRenderAdminOrderEdit start');
  294.         $parameters $event->getParameters();
  295.         if (is_null($parameters['Order'])) {
  296.             return;
  297.         }
  298.         $Order $parameters['Order'];
  299.         // クーポン受注情報を取得する
  300.         $CouponOrder $this->couponOrderRepository->findOneBy(['order_id' => $Order->getId()]);
  301.         if (is_null($CouponOrder)) {
  302.             return;
  303.         }
  304.         // set parameter for twig files
  305.         $parameters['coupon_cd'] = $CouponOrder->getCouponCd();
  306.         $parameters['coupon_name'] = $CouponOrder->getCouponName();
  307.         $parameters['coupon_change_status'] = $CouponOrder->getOrderChangeStatus();
  308.         $event->setParameters($parameters);
  309.         // add twig
  310.         $event->addSnippet('@Coupon4/admin/order_edit_coupon.twig');
  311.         log_info('Coupon trigger onRenderAdminOrderEdit finish');
  312.     }
  313.     private function applyCoupon(Order $Order$Customer$couponCd)
  314.     {
  315.         // サービスの取得
  316.         /** @var CouponService $service */
  317.         $service $this->couponService;
  318.         
  319.         // クーポンコードを取得する
  320.         $CouponOrder $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
  321.         if ($CouponOrder) {
  322.             return;
  323.         }
  324.         // ---------------------------------
  325.         // クーポンコード入力項目追加
  326.         // ----------------------------------
  327.         // クーポン情報を取得
  328.         $Coupon $this->couponRepository->findActiveCoupon($couponCd);
  329.         if ($Coupon) {
  330.             $Order->setUseCoupon("Y");
  331.             $couponProducts $service->existsCouponProduct($Coupon$Order);
  332.             // 値引き額を取得
  333.             $discount $service->recalcOrder($Coupon$couponProducts);
  334.             // クーポン情報を登録
  335.             $service->saveCouponOrder($Order$Coupon$couponCd$Customer$discount);
  336.             $this->entityManager->flush();
  337.         }
  338.     }
  339.     /**
  340.      * 明細追加処理. --> CouponProcessor.php
  341.      *
  342.      * 値引額の場合の計算方法
  343.      * クーポンで設定した価格で、クーポン値引の明細を生成する(税込価格、税率0%、課税)
  344.      * 税込1080円の商品に、1000円のクーポンを使用すると、980円の支払いになるイメージ
  345.      *
  346.      * 明細ごとに税込の値引額を集計し、クーポン値引の明細を生成する(税込価格、税率0%、課税)
  347.      * 軽減税率適用により税率が混在する場合もあるため、税込価格、税率0%で明細を生成する
  348.      * 税込1080円の商品に、10%OFFのクーポンを使用すると、100円の値引きになり、980円の支払いになるイメージ
  349.      *
  350.      * @see https://github.com/EC-CUBE/coupon-plugin/pull/77
  351.      *
  352.      * @param CouponOrder $CouponOrder
  353.      */
  354.     private function addCouponDiscountItem($OrderCouponOrder $CouponOrder=null)
  355.     {
  356.         if($CouponOrder == null){
  357.             return;
  358.         }
  359.         
  360.         $Coupon $this->couponRepository->find($CouponOrder->getCouponId());
  361.         $taxDisplayType TaxDisplayType::INCLUDED// 税込
  362.         $taxType TaxType::NON_TAXABLE// 不課税
  363.         $tax 0;
  364.         $taxRate 0;
  365.         $taxRuleId null;
  366.         $roundingType null;
  367.         $DiscountType $this->entityManager->find(OrderItemType::class, OrderItemType::DISCOUNT);
  368.         $TaxInclude $this->entityManager->find(TaxDisplayType::class, $taxDisplayType);
  369.         $Taxation $this->entityManager->find(TaxType::class, $taxType);
  370.         $OrderItem = new OrderItem();
  371.         $OrderItem->setProductName($CouponOrder->getCouponName())
  372.             ->setPrice($CouponOrder->getDiscount() * -1)
  373.             ->setQuantity(1)
  374.             ->setTax($tax)
  375.             ->setTaxRate($taxRate)
  376.             ->setTaxRuleId($taxRuleId)
  377.             ->setRoundingType($roundingType)
  378.             ->setOrderItemType($DiscountType)
  379.             ->setTaxDisplayType($TaxInclude)
  380.             ->setTaxType($Taxation)
  381.             ->setOrder($Order)
  382.             ->setProcessorName(CouponProcessor::class);
  383.         $Order->addItem($OrderItem);
  384.         $total $Order->getSubtotal() + $Order->getDeliveryFeeTotal() + $Order->getCharge();
  385.         $paymentTotal $total $CouponOrder->getDiscount();
  386.         $Order->setPaymentTotal($paymentTotal);
  387.     }
  388. }