src/Eccube/Controller/Admin/Order/OrderController.php line 196

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 Eccube\Common\Constant;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\ExportCsvRow;
  16. use Eccube\Entity\Master\CsvType;
  17. use Eccube\Entity\Master\OrderStatus;
  18. use Eccube\Entity\OrderPdf;
  19. use Eccube\Entity\Shipping;
  20. use Eccube\Event\EccubeEvents;
  21. use Eccube\Event\EventArgs;
  22. use Eccube\Form\Type\Admin\OrderPdfType;
  23. use Eccube\Form\Type\Admin\SearchOrderType;
  24. use Eccube\Repository\CustomerRepository;
  25. use Eccube\Repository\Master\OrderStatusRepository;
  26. use Eccube\Repository\Master\PageMaxRepository;
  27. use Eccube\Repository\Master\ProductStatusRepository;
  28. use Eccube\Repository\Master\SexRepository;
  29. use Eccube\Repository\OrderPdfRepository;
  30. use Eccube\Repository\OrderRepository;
  31. use Eccube\Repository\PaymentRepository;
  32. use Eccube\Repository\ProductStockRepository;
  33. use Eccube\Service\CsvExportService;
  34. use Eccube\Service\MailService;
  35. use Eccube\Service\OrderPdfService;
  36. use Eccube\Service\OrderStateMachine;
  37. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  38. use Eccube\Util\FormUtil;
  39. use Knp\Component\Pager\PaginatorInterface;
  40. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  41. use Symfony\Component\Form\FormBuilder;
  42. use Symfony\Component\HttpFoundation\RedirectResponse;
  43. use Symfony\Component\HttpFoundation\Request;
  44. use Symfony\Component\HttpFoundation\Response;
  45. use Symfony\Component\HttpFoundation\StreamedResponse;
  46. use Symfony\Component\Routing\Annotation\Route;
  47. use Symfony\Component\Validator\Constraints as Assert;
  48. use Symfony\Component\Validator\Validator\ValidatorInterface;
  49. class OrderController extends AbstractController
  50. {
  51.     /**
  52.      * @var PurchaseFlow
  53.      */
  54.     protected $purchaseFlow;
  55.     /**
  56.      * @var CsvExportService
  57.      */
  58.     protected $csvExportService;
  59.     /**
  60.      * @var CustomerRepository
  61.      */
  62.     protected $customerRepository;
  63.     /**
  64.      * @var PaymentRepository
  65.      */
  66.     protected $paymentRepository;
  67.     /**
  68.      * @var SexRepository
  69.      */
  70.     protected $sexRepository;
  71.     /**
  72.      * @var OrderStatusRepository
  73.      */
  74.     protected $orderStatusRepository;
  75.     /**
  76.      * @var PageMaxRepository
  77.      */
  78.     protected $pageMaxRepository;
  79.     /**
  80.      * @var ProductStatusRepository
  81.      */
  82.     protected $productStatusRepository;
  83.     /**
  84.      * @var OrderRepository
  85.      */
  86.     protected $orderRepository;
  87.     /** @var OrderPdfRepository */
  88.     protected $orderPdfRepository;
  89.     /**
  90.      * @var ProductStockRepository
  91.      */
  92.     protected $productStockRepository;
  93.     /** @var OrderPdfService */
  94.     protected $orderPdfService;
  95.     /**
  96.      * @var ValidatorInterface
  97.      */
  98.     protected $validator;
  99.     /**
  100.      * @var OrderStateMachine
  101.      */
  102.     protected $orderStateMachine;
  103.     /**
  104.      * @var MailService
  105.      */
  106.     protected $mailService;
  107.     /**
  108.      * OrderController constructor.
  109.      *
  110.      * @param PurchaseFlow $orderPurchaseFlow
  111.      * @param CsvExportService $csvExportService
  112.      * @param CustomerRepository $customerRepository
  113.      * @param PaymentRepository $paymentRepository
  114.      * @param SexRepository $sexRepository
  115.      * @param OrderStatusRepository $orderStatusRepository
  116.      * @param PageMaxRepository $pageMaxRepository
  117.      * @param ProductStatusRepository $productStatusRepository
  118.      * @param ProductStockRepository $productStockRepository
  119.      * @param OrderRepository $orderRepository
  120.      * @param OrderPdfRepository $orderPdfRepository
  121.      * @param ValidatorInterface $validator
  122.      * @param OrderStateMachine $orderStateMachine ;
  123.      */
  124.     public function __construct(
  125.         PurchaseFlow $orderPurchaseFlow,
  126.         CsvExportService $csvExportService,
  127.         CustomerRepository $customerRepository,
  128.         PaymentRepository $paymentRepository,
  129.         SexRepository $sexRepository,
  130.         OrderStatusRepository $orderStatusRepository,
  131.         PageMaxRepository $pageMaxRepository,
  132.         ProductStatusRepository $productStatusRepository,
  133.         ProductStockRepository $productStockRepository,
  134.         OrderRepository $orderRepository,
  135.         OrderPdfRepository $orderPdfRepository,
  136.         ValidatorInterface $validator,
  137.         OrderStateMachine $orderStateMachine,
  138.         MailService $mailService
  139.     ) {
  140.         $this->purchaseFlow $orderPurchaseFlow;
  141.         $this->csvExportService $csvExportService;
  142.         $this->customerRepository $customerRepository;
  143.         $this->paymentRepository $paymentRepository;
  144.         $this->sexRepository $sexRepository;
  145.         $this->orderStatusRepository $orderStatusRepository;
  146.         $this->pageMaxRepository $pageMaxRepository;
  147.         $this->productStatusRepository $productStatusRepository;
  148.         $this->productStockRepository $productStockRepository;
  149.         $this->orderRepository $orderRepository;
  150.         $this->orderPdfRepository $orderPdfRepository;
  151.         $this->validator $validator;
  152.         $this->orderStateMachine $orderStateMachine;
  153.         $this->mailService $mailService;
  154.     }
  155.     /**
  156.      * 受注一覧画面.
  157.      *
  158.      * - 検索条件, ページ番号, 表示件数はセッションに保持されます.
  159.      * - クエリパラメータでresume=1が指定された場合、検索条件, ページ番号, 表示件数をセッションから復旧します.
  160.      * - 各データの, セッションに保持するアクションは以下の通りです.
  161.      *   - 検索ボタン押下時
  162.      *      - 検索条件をセッションに保存します
  163.      *      - ページ番号は1で初期化し、セッションに保存します。
  164.      *   - 表示件数変更時
  165.      *      - クエリパラメータpage_countをセッションに保存します。
  166.      *      - ただし, mtb_page_maxと一致しない場合, eccube_default_page_countが保存されます.
  167.      *   - ページング時
  168.      *      - URLパラメータpage_noをセッションに保存します.
  169.      *   - 初期表示
  170.      *      - 検索条件は空配列, ページ番号は1で初期化し, セッションに保存します.
  171.      *
  172.      * @Route("/%eccube_admin_route%/order", name="admin_order", methods={"GET", "POST"})
  173.      * @Route("/%eccube_admin_route%/order/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_page", methods={"GET", "POST"})
  174.      * @Template("@admin/Order/index.twig")
  175.      */
  176.     public function index(Request $requestPaginatorInterface $paginator$page_no null)
  177.     {
  178.         $builder $this->formFactory
  179.             ->createBuilder(SearchOrderType::class);
  180.         $event = new EventArgs(
  181.             [
  182.                 'builder' => $builder,
  183.             ],
  184.             $request
  185.         );
  186.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE);
  187.         $searchForm $builder->getForm();
  188.         /**
  189.          * ページの表示件数は, 以下の順に優先される.
  190.          * - リクエストパラメータ
  191.          * - セッション
  192.          * - デフォルト値
  193.          * また, セッションに保存する際は mtb_page_maxと照合し, 一致した場合のみ保存する.
  194.          **/
  195.         $page_count $this->session->get('eccube.admin.order.search.page_count',
  196.             $this->eccubeConfig->get('eccube_default_page_count'));
  197.         $page_count_param = (int) $request->get('page_count');
  198.         $pageMaxis $this->pageMaxRepository->findAll();
  199.         if ($page_count_param) {
  200.             foreach ($pageMaxis as $pageMax) {
  201.                 if ($page_count_param == $pageMax->getName()) {
  202.                     $page_count $pageMax->getName();
  203.                     $this->session->set('eccube.admin.order.search.page_count'$page_count);
  204.                     break;
  205.                 }
  206.             }
  207.         }
  208.         if ('POST' === $request->getMethod()) {
  209.             $searchForm->handleRequest($request);
  210.             if ($searchForm->isValid()) {
  211.                 /**
  212.                  * 検索が実行された場合は, セッションに検索条件を保存する.
  213.                  * ページ番号は最初のページ番号に初期化する.
  214.                  */
  215.                 $page_no 1;
  216.                 $searchData $searchForm->getData();
  217.                 // 検索条件, ページ番号をセッションに保持.
  218.                 $this->session->set('eccube.admin.order.search'FormUtil::getViewData($searchForm));
  219.                 $this->session->set('eccube.admin.order.search.page_no'$page_no);
  220.             } else {
  221.                 // 検索エラーの際は, 詳細検索枠を開いてエラー表示する.
  222.                 return [
  223.                     'searchForm' => $searchForm->createView(),
  224.                     'pagination' => [],
  225.                     'pageMaxis' => $pageMaxis,
  226.                     'page_no' => $page_no,
  227.                     'page_count' => $page_count,
  228.                     'has_errors' => true,
  229.                 ];
  230.             }
  231.         } else {
  232.             if (null !== $page_no || $request->get('resume')) {
  233.                 /*
  234.                  * ページ送りの場合または、他画面から戻ってきた場合は, セッションから検索条件を復旧する.
  235.                  */
  236.                 if ($page_no) {
  237.                     // ページ送りで遷移した場合.
  238.                     $this->session->set('eccube.admin.order.search.page_no', (int) $page_no);
  239.                 } else {
  240.                     // 他画面から遷移した場合.
  241.                     $page_no $this->session->get('eccube.admin.order.search.page_no'1);
  242.                 }
  243.                 $viewData $this->session->get('eccube.admin.order.search', []);
  244.                 $searchData FormUtil::submitAndGetData($searchForm$viewData);
  245.             } else {
  246.                 /**
  247.                  * 初期表示の場合.
  248.                  */
  249.                 $page_no 1;
  250.                 $viewData = [];
  251.                 if ($statusId = (int) $request->get('order_status_id')) {
  252.                     $viewData = ['status' => [$statusId]];
  253.                 }
  254.                 $searchData FormUtil::submitAndGetData($searchForm$viewData);
  255.                 // セッション中の検索条件, ページ番号を初期化.
  256.                 $this->session->set('eccube.admin.order.search'$viewData);
  257.                 $this->session->set('eccube.admin.order.search.page_no'$page_no);
  258.             }
  259.         }
  260.         $qb $this->orderRepository->getQueryBuilderBySearchDataForAdmin($searchData);
  261.         $event = new EventArgs(
  262.             [
  263.                 'qb' => $qb,
  264.                 'searchData' => $searchData,
  265.             ],
  266.             $request
  267.         );
  268.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_INDEX_SEARCH);
  269.         $sortKey $searchData['sortkey'];
  270.         if (empty($this->orderRepository::COLUMNS[$sortKey]) || $sortKey == 'order_status') {
  271.             $pagination $paginator->paginate(
  272.                 $qb,
  273.                 $page_no,
  274.                 $page_count
  275.             );
  276.         } else {
  277.             $pagination $paginator->paginate(
  278.                 $qb,
  279.                 $page_no,
  280.                 $page_count,
  281.                 ['wrap-queries' => true]
  282.             );
  283.         }
  284.         return [
  285.             'searchForm' => $searchForm->createView(),
  286.             'pagination' => $pagination,
  287.             'pageMaxis' => $pageMaxis,
  288.             'page_no' => $page_no,
  289.             'page_count' => $page_count,
  290.             'has_errors' => false,
  291.             'OrderStatuses' => $this->orderStatusRepository->findBy([], ['sort_no' => 'ASC']),
  292.         ];
  293.     }
  294.     /**
  295.      * @Route("/%eccube_admin_route%/order/bulk_delete", name="admin_order_bulk_delete", methods={"POST"})
  296.      */
  297.     public function bulkDelete(Request $request)
  298.     {
  299.         $this->isTokenValid();
  300.         $ids $request->get('ids');
  301.         foreach ($ids as $order_id) {
  302.             $Order $this->orderRepository
  303.                 ->find($order_id);
  304.             if ($Order) {
  305.                 $this->entityManager->remove($Order);
  306.                 log_info('受注削除', [$Order->getId()]);
  307.             }
  308.         }
  309.         $this->entityManager->flush();
  310.         $this->addSuccess('admin.common.delete_complete''admin');
  311.         return $this->redirect($this->generateUrl('admin_order', ['resume' => Constant::ENABLED]));
  312.     }
  313.     /**
  314.      * 受注CSVの出力.
  315.      *
  316.      * @Route("/%eccube_admin_route%/order/export/order", name="admin_order_export_order", methods={"GET"})
  317.      *
  318.      * @param Request $request
  319.      *
  320.      * @return StreamedResponse
  321.      */
  322.     public function exportOrder(Request $request)
  323.     {
  324.         $filename 'order_'.(new \DateTime())->format('YmdHis').'.csv';
  325.         $response $this->exportCsv($requestCsvType::CSV_TYPE_ORDER$filename);
  326.         log_info('受注CSV出力ファイル名', [$filename]);
  327.         return $response;
  328.     }
  329.     /**
  330.      * 配送CSVの出力.
  331.      *
  332.      * @Route("/%eccube_admin_route%/order/export/shipping", name="admin_order_export_shipping", methods={"GET"})
  333.      *
  334.      * @param Request $request
  335.      *
  336.      * @return StreamedResponse
  337.      */
  338.     public function exportShipping(Request $request)
  339.     {
  340.         $filename 'shipping_'.(new \DateTime())->format('YmdHis').'.csv';
  341.         $response $this->exportCsv($requestCsvType::CSV_TYPE_SHIPPING$filename);
  342.         log_info('配送CSV出力ファイル名', [$filename]);
  343.         return $response;
  344.     }
  345.     /**
  346.      * @param Request $request
  347.      * @param $csvTypeId
  348.      * @param string $fileName
  349.      *
  350.      * @return StreamedResponse
  351.      */
  352.     protected function exportCsv(Request $request$csvTypeId$fileName)
  353.     {
  354.         // タイムアウトを無効にする.
  355.         set_time_limit(0);
  356.         // sql loggerを無効にする.
  357.         $em $this->entityManager;
  358.         $em->getConfiguration()->setSQLLogger(null);
  359.         $response = new StreamedResponse();
  360.         $response->setCallback(function () use ($request$csvTypeId) {
  361.             // CSV種別を元に初期化.
  362.             $this->csvExportService->initCsvType($csvTypeId);
  363.             // 受注データ検索用のクエリビルダを取得.
  364.             $qb $this->csvExportService
  365.                 ->getOrderQueryBuilder($request);
  366.             // ヘッダ行の出力.
  367.             $this->csvExportService->exportHeader();
  368.             // データ行の出力.
  369.             $this->csvExportService->setExportQueryBuilder($qb);
  370.             $this->csvExportService->exportData(function ($entity$csvService) use ($request) {
  371.                 $Csvs $csvService->getCsvs();
  372.                 $Order $entity;
  373.                 $OrderItems $Order->getOrderItems();
  374.                 foreach ($OrderItems as $OrderItem) {
  375.                     $ExportCsvRow = new ExportCsvRow();
  376.                     // CSV出力項目と合致するデータを取得.
  377.                     foreach ($Csvs as $Csv) {
  378.                         // 受注データを検索.
  379.                         $ExportCsvRow->setData($csvService->getData($Csv$Order));
  380.                         if ($ExportCsvRow->isDataNull()) {
  381.                             // 受注データにない場合は, 受注明細を検索.
  382.                             $ExportCsvRow->setData($csvService->getData($Csv$OrderItem));
  383.                         }
  384.                         if ($ExportCsvRow->isDataNull() && $Shipping $OrderItem->getShipping()) {
  385.                             // 受注明細データにない場合は, 出荷を検索.
  386.                             $ExportCsvRow->setData($csvService->getData($Csv$Shipping));
  387.                         }
  388.                         $event = new EventArgs(
  389.                             [
  390.                                 'csvService' => $csvService,
  391.                                 'Csv' => $Csv,
  392.                                 'OrderItem' => $OrderItem,
  393.                                 'ExportCsvRow' => $ExportCsvRow,
  394.                             ],
  395.                             $request
  396.                         );
  397.                         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER);
  398.                         $ExportCsvRow->pushData();
  399.                     }
  400.                     // $row[] = number_format(memory_get_usage(true));
  401.                     // 出力.
  402.                     $csvService->fputcsv($ExportCsvRow->getRow());
  403.                 }
  404.             });
  405.         });
  406.         $response->headers->set('Content-Type''application/octet-stream');
  407.         $response->headers->set('Content-Disposition''attachment; filename='.$fileName);
  408.         return $response;
  409.     }
  410.     /**
  411.      * Update to order status
  412.      *
  413.      * @Route("/%eccube_admin_route%/shipping/{id}/order_status", requirements={"id" = "\d+"}, name="admin_shipping_update_order_status", methods={"PUT"})
  414.      *
  415.      * @param Request $request
  416.      * @param Shipping $Shipping
  417.      *
  418.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  419.      */
  420.     public function updateOrderStatus(Request $requestShipping $Shipping)
  421.     {
  422.         if (!($request->isXmlHttpRequest() && $this->isTokenValid())) {
  423.             return $this->json(['status' => 'NG'], 400);
  424.         }
  425.         $Order $Shipping->getOrder();
  426.         $OrderStatus $this->entityManager->find(OrderStatus::class, $request->get('order_status'));
  427.         if (!$OrderStatus) {
  428.             return $this->json(['status' => 'NG'], 400);
  429.         }
  430.         $result = [];
  431.         try {
  432.             if ($Order->getOrderStatus()->getId() == $OrderStatus->getId()) {
  433.                 log_info('対応状況一括変更スキップ');
  434.                 $result = ['message' => trans('admin.order.skip_change_status', ['%name%' => $Shipping->getId()])];
  435.             } else {
  436.                 if ($this->orderStateMachine->can($Order$OrderStatus)) {
  437.                     if ($OrderStatus->getId() == OrderStatus::DELIVERED) {
  438.                         if (!$Shipping->isShipped()) {
  439.                             $Shipping->setShippingDate(new \DateTime());
  440.                         }
  441.                         $allShipped true;
  442.                         foreach ($Order->getShippings() as $Ship) {
  443.                             if (!$Ship->isShipped()) {
  444.                                 $allShipped false;
  445.                                 break;
  446.                             }
  447.                         }
  448.                         if ($allShipped) {
  449.                             $this->orderStateMachine->apply($Order$OrderStatus);
  450.                         }
  451.                     } else {
  452.                         $this->orderStateMachine->apply($Order$OrderStatus);
  453.                     }
  454.                     if ($request->get('notificationMail')) { // for SimpleStatusUpdate
  455.                         $this->mailService->sendShippingNotifyMail($Shipping);
  456.                         $Shipping->setMailSendDate(new \DateTime());
  457.                         $result['mail'] = true;
  458.                     } else {
  459.                         $result['mail'] = false;
  460.                     }
  461.                     // 対応中・キャンセルの更新時は商品在庫を増減させているので商品情報を更新
  462.                     //if ($OrderStatus->getId() == OrderStatus::IN_PROGRESS || $OrderStatus->getId() == OrderStatus::CANCEL) {
  463.                     if ($OrderStatus->getId() == OrderStatus::IN_PROGRESS || $OrderStatus->getId() == OrderStatus::CANCEL || $OrderStatus->getId() == OrderStatus::CANCEL_PAYPROCESS) {
  464.                         foreach ($Order->getOrderItems() as $OrderItem) {
  465.                             $ProductClass $OrderItem->getProductClass();
  466.                             if ($OrderItem->isProduct() && !$ProductClass->isStockUnlimited()) {
  467.                                 $this->entityManager->persist($ProductClass);
  468.                                 $this->entityManager->flush();
  469.                                 $ProductStock $this->productStockRepository->findOneBy(['ProductClass' => $ProductClass]);
  470.                                 $this->entityManager->persist($ProductStock);
  471.                                 $this->entityManager->flush();
  472.                             }
  473.                         }
  474.                     }
  475.                     $this->entityManager->persist($Order);
  476.                     $this->entityManager->persist($Shipping);
  477.                     $this->entityManager->flush();
  478.                     // 会員の場合、購入回数、購入金額などを更新
  479.                     if ($Customer $Order->getCustomer()) {
  480.                         $this->orderRepository->updateOrderSummary($Customer);
  481.                         $this->entityManager->persist($Customer);
  482.                         $this->entityManager->flush();
  483.                     }
  484.                 } else {
  485.                     $from $Order->getOrderStatus()->getName();
  486.                     $to $OrderStatus->getName();
  487.                     $result = ['message' => trans('admin.order.failed_to_change_status', [
  488.                         '%name%' => $Shipping->getId(),
  489.                         '%from%' => $from,
  490.                         '%to%' => $to,
  491.                     ])];
  492.                 }
  493.                 log_info('対応状況一括変更処理完了', [$Order->getId()]);
  494.             }
  495.         } catch (\Exception $e) {
  496.             log_error('予期しないエラーです', [$e->getMessage()]);
  497.             return $this->json(['status' => 'NG'], 500);
  498.         }
  499.         return $this->json(array_merge(['status' => 'OK'], $result));
  500.     }
  501.     /**
  502.      * Update to Tracking number.
  503.      *
  504.      * @Route("/%eccube_admin_route%/shipping/{id}/tracking_number", requirements={"id" = "\d+"}, name="admin_shipping_update_tracking_number", methods={"PUT"})
  505.      *
  506.      * @param Request $request
  507.      * @param Shipping $shipping
  508.      *
  509.      * @return Response
  510.      */
  511.     public function updateTrackingNumber(Request $requestShipping $shipping)
  512.     {
  513.         if (!($request->isXmlHttpRequest() && $this->isTokenValid())) {
  514.             return $this->json(['status' => 'NG'], 400);
  515.         }
  516.         $trackingNumber mb_convert_kana($request->get('tracking_number'), 'a''utf-8');
  517.         /** @var \Symfony\Component\Validator\ConstraintViolationListInterface $errors */
  518.         $errors $this->validator->validate(
  519.             $trackingNumber,
  520.             [
  521.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
  522.                 new Assert\Regex(
  523.                     ['pattern' => '/^[0-9a-zA-Z-]+$/u''message' => trans('admin.order.tracking_number_error')]
  524.                 ),
  525.             ]
  526.         );
  527.         if ($errors->count() != 0) {
  528.             log_info('送り状番号入力チェックエラー');
  529.             $messages = [];
  530.             /** @var \Symfony\Component\Validator\ConstraintViolationInterface $error */
  531.             foreach ($errors as $error) {
  532.                 $messages[] = $error->getMessage();
  533.             }
  534.             return $this->json(['status' => 'NG''messages' => $messages], 400);
  535.         }
  536.         try {
  537.             $shipping->setTrackingNumber($trackingNumber);
  538.             $this->entityManager->persist($shipping);
  539.             $this->entityManager->flush();
  540.             log_info('送り状番号変更処理完了', [$shipping->getId()]);
  541.             $message = ['status' => 'OK''shipping_id' => $shipping->getId(), 'tracking_number' => $trackingNumber];
  542.             return $this->json($message);
  543.         } catch (\Exception $e) {
  544.             log_error('予期しないエラー', [$e->getMessage()]);
  545.             return $this->json(['status' => 'NG'], 500);
  546.         }
  547.     }
  548.     /**
  549.      * @Route("/%eccube_admin_route%/order/export/pdf", name="admin_order_export_pdf", methods={"GET", "POST"})
  550.      * @Template("@admin/Order/order_pdf.twig")
  551.      *
  552.      * @param Request $request
  553.      *
  554.      * @return array|RedirectResponse
  555.      */
  556.     public function exportPdf(Request $request)
  557.     {
  558.         // requestから出荷番号IDの一覧を取得する.
  559.         $ids $request->get('ids', []);
  560.         if (count($ids) == 0) {
  561.             $this->addError('admin.order.delivery_note_parameter_error''admin');
  562.             log_info('The Order cannot found!');
  563.             return $this->redirectToRoute('admin_order');
  564.         }
  565.         /** @var OrderPdf $OrderPdf */
  566.         $OrderPdf $this->orderPdfRepository->find($this->getUser());
  567.         if (!$OrderPdf) {
  568.             $OrderPdf = new OrderPdf();
  569.             $OrderPdf
  570.                 ->setTitle(trans('admin.order.delivery_note_title__default'))
  571.                 ->setMessage1(trans('admin.order.delivery_note_message__default1'))
  572.                 ->setMessage2(trans('admin.order.delivery_note_message__default2'))
  573.                 ->setMessage3(trans('admin.order.delivery_note_message__default3'));
  574.         }
  575.         /**
  576.          * @var FormBuilder
  577.          */
  578.         $builder $this->formFactory->createBuilder(OrderPdfType::class, $OrderPdf);
  579.         /* @var \Symfony\Component\Form\Form $form */
  580.         $form $builder->getForm();
  581.         // Formへの設定
  582.         $form->get('ids')->setData(implode(','$ids));
  583.         return [
  584.             'form' => $form->createView(),
  585.         ];
  586.     }
  587.     /**
  588.      * @Route("/%eccube_admin_route%/order/export/pdf/download", name="admin_order_pdf_download", methods={"POST"})
  589.      * @Template("@admin/Order/order_pdf.twig")
  590.      *
  591.      * @param Request $request
  592.      *
  593.      * @return Response
  594.      */
  595.     public function exportPdfDownload(Request $requestOrderPdfService $orderPdfService)
  596.     {
  597.         /**
  598.          * @var FormBuilder
  599.          */
  600.         $builder $this->formFactory->createBuilder(OrderPdfType::class);
  601.         /* @var \Symfony\Component\Form\Form $form */
  602.         $form $builder->getForm();
  603.         $form->handleRequest($request);
  604.         // Validation
  605.         if (!$form->isValid()) {
  606.             log_info('The parameter is invalid!');
  607.             return $this->render('@admin/Order/order_pdf.twig', [
  608.                 'form' => $form->createView(),
  609.             ]);
  610.         }
  611.         $arrData $form->getData();
  612.         // 購入情報からPDFを作成する
  613.         $status $orderPdfService->makePdf($arrData);
  614.         // 異常終了した場合の処理
  615.         if (!$status) {
  616.             $this->addError('admin.order.export.pdf.download.failure''admin');
  617.             log_info('Unable to create pdf files! Process have problems!');
  618.             return $this->render('@admin/Order/order_pdf.twig', [
  619.                 'form' => $form->createView(),
  620.             ]);
  621.         }
  622.         // TCPDF::Outputを実行するとプロパティが初期化されるため、ファイル名を事前に取得しておく
  623.         $pdfFileName $orderPdfService->getPdfFileName();
  624.         // ダウンロードする
  625.         $response = new Response(
  626.             $orderPdfService->outputPdf(),
  627.             200,
  628.             ['content-type' => 'application/pdf']
  629.         );
  630.         $downloadKind $form->get('download_kind')->getData();
  631.         // レスポンスヘッダーにContent-Dispositionをセットし、ファイル名を指定
  632.         if ($downloadKind == 1) {
  633.             $response->headers->set('Content-Disposition''attachment; filename="'.$pdfFileName.'"');
  634.         } else {
  635.             $response->headers->set('Content-Disposition''inline; filename="'.$pdfFileName.'"');
  636.         }
  637.         log_info('OrderPdf download success!', ['Order ID' => implode(','$request->get('ids', []))]);
  638.         $isDefault = isset($arrData['default']) ? $arrData['default'] : false;
  639.         if ($isDefault) {
  640.             // Save input to DB
  641.             $arrData['admin'] = $this->getUser();
  642.             $this->orderPdfRepository->save($arrData);
  643.         }
  644.         return $response;
  645.     }
  646. }