-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.php
More file actions
271 lines (236 loc) · 9.15 KB
/
Event.php
File metadata and controls
271 lines (236 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
namespace Plugin\GHNDelivery;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Entity\Order;
use Eccube\Entity\OrderItem;
use Eccube\Entity\Shipping;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\GHNDelivery\Entity\GHNConfig;
use Plugin\GHNDelivery\Repository\GHNConfigRepository;
use Plugin\GHNDelivery\Repository\GHNDeliveryRepository;
use Plugin\GHNDelivery\Repository\GHNOrderRepository;
use Plugin\GHNDelivery\Repository\GHNServiceRepository;
use Plugin\GHNDelivery\Repository\GHNWarehouseRepository;
use Plugin\GHNDelivery\Service\ApiService;
use Plugin\GHNDelivery\Service\PurchaseFlow\GHNProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
class Event implements EventSubscriberInterface
{
use ControllerTrait;
/** @var Session */
protected $session;
/** @var ContainerInterface */
protected $container;
/** @var EccubeConfig */
protected $eccubeConfig;
/** @var GHNDeliveryRepository */
protected $ghnDeliveryRepo;
/** @var ApiService */
protected $apiService;
/** @var GHNWarehouseRepository */
protected $warehouseRepo;
/** @var GHNOrderRepository */
protected $ghnOrderRepo;
/** @var GHNConfigRepository */
protected $configRepo;
/** @var EntityManagerInterface */
protected $entityManager;
/** @var GHNServiceRepository */
protected $serviceRepo;
/**
* @var RequestStack
*/
protected $request;
/**
* Event constructor.
* @param Session $session
* @param ContainerInterface $container
* @param EccubeConfig $eccubeConfig
* @param GHNDeliveryRepository $GHNDeliveryRepo
* @param ApiService $apiService
* @param GHNWarehouseRepository $warehouseRepo
* @param GHNOrderRepository $GHNOrderRepo
* @param GHNConfigRepository $configRepo
* @param EntityManagerInterface $entityManager
* @param GHNServiceRepository $serviceRepo
* @param RequestStack $request
*/
public function __construct(Session $session, ContainerInterface $container, EccubeConfig $eccubeConfig, GHNDeliveryRepository $GHNDeliveryRepo, ApiService $apiService, GHNWarehouseRepository $warehouseRepo, GHNOrderRepository $GHNOrderRepo, GHNConfigRepository $configRepo, EntityManagerInterface $entityManager, GHNServiceRepository $serviceRepo, RequestStack $request)
{
$this->session = $session;
$this->container = $container;
$this->eccubeConfig = $eccubeConfig;
$this->ghnDeliveryRepo = $GHNDeliveryRepo;
$this->apiService = $apiService;
$this->warehouseRepo = $warehouseRepo;
$this->ghnOrderRepo = $GHNOrderRepo;
$this->configRepo = $configRepo;
$this->entityManager = $entityManager;
$this->serviceRepo = $serviceRepo;
$this->request = $request;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'shoppingIndex',
EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => 'orderEditHandle',
EccubeEvents::ADMIN_ORDER_UPDATE_STATUS_COMPLETE => 'createGHNOrderForOneShipping',
EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS => 'calcGHNServeFeeOneShipping',
EccubeEvents::ADMIN_SHIPPING_EDIT_INDEX_COMPLETE => 'calcGHNServeFeeMultiShipping',
'@admin/Order/edit.twig' => 'renderOrder'
];
}
public function renderOrder(TemplateEvent $event)
{
$order = $event->getParameter('Order');
$ghnOrder = $this->ghnOrderRepo->findBy(['Order' => $order]);
$event->setParameter('GHNOrders', $ghnOrder);
$event->addSnippet('@GHNDelivery/admin/render_order.twig');
}
public function calcGHNServeFeeMultiShipping(EventArgs $eventArgs)
{
$config = $this->configRepo->find(1);
if (!$config) {
return;
}
/** @var Shipping[] $targetShippings */
$targetShippings = $eventArgs['TargetShippings'];
$request = $this->request->getCurrentRequest();
$dataRequest = $request->get('form')['shippings'];
foreach ($targetShippings as $key => $shipping) {
$mainServiceId = $dataRequest[$key]['main_service_id'];
$this->serviceRepo->createGHNServiceByShipping($shipping, $mainServiceId, $key);
}
}
/**
* For one shipping only
*
* @param EventArgs $eventArgs
* @throws \Doctrine\ORM\ORMException
*/
public function calcGHNServeFeeOneShipping(EventArgs $eventArgs)
{
$config = $this->configRepo->find(1);
if (!$config) {
return;
}
/** @var Order $order */
$order = $eventArgs['TargetOrder'];
if ($order->isMultiple()) {
return;
}
$requestData = $this->request->getCurrentRequest()->get('order');
if (!isset($requestData['Shipping']['main_service_id']) || !is_numeric($requestData['Shipping']['main_service_id'])) {
$this->addFlash('eccube.admin.error', 'ghn.shopping.delivery.service_incorrect');
return;
}
/** @var Shipping $shp */
$shp = $order->getShippings()->first();
$mainServiceId = $requestData['Shipping']['main_service_id'];
$this->serviceRepo->createGHNServiceByShipping($shp, $mainServiceId);
// do not flush
}
public function createGHNOrderForOneShipping(EventArgs $event)
{
/** @var GHNConfig $config */
$config = $this->configRepo->find(1);
if (!$config) {
return;
}
/** @var Shipping $shipping */
$shipping = $event['Shipping'];
/** @var Order $order */
$order = $shipping->getOrder();
if ($order->getOrderStatus()->getId() == OrderStatus::CANCEL) {
// do cancel
$this->ghnOrderRepo->cancelGHNOrderByShipping($shipping);
$this->entityManager->flush();
}
if ($order->getOrderStatus()->getId() != OrderStatus::IN_PROGRESS) {
return;
}
$this->ghnOrderRepo->createGHNOrderByShipping($shipping);
// please flush all for update order
$this->entityManager->flush();
}
/**
* @param EventArgs $event
* @throws \Doctrine\ORM\ORMException
*/
public function orderEditHandle(EventArgs $event)
{
/** @var GHNConfig $config */
$config = $this->configRepo->find(1);
if (!$config) {
return;
}
/** @var Order $order */
$order = $event['TargetOrder'];
if ($order->getOrderStatus()->getId() == OrderStatus::CANCEL) {
// do cancel
$this->ghnOrderRepo->cancelGHNOrderByOrder($order);
$this->addFlash('eccube.admin.success', 'ghn.shipping.cancel.success');
$this->entityManager->flush();
return;
}
if ($order->getOrderStatus()->getId() != OrderStatus::IN_PROGRESS) {
return;
}
$this->ghnOrderRepo->createGHNOrderByOrder($order);
$this->addFlash('eccube.admin.success', 'ghn.shipping.create.success');
// please flush all for update order
$this->entityManager->flush();
}
/**
* @param TemplateEvent $templateEvent
*/
public function shoppingIndex(TemplateEvent $templateEvent)
{
$config = $this->configRepo->find(1);
if (!$config) {
return;
}
$parameters = $templateEvent->getParameters();
/** @var Order $order */
$order = $parameters['Order'];
$redirects = [];
// check GHN delivery in order
foreach ($order->getShippings() as $shipping) {
$redirects[$shipping->getId()] = false;
$delivery = $shipping->getDelivery();
$GHNDelivery = $this->ghnDeliveryRepo->find($delivery->getId());
// if ghn delivery => add script to redirect to GHN page
if ($GHNDelivery) {
$redirects[$shipping->getId()] = true;
/** @var OrderItem $shippingOrderItem */
foreach ($shipping->getOrderItems() as $shippingOrderItem) {
// is exist GHN delivery fee
if ($shippingOrderItem->isDeliveryFee() && $shippingOrderItem->getProcessorName() == GHNProcessor::class) {
$redirects[$shipping->getId()] = false;
}
}
}
}
// save to session
$this->session->set($this->eccubeConfig->get('ghn_session_redirect'), $redirects);
// change tempalte
foreach ($redirects as $key => $value) {
if ($value) {
$templateEvent->setResponse($this->redirectToRoute('ghn_delivery_shopping', ['id' => $key]));
$templateEvent->setSource($templateEvent->getResponse()->getContent());
break;
}
}
}
}