<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\ClaimPdf42\Controller\Admin\Order;
use Eccube\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Repository\ShippingRepository;
use Plugin\ClaimPdf42\Entity\ClaimPdf;
use Plugin\ClaimPdf42\Form\Type\Admin\ClaimPdfType;
use Plugin\ClaimPdf42\Repository\ClaimPdfRepository;
use Plugin\ClaimPdf42\Service\ClaimPdfService;
class ClaimPdf_Controller extends AbstractController
{
/**
* @var ShippingRepository
*/
protected $shippingRepository;
/**
* @var ClaimPdfRepository
*/
protected $claimPdfRepository;
/**
* @var ClaimPdfService
*/
protected $claimPdfService;
/**
* ClaimPdf_Controller constructor.
*
* @param ShippingRepository $shippingRepository
* @param ClaimPdfRepository $claimPdfRepository
* @param ClaimPdfService $claimPdfService;
*/
public function __construct(
ShippingRepository $shippingRepository,
ClaimPdfRepository $claimPdfRepository,
ClaimPdfService $claimPdfService
) {
$this->shippingRepository = $shippingRepository;
$this->claimPdfRepository = $claimPdfRepository;
$this->claimPdfService = $claimPdfService;
}
/**
* @Route("/%eccube_admin_route%/order/export/claimpdf", name="admin_order_export_claimpdf")
* @Template("@ClaimPdf42/admin/Order/claimpdf.twig")
*
* @param Request $request
*
* @return array|RedirectResponse
*/
public function exportClaimtPdf(Request $request)
{
// セッションから注文日(開始)と注文日(終了)を取得する
$orderDatetimeStart = null;
$orderDatetimeEnd = null;
$viewData = $this->session->get('eccube.admin.order.search', []);
if (isset($viewData['order_datetime_start'])) {
$orderDatetimeStart = substr($viewData['order_datetime_start'], 0, 10);
}
if (isset($viewData['order_datetime_end'])) {
$orderDatetimeEnd = substr($viewData['order_datetime_end'], 0, 10);
}
// requestから出荷番号IDの一覧を取得する.
$ids = $request->get('ids', []);
if (count($ids) == 0) {
$this->addError('admin.order.delivery_note_parameter_error', 'admin');
log_info('The Order cannot found!');
return $this->redirectToRoute('admin_order');
}
// 出荷番号を昇順にソート
sort($ids, SORT_REGULAR);
$orderId_tmp = "";
$orderId_cnt = 0;
// 受注ID配列テーブルを作成する
foreach ($ids as $id) {
// 出荷番号から出荷情報を取得する
$Shipping = $this->shippingRepository->find($id);
// 出荷情報から受注情報を取得する
$orderData_tmp = $Shipping->getOrder();
// 設定条件:初回または同一受注番号でない場合(複数配送の場合は同一受注番号が複数存在する)
if ($orderId_tmp == "" or $orderId_tmp != $orderData_tmp->getId()) {
// 受注番号を取得して設定
$orderIdDatas[$orderId_cnt] = $orderData_tmp->getId();
$orderId_tmp = $orderIdDatas[$orderId_cnt];
$orderId_cnt++;
}
}
/** @var ClaimPdf $ClaimPdf */
$ClaimPdf = $this->claimPdfRepository->find($this->getUser());
//dump($ClaimPdf);
if (empty($ClaimPdf)) {
$ClaimPdf = new ClaimPdf();
$ClaimPdf
->setTitleNouhin(trans('admin.order.delivery_note_title_nouhin__default'))
->setMessage1Nouhin(trans('admin.order.delivery_note_message__default1'))
->setMessage2Nouhin(trans('admin.order.delivery_note_message__default2'))
->setMessage3Nouhin(trans('admin.order.delivery_note_message__default3'))
->setTitleNouhinShipping(trans('admin.order.delivery_note_title_otodoke__default'))
->setMessage1NouhinShipping(trans('admin.order.delivery_note_message__default7'))
->setMessage2NouhinShipping(trans('admin.order.delivery_note_message__default8'))
->setMessage3NouhinShipping(trans(''))
->setTitleOtodoke(trans('admin.order.delivery_note_title_otodoke__default'))
->setMessage1Otodoke(trans('admin.order.delivery_note_message__default7'))
->setMessage2Otodoke(trans('admin.order.delivery_note_message__default8'))
->setMessage3Otodoke(trans(''))
->setTitleSeikyu(trans('admin.order.delivery_note_title_seikyu__default'))
->setMessage1Seikyu(trans('admin.order.delivery_note_message__default4'))
->setMessage2Seikyu(trans('admin.order.delivery_note_message__default5'))
->setMessage3Seikyu(trans('admin.order.delivery_note_message__default6'))
->setHurikomiBank(trans(''))
->setHurikomiBankSiten(trans(''))
->setHurikomiBankKouza(trans(''))
->setHurikomiFreeArea(trans(''));
}
/**
* @var FormBuilder
*/
$builder = $this->formFactory->createBuilder(ClaimPdfType::class, $ClaimPdf);
/* @var \Symfony\Component\Form\Form $form */
$form = $builder->getForm();
// Formへの設定
$form->get('ids')->setData(implode(',', $ids));
$form->get('orderids')->setData(implode(',', $orderIdDatas));
$form->get('orderdatetime_st')->setData($orderDatetimeStart);
$form->get('orderdatetime_en')->setData($orderDatetimeEnd);
return [
'form' => $form->createView(),
];
}
/**
* @Route("/%eccube_admin_route%/order/export/claimpdf/download", name="admin_order_export_claimpdf_download")
* @Template("@ClaimPdf42/admin/Order/claimpdf.twig")
*
* @param Request $request
*
* @return Response
*/
public function exportClaimPdfDownload(Request $request)
{
/**
* @var FormBuilder
*/
$builder = $this->formFactory->createBuilder(ClaimPdfType::class);
/* @var \Symfony\Component\Form\Form $form */
$form = $builder->getForm();
$form->handleRequest($request);
// Validation
if (!$form->isValid()) {
log_info('The parameter is invalid!');
return $this->render('@ClaimPdf42/admin/Order/claimpdf.twig', [
'form' => $form->createView(),
]);
}
$formData = $form->getData();
$data = array();
$data["orderDatetime"]["st"] = $form->get('orderdatetime_st')->getData();
$data["orderDatetime"]["en"] = $form->get('orderdatetime_en')->getData();
$data["orderIds"] = $form->get('orderids')->getData();
$data["outputKbn"] = $form->get('outputkbn')->getData();
$data["title"] = $form->get('title')->getData();
$data["message1"] = $form->get('message1')->getData();
$data["message2"] = $form->get('message2')->getData();
$data["message3"] = $form->get('message3')->getData();
$data["atenaKbn"] = $form->get('ryosyu_atena')->getData();
$data["ryosyu_add_company"] = $form->get('ryosyu_add_company')->getData(); // 会社名追加@hanari
$data["ryosyu_add_name"] = $form->get('ryosyu_add_name')->getData(); // 宛名追加@hanari
// 出力帳票により処理を振り分ける
switch($data["outputKbn"]) {
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin'):
$formData['title_nouhin'] = $data["title"];
$formData['message1_nouhin'] = $data["message1"];
$formData['message2_nouhin'] = $data["message2"];
$formData['message3_nouhin'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin_shipping'):
$formData['title_nouhin_shipping'] = $data["title"];
$formData['message1_nouhin_shipping'] = $data["message1"];
$formData['message2_nouhin_shipping'] = $data["message2"];
$formData['message3_nouhin_shipping'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_nouhin_otodoke'):
$formData['title_otodoke'] = $data["title"];
$formData['message1_otodoke'] = $data["message1"];
$formData['message2_otodoke'] = $data["message2"];
$formData['message3_otodoke'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu'):
$formData['title_seikyu'] = $data["title"];
$formData['message1_seikyu'] = $data["message1"];
$formData['message2_seikyu'] = $data["message2"];
$formData['message3_seikyu'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu_matome'):
$formData['title_seikyu'] = $data["title"];
$formData['message1_seikyu'] = $data["message1"];
$formData['message2_seikyu'] = $data["message2"];
$formData['message3_seikyu'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_seikyu_matome_company'):
$formData['title_seikyu'] = $data["title"];
$formData['message1_seikyu'] = $data["message1"];
$formData['message2_seikyu'] = $data["message2"];
$formData['message3_seikyu'] = $data["message3"];
break;
case $this->eccubeConfig->get('eccube_claim_pdf_output_kbn_ryosyu'):
break;
default:
}
// 購入情報からPDFを作成する
$status = $this->claimPdfService->makePdf($formData, $data);
// 異常終了した場合の処理
if (!$status) {
$this->addError('admin.order.delivery_note_download_error', 'admin');
log_info('Unable to create pdf files! Process have problems!');
return $this->render('@ClaimPdf42/admin/Order/claimpdf.twig', [
'form' => $form->createView(),
]);
}
// ダウンロードする
$response = new Response(
$this->claimPdfService->outputPdf(),
200,
['content-type' => 'application/pdf']
);
$downloadKind = $form->get('download_kind')->getData();
// レスポンスヘッダーにContent-Dispositionをセットし、ファイル名を指定
if ($downloadKind == 1) {
$response->headers->set('Content-Disposition', 'attachment; filename="'.$this->claimPdfService->getPdfFileName().'"');
} else {
$response->headers->set('Content-Disposition', 'inline; filename="'.$this->claimPdfService->getPdfFileName().'"');
}
log_info('ClaimPdf download success!', ['Order ID' => implode(',', $request->get('ids', []))]);
$isDefault = isset($formData['default']) ? $formData['default'] : false;
if ($isDefault) {
// Save input to DB
$formData['admin'] = $this->getUser();
$this->claimPdfRepository->save($formData);
}
return $response;
}
}