<?php
/*
* Plugin Name : DeliveryDate4
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\DeliveryDate42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Plugin\DeliveryDate42\Service\ShoppingService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ShoppingEvent implements EventSubscriberInterface
{
private $entityManager;
private $shoppingService;
public function __construct(
EntityManagerInterface $entityManager,
ShoppingService $shoppingService
)
{
$this->entityManager = $entityManager;
$this->shoppingService = $shoppingService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE => 'hookFrontShoppingCompleteInitialize',
];
}
public function hookFrontShoppingCompleteInitialize(EventArgs $event)
{
$Order = $event->getArgument('Order');
foreach($Order->getShippings() as $Shipping){
$shippingDate = null;
/** @var \Eccube\Entity\Shipping $Shipping */
if(method_exists($Shipping, 'setShippingScheduleDate')){
$shippingDeliveryDate = $Shipping->getShippingDeliveryDate();
if(!is_null($shippingDeliveryDate))$shipping_date = $shippingDeliveryDate->format('Y/m/d');
if(isset($shipping_date)){
$Delivery = $Shipping->getDelivery();
$shippingDate = new \DateTime($shipping_date);
$dates = $this->shoppingService->getDeliveryDates($Delivery, $Shipping->getPref());
if(!is_null($dates)){
$shippingDate->modify('-' . $dates . 'days');
}
}else{
$minDate = $this->shoppingService->getShippingDates($Shipping);
if(!is_null($minDate)){
$shippingDate = $Order->getCreateDate();
$shippingDate->modify('+' . $minDate . 'days');
}
}
if(!is_null($shippingDate)){
$Shipping->setShippingScheduleDate($shippingDate);
$this->entityManager->persist($Shipping);
}
}
}
$this->entityManager->flush();
}
}