src/Eccube/Controller/Admin/Order/ShippingController.php line 304

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 Eccube\Controller\Admin\Order;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\Order;
  16. use Eccube\Entity\OrderItem;
  17. use Eccube\Entity\Shipping;
  18. use Eccube\Form\Type\Admin\SearchProductType;
  19. use Eccube\Form\Type\Admin\ShippingType;
  20. use Eccube\Repository\CategoryRepository;
  21. use Eccube\Repository\DeliveryRepository;
  22. use Eccube\Repository\OrderItemRepository;
  23. use Eccube\Repository\ShippingRepository;
  24. use Eccube\Service\MailService;
  25. use Eccube\Service\OrderStateMachine;
  26. use Eccube\Service\PurchaseFlow\PurchaseContext;
  27. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  28. use Eccube\Service\TaxRuleService;
  29. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  30. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  31. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  32. use Symfony\Component\Form\FormEvent;
  33. use Symfony\Component\Form\FormEvents;
  34. use Symfony\Component\HttpFoundation\JsonResponse;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\Response;
  37. use Symfony\Component\Routing\Annotation\Route;
  38. use Symfony\Component\Serializer\SerializerInterface;
  39. class ShippingController extends AbstractController
  40. {
  41.     /**
  42.      * @var OrderItemRepository
  43.      */
  44.     protected $orderItemRepository;
  45.     /**
  46.      * @var CategoryRepository
  47.      */
  48.     protected $categoryRepository;
  49.     /**
  50.      * @var DeliveryRepository
  51.      */
  52.     protected $deliveryRepository;
  53.     /**
  54.      * @var TaxRuleService
  55.      */
  56.     protected $taxRuleService;
  57.     /**
  58.      * @var ShippingRepository
  59.      */
  60.     protected $shippingRepository;
  61.     /**
  62.      * @var SerializerInterface
  63.      */
  64.     protected $serializer;
  65.     /**
  66.      * @var \Eccube\Service\MailService
  67.      */
  68.     protected $mailService;
  69.     /**
  70.      * @var OrderStateMachine
  71.      */
  72.     protected $orderStateMachine;
  73.     /**
  74.      * @var PurchaseFlow
  75.      */
  76.     protected $purchaseFlow;
  77.     /**
  78.      * EditController constructor.
  79.      *
  80.      * @param MailService $mailService
  81.      * @param OrderItemRepository $orderItemRepository
  82.      * @param CategoryRepository $categoryRepository
  83.      * @param DeliveryRepository $deliveryRepository
  84.      * @param TaxRuleService $taxRuleService
  85.      * @param ShippingRepository $shippingRepository
  86.      * @param SerializerInterface $serializer
  87.      * @param OrderStateMachine $orderStateMachine
  88.      * @param PurchaseFlow $orderPurchaseFlow
  89.      */
  90.     public function __construct(
  91.         MailService $mailService,
  92.         OrderItemRepository $orderItemRepository,
  93.         CategoryRepository $categoryRepository,
  94.         DeliveryRepository $deliveryRepository,
  95.         TaxRuleService $taxRuleService,
  96.         ShippingRepository $shippingRepository,
  97.         SerializerInterface $serializer,
  98.         OrderStateMachine $orderStateMachine,
  99.         PurchaseFlow $orderPurchaseFlow
  100.     ) {
  101.         $this->mailService $mailService;
  102.         $this->orderItemRepository $orderItemRepository;
  103.         $this->categoryRepository $categoryRepository;
  104.         $this->deliveryRepository $deliveryRepository;
  105.         $this->taxRuleService $taxRuleService;
  106.         $this->shippingRepository $shippingRepository;
  107.         $this->serializer $serializer;
  108.         $this->orderStateMachine $orderStateMachine;
  109.         $this->purchaseFlow $orderPurchaseFlow;
  110.     }
  111.     /**
  112.      * 出荷登録/編集画面.
  113.      *
  114.      * @Route("/%eccube_admin_route%/shipping/{id}/edit", requirements={"id" = "\d+"}, name="admin_shipping_edit", methods={"GET", "POST"})
  115.      * @Template("@admin/Order/shipping.twig")
  116.      */
  117.     public function index(Request $requestOrder $Order)
  118.     {
  119.         $OriginOrder = clone $Order;
  120.         $purchaseContext = new PurchaseContext($OriginOrder$OriginOrder->getCustomer());
  121.         $TargetShippings $Order->getShippings();
  122.         // 編集前の受注情報を保持
  123.         $OriginShippings = new ArrayCollection();
  124.         $OriginOrderItems = [];
  125.         foreach ($TargetShippings as $key => $TargetShipping) {
  126.             $OriginShippings->add($TargetShipping);
  127.             // 編集前のお届け先のアイテム情報を保持
  128.             $OriginOrderItems[$key] = new ArrayCollection();
  129.             foreach ($TargetShipping->getOrderItems() as $OrderItem) {
  130.                 $OriginOrderItems[$key]->add($OrderItem);
  131.             }
  132.         }
  133.         $builder $this->formFactory->createBuilder();
  134.         $builder
  135.             ->add('shippings'CollectionType::class, [
  136.                 'entry_type' => ShippingType::class,
  137.                 'data' => $TargetShippings,
  138.                 'allow_add' => true,
  139.                 'allow_delete' => true,
  140.                 'prototype' => true,
  141.             ]);
  142.         // 配送先の追加フラグ
  143.         $builder
  144.             ->add('add_shipping'HiddenType::class, [
  145.                 'mapped' => false,
  146.             ]);
  147.         // 配送先の追加フラグが立っている場合は新しいお届け先を追加
  148.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  149.             $data $event->getData();
  150.             if ($data['add_shipping']) {
  151.                 $Shippings $data['shippings'];
  152.                 $newShipping = ['Delivery' => ''];
  153.                 $Shippings[] = $newShipping;
  154.                 $data['shippings'] = $Shippings;
  155.                 $data['add_shipping'] = '';
  156.                 $event->setData($data);
  157.             }
  158.         });
  159.         $form $builder->getForm();
  160.         $form->handleRequest($request);
  161.         if ($form->isSubmitted() && $form->isValid()) {
  162.             // 削除された項目の削除
  163.             /** @var Shipping $OriginShipping */
  164.             foreach ($OriginShippings as $key => $OriginShipping) {
  165.                 if (false === $TargetShippings->contains($OriginShipping)) {
  166.                     // お届け先自体が削除された場合
  167.                     // 削除されたお届け先に紐づく明細の削除
  168.                     /** @var OrderItem $OriginOrderItem */
  169.                     foreach ($OriginOrderItems[$key] as $OriginOrderItem) {
  170.                         $Order->removeOrderItem($OriginOrderItem);
  171.                         $this->entityManager->remove($OriginOrderItem);
  172.                     }
  173.                     // 削除されたお届け先の削除
  174.                     $this->entityManager->remove($OriginShipping);
  175.                 } else {
  176.                     // お届け先は削除されていない場合
  177.                     // 削除された明細の削除
  178.                     /** @var OrderItem $OriginOrderItem */
  179.                     foreach ($OriginOrderItems[$key] as $OriginOrderItem) {
  180.                         if (false === $TargetShippings[$key]->getOrderItems()->contains($OriginOrderItem)) {
  181.                             $Order->removeOrderItem($OriginOrderItem);
  182.                             $this->entityManager->remove($OriginOrderItem);
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.             // 追加された項目の追加
  188.             foreach ($TargetShippings as $TargetShipping) {
  189.                 // 追加された明細の追加
  190.                 /** @var OrderItem $OrderItem */
  191.                 foreach ($TargetShipping->getOrderItems() as $OrderItem) {
  192.                     $OrderItem->setShipping($TargetShipping);
  193.                     if (is_null($OrderItem->getOrder())) {
  194.                         $OrderItem->setOrder($Order);
  195.                         $Order->addOrderItem($OrderItem);
  196.                     }
  197.                 }
  198.                 // 追加されたお届け先の追加
  199.                 $TargetShipping->setOrder($Order);
  200.             }
  201.             $flowResult $this->purchaseFlow->validate($Order$purchaseContext);
  202.             if ($flowResult->hasWarning()) {
  203.                 foreach ($flowResult->getWarning() as $warning) {
  204.                     $this->addWarning($warning->getMessage(), 'admin');
  205.                 }
  206.             }
  207.             if ($flowResult->hasError()) {
  208.                 foreach ($flowResult->getErrors() as $error) {
  209.                     $this->addError($error->getMessage(), 'admin');
  210.                 }
  211.             }
  212.             if (!$flowResult->hasError() && $request->get('mode') == 'register') {
  213.                 log_info('出荷登録開始', [$Order->getId()]);
  214.                 try {
  215.                     $this->purchaseFlow->prepare($Order$purchaseContext);
  216.                     $this->purchaseFlow->commit($Order$purchaseContext);
  217.                     $this->entityManager->persist($Order);
  218.                     foreach ($TargetShippings as $TargetShipping) {
  219.                         $this->entityManager->persist($TargetShipping);
  220.                     }
  221.                     $this->entityManager->flush();
  222.                     $this->addInfo('admin.order.shipping_save_message''admin');
  223.                     $this->addSuccess('admin.common.save_complete''admin');
  224.                     log_info('出荷登録完了', [$Order->getId()]);
  225.                     return $this->redirectToRoute('admin_shipping_edit', ['id' => $Order->getId()]);
  226.                 } catch (\Exception $e) {
  227.                     log_error('出荷登録エラー', [$Order->getId(), $e]);
  228.                     $this->addError('admin.flash.register_failed''admin');
  229.                 }
  230.             } elseif ($request->get('mode') == 'register' && $form->getErrors(true)) {
  231.                 $this->addError('admin.common.save_error''admin');
  232.             }
  233.         }
  234.         // 商品検索フォーム
  235.         $builder $this->formFactory
  236.             ->createBuilder(SearchProductType::class);
  237.         $searchProductModalForm $builder->getForm();
  238.         // 配送業者のお届け時間
  239.         $times = [];
  240.         $deliveries $this->deliveryRepository->findAll();
  241.         foreach ($deliveries as $Delivery) {
  242.             $deliveryTimes $Delivery->getDeliveryTimes();
  243.             foreach ($deliveryTimes as $DeliveryTime) {
  244.                 $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
  245.             }
  246.         }
  247.         return [
  248.             'form' => $form->createView(),
  249.             'searchProductModalForm' => $searchProductModalForm->createView(),
  250.             'Order' => $Order,
  251.             'shippingDeliveryTimes' => $this->serializer->serialize($times'json'),
  252.         ];
  253.     }
  254.     /**
  255.      * @Route("/%eccube_admin_route%/shipping/preview_notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_preview_notify_mail", methods={"GET"})
  256.      *
  257.      * @param Shipping $Shipping
  258.      *
  259.      * @return Response
  260.      *
  261.      * @throws \Twig_Error
  262.      */
  263.     public function previewShippingNotifyMail(Shipping $Shipping)
  264.     {
  265.         return new Response($this->mailService->getShippingNotifyMailBody($Shipping$Shipping->getOrder(), nulltrue));
  266.     }
  267.     /**
  268.      * @Route("/%eccube_admin_route%/shipping/notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail", methods={"PUT"})
  269.      *
  270.      * @param Shipping $Shipping
  271.      *
  272.      * @return JsonResponse
  273.      *
  274.      * @throws \Twig_Error
  275.      */
  276.     public function notifyMail(Shipping $Shipping)
  277.     {
  278.         $this->isTokenValid();
  279.         $this->mailService->sendShippingNotifyMail($Shipping);
  280.         $Shipping->setMailSendDate(new \DateTime());
  281.         $this->shippingRepository->save($Shipping);
  282.         $this->entityManager->flush();
  283.         return $this->json([
  284.             'mail' => true,
  285.             'shipped' => false,
  286.         ]);
  287.     }
  288. }