These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of EC-CUBE |
||
5 | * |
||
6 | * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. |
||
7 | * |
||
8 | * http://www.ec-cube.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\Order; |
||
15 | |||
16 | use Doctrine\Common\Collections\ArrayCollection; |
||
17 | use Eccube\Controller\AbstractController; |
||
18 | use Eccube\Entity\Order; |
||
19 | use Eccube\Entity\OrderItem; |
||
20 | use Eccube\Entity\Shipping; |
||
21 | use Eccube\Form\Type\Admin\SearchProductType; |
||
22 | use Eccube\Form\Type\Admin\ShippingType; |
||
23 | use Eccube\Repository\CategoryRepository; |
||
24 | use Eccube\Repository\DeliveryRepository; |
||
25 | use Eccube\Repository\OrderItemRepository; |
||
26 | use Eccube\Repository\ShippingRepository; |
||
27 | use Eccube\Service\MailService; |
||
28 | use Eccube\Service\OrderStateMachine; |
||
29 | use Eccube\Service\TaxRuleService; |
||
30 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
||
31 | use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
||
32 | use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
||
33 | use Symfony\Component\Form\FormEvent; |
||
34 | use Symfony\Component\Form\FormEvents; |
||
35 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
36 | use Symfony\Component\HttpFoundation\Request; |
||
37 | use Symfony\Component\HttpFoundation\Response; |
||
38 | use Symfony\Component\Routing\Annotation\Route; |
||
39 | use Symfony\Component\Serializer\SerializerInterface; |
||
40 | |||
41 | class ShippingController extends AbstractController |
||
42 | { |
||
43 | /** |
||
44 | * @var OrderItemRepository |
||
45 | */ |
||
46 | protected $orderItemRepository; |
||
47 | |||
48 | /** |
||
49 | * @var CategoryRepository |
||
50 | */ |
||
51 | protected $categoryRepository; |
||
52 | |||
53 | /** |
||
54 | * @var DeliveryRepository |
||
55 | */ |
||
56 | protected $deliveryRepository; |
||
57 | |||
58 | /** |
||
59 | * @var TaxRuleService |
||
60 | */ |
||
61 | protected $taxRuleService; |
||
62 | |||
63 | /** |
||
64 | * @var ShippingRepository |
||
65 | */ |
||
66 | protected $shippingRepository; |
||
67 | |||
68 | /** |
||
69 | * @var SerializerInterface |
||
70 | */ |
||
71 | protected $serializer; |
||
72 | |||
73 | /** |
||
74 | * @var \Eccube\Service\MailService |
||
75 | */ |
||
76 | protected $mailService; |
||
77 | |||
78 | /** |
||
79 | * @var OrderStateMachine |
||
80 | */ |
||
81 | private $orderStateMachine; |
||
82 | |||
83 | /** |
||
84 | * EditController constructor. |
||
85 | * |
||
86 | * @param MailService $mailService |
||
87 | * @param OrderItemRepository $orderItemRepository |
||
88 | * @param CategoryRepository $categoryRepository |
||
89 | * @param DeliveryRepository $deliveryRepository |
||
90 | * @param TaxRuleService $taxRuleService |
||
91 | * @param ShippingRepository $shippingRepository |
||
92 | * @param SerializerInterface $serializer |
||
93 | * @param OrderStateMachine $orderStateMachine |
||
94 | */ |
||
95 | public function __construct( |
||
96 | 6 | MailService $mailService, |
|
97 | OrderItemRepository $orderItemRepository, |
||
98 | CategoryRepository $categoryRepository, |
||
99 | DeliveryRepository $deliveryRepository, |
||
100 | TaxRuleService $taxRuleService, |
||
101 | ShippingRepository $shippingRepository, |
||
102 | SerializerInterface $serializer, |
||
103 | OrderStateMachine $orderStateMachine |
||
104 | ) { |
||
105 | $this->mailService = $mailService; |
||
106 | 6 | $this->orderItemRepository = $orderItemRepository; |
|
107 | 6 | $this->categoryRepository = $categoryRepository; |
|
108 | 6 | $this->deliveryRepository = $deliveryRepository; |
|
109 | 6 | $this->taxRuleService = $taxRuleService; |
|
110 | 6 | $this->shippingRepository = $shippingRepository; |
|
111 | 6 | $this->serializer = $serializer; |
|
112 | 6 | $this->orderStateMachine = $orderStateMachine; |
|
113 | 6 | } |
|
114 | |||
115 | /** |
||
116 | * 出荷登録/編集画面. |
||
117 | * |
||
118 | * @Route("/%eccube_admin_route%/shipping/{id}/edit", requirements={"id" = "\d+"}, name="admin_shipping_edit") |
||
119 | * @Template("@admin/Order/shipping.twig") |
||
120 | */ |
||
121 | public function index(Request $request, Order $Order) |
||
122 | 3 | { |
|
123 | $TargetShippings = $Order->getShippings(); |
||
124 | 3 | ||
125 | // 編集前の受注情報を保持 |
||
126 | $OriginShippings = new ArrayCollection(); |
||
127 | 3 | $OriginOrderItems = []; |
|
128 | 3 | ||
129 | foreach ($TargetShippings as $key => $TargetShipping) { |
||
130 | 3 | $OriginShippings->add($TargetShipping); |
|
131 | 3 | ||
132 | // 編集前のお届け先のアイテム情報を保持 |
||
133 | $OriginOrderItems[$key] = new ArrayCollection(); |
||
134 | 3 | ||
135 | foreach ($TargetShipping->getOrderItems() as $OrderItem) { |
||
136 | 3 | $OriginOrderItems[$key]->add($OrderItem); |
|
137 | 3 | } |
|
138 | } |
||
139 | |||
140 | $builder = $this->formFactory->createBuilder(); |
||
141 | 3 | $builder |
|
142 | ->add('shippings', CollectionType::class, [ |
||
143 | 3 | 'entry_type' => ShippingType::class, |
|
144 | 3 | 'data' => $TargetShippings, |
|
145 | 3 | 'allow_add' => true, |
|
146 | 'allow_delete' => true, |
||
147 | 'prototype' => true, |
||
148 | ]); |
||
149 | |||
150 | // 配送先の追加フラグ |
||
151 | $builder |
||
152 | ->add('add_shipping', HiddenType::class, [ |
||
153 | 3 | 'mapped' => false, |
|
154 | 3 | ]); |
|
155 | |||
156 | // 配送先の追加フラグが立っている場合は新しいお届け先を追加 |
||
157 | $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { |
||
158 | 3 | $data = $event->getData(); |
|
159 | 3 | if ($data['add_shipping']) { |
|
160 | 3 | $Shippings = $data['shippings']; |
|
161 | 1 | $newShipping = ['Delivery' => '']; |
|
162 | 1 | $Shippings[] = $newShipping; |
|
163 | 1 | $data['shippings'] = $Shippings; |
|
164 | 1 | $data['add_shipping'] = ''; |
|
165 | 1 | $event->setData($data); |
|
166 | 1 | } |
|
167 | }); |
||
168 | 3 | ||
169 | $form = $builder->getForm(); |
||
170 | 3 | ||
171 | $form->handleRequest($request); |
||
172 | 3 | ||
173 | if ($form->isSubmitted() && $form->isValid() && $request->get('mode') == 'register') { |
||
174 | 3 | log_info('出荷登録開始', [$TargetShipping->getId()]); |
|
0 ignored issues
–
show
|
|||
175 | 3 | ||
176 | // 削除された項目の削除 |
||
177 | /** @var Shipping $OriginShipping */ |
||
178 | foreach ($OriginShippings as $key => $OriginShipping) { |
||
179 | 3 | if (false === $TargetShippings->contains($OriginShipping)) { |
|
180 | 3 | // お届け先自体が削除された場合 |
|
181 | // 削除されたお届け先に紐づく明細の削除 |
||
182 | /** @var OrderItem $OriginOrderItem */ |
||
183 | foreach ($OriginOrderItems[$key] as $OriginOrderItem) { |
||
184 | 1 | $this->entityManager->remove($OriginOrderItem); |
|
185 | 1 | } |
|
186 | |||
187 | // 削除されたお届け先の削除 |
||
188 | $this->entityManager->remove($OriginShipping); |
||
189 | 1 | } else { |
|
190 | // お届け先は削除されていない場合 |
||
191 | // 削除された明細の削除 |
||
192 | /** @var OrderItem $OriginOrderItem */ |
||
193 | foreach ($OriginOrderItems[$key] as $OriginOrderItem) { |
||
194 | 3 | if (false === $TargetShippings[$key]->getOrderItems()->contains($OriginOrderItem)) { |
|
195 | 3 | $this->entityManager->remove($OriginOrderItem); |
|
196 | 3 | } |
|
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | // 追加された項目の追加 |
||
202 | foreach ($TargetShippings as $TargetShipping) { |
||
203 | 3 | // 追加された明細の追加 |
|
204 | foreach ($TargetShipping->getOrderItems() as $OrderItem) { |
||
205 | 3 | $OrderItem->setShipping($TargetShipping); |
|
206 | 3 | $OrderItem->setOrder($Order); |
|
207 | 3 | } |
|
208 | |||
209 | // 追加されたお届け先の追加 |
||
210 | $TargetShipping->setOrder($Order); |
||
211 | 3 | } |
|
212 | |||
213 | try { |
||
214 | foreach ($TargetShippings as $TargetShipping) { |
||
215 | 3 | $this->entityManager->persist($TargetShipping); |
|
216 | 3 | } |
|
217 | $this->entityManager->flush(); |
||
218 | 3 | ||
219 | $this->addInfo('admin.order.shipping_save_message', 'admin'); |
||
220 | 3 | $this->addSuccess('admin.common.save_complete', 'admin'); |
|
221 | 3 | log_info('出荷登録完了', [$Order->getId()]); |
|
222 | 3 | ||
223 | return $this->redirectToRoute('admin_shipping_edit', ['id' => $Order->getId()]); |
||
224 | 3 | } catch (\Exception $e) { |
|
225 | log_error('出荷登録エラー', [$Order->getId(), $e]); |
||
226 | $this->addError('admin.flash.register_failed', 'admin'); |
||
227 | } |
||
228 | } elseif ($form->isSubmitted() && $request->get('mode') == 'register' && $form->getErrors(true)) { |
||
229 | 3 | $this->addError('admin.common.save_error', 'admin'); |
|
230 | 1 | } |
|
231 | |||
232 | // 商品検索フォーム |
||
233 | $builder = $this->formFactory |
||
234 | 3 | ->createBuilder(SearchProductType::class); |
|
235 | 3 | ||
236 | $searchProductModalForm = $builder->getForm(); |
||
237 | 3 | ||
238 | // 配送業者のお届け時間 |
||
239 | $times = []; |
||
240 | 3 | $deliveries = $this->deliveryRepository->findAll(); |
|
241 | 3 | View Code Duplication | foreach ($deliveries as $Delivery) { |
242 | 3 | $deliveryTimes = $Delivery->getDeliveryTimes(); |
|
243 | 3 | foreach ($deliveryTimes as $DeliveryTime) { |
|
244 | 3 | $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime(); |
|
245 | 3 | } |
|
246 | } |
||
247 | |||
248 | return [ |
||
249 | 'form' => $form->createView(), |
||
250 | 3 | 'searchProductModalForm' => $searchProductModalForm->createView(), |
|
251 | 3 | 'Order' => $Order, |
|
252 | 3 | 'shippingDeliveryTimes' => $this->serializer->serialize($times, 'json'), |
|
253 | 3 | ]; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * @Route("/%eccube_admin_route%/shipping/preview_notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_preview_notify_mail") |
||
258 | * |
||
259 | * @param Shipping $Shipping |
||
260 | * |
||
261 | * @return Response |
||
262 | * |
||
263 | * @throws \Twig_Error |
||
264 | */ |
||
265 | public function previewShippingNotifyMail(Shipping $Shipping) |
||
266 | { |
||
267 | return new Response($this->mailService->getShippingNotifyMailBody($Shipping, $Shipping->getOrder(), null, true)); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @Route("/%eccube_admin_route%/shipping/notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail", methods={"PUT"}) |
||
272 | * |
||
273 | * @param Shipping $Shipping |
||
274 | * |
||
275 | * @return JsonResponse |
||
276 | * |
||
277 | * @throws \Twig_Error |
||
278 | */ |
||
279 | public function notifyMail(Shipping $Shipping) |
||
280 | { |
||
281 | 2 | $this->isTokenValid(); |
|
282 | |||
283 | 2 | $this->mailService->sendShippingNotifyMail($Shipping); |
|
284 | |||
285 | 2 | $Shipping->setMailSendDate(new \DateTime()); |
|
286 | $this->shippingRepository->save($Shipping); |
||
287 | 2 | $this->entityManager->flush(); |
|
288 | 2 | ||
289 | 2 | return $this->json([ |
|
290 | 'mail' => true, |
||
291 | 2 | 'shipped' => false, |
|
292 | 2 | ]); |
|
293 | } |
||
294 | } |
||
295 |
It seems like you are relying on a variable being defined by an iteration: