app/Customize/Controller/SubscriptionController.php line 196

Open in your IDE?
  1. <?php namespace Customize\Controller;
  2. use Eccube\Entity\BaseInfo;
  3. use Eccube\Entity\Master\ProductStatus;
  4. use Eccube\Entity\Product;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Event\EventArgs;
  7. use Eccube\Form\Type\AddCartType;
  8. use Eccube\Form\Type\SearchProductType;
  9. use Eccube\Repository\BaseInfoRepository;
  10. use Eccube\Repository\CustomerFavoriteProductRepository;
  11. use Eccube\Repository\Master\ProductListMaxRepository;
  12. use Eccube\Repository\ProductRepository;
  13. use Eccube\Service\CartService;
  14. use Eccube\Service\PurchaseFlow\PurchaseContext;
  15. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  16. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  25. use Eccube\Controller\AbstractController;
  26. use Customize\Repository\CustomizeStripeRecOrderRepository;
  27. class SubscriptionController extends AbstractController {
  28.     /**
  29.      * @var PurchaseFlow
  30.      */
  31.     protected $purchaseFlow;
  32.     /**
  33.      * @var CustomerFavoriteProductRepository
  34.      */
  35.     protected $customerFavoriteProductRepository;
  36.     /**
  37.      * @var CartService
  38.      */
  39.     protected $cartService;
  40.     /**
  41.      * @var ProductRepository
  42.      */
  43.     protected $productRepository;
  44.     /**
  45.      * @var BaseInfo
  46.      */
  47.     protected $BaseInfo;
  48.     /**
  49.      * @var AuthenticationUtils
  50.      */
  51.     protected $helper;
  52.     /**
  53.      * @var ProductListMaxRepository
  54.      */
  55.     protected $productListMaxRepository;
  56.     private $title '';
  57.     protected CustomizeStripeRecOrderRepository $customizeStripeRecOrderRepository;
  58.     /**
  59.      * ProductController constructor.
  60.      *
  61.      * @param PurchaseFlow $cartPurchaseFlow
  62.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  63.      * @param CartService $cartService
  64.      * @param ProductRepository $productRepository
  65.      * @param BaseInfoRepository $baseInfoRepository
  66.      * @param AuthenticationUtils $helper
  67.      * @param ProductListMaxRepository $productListMaxRepository
  68.      */
  69.     public function __construct(
  70.         PurchaseFlow $cartPurchaseFlow,
  71.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  72.         CartService $cartService,
  73.         ProductRepository $productRepository,
  74.         BaseInfoRepository $baseInfoRepository,
  75.         AuthenticationUtils $helper,
  76.         ProductListMaxRepository $productListMaxRepository,
  77.         CustomizeStripeRecOrderRepository $customizeStripeRecOrderRepository
  78.     ) {
  79.         $this->purchaseFlow $cartPurchaseFlow;
  80.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  81.         $this->cartService $cartService;
  82.         $this->productRepository $productRepository;
  83.         $this->BaseInfo $baseInfoRepository->get();
  84.         $this->helper $helper;
  85.         $this->productListMaxRepository $productListMaxRepository;
  86.         $this->customizeStripeRecOrderRepository $customizeStripeRecOrderRepository;
  87.     }
  88.     /**
  89.      * @Route("/subscription", name="subscription", methods={"GET"})
  90.      * @Template("@user_data/subscription.twig")
  91.      */
  92.     public function index() {
  93.         return [];
  94.     }
  95.     /**
  96.      * 商品詳細画面.
  97.      *
  98.      * @Route("/subscription/detail/{id}", name="subscription_detail", methods={"GET"}, requirements={"id" = "\d+"})
  99.      * @Template("@user_data/subscription_detail.twig")
  100.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  101.      *
  102.      * @param Request $request
  103.      * @param Product $Product
  104.      *
  105.      * @return array
  106.      */
  107.     public function detail(Request $requestProduct $Product)
  108.     {
  109.         if (!$this->checkVisibility($Product)) {
  110.             throw new NotFoundHttpException();
  111.         }
  112.         $builder $this->formFactory->createNamedBuilder(
  113.             '',
  114.             AddCartType::class,
  115.             null,
  116.             [
  117.                 'product' => $Product,
  118.                 'id_add_product_id' => false,
  119.             ]
  120.         );
  121.         $event = new EventArgs(
  122.             [
  123.                 'builder' => $builder,
  124.                 'Product' => $Product,
  125.             ],
  126.             $request
  127.         );
  128.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE);
  129.         $Customer null;
  130.         $isSubscription false;
  131.         if ($this->isGranted('ROLE_USER')) {
  132.             $Customer $this->getUser();
  133.             // Stripe 定期購入申し込み履歴取得
  134.             $qb_rec $this->customizeStripeRecOrderRepository->getRecOrdersQueryBuilderByCustomer($Customer);
  135.             $qb_rec
  136.                 ->leftJoin('ro.OrderItems''roi')
  137.                 ->addSelect('roi');
  138.             // qb実行(サブスクステータス)
  139.             $stripe_rec_orders $qb_rec->getQuery()->getResult();
  140.             if (!empty($stripe_rec_orders)) {
  141.                 // ② 最初の要素を取得
  142.                 $firstRecOrder reset($stripe_rec_orders);
  143.                 // ③ RecStatus 判定
  144.                 if (!in_array($firstRecOrder->getRecStatus(), ['canceled''scheduled_canceled'], true)) {
  145.                     $isSubscription true;
  146.                 }
  147.             }
  148.         }
  149.         $qb $this->productRepository->createQueryBuilder('p')
  150.             ->leftJoin('p.ProductClasses''pc')
  151.             ->addSelect('pc')
  152.             ->orderBy('p.id''DESC');
  153.         $allProducts $qb->getQuery()->getResult();
  154.         return [
  155.             'title' => $this->title,
  156.             'subtitle' => $Product->getName(),
  157.             'form' => $builder->getForm()->createView(),
  158.             'Product' => $Product,
  159.             'Customer' => $Customer,
  160.             "isSubscription" => $isSubscription
  161.         ];
  162.     }
  163.     /**
  164.      * 閲覧可能な商品かどうかを判定
  165.      *
  166.      * @param Product $Product
  167.      *
  168.      * @return boolean 閲覧可能な場合はtrue
  169.      */
  170.     protected function checkVisibility(Product $Product)
  171.     {
  172.         $is_admin $this->session->has('_security_admin');
  173.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  174.         if (!$is_admin) {
  175.             // 在庫なし商品の非表示オプションが有効な場合.
  176.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  177.             //     if (!$Product->getStockFind()) {
  178.             //         return false;
  179.             //     }
  180.             // }
  181.             // 公開ステータスでない商品は表示しない.
  182.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  183.                 return false;
  184.             }
  185.         }
  186.         return true;
  187.     }
  188. }