Failed Conditions
Pull Request — experimental/sf (#3385)
by chihiro
57:31 queued 48:54
created

EditController::searchItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 8
cp 0
crap 12
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
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
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
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
76
     * @param OrderItemRepository $orderItemRepository
77
     * @param CategoryRepository $categoryRepository
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
78
     * @param DeliveryRepository $deliveryRepository
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
79
     * @param TaxRuleService $taxRuleService
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
80
     * @param ShippingRepository $shippingRepository
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
81
     * @param SerializerInterface $serializer
82
     */
83 3
    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 3
        $this->mailService = $mailService;
93 3
        $this->orderItemRepository = $orderItemRepository;
94 3
        $this->categoryRepository = $categoryRepository;
95 3
        $this->deliveryRepository = $deliveryRepository;
96 3
        $this->taxRuleService = $taxRuleService;
97 3
        $this->shippingRepository = $shippingRepository;
98 3
        $this->serializer = $serializer;
99
    }
100
101
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
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
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
108 3
    public function edit(Request $request, $id = null)
109
    {
110 3
        $TargetShipping = null;
0 ignored issues
show
Unused Code introduced by
$TargetShipping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111 3
        $OriginShipping = null;
0 ignored issues
show
Unused Code introduced by
$OriginShipping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113 3
        if (null === $id) {
114
            // 空のエンティティを作成.
115 2
            $TargetShipping = new Shipping();
116
        } else {
117 2
            $TargetShipping = $this->shippingRepository->find($id);
118 2
            if (null === $TargetShipping) {
119
                throw new NotFoundHttpException();
120
            }
121
        }
122
123
        // 編集前の受注情報を保持
124 3
        $OriginShipping = clone $TargetShipping;
125
        // 編集前のお届け先のアイテム情報を保持
126 3
        $OriginalOrderItems = new ArrayCollection();
127
128 3
        foreach ($TargetShipping->getOrderItems() as $OrderItem) {
129 1
            $OriginalOrderItems->add($OrderItem);
130
        }
131
132 3
        $builder = $this->formFactory
133 3
            ->createBuilder(ShippingType::class, $TargetShipping);
134
135 3
        $form = $builder->getForm();
136 3
        $form->handleRequest($request);
137
138 3
        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 2
            $OrderItems = $TargetShipping->getOrderItems();
144 2
            $taxtotal = 0;
145 2
            foreach ($OrderItems as $OrderItem) {
146 1
                $tax = $this->taxRuleService
147 1
                    ->calcTax($OrderItem->getPrice(), $OrderItem->getTaxRate(), $OrderItem->getTaxRule());
148 1
                $OrderItem->setPriceIncTax($OrderItem->getPrice() + $tax);
149
150 1
                $taxtotal += $tax * $OrderItem->getQuantity();
151
            }
152
153 2
            log_info('出荷登録開始', [$TargetShipping->getId()]);
154
            // TODO 在庫の有無や販売制限数のチェックなども行う必要があるため、完了処理もcaluclatorのように抽象化できないか検討する.
155
            // TODO 後続にある会員情報の更新のように、完了処理もcaluclatorのように抽象化できないか検討する.
156
            // 画面上で削除された明細をremove
157 2
            foreach ($OriginalOrderItems as $OrderItem) {
158 1
                if (false === $TargetShipping->getOrderItems()->contains($OrderItem)) {
159 1
                    $OrderItem->setShipping(null);
160
                }
161
            }
162
163 2
            foreach ($TargetShipping->getOrderItems() as $OrderItem) {
164 1
                $OrderItem->setShipping($TargetShipping);
165
            }
166
167
            // 出荷ステータス変更時の処理
168 2
            if ($TargetShipping->isShipped()) {
169
                // 「出荷済み」にステータスが変更された場合
170
                if ($OriginShipping->isShipped() == false) {
171
                    // 出荷メールを送信
172
                    if ($form->get('notify_email')->getData()) {
173
                        try {
174
                            $this->mailService->sendShippingNotifyMail(
175
                              $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 2
                $this->entityManager->persist($TargetShipping);
190 2
                $this->entityManager->flush();
191
192 2
                $this->addSuccess('admin.shipping.edit.save.complete', 'admin');
193 2
                $this->addInfo('admin.shipping.edit.save.info', 'admin');
194 2
                log_info('出荷登録完了', [$TargetShipping->getId()]);
195
196 2
                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 3
        } elseif ($form->isSubmitted() && $form->getErrors(true)) {
202
            $this->addError('admin.flash.register_failed', 'admin');
203
        }
204
205
        // 配送業者のお届け時間
206 3
        $times = [];
207 3
        $deliveries = $this->deliveryRepository->findAll();
208 3 View Code Duplication
        foreach ($deliveries as $Delivery) {
209 3
            $deliveryTiems = $Delivery->getDeliveryTimes();
210 3
            foreach ($deliveryTiems as $DeliveryTime) {
211 3
                $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
212
            }
213
        }
214
215
        return [
216 3
            'form' => $form->createView(),
217 3
            'Shipping' => $TargetShipping,
218 3
            'shippingDeliveryTimes' => $this->serializer->serialize($times, 'json'),
219
        ];
220
    }
221
222
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$paginator" missing
Loading history...
223
     * @Route("/%eccube_admin_route%/shipping/search/product", name="admin_shipping_search_product")
224
     * @Template("@admin/Shipping/search_product.twig")
225
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
255
     * @Route("/%eccube_admin_route%/shipping/search/item", name="admin_shipping_search_item")
256
     * @Template("@admin/Shipping/order_item_prototype.twig")
257
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
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