Completed
Pull Request — master (#2018)
by
unknown
58:04 queued 17:00
created

AdminController::findOrderStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
c 0
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;
26
27
use Doctrine\ORM\NoResultException;
28
use Doctrine\ORM\Query\ResultSetMapping;
29
use Doctrine\ORM\QueryBuilder;
30
use Eccube\Application;
31
use Eccube\Common\Constant;
32
use Eccube\Controller\AbstractController;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Symfony\Component\HttpFoundation\Request;
36
37
class AdminController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39 6
    public function login(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41 6
        if ($app->isGranted('ROLE_ADMIN')) {
42
            return $app->redirect($app->url('admin_homepage'));
43
        }
44
45
        /* @var $form \Symfony\Component\Form\FormInterface */
46 6
        $builder = $app['form.factory']
47 6
            ->createNamedBuilder('', 'admin_login');
48
49 6
        $event = new EventArgs(
50
            array(
51 6
                'builder' => $builder,
52
            ),
53
            $request
54
        );
55 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_LOGIN_INITIALIZE, $event);
56
57 6
        $form = $builder->getForm();
58
59 6
        return $app->render('login.twig', array(
60 6
            'error' => $app['security.last_error']($request),
61 6
            'form' => $form->createView(),
62
        ));
63
    }
64
65 3
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
66
    {
67
        // install.phpのチェック.
68 3
        if (isset($app['config']['eccube_install']) && $app['config']['eccube_install'] == 1) {
69 3
            $file = $app['config']['root_dir'] . '/html/install.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
70 3 View Code Duplication
            if (file_exists($file)) {
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...
71 3
                $message = $app->trans('admin.install.warning', array('installphpPath' => 'html/install.php'));
72 3
                $app->addWarning($message, 'admin');
73
            }
74 3
            $fileOnRoot = $app['config']['root_dir'] . '/install.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
75 3 View Code Duplication
            if (file_exists($fileOnRoot)) {
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...
76
                $message = $app->trans('admin.install.warning', array('installphpPath' => 'install.php'));
77
                $app->addWarning($message, 'admin');
78
            }
79
        }
80
81
        // 受注マスター検索用フォーム
82 3
        $searchOrderBuilder = $app['form.factory']
83 3
            ->createBuilder('admin_search_order');
84
        // 商品マスター検索用フォーム
85 3
        $searchProductBuilder = $app['form.factory']
86 3
            ->createBuilder('admin_search_product');
87
        // 会員マスター検索用フォーム
88 3
        $searchCustomerBuilder = $app['form.factory']
89 3
            ->createBuilder('admin_search_customer');
90
91 3
        $event = new EventArgs(
92
            array(
93 3
                'searchOrderBuilder' => $searchOrderBuilder,
94 3
                'searchProductBuilder' => $searchProductBuilder,
95 3
                'searchCustomerBuilder' => $searchCustomerBuilder,
96
            ),
97
            $request
98
        );
99 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_INITIALIZE, $event);
100
101
        // 受注マスター検索用フォーム
102 3
        $searchOrderForm = $searchOrderBuilder->getForm();
103
104
        // 商品マスター検索用フォーム
105 3
        $searchProductForm = $searchProductBuilder->getForm();
106
107
        // 会員マスター検索用フォーム
108 3
        $searchCustomerForm = $searchCustomerBuilder->getForm();
109
110
        /**
111
         * 受注状況.
112
         */
113 3
        $excludes = array();
114 3
        $excludes[] = $app['config']['order_pending'];
115 3
        $excludes[] = $app['config']['order_processing'];
116 3
        $excludes[] = $app['config']['order_cancel'];
117 3
        $excludes[] = $app['config']['order_deliv'];
118
119 3
        $event = new EventArgs(
120
            array(
121 3
                'excludes' => $excludes,
122
            ),
123
            $request
124
        );
125 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_ORDER, $event);
126
127
        // 受注ステータスごとの受注件数.
128 3
        $Orders = $this->getOrderEachStatus($app['orm.em'], $excludes);
129
        // 受注ステータスの一覧.
130 3
        $OrderStatuses = $this->findOrderStatus($app['orm.em'], $excludes);
131
132
        /**
133
         * 売り上げ状況
134
         */
135 3
        $excludes = array();
136 3
        $excludes[] = $app['config']['order_processing'];
137 3
        $excludes[] = $app['config']['order_cancel'];
138 3
        $excludes[] = $app['config']['order_pending'];
139
140 3
        $event = new EventArgs(
141
            array(
142 3
                'excludes' => $excludes,
143
            ),
144
            $request
145
        );
146 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_SALES, $event);
147
148
        // 今日の売上/件数
149 3
        $salesToday = $this->getSalesByDay($app['orm.em'], new \DateTime(), $excludes);
150
        // 昨日の売上/件数
151 3
        $salesYesterday = $this->getSalesByDay($app['orm.em'], new \DateTime('-1 day'), $excludes);
