1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Controller\Admin\Shipping; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Eccube\Controller\AbstractController; |
18
|
|
|
use Eccube\Entity\OrderItem; |
19
|
|
|
use Eccube\Entity\Shipping; |
20
|
|
|
use Eccube\Form\Type\Admin\ShippingType; |
21
|
|
|
use Eccube\Repository\CategoryRepository; |
22
|
|
|
use Eccube\Repository\DeliveryRepository; |
23
|
|
|
use Eccube\Repository\OrderItemRepository; |
24
|
|
|
use Eccube\Repository\ShippingRepository; |
25
|
|
|
use Eccube\Service\TaxRuleService; |
26
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
27
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
28
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
33
|
|
|
use Eccube\Service\MailService; |
34
|
|
|
|
35
|
|
|
class EditController extends AbstractController |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var OrderItemRepository |
39
|
|
|
*/ |
40
|
|
|
protected $orderItemRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CategoryRepository |
44
|
|
|
*/ |
45
|
|
|
protected $categoryRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var DeliveryRepository |
49
|
|
|
*/ |
50
|
|
|
protected $deliveryRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var TaxRuleService |
54
|
|
|
*/ |
55
|
|
|
protected $taxRuleService; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ShippingRepository |
59
|
|
|
*/ |
60
|
|
|
protected $shippingRepository; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var SerializerInterface |
64
|
|
|
*/ |
65
|
|
|
protected $serializer; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \Eccube\Service\MailService |
69
|
|
|
*/ |
70
|
|
|
protected $mailService; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* EditController constructor. |
74
|
|
|
* |
75
|
|
|
* @param MailService $mailService |
|
|
|
|
76
|
|
|
* @param OrderItemRepository $orderItemRepository |
77
|
|
|
* @param CategoryRepository $categoryRepository |
|
|
|
|
78
|
|
|
* @param DeliveryRepository $deliveryRepository |
|
|
|
|
79
|
|
|
* @param TaxRuleService $taxRuleService |
|
|
|
|
80
|
|
|
* @param ShippingRepository $shippingRepository |
|
|
|
|
81
|
|
|
* @param SerializerInterface $serializer |
82
|
|
|
*/ |
83
|
6 |
|
public function __construct( |
84
|
|
|
MailService $mailService, |
85
|
|
|
OrderItemRepository $orderItemRepository, |
86
|
|
|
CategoryRepository $categoryRepository, |
87
|
|
|
DeliveryRepository $deliveryRepository, |
88
|
|
|
TaxRuleService $taxRuleService, |
89
|
|
|
ShippingRepository $shippingRepository, |
90
|
|
|
SerializerInterface $serializer |
91
|
|
|
) { |
92
|
6 |
|
$this->mailService = $mailService; |
93
|
6 |
|
$this->orderItemRepository = $orderItemRepository; |
94
|
6 |
|
$this->categoryRepository = $categoryRepository; |
95
|
6 |
|
$this->deliveryRepository = $deliveryRepository; |
96
|
6 |
|
$this->taxRuleService = $taxRuleService; |
97
|
6 |
|
$this->shippingRepository = $shippingRepository; |
98
|
6 |
|
$this->serializer = $serializer; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* 出荷登録/編集画面. |
103
|
|
|
* |
104
|
|
|
* @Route("/%eccube_admin_route%/shipping/new", name="admin_shipping_new") |
105
|
|
|
* @Route("/%eccube_admin_route%/shipping/{id}/edit", requirements={"id" = "\d+"}, name="admin_shipping_edit") |
106
|
|
|
* @Template("@admin/Shipping/edit.twig") |
107
|
|
|
*/ |
|
|
|
|
108
|
6 |
|
public function edit(Request $request, $id = null) |
109
|
|
|
{ |
110
|
6 |
|
$TargetShipping = null; |
|
|
|
|
111
|
6 |
|
$OriginShipping = null; |
|
|
|
|
112
|
|
|
|
113
|
6 |
|
if (null === $id) { |
114
|
|
|
// 空のエンティティを作成. |
115
|
2 |
|
$TargetShipping = new Shipping(); |
116
|
|
|
} else { |
117
|
5 |
|
$TargetShipping = $this->shippingRepository->find($id); |
118
|
5 |
|
if (null === $TargetShipping) { |
119
|
|
|
throw new NotFoundHttpException(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// 編集前の受注情報を保持 |
124
|
6 |
|
$OriginShipping = clone $TargetShipping; |
125
|
|
|
// 編集前のお届け先のアイテム情報を保持 |
126
|
6 |
|
$OriginalOrderItems = new ArrayCollection(); |
127
|
|
|
|
128
|
6 |
|
foreach ($TargetShipping->getOrderItems() as $OrderItem) { |
129
|
4 |
|
$OriginalOrderItems->add($OrderItem); |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$builder = $this->formFactory |
133
|
6 |
|
->createBuilder(ShippingType::class, $TargetShipping); |
134
|
|
|
|
135
|
6 |
|
$form = $builder->getForm(); |
136
|
6 |
|
$form->handleRequest($request); |
137
|
|
|
|
138
|
6 |
|
if ($form->isSubmitted() && $form->isValid()) { |
139
|
|
|
// TODO: Should move logic out of controller such as service, modal |
140
|
|
|
|
141
|
|
|
// FIXME 税額計算は CalculateService で処理する. ここはテストを通すための暫定処理 |
142
|
|
|
// see EditControllerTest::testOrderProcessingWithTax |
143
|
5 |
|
$OrderItems = $TargetShipping->getOrderItems(); |
144
|
5 |
|
$taxtotal = 0; |
145
|
5 |
|
foreach ($OrderItems as $OrderItem) { |
146
|
4 |
|
$tax = $this->taxRuleService |
147
|
4 |
|
->calcTax($OrderItem->getPrice(), $OrderItem->getTaxRate(), $OrderItem->getTaxRule()); |
148
|
4 |
|
$OrderItem->setPriceIncTax($OrderItem->getPrice() + $tax); |
149
|
|
|
|
150
|
4 |
|
$taxtotal += $tax * $OrderItem->getQuantity(); |
151
|
|
|
} |
152
|
|
|
|
153
|
5 |
|
log_info('出荷登録開始', [$TargetShipping->getId()]); |
154
|
|
|
// TODO 在庫の有無や販売制限数のチェックなども行う必要があるため、完了処理もcaluclatorのように抽象化できないか検討する. |
155
|
|
|
// TODO 後続にある会員情報の更新のように、完了処理もcaluclatorのように抽象化できないか検討する. |
156
|
|
|
// 画面上で削除された明細をremove |
157
|
5 |
|
foreach ($OriginalOrderItems as $OrderItem) { |
158
|
4 |
|
if (false === $TargetShipping->getOrderItems()->contains($OrderItem)) { |
159
|
4 |
|
$OrderItem->setShipping(null); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
5 |
|
foreach ($TargetShipping->getOrderItems() as $OrderItem) { |
164
|
4 |
|
$OrderItem->setShipping($TargetShipping); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// 出荷ステータス変更時の処理 |
168
|
5 |
|
if ($TargetShipping->isShipped()) { |
169
|
|
|
// 「出荷済み」にステータスが変更された場合 |
170
|
2 |
|
if ($OriginShipping->isShipped() == false) { |
171
|
|
|
// 出荷メールを送信 |
172
|
2 |
|
if ($form->get('notify_email')->getData()) { |
173
|
|
|
try { |
174
|
1 |
|
$this->mailService->sendShippingNotifyMail( |
175
|
1 |
|
$TargetShipping |
176
|
|
|
); |
177
|
|
|
} catch (\Exception $e) { |
178
|
|
|
log_error('メール通知エラー', [$TargetShipping->getId(), $e]); |
179
|
|
|
$this->addError( |
180
|
|
|
'admin.shipping.edit.shipped_mail_failed', |
181
|
|
|
'admin' |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
try { |
189
|
5 |
|
$this->entityManager->persist($TargetShipping); |
190
|
5 |
|
$this->entityManager->flush(); |
191
|
|
|
|
192
|
5 |
|
$this->addSuccess('admin.shipping.edit.save.complete', 'admin'); |
193
|
5 |
|
$this->addInfo('admin.shipping.edit.save.info', 'admin'); |
194
|
5 |
|
log_info('出荷登録完了', [$TargetShipping->getId()]); |
195
|
|
|
|
196
|
5 |
|
return $this->redirectToRoute('admin_shipping_edit', ['id' => $TargetShipping->getId()]); |
197
|
|
|
} catch (\Exception $e) { |
198
|
|
|
log_error('出荷登録エラー', [$TargetShipping->getId(), $e]); |
199
|
|
|
$this->addError('admin.flash.register_failed', 'admin'); |
200
|
|
|
} |
201
|
6 |
|
} elseif ($form->isSubmitted() && $form->getErrors(true)) { |
202
|
|
|
$this->addError('admin.flash.register_failed', 'admin'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// 配送業者のお届け時間 |
206
|
6 |
|
$times = []; |
207
|
6 |
|
$deliveries = $this->deliveryRepository->findAll(); |
208
|
6 |
View Code Duplication |
foreach ($deliveries as $Delivery) { |
209
|
6 |
|
$deliveryTiems = $Delivery->getDeliveryTimes(); |
210
|
6 |
|
foreach ($deliveryTiems as $DeliveryTime) { |
211
|
6 |
|
$times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return [ |
216
|
6 |
|
'form' => $form->createView(), |
217
|
6 |
|
'Shipping' => $TargetShipping, |
218
|
6 |
|
'shippingDeliveryTimes' => $this->serializer->serialize($times, 'json'), |
219
|
|
|
]; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
|
|
|
|
223
|
|
|
* @Route("/%eccube_admin_route%/shipping/search/product", name="admin_shipping_search_product") |
224
|
|
|
* @Template("@admin/Shipping/search_product.twig") |
225
|
|
|
*/ |
|
|
|
|
226
|
|
|
public function searchProduct(Request $request, PaginatorInterface $paginator) |
227
|
|
|
{ |
228
|
|
|
if (!$request->isXmlHttpRequest()) { |
229
|
|
|
throw new BadRequestHttpException(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// FIXME: should use consistent param for pageno ? Other controller use page_no, but here use pageno, then I change from pageno to page_no |
233
|
|
|
$page_no = (int) $request->get('page_no', 1); |
234
|
|
|
$page_count = $this->eccubeConfig['eccube_default_page_count']; |
235
|
|
|
|
236
|
|
|
// TODO OrderItemRepository に移動 |
237
|
|
|
$qb = $this->orderItemRepository->createQueryBuilder('s') |
238
|
|
|
->where('s.Shipping is null AND s.Order is not null') |
239
|
|
|
->andWhere('s.OrderItemType in (1, 2)'); |
240
|
|
|
|
241
|
|
|
/** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */ |
242
|
|
|
$pagination = $paginator->paginate( |
243
|
|
|
$qb, |
244
|
|
|
$page_no, |
245
|
|
|
$page_count, |
246
|
|
|
['wrap-queries' => true] |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
return [ |
250
|
|
|
'pagination' => $pagination, |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
|
|
|
|
255
|
|
|
* @Route("/%eccube_admin_route%/shipping/search/item", name="admin_shipping_search_item") |
256
|
|
|
* @Template("@admin/Shipping/order_item_prototype.twig") |
257
|
|
|
*/ |
|
|
|
|
258
|
|
|
public function searchItem(Request $request) |
259
|
|
|
{ |
260
|
|
|
if (!$request->isXmlHttpRequest()) { |
261
|
|
|
throw new BadRequestHttpException(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$id = (int) $request->get('order-item-id'); |
265
|
|
|
/** @var OrderItem $OrderItem */ |
266
|
|
|
$OrderItem = $this->orderItemRepository->find($id); |
267
|
|
|
if (null === $OrderItem) { |
268
|
|
|
// not found. |
269
|
|
|
return $this->json([], 404); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return [ |
273
|
|
|
'OrderItem' => $OrderItem, |
274
|
|
|
]; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|