Completed
Push — master ( e81574...e39dc3 )
by Ryo
313:38 queued 307:07
created

Eccube/Controller/Admin/Order/OrderController.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
37
{
38
39 9
    public function index(Application $app, Request $request, $page_no = null)
40
    {
41
42 9
        $session = $request->getSession();
43
44 9
        $builder = $app['form.factory']
45 9
            ->createBuilder('admin_search_order');
46
47 9
        $event = new EventArgs(
48
            array(
49 9
                'builder' => $builder,
50
            ),
51
            $request
52
        );
53 9
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event);
54
55 9
        $searchForm = $builder->getForm();
56
57 9
        $pagination = array();
58
59 9
        $disps = $app['eccube.repository.master.disp']->findAll();
60 9
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
61 9
        $page_count = $app['config']['default_page_count'];
62 9
        $page_status = null;
63 9
        $active = false;
64
65 9
        if ('POST' === $request->getMethod()) {
66
67 7
            $searchForm->handleRequest($request);
68
69 7
            if ($searchForm->isValid()) {
70 7
                $searchData = $searchForm->getData();
71
72
                // paginator
73 7
                $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
74
75 7
                $event = new EventArgs(
76
                    array(
77 7
                        'form' => $searchForm,
78 7
                        'qb' => $qb,
79
                    ),
80
                    $request
81
                );
82 7
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
83
84 7
                $page_no = 1;
85 7
                $pagination = $app['paginator']()->paginate(
86
                    $qb,
87
                    $page_no,
88
                    $page_count
89
                );
90
91
                // sessionのデータ保持
92 7
                $session->set('eccube.admin.order.search', $searchData);
93 7
                $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)) {
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
                    if (count($searchData['multi_status']) > 0) {
149
                        $statusIds = array();
150
                        foreach ($searchData['multi_status'] as $Status) {
151
                            $statusIds[] = $Status->getId();
152
                        }
153
                        $searchData['multi_status'] = $app['eccube.repository.master.order_status']->findBy(array('id' => $statusIds));
154
                    }
155 2 View Code Duplication
                    if (count($searchData['sex']) > 0) {
156 2
                        $sex_ids = array();
157 2
                        foreach ($searchData['sex'] as $Sex) {
158 2
                            $sex_ids[] = $Sex->getId();
159
                        }
160 2
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
161
                    }
162 2 View Code Duplication
                    if (count($searchData['payment']) > 0) {
163 2
                        $payment_ids = array();
164 2
                        foreach ($searchData['payment'] as $Payment) {
165 2
                            $payment_ids[] = $Payment->getId();
166
                        }
167 2
                        $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids));
168
                    }
169 2
                    $searchForm->setData($searchData);
170
                }
171
            }
172
        }
173
174 9
        return $app->render('Order/index.twig', array(
175 9
            'searchForm' => $searchForm->createView(),
176 9
            'pagination' => $pagination,
177 9
            'disps' => $disps,
178 9
            'pageMaxis' => $pageMaxis,
179 9
            'page_no' => $page_no,
180 9
            'page_status' => $page_status,
181 9
            'page_count' => $page_count,
182 9
            'active' => $active,
183
        ));
184
185
    }
186
187 2
    public function delete(Application $app, Request $request, $id)
188
    {
189 2
        $this->isTokenValid($app);
190 2
        $session = $request->getSession();
191 2
        $page_no = intval($session->get('eccube.admin.order.search.page_no'));
192 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
193
194 2
        $Order = $app['orm.em']->getRepository('Eccube\Entity\Order')
195 2
            ->find($id);
196
197 2
        if (!$Order) {
198
            $app->deleteMessage();
199
            return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
200
        }
201
202 2
        log_info('受注削除開始', array($Order->getId()));
203
204 2
        $Order->setDelFlg(Constant::ENABLED);
205
206 2
        $app['orm.em']->persist($Order);
207 2
        $app['orm.em']->flush();
208
209 2
        $Customer = $Order->getCustomer();
210 2
        if ($Customer) {
211
            // 会員の場合、購入回数、購入金額などを更新
212 2
            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $Order->getOrderStatus()->getId());
213
        }
214
215 2
        $event = new EventArgs(
216
            array(
217 2
                'Order' => $Order,
218 2
                'Customer' => $Customer,
219
            ),
220
            $request
221
        );
222 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_DELETE_COMPLETE, $event);
223
224 2
        $app->addSuccess('admin.order.delete.complete', 'admin');
225
226 2
        log_info('受注削除完了', array($Order->getId()));
227
228 2
        return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
229
    }
230
231
232
    /**
233
     * 受注CSVの出力.
234
     *
235
     * @param Application $app
236
     * @param Request $request
237
     * @return StreamedResponse
238
     */
239
    public function exportOrder(Application $app, Request $request)
