app/Plugin/ClaimPdf42/Controller/Admin/Order/ClaimPdf_Controller.php line 80

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\ClaimPdf42\Controller\Admin\Order;
  13. use Eccube\Controller\AbstractController;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Component\Form\FormBuilder;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\StreamedResponse;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Eccube\Repository\ShippingRepository;
  22. use Plugin\ClaimPdf42\Entity\ClaimPdf;
  23. use Plugin\ClaimPdf42\Form\Type\Admin\ClaimPdfType;
  24. use Plugin\ClaimPdf42\Repository\ClaimPdfRepository;
  25. use Plugin\ClaimPdf42\Service\ClaimPdfService;
  26. class ClaimPdf_Controller extends AbstractController
  27. {
  28.     /**
  29.      * @var ShippingRepository
  30.      */
  31.     protected $shippingRepository;
  32.     /**
  33.      * @var ClaimPdfRepository
  34.      */
  35.     protected $claimPdfRepository;
  36.     /**
  37.      * @var ClaimPdfService
  38.      */
  39.     protected $claimPdfService;
  40.     /**
  41.      * ClaimPdf_Controller constructor.
  42.      *
  43.      * @param ShippingRepository $shippingRepository
  44.      * @param ClaimPdfRepository $claimPdfRepository
  45.      * @param ClaimPdfService $claimPdfService;
  46.      */
  47.     public function __construct(
  48.         ShippingRepository $shippingRepository,
  49.         ClaimPdfRepository $claimPdfRepository,
  50.         ClaimPdfService $claimPdfService
  51.     ) {
  52.         $this->shippingRepository $shippingRepository;
  53.         $this->claimPdfRepository $claimPdfRepository;
  54.         $this->claimPdfService $claimPdfService;
  55.     }
  56.     /**
  57.      * @Route("/%eccube_admin_route%/order/export/claimpdf", name="admin_order_export_claimpdf")
  58.      * @Template("@ClaimPdf42/admin/Order/claimpdf.twig")
  59.      *
  60.      * @param Request $request
  61.      *
  62.      * @return array|RedirectResponse
  63.      */
  64.     public function exportClaimtPdf(Request $request)
  65.     {
  66.         // セッションから注文日(開始)と注文日(終了)を取得する
  67.         $orderDatetimeStart null;
  68.         $orderDatetimeEnd null;
  69.         $viewData $this->session->get('eccube.admin.order.search', []);
  70.         if (isset($viewData['order_datetime_start'])) {
  71.             $orderDatetimeStart substr($viewData['order_datetime_start'], 010);
  72.         }
  73.         if (isset($viewData['order_datetime_end'])) {
  74.             $orderDatetimeEnd substr($viewData['order_datetime_end'], 010);
  75.         }
  76.         // requestから出荷番号IDの一覧を取得する.
  77.         $ids $request->get('ids', []);
  78.         if (count($ids) == 0) {
  79.             $this->addError('admin.order.delivery_note_parameter_error''admin');
  80.             log_info('The Order cannot found!');
  81.             return $this->redirectToRoute('admin_order');
  82.         }
  83.         // 出荷番号を昇順にソート
  84.         sort($idsSORT_REGULAR);
  85.         $orderId_tmp "";
  86.         $orderId_cnt 0;
  87.         // 受注ID配列テーブルを作成する
  88.         foreach ($ids as $id) {
  89.             // 出荷番号から出荷情報を取得する
  90.             $Shipping $this->shippingRepository->find($id);
  91.             // 出荷情報から受注情報を取得する
  92.             $orderData_tmp $Shipping->getOrder();
  93.             // 設定条件:初回または同一受注番号でない場合(複数配送の場合は同一受注番号が複数存在する)
  94.             if ($orderId_tmp == "" or $orderId_tmp != $orderData_tmp->getId()) {
  95.                 // 受注番号を取得して設定
  96.                 $orderIdDatas[$orderId_cnt] = $orderData_tmp->getId();
  97.                 $orderId_tmp $orderIdDatas[$orderId_cnt];
  98.                 $orderId_cnt++;
  99.             }
  100.         }
  101.         /** @var ClaimPdf $ClaimPdf */
  102.         $ClaimPdf $this->claimPdfRepository->find($this->getUser());
  103.         //dump($ClaimPdf);
  104.         if (empty($ClaimPdf)) {
  105.             $ClaimPdf = new ClaimPdf();
  106.             $ClaimPdf
  107.                 ->setTitleNouhin(trans('admin.order.delivery_note_title_nouhin__default'))
  108.                 ->setMessage1Nouhin(trans('admin.order.delivery_note_message__default1'))
  109.                 ->setMessage2Nouhin(trans('admin.order.delivery_note_message__default2'))
  110.                 ->setMessage3Nouhin(trans('admin.order.delivery_note_message__default3'))
  111.                 ->setTitleNouhinShipping(trans('admin.order.delivery_note_title_otodoke__default'))
  112.                 ->setMessage1NouhinShipping(trans('admin.order.delivery_note_message__default7'))
  113.                 ->setMessage2NouhinShipping(trans('admin.order.delivery_note_message__default8'))
  114.                 ->setMessage3NouhinShipping(trans(''))
  115.                 ->setTitleOtodoke(trans('admin.order.delivery_note_title_otodoke__default'))
  116.                 ->setMessage1Otodoke(trans('admin.order.delivery_note_message__default7'))
  117.                 ->setMessage2Otodoke(trans('admin.order.delivery_note_message__default8'))
  118.                 ->setMessage3Otodoke(trans(''))
  119.                 ->setTitleSeikyu(trans('admin.order.delivery_note_title_seikyu__default'))
  120.                 ->setMessage1Seikyu(trans('admin.order.delivery_note_message__default4'))
  121.                 ->setMessage2Seikyu(trans('admin.order.delivery_note_message__default5'))
  122.                 ->setMessage3Seikyu(trans('admin.order.delivery_note_message__default6'))
  123.                 ->setHurikomiBank(trans(''))
  124.                 ->setHurikomiBankSiten(trans(''))
  125.                 ->setHurikomiBankKouza(trans(''))
  126.                 ->setHurikomiFreeArea(trans(''));
  127.         }
  128.         /**
  129.          * @var FormBuilder
  130.          */
  131.         $builder $this->formFactory->createBuilder(ClaimPdfType::class, $ClaimPdf);
  132.         /* @var \Symfony\Component\Form\Form $form */
  133.         $form $builder->getForm();
  134.         // Formへの設定
  135.         $form->get('ids')->setData(implode(','$ids));
  136.         $form->get('orderids')->setData(implode(','$orderIdDatas));
  137.         $form->get('orderdatetime_st')->setData($orderDatetimeStart);
  138.         $form->get('orderdatetime_en')->setData($orderDatetimeEnd);
  139.         return [
  140.             'form' => $form->createView(),
  141.         ];
  142.     }
  143.     /**
  144.      * @Route("/%eccube_admin_route%/order/export/claimpdf/download", name="admin_order_export_claimpdf_download")
  145.      * @Template("@ClaimPdf42/admin/Order/claimpdf.twig")
  146.      *
  147.      * @param Request $request
  148.      *
  149.      * @return Response
  150.      */
  151.     public function exportClaimPdfDownload(Request $request)
  152.     {
  153.         /**
  154.          * @var FormBuilder
  155.          */
  156.         $builder $this->formFactory->createBuilder(ClaimPdfType::class);
  157.         /* @var \Symfony\Component\Form\Form $form */
  158.         $form $builder->getForm();
  159.         $form->handleRequest($request);
  160.         // Validation
  161.         if (!$form->isValid()) {
  162.             log_info('The parameter is invalid!');
  163.             return $this->render('@ClaimPdf42/admin/Order/claimpdf.twig', [
  164.                 'form' => $form->createView(),
  165.             ]);
  166.         }
  167.         $formData $form->getData();
  168.         $data = array();
  169.         $data["orderDatetime"]["st"] = $form->get('orderdatetime_st')->getData();
  170.         $data["orderDatetime"]["en"] = $form->get('orderdatetime_en')->getData();
  171.         $data["orderIds"] = $form->get('orderids')->getData();
  172.         $data["outputKbn"] = $form->get('outputkbn')->getData();
  173.         $data["title"] = $form->get('title')->getData();
  174.         $data["message1"] = $form->get('message1')->getData();
  175.         $data["message2"] = $form->get('message2')->getData();
  176.         $data["message3"] = $form->get('message3')->getData();
  177.         $data["atenaKbn"] = $form->get('ryosyu_atena')->getData();
  178.         $data["ryosyu_add_company"] = $form->get('ryosyu_add_company')->getData(); // 会社名追加@hanari
  179.         $data["ryosyu_add_name"] = $form->get('ryosyu_add_name')->getData();  // 宛名追加@hanari
  180.         // 出力帳票により処理を振り分ける
  181.         switch($data["outputKbn"]) {
  182.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin'):
  183.                 $formData['title_nouhin'] = $data["title"];
  184.                 $formData['message1_nouhin'] = $data["message1"];
  185.                 $formData['message2_nouhin'] = $data["message2"];
  186.                 $formData['message3_nouhin'] = $data["message3"];
  187.                 break;
  188.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin_shipping'):
  189.                 $formData['title_nouhin_shipping'] = $data["title"];
  190.                 $formData['message1_nouhin_shipping'] = $data["message1"];
  191.                 $formData['message2_nouhin_shipping'] = $data["message2"];
  192.                 $formData['message3_nouhin_shipping'] = $data["message3"];
  193.                 break;
  194.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin_otodoke'):
  195.                 $formData['title_otodoke'] = $data["title"];
  196.                 $formData['message1_otodoke'] = $data["message1"];
  197.                 $formData['message2_otodoke'] = $data["message2"];
  198.                 $formData['message3_otodoke'] = $data["message3"];
  199.                 break;
  200.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu'):
  201.                 $formData['title_seikyu'] = $data["title"];
  202.                 $formData['message1_seikyu'] = $data["message1"];
  203.                 $formData['message2_seikyu'] = $data["message2"];
  204.                 $formData['message3_seikyu'] = $data["message3"];
  205.                 break;
  206.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu_matome'):
  207.                 $formData['title_seikyu'] = $data["title"];
  208.                 $formData['message1_seikyu'] = $data["message1"];
  209.                 $formData['message2_seikyu'] = $data["message2"];
  210.                 $formData['message3_seikyu'] = $data["message3"];
  211.                 break;
  212.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu_matome_company'):
  213.                 $formData['title_seikyu'] = $data["title"];
  214.                 $formData['message1_seikyu'] = $data["message1"];
  215.                 $formData['message2_seikyu'] = $data["message2"];
  216.                 $formData['message3_seikyu'] = $data["message3"];
  217.                 break;
  218.             case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_ryosyu'):
  219.                 break;
  220.             default:
  221.         }
  222.         // 購入情報からPDFを作成する
  223.         $status $this->claimPdfService->makePdf($formData$data);
  224.         // 異常終了した場合の処理
  225.         if (!$status) {
  226.             $this->addError('admin.order.delivery_note_download_error''admin');
  227.             log_info('Unable to create pdf files! Process have problems!');
  228.             return $this->render('@ClaimPdf42/admin/Order/claimpdf.twig', [
  229.                 'form' => $form->createView(),
  230.             ]);
  231.         }
  232.         // ダウンロードする
  233.         $response = new Response(
  234.             $this->claimPdfService->outputPdf(),
  235.             200,
  236.             ['content-type' => 'application/pdf']
  237.         );
  238.         $downloadKind $form->get('download_kind')->getData();
  239.         // レスポンスヘッダーにContent-Dispositionをセットし、ファイル名を指定
  240.         if ($downloadKind == 1) {
  241.             $response->headers->set('Content-Disposition''attachment; filename="'.$this->claimPdfService->getPdfFileName().'"');
  242.         } else {
  243.             $response->headers->set('Content-Disposition''inline; filename="'.$this->claimPdfService->getPdfFileName().'"');
  244.         }
  245.         log_info('ClaimPdf download success!', ['Order ID' => implode(','$request->get('ids', []))]);
  246.         $isDefault = isset($formData['default']) ? $formData['default'] : false;
  247.         if ($isDefault) {
  248.             // Save input to DB
  249.             $formData['admin'] = $this->getUser();
  250.             $this->claimPdfRepository->save($formData);
  251.         }
  252.         return $response;
  253.     }
  254. }