152
        // 今月の売上/件数
153 3
        $salesThisMonth = $this->getSalesByMonth($app['orm.em'], new \DateTime(), $excludes);
154
155
        /**
156
         * ショップ状況
157
         */
158
        // 在庫切れ商品数
159 3
        $countNonStockProducts = $this->countNonStockProducts($app['orm.em']);
160
        // 本会員数
161 3
        $countCustomers = $this->countCustomers($app['orm.em']);
162
163 3
        $event = new EventArgs(
164
            array(
165 3
                'Orders' => $Orders,
166 3
                'OrderStatuses' => $OrderStatuses,
167 3
                'salesThisMonth' => $salesThisMonth,
168 3
                'salesToday' => $salesToday,
169 3
                'salesYesterday' => $salesYesterday,
170 3
                'countNonStockProducts' => $countNonStockProducts,
171 3
                'countCustomers' => $countCustomers,
172
            ),
173
            $request
174
        );
175 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_COMPLETE, $event);
176
177 3
        return $app->render('index.twig', array(
178 3
            'searchOrderForm' => $searchOrderForm->createView(),
179 3
            'searchProductForm' => $searchProductForm->createView(),
180 3
            'searchCustomerForm' => $searchCustomerForm->createView(),
181 3
            'Orders' => $Orders,
182 3
            'OrderStatuses' => $OrderStatuses,
183 3
            'salesThisMonth' => $salesThisMonth,
184 3
            'salesToday' => $salesToday,
185 3
            'salesYesterday' => $salesYesterday,
186 3
            'countNonStockProducts' => $countNonStockProducts,
187 3
            'countCustomers' => $countCustomers,
188
        ));
189
    }
190
191
    /**
192
     * パスワード変更画面
193
     *
194
     * @param Application $app
195
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
196
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
197
     */
198 3
    public function changePassword(Application $app, Request $request)
199
    {
200 3
        $builder = $app['form.factory']
201 3
            ->createBuilder('admin_change_password');
202
203 3
        $event = new EventArgs(
204
            array(
205 3
                'builder' => $builder,
206
            ),
207
            $request
208
        );
209 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_CHANGE_PASSWORD_INITIALIZE, $event);
210
211 3
        $form = $builder->getForm();
212 3
        $form->handleRequest($request);
213
214 3
        if ($form->isSubmitted() && $form->isValid()) {
215 1
            $password = $form->get('change_password')->getData();
216
217 1
            $Member = $app->user();
218
219 1
            $dummyMember = clone $Member;
220 1
            $dummyMember->setPassword($password);
221 1
            $salt = $dummyMember->getSalt();
222 1
            if (!isset($salt)) {
223
                $salt = $app['eccube.repository.member']->createSalt(5);
224
                $dummyMember->setSalt($salt);
225
            }
226
227 1
            $encryptPassword = $app['eccube.repository.member']->encryptPassword($dummyMember);
228
229
            $Member
230 1
                ->setPassword($encryptPassword)
231 1
                ->setSalt($salt);
232
233 1
            $status = $app['eccube.repository.member']->save($Member);
234 1
            if ($status) {
235 1
                $event = new EventArgs(
236
                    array(
237 1
                        'form' => $form,
238
                    ),
239
                    $request
240
                );
241 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIN_CHANGE_PASSWORD_COMPLETE, $event);
242
243 1
                $app->addSuccess('admin.change_password.save.complete', 'admin');
244
245 1
                return $app->redirect($app->url('admin_change_password'));
246
            }
247
248
            $app->addError('admin.change_password.save.error', 'admin');
249
        }
250
251 2
        return $app->render('change_password.twig', array(
252 2
            'form' => $form->createView(),
253
        ));
254
    }
255
256
    /**
257
     * 在庫なし商品の検索結果を表示する.
258
     *
259
     * @param Application $app
260
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
261
     * @return \Symfony\Component\HttpFoundation\Response
262
     */
263 2
    public function searchNonStockProducts(Application $app, Request $request)
264
    {
265
        // 商品マスター検索用フォーム
266 2
        $form = $app['form.factory']
267 2
            ->createBuilder('admin_search_product')
268 2
            ->getForm();
269
270 2
        if ('POST' === $request->getMethod()) {
271 2
            $form->handleRequest($request);
272
273 2
            if ($form->isValid()) {
274
                // 在庫なし商品の検索条件をセッションに付与し, 商品マスタへリダイレクトする.
275
                $searchData = array();
276
                $searchData['stock_status'] = Constant::DISABLED;
277
                $session = $request->getSession();
278
                $session->set('eccube.admin.product.search', $searchData);
279
280
                return $app->redirect($app->url('admin_product_page', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
281
                    'page_no' => 1,
282
                    'status' => $app['config']['admin_product_stock_status'])));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
283
            }
284
        }
285
286 2
        return $app->redirect($app->url('admin_homepage'));
287
    }