240
    {
241
242
        // タイムアウトを無効にする.
243
        set_time_limit(0);
244
245
        // sql loggerを無効にする.
246
        $em = $app['orm.em'];
247
        $em->getConfiguration()->setSQLLogger(null);
248
249
        $response = new StreamedResponse();
250
        $response->setCallback(function () use ($app, $request) {
251
252
            // CSV種別を元に初期化.
253
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_ORDER);
254
255
            // ヘッダ行の出力.
256
            $app['eccube.service.csv.export']->exportHeader();
257
258
            // 受注データ検索用のクエリビルダを取得.
259
            $qb = $app['eccube.service.csv.export']
260
                ->getOrderQueryBuilder($request);
261
262
            // データ行の出力.
263
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
264 View Code Duplication
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
265
266
                $Csvs = $csvService->getCsvs();
267
268
                $Order = $entity;
269
                $OrderDetails = $Order->getOrderDetails();
270
271
                foreach ($OrderDetails as $OrderDetail) {
272
                    $row = array();
273
274
                    // CSV出力項目と合致するデータを取得.
275
                    foreach ($Csvs as $Csv) {
276
                        // 受注データを検索.
277
                        $data = $csvService->getData($Csv, $Order);
278
                        if (is_null($data)) {
279
                            // 受注データにない場合は, 受注明細を検索.
280
                            $data = $csvService->getData($Csv, $OrderDetail);
281
                        }
282
                        $row[] = $data;
283
0 ignored issues
show
Blank line found at end of control structure
Loading history...
284
                    }
285
286
                    //$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...
287
                    // 出力.
288
                    $csvService->fputcsv($row);
289
                }
290
            });
291
        });
292
293
        $now = new \DateTime();
294
        $filename = 'order_' . $now->format('YmdHis') . '.csv';
295
        $response->headers->set('Content-Type', 'application/octet-stream');
296
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Concat operator must not be surrounded by spaces
Loading history...
297
        $response->send();
298
299
        log_info('受注CSV出力ファイル名', array($filename));
300
301
        return $response;
302
    }
303
304
    /**
305
     * 配送CSVの出力.
306
     *
307
     * @param Application $app
308
     * @param Request $request
0 ignored issues
show
Expected 5 spaces after parameter type; 1 found
Loading history...
309
     * @return StreamedResponse
310
     */
311
    public function exportShipping(Application $app, Request $request)
312
    {
313
        // タイムアウトを無効にする.
314
        set_time_limit(0);
315
316
        // sql loggerを無効にする.
317
        $em = $app['orm.em'];
318
        $em->getConfiguration()->setSQLLogger(null);
319
320
        $response = new StreamedResponse();
321
        $response->setCallback(function () use ($app, $request) {
322
323
            // CSV種別を元に初期化.
324
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_SHIPPING);
325
326
            // ヘッダ行の出力.
327
            $app['eccube.service.csv.export']->exportHeader();
328
329
            // 受注データ検索用のクエリビルダを取得.
330
            $qb = $app['eccube.service.csv.export']
331
                ->getOrderQueryBuilder($request);
332
333
            // データ行の出力.
334
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
335
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
336
337
                $Csvs = $csvService->getCsvs();
338
339
                /** @var $Order \Eccube\Entity\Order */
340
                $Order = $entity;
341
                /** @var $Shippings \Eccube\Entity\Shipping[] */
342
                $Shippings = $Order->getShippings();
343
344
                foreach ($Shippings as $Shipping) {
345
                    /** @var $ShipmentItems \Eccube\Entity\ShipmentItem */
346
                    $ShipmentItems = $Shipping->getShipmentItems();
347
                    foreach ($ShipmentItems as $ShipmentItem) {
348
                        $row = array();
349
350
                        // CSV出力項目と合致するデータを取得.
351
                        foreach ($Csvs as $Csv) {
352
                            // 受注データを検索.
353
                            $data = $csvService->getData($Csv, $Order);
354
                            if (is_null($data)) {
355
                                // 配送情報を検索.
356
                                $data = $csvService->getData($Csv, $Shipping);
357
                            }
358
                            if (is_null($data)) {
359
                                // 配送商品を検索.
360
                                $data = $csvService->getData($Csv, $ShipmentItem);
361
                            }
362
                            $row[] = $data;
363
                        }
364
                        //$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...
365
                        // 出力.
366
                        $csvService->fputcsv($row);
367
                    }
368
                }
369
            });
370
        });
371
372
        $now = new \DateTime();
373
        $filename = 'shipping_' . $now->format('YmdHis') . '.csv';
374
        $response->headers->set('Content-Type', 'application/octet-stream');
375
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Concat operator must not be surrounded by spaces
Loading history...
376
        $response->send();
377
378
        log_info('配送CSV出力ファイル名', array($filename));
379
380
        return $response;
381
    }
382
}
383