<?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 Customize\Controller\Admin\Order;
use Doctrine\Common\Collections\ArrayCollection;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Order;
use Eccube\Entity\OrderItem;
use Eccube\Entity\Shipping;
use Eccube\Form\Type\Admin\SearchProductType;
use Eccube\Form\Type\Admin\ShippingType;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\DeliveryRepository;
use Eccube\Repository\OrderItemRepository;
use Eccube\Repository\ShippingRepository;
use Customize\Service\MailService;
use Eccube\Service\OrderStateMachine;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Eccube\Service\TaxRuleService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Eccube\Repository\OrderPdfRepository;
use Eccube\Service\OrderPdfService;
use Eccube\Entity\OrderPdf;
use Eccube\Form\Type\Admin\OrderPdfType;
use Eccube\Entity\MailHistory;
use Eccube\Repository\MailTemplateRepository;
class ShippingController extends AbstractController
{
/**
* @var OrderItemRepository
*/
protected $orderItemRepository;
/**
* @var CategoryRepository
*/
protected $categoryRepository;
/**
* @var DeliveryRepository
*/
protected $deliveryRepository;
/**
* @var TaxRuleService
*/
protected $taxRuleService;
/**
* @var ShippingRepository
*/
protected $shippingRepository;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* @var \Eccube\Service\MailService
*/
protected $mailService;
/**
* @var OrderStateMachine
*/
protected $orderStateMachine;
/**
* @var PurchaseFlow
*/
protected $purchaseFlow;
/** @var OrderPdfRepository */
protected $orderPdfRepository;
/** @var OrderPdfService */
protected $orderPdfService;
/**
* @var MailTemplateRepository
*/
protected $mailTemplateRepository;
/**
* EditController constructor.
*
* @param MailService $mailService
* @param OrderItemRepository $orderItemRepository
* @param CategoryRepository $categoryRepository
* @param DeliveryRepository $deliveryRepository
* @param TaxRuleService $taxRuleService
* @param ShippingRepository $shippingRepository
* @param SerializerInterface $serializer
* @param OrderStateMachine $orderStateMachine
* @param PurchaseFlow $orderPurchaseFlow
*/
public function __construct(
MailService $mailService,
OrderItemRepository $orderItemRepository,
CategoryRepository $categoryRepository,
DeliveryRepository $deliveryRepository,
TaxRuleService $taxRuleService,
ShippingRepository $shippingRepository,
SerializerInterface $serializer,
OrderStateMachine $orderStateMachine,
OrderPdfRepository $orderPdfRepository,
PurchaseFlow $orderPurchaseFlow,
OrderPdfService $orderPdfService,
MailTemplateRepository $mailTemplateRepository
) {
$this->mailService = $mailService;
$this->orderItemRepository = $orderItemRepository;
$this->categoryRepository = $categoryRepository;
$this->deliveryRepository = $deliveryRepository;
$this->taxRuleService = $taxRuleService;
$this->shippingRepository = $shippingRepository;
$this->serializer = $serializer;
$this->orderStateMachine = $orderStateMachine;
$this->purchaseFlow = $orderPurchaseFlow;
$this->orderPdfRepository = $orderPdfRepository;
$this->orderPdfService = $orderPdfService;
$this->mailTemplateRepository = $mailTemplateRepository;
}
/**
* @Route("/%eccube_admin_route%/shipping/notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail", methods={"PUT"})
*
* @param Shipping $Shipping
*
* @return JsonResponse
*
* @throws \Twig_Error
*/
public function notifyMail(Request $request, Shipping $Shipping)
{
$this->isTokenValid();
$pdfPath = null;
if ($request->get('deliveryNote', false)) {
$pdfPath = $this->createPdf($Shipping->getId());
}
$this->mailService->sendShippingNotifyMail($Shipping, $request->get('deliveryNote', false), $pdfPath);
$Shipping->setMailSendDate(new \DateTime());
$this->shippingRepository->save($Shipping);
$this->entityManager->flush();
return $this->json([
'mail' => true,
'shipped' => false,
]);
}
/**
* @Route("/%eccube_admin_route%/shipping/notify_mail_order/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail_order", methods={"PUT"})
*
* @param Shipping $Shipping
*
* @return JsonResponse
*
* @throws \Twig_Error
*/
public function notifyMailOrder(Request $request, Shipping $Shipping)
{
$this->isTokenValid();
$Order = $Shipping->getOrder();
// メール送信
$subject = $request->get('subject', '');
$data = $request->get('tpl_data', '');
$template_id = $request->get('template_id', null);
if($template_id) {
$MailTemplate = $this->mailTemplateRepository->find($template_id);
}
switch($template_id) {
case $this->getParameter('eccube_order_mail_template_id'):
$this->mailService->sendOrderMail($Order);
break;
case $this->getParameter('eccube_temporary_order_notify_mail_template_id'):
$this->mailService->sendOrderTemporaryMail($Order);
break;
default:
break;
}
$message = $this->mailService->sendCustomMail($Shipping, $template_id);
// $message = $this->mailService->sendAdminOrderMail($Order, $data);
// 送信履歴を保存.
$MailHistory = new MailHistory();
$MailHistory
->setMailSubject($message->getSubject())
->setMailBody($message->getTextBody())
->setSendDate(new \DateTime())
->setOrder($Order);
$this->entityManager->persist($MailHistory);
$this->entityManager->flush();
return $this->json([
'mail' => true,
'shipped' => false,
]);
}
private function createPdf($id)
{
/** @var OrderPdf $OrderPdf */
$OrderPdf = $this->orderPdfRepository->find($this->getUser());
if (!$OrderPdf) {
$OrderPdf = new OrderPdf();
$OrderPdf
->setTitle(trans('admin.order.delivery_note_title__default'))
->setMessage1(trans('admin.order.delivery_note_message__default1'))
->setMessage2(trans('admin.order.delivery_note_message__default2'))
->setMessage3(trans('admin.order.delivery_note_message__default3'));
}
$arrData = array();
$arrData['ids'] = $id;
$arrData['issue_date'] = new \DateTime();
$arrData['title'] = trans('admin.order.delivery_note_title__default');
$arrData['message1'] = trans('admin.order.delivery_note_message__default1');
$arrData['message2'] = trans('admin.order.delivery_note_message__default2');
$arrData['message3'] = trans('admin.order.delivery_note_message__default3');
$arrData['note1'] = '';
$arrData['note2'] = '';
$arrData['note3'] = '';
// 購入情報からPDFを作成する
$status = $this->orderPdfService->makePdf($arrData);
// 異常終了した場合の処理
if (!$status) {
$this->addError('admin.order.export.pdf.download.failure', 'admin');
log_info('Unable to create pdf files! Process have problems!');
return;
}
// TCPDF::Outputを実行するとプロパティが初期化されるため、ファイル名を事前に取得しておく
$pdfFileName = $this->orderPdfService->getPdfFileName();
$pdfPath = $this->eccubeConfig['eccube_theme_user_data_dir'].'/order_pdf/'.$pdfFileName;
$this->orderPdfService->Output($pdfPath, 'F');
return $pdfPath;
}
}