app/Plugin/EccubePaymentLite42/Controller/Admin/Regular/EditController.php line 252

Open in your IDE?
  1. <?php
  2. namespace Plugin\EccubePaymentLite42\Controller\Admin\Regular;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Eccube\Controller\AbstractController;
  5. use Eccube\Entity\Master\SaleType;
  6. use Eccube\Entity\Order;
  7. use Eccube\Entity\Product;
  8. use Eccube\Form\Type\AddCartType;
  9. use Eccube\Form\Type\Admin\SearchProductType;
  10. use Eccube\Repository\CategoryRepository;
  11. use Eccube\Repository\DeliveryRepository;
  12. use Eccube\Repository\Master\SaleTypeRepository;
  13. use Eccube\Repository\OrderRepository;
  14. use Eccube\Repository\ProductRepository;
  15. use Eccube\Service\PurchaseFlow\PurchaseContext;
  16. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  17. use Knp\Component\Pager\Pagination\SlidingPagination;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Plugin\EccubePaymentLite42\Entity\Config;
  20. use Plugin\EccubePaymentLite42\Entity\RegularOrder;
  21. use Plugin\EccubePaymentLite42\Entity\RegularStatus;
  22. use Plugin\EccubePaymentLite42\Form\Type\Admin\RegularOrderType;
  23. use Plugin\EccubePaymentLite42\Repository\ConfigRepository;
  24. use Plugin\EccubePaymentLite42\Repository\SearchProductRepository;
  25. use Plugin\EccubePaymentLite42\Repository\RegularOrderRepository;
  26. use Plugin\EccubePaymentLite42\Service\CalculateNextDeliveryDateService;
  27. use Plugin\EccubePaymentLite42\Service\IsActiveRegularService;
  28. use Plugin\EccubePaymentLite42\Service\RegularCreditService;
  29. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Symfony\Component\Serializer\SerializerInterface;
  34. class EditController extends AbstractController
  35. {
  36.     /**
  37.      * @var RegularOrderRepository
  38.      */
  39.     private $regularOrderRepository;
  40.     /**
  41.      * @var RegularCreditService
  42.      */
  43.     private $regularCreditService;
  44.     /**
  45.      * @var ProductRepository
  46.      */
  47.     private $productRepository;
  48.     /**
  49.      * @var CategoryRepository
  50.      */
  51.     private $categoryRepository;
  52.     /**
  53.      * @var SerializerInterface
  54.      */
  55.     private $serializer;
  56.     /**
  57.      * @var DeliveryRepository
  58.      */
  59.     private $deliveryRepository;
  60.     /**
  61.      * @var PurchaseFlow
  62.      */
  63.     private $purchaseFlow;
  64.     /**
  65.      * @var OrderRepository
  66.      */
  67.     private $orderRepository;
  68.     /**
  69.      * @var CalculateNextDeliveryDateService
  70.      */
  71.     private $calculateNextDeliveryDateService;
  72.     /**
  73.      * @var ConfigRepository
  74.      */
  75.     private $configRepository;
  76.     /**
  77.      * @var IsActiveRegularService
  78.      */
  79.     private $isActiveRegularService;
  80.     /**
  81.      * @var SearchProductRepository
  82.      */
  83.     private $searchProductRepository;
  84.     /**
  85.      * @var SaleTypeRepository
  86.      */
  87.     private $saleTypeRepository;
  88.     public function __construct(
  89.         RegularOrderRepository $regularOrderRepository,
  90.         RegularCreditService $regularCreditService,
  91.         ProductRepository $productRepository,
  92.         CategoryRepository $categoryRepository,
  93.         SerializerInterface $serializer,
  94.         DeliveryRepository $deliveryRepository,
  95.         PurchaseFlow $orderPurchaseFlow,
  96.         OrderRepository $orderRepository,
  97.         CalculateNextDeliveryDateService $calculateNextDeliveryDateService,
  98.         ConfigRepository $configRepository,
  99.         IsActiveRegularService $isActiveRegularService,
  100.         SearchProductRepository $searchProductRepository,
  101.         SaleTypeRepository $saleTypeRepository
  102.     ) {
  103.         $this->regularOrderRepository $regularOrderRepository;
  104.         $this->regularCreditService $regularCreditService;
  105.         $this->productRepository $productRepository;
  106.         $this->categoryRepository $categoryRepository;
  107.         $this->serializer $serializer;
  108.         $this->deliveryRepository $deliveryRepository;
  109.         $this->purchaseFlow $orderPurchaseFlow;
  110.         $this->orderRepository $orderRepository;
  111.         $this->calculateNextDeliveryDateService $calculateNextDeliveryDateService;
  112.         $this->configRepository $configRepository;
  113.         $this->isActiveRegularService $isActiveRegularService;
  114.         $this->searchProductRepository $searchProductRepository;
  115.         $this->saleTypeRepository $saleTypeRepository;
  116.     }
  117.     /**
  118.      * @Route(
  119.      *     "/%eccube_admin_route%/eccube_payment_lite/regular/order/{id}/edit",
  120.      *     name="eccube_payment_lite42_admin_regular_order_edit",
  121.      *     requirements={"id" = "\d+"}
  122.      * )
  123.      * @Template("@EccubePaymentLite42/admin/Regular/Order/edit.twig")
  124.      */
  125.     public function edit(Request $request$id null)
  126.     {
  127.         if (!$this->isActiveRegularService->isActive()) {
  128.             throw new NotFoundHttpException();
  129.         }
  130.         /** @var RegularOrder $TargetRegularOrder */
  131.         $TargetRegularOrder $this->regularOrderRepository->find($id);
  132.         if ($TargetRegularOrder === null) {
  133.             throw new NotFoundHttpException();
  134.         }
  135.         // 編集前の受注情報を保持
  136.         $OriginRegularOrder = clone $TargetRegularOrder;
  137.         $OriginItems = new ArrayCollection();
  138.         foreach ($TargetRegularOrder->getRegularOrderItems() as $Item) {
  139.             $OriginItems->add($Item);
  140.         }
  141.         $form $this->createForm(RegularOrderType::class, $TargetRegularOrder);
  142.         //dd($TargetRegularOrder);
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted() && $form->isValid()) {
  145.             $OriginOrder $this->regularCreditService->createOrderData($OriginRegularOrder);
  146.             $TargetOrder $this->regularCreditService->createOrderData($TargetRegularOrder);
  147.             // apply purchase
  148.             $purchaseContext = new PurchaseContext($OriginOrder$OriginOrder->getCustomer());
  149.             $flowResult $this->purchaseFlow->validate($TargetOrder$purchaseContext);
  150.             $this->calculate($TargetRegularOrder$TargetOrder);
  151.             // 登録ボタン押下
  152.             switch ($request->get('mode')) {
  153.                 case 'register':
  154.                     if (!$flowResult->hasError() && $form->isValid()) {
  155.                         $this->entityManager->persist($TargetRegularOrder);
  156.                         $this->entityManager->flush();
  157.                         foreach ($OriginItems as $Item) {
  158.                             if (!$TargetRegularOrder->getRegularOrderItems()->contains($Item)) {
  159.                                 $this->entityManager->remove($Item);
  160.                             }
  161.                         }
  162.                         // 休止、解約にする場合は次回お届け予定日を空にする
  163.                         if ($OriginRegularOrder->getRegularStatus()->getId() === RegularStatus::CONTINUE &&
  164.                              ($TargetRegularOrder->getRegularStatus()->getId() === RegularStatus::SUSPEND ||
  165.                              $TargetRegularOrder->getRegularStatus()->getId() === RegularStatus::CANCELLATION)
  166.                         ) {
  167.                             $TargetRegularOrder->getRegularShippings()->first()->setNextDeliveryDate(null);
  168.                             $this->entityManager->persist($TargetRegularOrder);
  169.                         }
  170.                         // 再開時は次回お届け予定日を再計算して保存する
  171.                         /** @var Config $Config */
  172.                         $Config $this->configRepository->find(1);
  173.                         if ($OriginRegularOrder->getRegularStatus()->getId() === RegularStatus::CANCELLATION &&
  174.                            $TargetRegularOrder->getRegularStatus()->getId() === RegularStatus::CONTINUE
  175.                         ) {
  176.                             $TargetRegularOrder->getRegularShippings()->first()->setNextDeliveryDate(
  177.                                 $this->calculateNextDeliveryDateService->calc($TargetRegularOrder->getRegularCycle(), $Config->getFirstDeliveryDays())
  178.                             );
  179.                         }
  180.                         // 休止にする場合は、定期休止日を設定する
  181.                         if ($TargetRegularOrder->getRegularStatus()->getId() === RegularStatus::SUSPEND) {
  182.                             $TargetRegularOrder->setRegularStopDate(new \DateTime());
  183.                         }
  184.                         $this->entityManager->flush();
  185.                         $this->addSuccess('admin.common.save_complete''admin');
  186.                         return $this->redirectToRoute('eccube_payment_lite42_admin_regular_order_edit', ['id' => $TargetRegularOrder->getId()]);
  187.                     }
  188.                     break;
  189.                 default:
  190.                     break;
  191.             }
  192.         }
  193.         // 商品検索フォーム
  194.         $searchProductModalForm $this->createForm(SearchProductType::class);
  195.         // 配送業者のお届け時間
  196.         $times = [];
  197.         $deliveries $this->deliveryRepository->findAll();
  198.         foreach ($deliveries as $Delivery) {
  199.             $deliveryTimes $Delivery->getDeliveryTimes();
  200.             foreach ($deliveryTimes as $DeliveryTime) {
  201.                 $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
  202.             }
  203.         }
  204.         $OrderHistories $this->orderRepository->findby([
  205.             'RegularOrder' => $TargetRegularOrder,
  206.         ]);
  207.         return [
  208.             'form' => $form->createView(),
  209.             'searchProductModalForm' => $searchProductModalForm->createView(),
  210.             'RegularOrder' => $TargetRegularOrder,
  211.             'id' => $id,
  212.             'shippingDeliveryTimes' => $this->serializer->serialize($times'json'),
  213.             'OrderHistories' => $OrderHistories,
  214.         ];
  215.     }
  216.     /**
  217.      * @Route(
  218.      *     "/%eccube_admin_route%/eccube_payment_lite/regular/search_product",
  219.      *     name="eccube_payment_lite42_admin_regular_order_search_product"
  220.      * )
  221.      * @Route(
  222.      *     "/%eccube_admin_route%/epsilon_regular_order/regular/search_product/page/{page_no}",
  223.      *     requirements={"page_no" = "\d+"},
  224.      *     name="eccube_payment_lite42_admin_regular_order_search_product_page"
  225.      * )
  226.      * @Template("@EccubePaymentLite42/admin/Order/regular_search_product.twig")
  227.      */
  228.     public function searchProduct(Request $request$page_no nullPaginatorInterface $paginator)
  229.     {
  230.         if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
  231.             log_debug('search regular product start.');
  232.             $page_count $this->eccubeConfig['eccube_default_page_count'];
  233.             if ('POST' === $request->getMethod()) {
  234.                 $page_no 1;
  235.                 $searchData = [
  236.                     'id' => $request->get('id'),
  237.                 ];
  238.                 if ($categoryId $request->get('category_id')) {
  239.                     $Category $this->categoryRepository->find($categoryId);
  240.                     $searchData['category_id'] = $Category;
  241.                 }
  242.                 $this->session->set('eccube.admin.order.regular_product.search'$searchData);
  243.                 $this->session->set('eccube.admin.order.regular_product.search.page_no'$page_no);
  244.             } else {
  245.                 $searchData = (array) $this->session->get('eccube.admin.order.regular_product.search');
  246.                 if (is_null($page_no)) {
  247.                     $page_no intval($this->session->get('eccube.admin.order.regular_product.search.page_no'));
  248.                 } else {
  249.                     $this->session->set('eccube.admin.order.regular_product.search.page_no'$page_no);
  250.                 }
  251.             }
  252.             /** @var SaleType $SaleType */
  253.             $SaleType $this->saleTypeRepository->findOneBy([
  254.                 'name' => '定期商品',
  255.             ]);
  256.             $qb $this->searchProductRepository->getQueryBuilderBySearchDataProductForAdmin($searchData$SaleType);
  257.             /** @var SlidingPagination $pagination */
  258.             $pagination $paginator->paginate(
  259.                 $qb,
  260.                 $page_no,
  261.                 $page_count,
  262.                 ['wrap-queries' => true]
  263.             );
  264.             /** @var $Products \Eccube\Entity\Product[] */
  265.             $Products $pagination->getItems();
  266.             if (empty($Products)) {
  267.                 log_debug('search product not found.');
  268.             }
  269.             $forms = [];
  270.             foreach ($Products as $Product) {
  271.                 $builder $this->formFactory->createNamedBuilder(''AddCartType::class, null, [
  272.                     'product' => $this->productRepository->findWithSortedClassCategories($Product->getId()),
  273.                 ]);
  274.                 $addCartForm $builder->getForm();
  275.                 $forms[$Product->getId()] = $addCartForm->createView();
  276.             }
  277.             return [
  278.                 'forms' => $forms,
  279.                 'Products' => $Products,
  280.                 'pagination' => $pagination,
  281.             ];
  282.         }
  283.     }
  284.     /**
  285.      * Calculate RegularOrder
  286.      *
  287.      * @param RegularOrder $RegularOrder
  288.      * @param Order $Order
  289.      */
  290.     public function calculate($RegularOrder$Order)
  291.     {
  292.         $RegularOrder
  293.             ->setSubtotal($Order->getSubtotal())
  294.             ->setDiscount($Order->getDiscount())
  295.             ->setDeliveryFeeTotal($Order->getDeliveryFeeTotal())
  296.             ->setCharge($Order->getCharge())
  297.             ->setTax($Order->getTax())
  298.             ->setTotal($Order->getTotal())
  299.             ->setPaymentTotal($Order->getPaymentTotal());
  300.     }
  301. }