Completed
Push — master ( 851875...5c8cb5 )
by Kentaro
20s
created

OrderController::delete()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0092

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 26
nc 6
nop 3
dl 0
loc 43
ccs 22
cts 24
cp 0.9167
crap 4.0092
rs 8.5806
c 2
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Order;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Entity\Master\CsvType;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\StreamedResponse;
35
36
class OrderController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38
39 6
    public function index(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41
42 6
        $session = $request->getSession();
43
44 6
        $builder = $app['form.factory']
45 6
            ->createBuilder('admin_search_order');
46
47 6
        $event = new EventArgs(
48
            array(
49 6
                'builder' => $builder,
50
            ),
51
            $request
52
        );
53 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event);
54
55 6
        $searchForm = $builder->getForm();
56
57 6
        $pagination = array();
58
59 6
        $disps = $app['eccube.repository.master.disp']->findAll();
60 6
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
61 6
        $page_count = $app['config']['default_page_count'];
62 6
        $page_status = null;
63 6
        $active = false;
64
65 6
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
66
67 4
            $searchForm->handleRequest($request);
68
69 4 View Code Duplication
            if ($searchForm->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70 4
                $searchData = $searchForm->getData();
71
72
                // paginator
73 4
                $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
74
75 4
                $event = new EventArgs(
76
                    array(
77 4
                        'form' => $searchForm,
78 4
                        'qb' => $qb,
79
                    ),
80
                    $request
81
                );
82 4
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
83
84 4
                $page_no = 1;
85 4
                $pagination = $app['paginator']()->paginate(
86
                    $qb,
87
                    $page_no,
88
                    $page_count
89
                );
90
91
                // sessionのデータ保持
92 4
                $session->set('eccube.admin.order.search', $searchData);
93 4
                $session->set('eccube.admin.order.search.page_no', $page_no);
94
            }
95
        } else {
96 4
            if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) {
97
                // sessionを削除
98 2
                $session->remove('eccube.admin.order.search');
99 2
                $session->remove('eccube.admin.order.search.page_no');
100
            } else {
101
                // pagingなどの処理
102 2
                $searchData = $session->get('eccube.admin.order.search');
103 2
                if (is_null($page_no)) {
104
                    $page_no = intval($session->get('eccube.admin.order.search.page_no'));
105
                } else {
106 2
                    $session->set('eccube.admin.order.search.page_no', $page_no);
107
                }
108
109 2
                if (!is_null($searchData)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
110
111
                    // 公開ステータス
112 2
                    $status = $request->get('status');
113 2
                    if (!empty($status)) {
114
                        if ($status != $app['config']['admin_product_stock_status']) {
115
                            $searchData['status']->clear();
116
                            $searchData['status']->add($status);
117
                        } else {
118
                            $searchData['stock_status'] = $app['config']['disabled'];
119
                        }
120
                        $page_status = $status;
121
                    }
122
                    // 表示件数
123 2
                    $pcount = $request->get('page_count');
124
125 2
                    $page_count = empty($pcount) ? $page_count : $pcount;
126
127 2
                    $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
128
129 2
                    $event = new EventArgs(
130
                        array(
131 2
                            'form' => $searchForm,
132 2
                            'qb' => $qb,
133
                        ),
134
                        $request
135
                    );
136 2
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
137
138 2
                    $pagination = $app['paginator']()->paginate(
139
                        $qb,
140
                        $page_no,
141
                        $page_count
142
                    );
143
144
                    // セッションから検索条件を復元
145 2
                    if (!empty($searchData['status'])) {
146 2
                        $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']);
147
                    }
148 2 View Code Duplication
                    if (count($searchData['sex']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149 2
                        $sex_ids = array();
150 2
                        foreach ($searchData['sex'] as $Sex) {
151 2
                            $sex_ids[] = $Sex->getId();
152
                        }
153 2
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
154
                    }
155 2 View Code Duplication
                    if (count($searchData['payment']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156 2
                        $payment_ids = array();
157 2
                        foreach ($searchData['payment'] as $Payment) {
158 2
                            $payment_ids[] = $Payment->getId();
159
                        }
160 2
                        $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids));
161
                    }
162 2
                    $searchForm->setData($searchData);
163
                }
164
            }
165
        }
166
167 6
        return $app->render('Order/index.twig', array(
168 6
            'searchForm' => $searchForm->createView(),
169 6
            'pagination' => $pagination,
170 6
            'disps' => $disps,
171 6
            'pageMaxis' => $pageMaxis,
172 6
            'page_no' => $page_no,
173 6
            'page_status' => $page_status,
174 6
            'page_count' => $page_count,
175 6
            'active' => $active,
176
        ));
177
178
    }
179
180 2
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
181
    {
182 2
        $this->isTokenValid($app);
183 2
        $session = $request->getSession();
184 2
        $page_no = intval($session->get('eccube.admin.order.search.page_no'));
185 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
186
187 2
        $Order = $app['orm.em']->getRepository('Eccube\Entity\Order')
188 2
            ->find($id);
189
190 2
        if (!$Order) {
191
            $app->deleteMessage();
192
            return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
193
        }
194
195 2
        log_info('受注削除開始', array($Order->getId()));
196
197 2
        $Order->setDelFlg(Constant::ENABLED);
198
199 2
        $app['orm.em']->persist($Order);
200 2
        $app['orm.em']->flush();
201
202 2
        $Customer = $Order->getCustomer();
203 2
        if ($Customer) {
204
            // 会員の場合、購入回数、購入金額などを更新
205 2
            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $Order->getOrderStatus()->getId());
206
        }
