<?php
namespace Plugin\SlnRegular4;
use Plugin\SlnRegular4\Repository\SlnRegularPluginConfigRepository;
use Plugin\SlnRegular4\Repository\SlnRegularOrderRepository;
use Plugin\SlnRegular4\Repository\SlnRegularOrderToOrderRepository;
use Plugin\SlnRegular4\Repository\SlnRegularSaleTypeRepository;
use Plugin\SlnRegular4\Repository\SlnRegularDeliveryPriceRepository;
use Plugin\SlnRegular4\Entity\ConfigSubData;
use Plugin\SlnRegular4\Service\BasicItem;
// 差し替え
// use Plugin\SlnRegular4\Service\RegularOrderService;
use Customize\Service\RegularOrderService;
// 差し替え
// use Plugin\SlnRegular4\Service\ShoppingService;
use Customize\Service\ShoppingService;
use Eccube\Form\Type\PriceType;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Event\TemplateEvent;
use Eccube\Event\EventArgs;
use Eccube\Event\EccubeEvents;
use Eccube\Service\CartService;
use Eccube\Service\TaxRuleService;
use Eccube\Common\Constant;
use Eccube\Common\EccubeConfig;
use Eccube\Repository\DeliveryRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Doctrine\ORM\EntityManagerInterface;
class SlnRegular4Event implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var EccubeConfig
*/
protected $eccubeConfig;
/**
* @var CartService
*/
private $cartService;
/**
* @var TaxRuleService
*/
private $taxRuleService;
/**
* @var DeliveryRepository
*/
protected $deliveryRepository;
/**
* @var SlnRegularPluginConfigRepository
*/
protected $regularPluginConfigRepository;
/**
* @var SlnRegularOrderRepository
*/
protected $regularOrderRepository;
/**
* @var SlnRegularOrderToOrderRepository
*/
protected $regularOrderToOrderRepository;
/**
* @var SlnRegularSaleTypeRepository
*/
protected $regularSaleTypeRepository;
/**
* @var SlnRegularDeliveryPriceRepository
*/
protected $regularDeliveryPriceRepository;
/**
* @var BasiItem
*/
protected $basicItem;
/**
* @var RegularOrderService
*/
protected $regularOrderService;
/**
* @var ShoppingService
*/
protected $shoppingService;
/**
* コンストラクタ
*/
public function __construct(
EntityManagerInterface $entityManager,
EccubeConfig $eccubeConfig,
CartService $cartService,
TaxRuleService $taxRuleService,
DeliveryRepository $deliveryRepository,
SlnRegularPluginConfigRepository $regularPluginConfigRepository,
SlnRegularOrderRepository $regularOrderRepository,
SlnRegularOrderToOrderRepository $regularOrderToOrderRepository,
SlnRegularSaleTypeRepository $regularSaleTypeRepository,
SlnRegularDeliveryPriceRepository $regularDeliveryPriceRepository,
BasicItem $basicItem,
RegularOrderService $regularOrderService,
ShoppingService $shoppingService
) {
$this->entityManager = $entityManager;
$this->eccubeConfig = $eccubeConfig;
$this->cartService = $cartService;
$this->taxRuleService = $taxRuleService;
$this->deliveryRepository = $deliveryRepository;
$this->regularPluginConfigRepository = $regularPluginConfigRepository;
$this->regularOrderRepository = $regularOrderRepository;
$this->regularOrderToOrderRepository = $regularOrderToOrderRepository;
$this->regularSaleTypeRepository = $regularSaleTypeRepository;
$this->regularDeliveryPriceRepository = $regularDeliveryPriceRepository;
$this->basicItem = $basicItem;
$this->regularOrderService = $regularOrderService;
$this->shoppingService = $shoppingService;
}
/**
* リッスンしたいサブスクライバのイベント名の配列を返します。
* 配列のキーはイベント名、値は以下のどれかをしてします。
* - 呼び出すメソッド名
* - 呼び出すメソッド名と優先度の配列
* - 呼び出すメソッド名と優先度の配列の配列
* 優先度を省略した場合は0
*
* 例:
* - array('eventName' => 'methodName')
* - array('eventName' => array('methodName', $priority))
* - array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/edit.twig' => 'onAdminOrderEditTwig',
'@admin/Setting/Shop/delivery.twig' => 'onAdminSettingShopDeliveryTwig',
'@admin/Setting/Shop/shop_master.twig' => 'onAdminSettingShopShopMasterTwig',
EccubeEvents::ADMIN_SETTING_SHOP_SHOP_INDEX_INITIALIZE => 'onAdminSettingShopInitialize',
EccubeEvents::ADMIN_SETTING_SHOP_SHOP_INDEX_COMPLETE => 'onAdminSettingShopComplete',
'Product/list.twig' => 'onProductListTwig',
'Product/detail.twig' => 'onProductDetailTwig',
'Shopping/login.twig' => 'onShoppingLoginTwig',
'Shopping/index.twig' => 'onShoppingIndexTwig',
'Shopping/confirm.twig' => 'onShoppingConfirmTwig',
'Mypage/index.twig' => 'onMypageTwig',
'Mypage/history.twig' => 'onMypageHistoryTwig',
'Mypage/favorite.twig' => 'onMypageTwig',
'Mypage/change.twig' => 'onMypageTwig',
'Mypage/change_complete.twig' => 'onMypageTwig',
'Mypage/delivery.twig' => 'onMypageTwig',
'Mypage/delivery_edit.twig' => 'onMypageTwig',
'Mypage/withdraw.twig' => 'onMypageTwig',
'@SlnPayment41/sln_edit_card.twig' => 'onMypageTwig',
'@SlnRegular4/Mypage/regular_order.twig' => 'onMypageTwig',
'@SlnRegular4/Mypage/regular_history.twig' => 'onMypageTwig',
'sln.front.shopping.confirm.processing' => 'onSlnFrontShoppingConfirmProcessing',
'sln.payment.util.merchant_free' => 'onSlnPaymentUtilMerchantFree',
'sln.payment.shopping.add_mem' => 'onSlnPaymentShoppingAddMem',
'sln.payment.shopping.add_mem_view' => 'onSlnPaymentShoppingAddMemView',
];
}
/**
* 管理画面 - 受注登録画面介入
*/
public function onAdminOrderEditTwig(TemplateEvent $event) {
$Order = $event->getParameter('Order');
$OrderToOrders = $this->regularOrderToOrderRepository->findBy(['Order' => $Order]);
$RegularOrders = [];
foreach ($OrderToOrders as $OrderToOrder) {
$RegularOrders[] = $OrderToOrder->getRegularOrder();
}
if (count($RegularOrders)) {
$event->setParameter('RegularOrders', $RegularOrders);
$event->setParameter('terminalStatus', $this->basicItem->getRegularStatus());
$event->addSnippet('@SlnRegular4/admin/Order/edit.twig');
}
}
/**
* 管理画面 - 配送方法一覧画面介入
*/
public function onAdminSettingShopDeliveryTwig(TemplateEvent $event) {
$slnSaleTypeFlagMap = [];
$Deliveries = $this->deliveryRepository->findBy([], ['sort_no' => 'DESC']);
foreach ($Deliveries as $Delivery) {
$saleTypes = $this->regularSaleTypeRepository->findBy([
'saleTypeId' => $Delivery->getSaleType()->getId(),
]);
$slnSaleTypeFlagMap[$Delivery->getId()] = !empty($saleTypes);
}
// 定期購入販売種別フラグマップの設定
$event->setParameter('sln_sale_type_flag_map', $slnSaleTypeFlagMap);
$event->addSnippet('@SlnRegular4/admin/delivery.twig');
}
/**
* 管理画面 - 店舗マスター編集画面介入
* @param TemplateEvent $event
*/
public function onAdminSettingShopShopMasterTwig(TemplateEvent $event)
{
$event->addSnippet('@SlnRegular4/admin/shop_master.twig');
}
/**
* 管理画面 - 店舗マスター編集初期化
* @param EventArgs $event
*/
public function onAdminSettingShopInitialize(EventArgs $event)
{
$price_len = $this->eccubeConfig->get('eccube_price_len');
$builder = $event->getArgument('builder');
// 送料設定
$builder->add('delivery_free_amount2', PriceType::class, array(
'label' => '定期二回目送料無料条件(金額)',
'required' => false,
'currency' => 'JPY',
'scale' => 0,
'mapped' => false,
'constraints' => array(
new Assert\Length(array(
'max' => $price_len,
)),
new Assert\Regex(array(
'pattern' => "/^\d+$/u",
'message' => 'form.type.numeric.invalid'
)),
),
))
->add('delivery_free_quantity2', IntegerType::class, array(
'label' => '定期二回目送料無料条件(数量)',
'required' => false,
'mapped' => false,
'constraints' => array(
new Assert\Regex(array(
'pattern' => "/^\d+$/u",
'message' => 'form.type.numeric.invalid'
)),
),
));
/* @var $configRepository \Plugin\SlnRegular\Repository\PlgSlnRegularPluginConfigRepository */
$configdata = $this->regularPluginConfigRepository->getConfig();
// 初期値を設定
$builder->get('delivery_free_amount2')->setData($configdata->getDeliveryFreeAmount2());
$builder->get('delivery_free_quantity2')->setData($configdata->getDeliveryFreeQuantity2());
}
/**
* 管理画面 - 店舗マスター編集保存
* @param EventArgs $event
*/
public function onAdminSettingShopComplete(EventArgs $event)
{
$form = $event->getArgument('form');
//無料価格条件
$deliveryFreeAmount2 = $form['delivery_free_amount2']->getData();
//無料個数条件
$deliveryFreeQuantity2 = $form['delivery_free_quantity2']->getData();
/* @var $configRepository \Plugin\SlnRegular\Repository\PlgSlnRegularPluginConfigRepository */
$configdata = $this->regularPluginConfigRepository->getConfig();
$configdata->setDeliveryFreeAmount2($deliveryFreeAmount2);
$configdata->setDeliveryFreeQuantity2($deliveryFreeQuantity2);
$this->regularPluginConfigRepository->saverConfig($configdata, false);
}
/**
* 管理画面 - 商品編集画面介入
* @param TemplateEvent $event
*/
public function onAdminProductTwig(TemplateEvent $event) {
$event->addSnippet('@SlnRegular4/admin/product.twig');
}
/**
* 管理画面 - 商品編集画面初期化
* @param EventArgs $event
*/
public function onAdminProductEditInitialize(EventArgs $event)
{
/* @var $Product \Eccube\Entity\Product */
$Product = $event->getArgument('Product');
$has_class = $Product->hasProductClass(); //商品クラスが規格クラスを持っているかどうか
if (!$has_class) {
$builder = $event->getArgument('builder')->get('class');
$builder->add('price2', PriceType::class, array(
'label' => '2回目以降の販売価格',
'currency' => 'JPY',
'scale' => 0,
'mapped' => false,
'constraints' => array(
new Assert\Length(array(
'max' => 10,
)),
new Assert\Regex(array(
'pattern' => "/^\d+$/u",
'message' => 'form.type.numeric.invalid'
)),
),
));
// 初期値を設定
$productClasses = $Product->getProductClasses();
$DeliveryPrice = $this->regularDeliveryPriceRepository->findOrCreate($Product->getId(), $productClasses[0]->getId());
$builder->get('price2')->setData($DeliveryPrice->getPrice2());
}
}
/**
* 管理画面 - 商品編集画面初期化
* @param EventArgs $event
*/
public function onAdminProductEditComplete(EventArgs $event)
{
/* @var $Product \Eccube\Entity\Product */
$Product = $event->getArgument('Product');
$has_class = $Product->hasProductClass();
if (!$has_class) {//商品規格存在していない場合
$form = $event->getArgument('form');
//定期購入二回目価格
$price2 = $form->get('class')->get('price2')->getData();
$productClasses = $Product->getProductClasses();
$DeliveryPrice = $this->regularDeliveryPriceRepository->findOrCreate($Product->getId(), $productClasses[0]->getId());
$DeliveryPrice->setPrice2($price2);
$this->entityManager->persist($DeliveryPrice);
$this->entityManager->flush();
}
}
/**
* フロント画面 - 商品一覧画面介入
*/
public function onProductListTwig(TemplateEvent $event) {
$pagination = $event->getParameter('pagination');
$hasPrice2Data = [];
$productData = [];
foreach ($pagination as $Product) {
$hasPrice2 = false;
$productInfo = [];
$price2IncTaxs = [];
$price02IncTaxs = [];
foreach ($Product->getProductClasses() as $ProductClass) {
$key = $this->getProductClassKey($Product, $ProductClass);
if (!is_null($ProductClass->getSlnRegularPrice2()) && $this->regularSaleTypeRepository->isRegularProductClass($ProductClass)) {
$hasPrice2 = true;
$price2IncTax = $this->getPrice2IncTax($Product, $ProductClass);
$price2IncTaxs[$key] = $price2IncTax;
}
$price02IncTax = $this->getPrice02IncTax($Product, $ProductClass);
$price02IncTaxs[$key] = $price02IncTax;
}
if (count($price2IncTaxs) > 0) {
$productInfo['price2IncTaxMin'] = min($price2IncTaxs);
$productInfo['price2IncTaxMax'] = max($price2IncTaxs);
}
$productInfo['price02IncTaxMin'] = min($price02IncTaxs);
$productInfo['price02IncTaxMax'] = max($price02IncTaxs);
if ($hasPrice2) {
$hasClassCat1 = count($Product->getClassCategories1()) > 0;
$hasClassCat2 = false;
if ($hasClassCat1) {
$hasClassCat2 = $Product->getProductClasses()[0]->hasClassCategory2();
}
$productInfo['hasClassCat1'] = $hasClassCat1;
$productInfo['hasClassCat2'] = $hasClassCat2;
$productInfo['price2IncTaxs'] = $price2IncTaxs;
$productInfo['price02IncTaxs'] = $price02IncTaxs;
$productData[$Product->getId()] = $productInfo;
}
$hasPrice2Data[$Product->getId()] = $hasPrice2;
}
$event->setParameter('hasPrice2Data', $hasPrice2Data);
$event->setParameter('productData', $productData);
$event->addSnippet('@SlnRegular4/Product/list.twig');
}
public function getPrice2IncTax($Product, $ProductClass) {
return $this->taxRuleService->getPriceIncTax($ProductClass->getSlnRegularPrice2(), $Product, $ProductClass);
}
public function getPrice02IncTax($Product, $ProductClass) {
return $this->taxRuleService->getPriceIncTax($ProductClass->getPrice02(), $Product, $ProductClass);
}
public function getProductClassKey($Product, $ProductClass) {
$key = $Product->getId();
if ($ProductClass->hasClassCategory1()) {
$key = $key . '_' . $ProductClass->getClassCategory1()->getId();
if ($ProductClass->hasClassCategory2()) {
$key = $key . '_' . $ProductClass->getClassCategory2()->getId();
}
}
return $key;
}
/**
* フロント画面 - 商品詳細画面介入
*/
public function onProductDetailTwig(TemplateEvent $event) {
$Product = $event->getParameter('Product');
$hasPrice2 = false;
$productInfo = [];
$price2IncTaxs = [];
$price02IncTaxs = [];
foreach ($Product->getProductClasses() as $ProductClass) {
$key = $this->getProductClassKey($Product, $ProductClass);
if (!is_null($ProductClass->getSlnRegularPrice2()) && $this->regularSaleTypeRepository->isRegularProductClass($ProductClass)) {
$hasPrice2 = true;
$price2IncTax = $this->getPrice2IncTax($Product, $ProductClass);
$price2IncTaxs[$key] = $price2IncTax;
$price02IncTax = $this->getPrice02IncTax($Product, $ProductClass);
$price02IncTaxs[$key] = $price02IncTax;
}
}
if (count($price2IncTaxs) > 0) {
$productInfo['price2IncTaxMin'] = min($price2IncTaxs);
$productInfo['price2IncTaxMax'] = max($price2IncTaxs);
}
if ($hasPrice2) {
$hasClassCat1 = count($Product->getClassCategories1()) > 0;
$hasClassCat2 = false;
if ($hasClassCat1) {
$hasClassCat2 = $Product->getProductClasses()[0]->hasClassCategory2();
}
$event->setParameter('hasClassCat1', $hasClassCat1);
$event->setParameter('hasClassCat2', $hasClassCat2);
$event->setParameter('productInfo', $productInfo);
$event->setParameter('price2IncTaxs', $price2IncTaxs);
$event->setParameter('price02IncTaxs', $price02IncTaxs);
$event->addSnippet('@SlnRegular4/Product/detail.twig');
}
}
/**
* フロント画面 - 購入時ログイン画面介入
*/
public function onShoppingLoginTwig(TemplateEvent $event) {
$Cart = $this->cartService->getCart();
$CartItems = $Cart->getCartItems();
foreach ($CartItems as $CartItem) {
$ProductClass = $CartItem->getProductClass();
if ($ProductClass) {
if ($this->regularSaleTypeRepository->isRegularProductClass($ProductClass)) {
$event->addSnippet('@SlnRegular4/Shopping/login.twig');
break;
}
}
}
}
/**
* フロント画面 - 購入画面介入
*/
public function onShoppingIndexTwig(TemplateEvent $event) {
$Order = $event->getParameter('Order');
if ($this->regularSaleTypeRepository->isRegularOrder($Order)) {
$event->addSnippet('@SlnRegular4/Shopping/index.twig');
}
}
/**
* フロント画面 - 購入確認画面介入
*/
public function onShoppingConfirmTwig(TemplateEvent $event) {
$Order = $event->getParameter('Order');
$this->shoppingService->saveRegularOrder($Order, 'confirm');
if ($this->regularSaleTypeRepository->isRegularOrder($Order)) {
$event->setParameter('arrRegularTypeText', $this->basicItem->getRegularTypeText());
$event->setParameter('arrRegularDeliveryDateText', $this->basicItem->getRegularDeliveryDateText());
$event->addSnippet('@SlnRegular4/Shopping/confirm.twig');
}
}
/**
* フロント画面 - マイページ画面介入
*/
public function onMypageTwig(TemplateEvent $event) {
$event->addSnippet('@SlnRegular4/Mypage/add_menu_item.twig');
}
/**
* フロント画面 - マイページ購入履歴詳細画面介入
*/
public function onMypageHistoryTwig(TemplateEvent $event) {
$this->onMypageTwig($event);
$data = $event->getParameters();
/* @var $Order \Eccube\Entity\Order */
$Order = $data['Order'];
$RegularOrders = $this->regularOrderToOrderRepository
->findBy(array('Order' => $Order));
if (count($RegularOrders)) {
/* @var $RegularOrder \Plugin\SlnRegular4\Entity\SlnRegularOrderToOrder */
foreach ($RegularOrders as $RegularOrder) {
try {
$data['regularOrders'][] = $RegularOrder;
} catch (\Exception $e) {
}
}
}
$event->setParameters($data);
if (count($RegularOrders)) {
$event->addSnippet('@SlnRegular4/Mypage/history.twig');
}
}
/**
* ソニー決済完了時
* @param EventArgs $event
*/
public function onSlnFrontShoppingConfirmProcessing(EventArgs $event)
{
$Order = $event->getArgument('Order');
$this->entityManager->getConnection()->beginTransaction();
/* @var $RegularOrder \Plugin\SlnRegular\Entity\PlgSlnRegularOrder */
$RegularOrder = $this->regularOrderRepository
->findOneBy(array('createOrder' => $Order));
if ($RegularOrder) {
if ($Order->getOrderStatus()->getId() == OrderStatus::PROCESSING || $Order->getOrderStatus()->getId() == OrderStatus::PENDING) {
$RegularOrder->setTerminalStatus(5);
} else {
$RegularOrder->setTerminalStatus(1);
}
$RegularOrder->setOrderDate($Order->getOrderDate());
$this->entityManager->persist($RegularOrder);
$this->entityManager->flush();
}
$this->entityManager->getConnection()->commit();
if ($RegularOrder) {//定期購入場合
//定期メールを送信する
$this->shoppingService->sendRegularOrderMail($RegularOrder, $Order, $event->getRequest()->getSession());
}
}
/**
* 送信情報を変更する
* @param EventArgs $event
*/
public function onSlnPaymentUtilMerchantFree(EventArgs $event)
{
/* @var $Order \Eccube\Entity\Order */
/* @var $Basic \Plugin\SlnPayment\Service\SlnAction\Content\Credit\Request\Auth */
$Order = $event->getArgument('Order');
$Basic = $event->getArgument('Basic');
/* @var $RegularOrderToOrders \Plugin\SlnRegular\Entity\PlgSlnRegularOrderToOrder[] */
$RegularOrderToOrders = $this->regularOrderToOrderRepository->findBy(array('Order' => $Order));
if (count($RegularOrderToOrders)) {
$ReIds = array();
foreach ($RegularOrderToOrders as $RegularOrderToOrder) {
$ReIds[] = $RegularOrderToOrder->getRegularOrderId();
}
if (count($ReIds)) {
$Basic['MerchantFree2'] = sprintf("%s|%s|%s", join($ReIds, ','), Constant::VERSION, $Order->getCustomer() ? $Order->getCustomer()->getId() : null);
}
}
}
public function onSlnPaymentShoppingAddMemView(EventArgs $event)
{
/* @var $Order \Eccube\Entity\Order */
$Order = $event->getArgument('Order');
/* @var $RegularOrderToOrders \Plugin\SlnRegular\Entity\PlgSlnRegularOrderToOrder[] */
$RegularOrderToOrders = $this->regularOrderToOrderRepository->findBy(array('Order' => $Order));
if (count($RegularOrderToOrders)) {
$event->setArgument('IsAddMemView', false);
}
}
/**
* 定期商品購入時にクレジットカードを登録する
* @param EventArgs $event
*/
public function onSlnPaymentShoppingAddMem(EventArgs $event)
{
/* @var $Order \Eccube\Entity\Order */
$Order = $event->getArgument('Order');
/* @var $RegularOrderToOrders \Plugin\SlnRegular\Entity\PlgSlnRegularOrderToOrder[] */
$RegularOrderToOrders = $this->regularOrderToOrderRepository->findBy(array('Order' => $Order));
if (count($RegularOrderToOrders)) {
$AddMem = $event->getArgument('AddMem');
$AddMem[0] = 1;
$event->setArgument('AddMem', $AddMem);
}
}
}