288
289 3
    protected function findOrderStatus($em, array $excludes)
290
    {
291
        /* @var $qb QueryBuilder */
292
        $qb = $em
293 3
            ->getRepository('Eccube\Entity\Master\OrderStatus')
294 3
            ->createQueryBuilder('os');
295
296
        return $qb
297 3
            ->where($qb->expr()->notIn('os.id', $excludes))
298 3
            ->orderBy('os.rank', 'ASC')
299 3
            ->getQuery()
300 3
            ->getResult();
301
    }
302
303 3
    protected function getOrderEachStatus($em, array $excludes)
304
    {
305
        $sql = 'SELECT
306
                    t1.status as status,
307
                    COUNT(t1.order_id) as count
308
                FROM
309
                    dtb_order t1
310
                WHERE
311
                    t1.del_flg = 0
312
                    AND t1.status NOT IN (:excludes)
313
                GROUP BY
314
                    t1.status
315
                ORDER BY
316 3
                    t1.status';
317 3
        $rsm = new ResultSetMapping();;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
318 3
        $rsm->addScalarResult('status', 'status');
319 3
        $rsm->addScalarResult('count', 'count');
320 3
        $query = $em->createNativeQuery($sql, $rsm);
321 3
        $query->setParameters(array(':excludes' => $excludes));
322 3
        $result = $query->getResult();
323 3
        $orderArray = array();
324 3
        foreach ($result as $row) {
325 3
            $orderArray[$row['status']] = $row['count'];
326
        }
327
328 3
        return $orderArray;
329
    }
330
331 3 View Code Duplication
    protected function getSalesByMonth($em, $dateTime, array $excludes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
332
    {
333
        // concat... for pgsql
334
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
335
        $dql = 'SELECT
336
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) AS order_month,
337
                  SUM(o.payment_total) AS order_amount,
338
                  COUNT(o) AS order_count
339
                FROM
340
                  Eccube\Entity\Order o
341
                WHERE
342
                    o.del_flg = 0
343
                    AND o.OrderStatus NOT IN (:excludes)
344
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) = SUBSTRING(:targetDate, 1, 7)
345
                GROUP BY
346 3
                  order_month';
347
348
        $q = $em
349 3
            ->createQuery($dql)
350 3
            ->setParameter(':excludes', $excludes)
351 3
            ->setParameter(':targetDate', $dateTime);
352
353 3
        $result = array();
354
        try {
355 3
            $result = $q->getSingleResult();
356 2
        } catch (NoResultException $e) {
357
            // 結果がない場合は空の配列を返す.
358
        }
359 3
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
360
    }
361
362 3 View Code Duplication
    protected function getSalesByDay($em, $dateTime, array $excludes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
363
    {
364
        // concat... for pgsql
365
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
366
        $dql = 'SELECT
367
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) AS order_day,
368
                  SUM(o.payment_total) AS order_amount,
369
                  COUNT(o) AS order_count
370
                FROM
371
                  Eccube\Entity\Order o
372
                WHERE
373
                    o.del_flg = 0
374
                    AND o.OrderStatus NOT IN (:excludes)
375
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) = SUBSTRING(:targetDate, 1, 10)
376
                GROUP BY
377 3
                  order_day';
378
379
        $q = $em
380 3
            ->createQuery($dql)
381 3
            ->setParameter(':excludes', $excludes)
382 3
            ->setParameter(':targetDate', $dateTime);
383
384 3
        $result = array();
385
        try {
386 3
            $result = $q->getSingleResult();
387 2
        } catch (NoResultException $e) {
388
            // 結果がない場合は空の配列を返す.
389
        }
390 3
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
391
    }
392
393 3
    protected function countNonStockProducts($em)
394
    {
395
        /** @var $qb \Doctrine\ORM\QueryBuilder */
396 3
        $qb = $em->getRepository('Eccube\Entity\Product')
397 3
            ->createQueryBuilder('p')
398 3
            ->select('count(p.id)')
399 3
            ->innerJoin('p.ProductClasses', 'pc')
400 3
            ->where('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
401 3
            ->setParameter('StockUnlimited', Constant::DISABLED);
402
403
        return $qb
404 3
            ->getQuery()
405 3
            ->getSingleScalarResult();
406
    }
407
408 3
    protected function countCustomers($em)
409
    {
410
        $Status = $em
411 3
            ->getRepository('Eccube\Entity\Master\CustomerStatus')
412 3
            ->find(2);
413
414
        /** @var $qb \Doctrine\ORM\QueryBuilder */
415 3
        $qb = $em->getRepository('Eccube\Entity\Customer')
416 3
            ->createQueryBuilder('c')
417 3
            ->select('count(c.id)')
418 3
            ->where('c.Status = :Status')
419 3
            ->setParameter('Status', $Status);
420
421
        return $qb
422 3
            ->getQuery()
423 3
            ->getSingleScalarResult();
424
    }
425
}