207
208 2
        $event = new EventArgs(
209
            array(
210 2
                'Order' => $Order,
211 2
                'Customer' => $Customer,
212
            ),
213
            $request
214
        );
215 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_DELETE_COMPLETE, $event);
216
217 2
        $app->addSuccess('admin.order.delete.complete', 'admin');
218
219 2
        log_info('受注削除完了', array($Order->getId()));
220
221 2
        return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
222
    }
223
224
225
    /**
226
     * 受注CSVの出力.
227
     *
228
     * @param Application $app
229
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
230
     * @return StreamedResponse
231
     */
232
    public function exportOrder(Application $app, Request $request)
233
    {
234
235
        // タイムアウトを無効にする.
236
        set_time_limit(0);
237
238
        // sql loggerを無効にする.
239
        $em = $app['orm.em'];
240
        $em->getConfiguration()->setSQLLogger(null);
241
242
        $response = new StreamedResponse();
243
        $response->setCallback(function () use ($app, $request) {
244
245
            // CSV種別を元に初期化.
246
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_ORDER);
247
248
            // ヘッダ行の出力.
249
            $app['eccube.service.csv.export']->exportHeader();
250
251
            // 受注データ検索用のクエリビルダを取得.
252
            $qb = $app['eccube.service.csv.export']
253
                ->getOrderQueryBuilder($request);
254
255
            // データ行の出力.
256
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
257 View Code Duplication
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
259
                $Csvs = $csvService->getCsvs();
260
261
                $Order = $entity;
262
                $OrderDetails = $Order->getOrderDetails();
263
264
                foreach ($OrderDetails as $OrderDetail) {
265
                    $row = array();
266
267
                    // CSV出力項目と合致するデータを取得.
268
                    foreach ($Csvs as $Csv) {
269
                        // 受注データを検索.
270
                        $data = $csvService->getData($Csv, $Order);
271
                        if (is_null($data)) {
272
                            // 受注データにない場合は, 受注明細を検索.
273
                            $data = $csvService->getData($Csv, $OrderDetail);
274
                        }
275
                        $row[] = $data;
276
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
277
                    }
278
279
                    //$row[] = number_format(memory_get_usage(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
280
                    // 出力.
281
                    $csvService->fputcsv($row);
282
                }
283
            });
284
        });
285
286
        $now = new \DateTime();
287
        $filename = 'order_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
288
        $response->headers->set('Content-Type', 'application/octet-stream');
289
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
290
        $response->send();
291
292
        log_info('受注CSV出力ファイル名', array($filename));
293
294
        return $response;
295
    }
296
297
    /**
298
     * 配送CSVの出力.
299
     *
300
     * @param Application $app
301
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
302
     * @return StreamedResponse
303
     */
304
    public function exportShipping(Application $app, Request $request)
305
    {
306
        // タイムアウトを無効にする.
307
        set_time_limit(0);
308
309
        // sql loggerを無効にする.
310
        $em = $app['orm.em'];
311
        $em->getConfiguration()->setSQLLogger(null);
312
313
        $response = new StreamedResponse();
314
        $response->setCallback(function () use ($app, $request) {
315
316
            // CSV種別を元に初期化.
317
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_SHIPPING);
318
319
            // ヘッダ行の出力.
320
            $app['eccube.service.csv.export']->exportHeader();
321
322
            // 受注データ検索用のクエリビルダを取得.
323
            $qb = $app['eccube.service.csv.export']
324
                ->getOrderQueryBuilder($request);
325
326
            // データ行の出力.
327
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
328
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
329
330
                $Csvs = $csvService->getCsvs();
331
332
                /** @var $Order \Eccube\Entity\Order */
333
                $Order = $entity;
334
                /** @var $Shippings \Eccube\Entity\Shipping[] */
335
                $Shippings = $Order->getShippings();
336
337
                foreach ($Shippings as $Shipping) {
338
                    /** @var $ShipmentItems \Eccube\Entity\ShipmentItem */
339
                    $ShipmentItems = $Shipping->getShipmentItems();
340
                    foreach ($ShipmentItems as $ShipmentItem) {
341
                        $row = array();
342
343
                        // CSV出力項目と合致するデータを取得.
344
                        foreach ($Csvs as $Csv) {
345
                            // 受注データを検索.
346
                            $data = $csvService->getData($Csv, $Order);
347
                            if (is_null($data)) {
348
                                // 配送情報を検索.
349
                                $data = $csvService->getData($Csv, $Shipping);
350
                            }
351
                            if (is_null($data)) {
352
                                // 配送商品を検索.
353
                                $data = $csvService->getData($Csv, $ShipmentItem);
354
                            }
355
                            $row[] = $data;
356
                        }
357
                        //$row[] = number_format(memory_get_usage(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
358
                        // 出力.
359
                        $csvService->fputcsv($row);
360
                    }
361
                }
362
            });
363
        });
364
365
        $now = new \DateTime();
366
        $filename = 'shipping_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
367
        $response->headers->set('Content-Type', 'application/octet-stream');
368
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
369
        $response->send();
370
371
        log_info('配送CSV出力ファイル名', array($filename));
372
373
        return $response;
374
    }
375
}
376