Completed
Push — master ( da479e...8daf89 )
by Ryo
282:35 queued 275:43
created

AdminController::findOrderStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
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 Eccube\Application;
30
use Eccube\Common\Constant;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Symfony\Component\HttpFoundation\Request;
35
36
class AdminController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38 6
    public function login(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40 6
        if ($app->isGranted('ROLE_ADMIN')) {
41
            return $app->redirect($app->url('admin_homepage'));
42
        }
43
44
        /* @var $form \Symfony\Component\Form\FormInterface */
45 6
        $builder = $app['form.factory']
46 6
            ->createNamedBuilder('', 'admin_login');
47
48 6
        $event = new EventArgs(
49
            array(
50 6
                'builder' => $builder,
51 6
            ),
52
            $request
53 6
        );
54 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_LOGIN_INITIALIZE, $event);
55
56 6
        $form = $builder->getForm();
57
58 6
        return $app->render('login.twig', array(
59 6
            'error' => $app['security.last_error']($request),
60 6
            'form' => $form->createView(),
61 6
        ));
62
    }
63
64 9
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
65
    {
66
        // install.phpのチェック.
67 3
        if (isset($app['config']['eccube_install']) && $app['config']['eccube_install'] == 1) {
68 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...
69 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...
70 3
                $message = $app->trans('admin.install.warning', array('installphpPath' => 'html/install.php'));
71 3
                $app->addWarning($message, 'admin');
72 3
            }
73 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...
74 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...
75
                $message = $app->trans('admin.install.warning', array('installphpPath' => 'install.php'));
76
                $app->addWarning($message, 'admin');
77
            }
78 3
        }
79
80
        // 受注マスター検索用フォーム
81 3
        $searchOrderBuilder = $app['form.factory']
82 3
            ->createBuilder('admin_search_order');
83
        // 商品マスター検索用フォーム
84 3
        $searchProductBuilder = $app['form.factory']
85 3
            ->createBuilder('admin_search_product');
86
        // 会員マスター検索用フォーム
87 3
        $searchCustomerBuilder = $app['form.factory']
88 3
            ->createBuilder('admin_search_customer');
89
90 3
        $event = new EventArgs(
91
            array(
92 3
                'searchOrderBuilder' => $searchOrderBuilder,
93 3
                'searchProductBuilder' => $searchProductBuilder,
94 3
                'searchCustomerBuilder' => $searchCustomerBuilder,
95 3
            ),
96
            $request
97 3
        );
98 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_INITIALIZE, $event);
99
100
        // 受注マスター検索用フォーム
101 3
        $searchOrderForm = $searchOrderBuilder->getForm();
102
103
        // 商品マスター検索用フォーム
104 3
        $searchProductForm = $searchProductBuilder->getForm();
105
106
        // 会員マスター検索用フォーム
107 3
        $searchCustomerForm = $searchCustomerBuilder->getForm();
108
109
        /**
110
         * 受注状況.
111
         */
112 3
        $excludes = array();
113 3
        $excludes[] = $app['config']['order_pending'];
114 3
        $excludes[] = $app['config']['order_processing'];
115 3
        $excludes[] = $app['config']['order_cancel'];
116 3
        $excludes[] = $app['config']['order_deliv'];
117
118 3
        $event = new EventArgs(
119
            array(
120 3
                'excludes' => $excludes,
121 3
            ),
122
            $request
123 3
        );
124 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_ORDER, $event);
125
126
        // 受注ステータスごとの受注件数.
127 3
        $Orders = $this->getOrderEachStatus($app['orm.em'], $excludes);
128
        // 受注ステータスの一覧.
129 3
        $OrderStatuses = $this->findOrderStatus($app['orm.em'], $excludes);
130
131
        /**
132
         * 売り上げ状況
133
         */
134 3
        $excludes = array();
135 3
        $excludes[] = $app['config']['order_processing'];
136 3
        $excludes[] = $app['config']['order_cancel'];
137 3
        $excludes[] = $app['config']['order_pending'];
138
139 3
        $event = new EventArgs(
140
            array(
141 3
                'excludes' => $excludes,
142 3
            ),
143
            $request
144 3
        );
145 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_SALES, $event);
146
147
        // 今日の売上/件数
148 3
        $salesToday = $this->getSalesByDay($app['orm.em'], new \DateTime(), $excludes);
149
        // 昨日の売上/件数
150 3
        $salesYesterday = $this->getSalesByDay($app['orm.em'], new \DateTime('-1 day'), $excludes);
151
        // 今月の売上/件数
152 3
        $salesThisMonth = $this->getSalesByMonth($app['orm.em'], new \DateTime(), $excludes);
153
154
        /**
155
         * ショップ状況
156
         */
157
        // 在庫切れ商品数
158 3
        $countNonStockProducts = $this->countNonStockProducts($app['orm.em']);
159
        // 本会員数
160 3
        $countCustomers = $this->countCustomers($app['orm.em']);
161
162 9
        $event = new EventArgs(
163
            array(
164 3
                'Orders' => $Orders,
165 3
                'OrderStatuses' => $OrderStatuses,
166 3
                'salesThisMonth' => $salesThisMonth,
167 3
                'salesToday' => $salesToday,
168 3
                'salesYesterday' => $salesYesterday,
169 3
                'countNonStockProducts' => $countNonStockProducts,
170 3
                'countCustomers' => $countCustomers,
171 3
            ),
172
            $request
173 3
        );
174 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_COMPLETE, $event);
175
176 3
        return $app->render('index.twig', array(
177 3
            'searchOrderForm' => $searchOrderForm->createView(),
178 3
            'searchProductForm' => $searchProductForm->createView(),
179 3
            'searchCustomerForm' => $searchCustomerForm->createView(),
180 3
            'Orders' => $Orders,
181 3
            'OrderStatuses' => $OrderStatuses,
182 3
            'salesThisMonth' => $salesThisMonth,
183 3
            'salesToday' => $salesToday,
184 3
            'salesYesterday' => $salesYesterday,
185 3
            'countNonStockProducts' => $countNonStockProducts,
186 3
            'countCustomers' => $countCustomers,
187 3
        ));
188
    }
189
190
    /**
191
     * パスワード変更画面
192
     *
193
     * @param Application $app
194
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
195
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
196
     */
197 12
    public function changePassword(Application $app, Request $request)
198
    {
199 3
        $builder = $app['form.factory']
200 3
            ->createBuilder('admin_change_password');
201
202 3
        $event = new EventArgs(
203
            array(
204 3
                'builder' => $builder,
205 3
            ),
206
            $request
207 3
        );
208 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_CHANGE_PASSWORD_INITIALIZE, $event);
209
210 3
        $form = $builder->getForm();
211 3
        $form->handleRequest($request);
212
213 3
        if ($form->isSubmitted() && $form->isValid()) {
214 1
            $password = $form->get('change_password')->getData();
215
216 1
            $Member = $app->user();
217
218 1
            $dummyMember = clone $Member;
219 1
            $dummyMember->setPassword($password);
220 1
            $salt = $dummyMember->getSalt();
221 1
            if (!isset($salt)) {
222
                $salt = $app['eccube.repository.member']->createSalt(5);
223
                $dummyMember->setSalt($salt);
224
            }
225
226 1
            $encryptPassword = $app['eccube.repository.member']->encryptPassword($dummyMember);
227
228
            $Member
229 1
                ->setPassword($encryptPassword)
230 1
                ->setSalt($salt);
231
232 1
            $status = $app['eccube.repository.member']->save($Member);
233 1
            if ($status) {
234 1
                $event = new EventArgs(
235
                    array(
236 1
                        'form' => $form,
237 1
                    ),
238
                    $request
239 12
                );
240 1
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIN_CHANGE_PASSWORD_COMPLETE, $event);
241
242 1
                $app->addSuccess('admin.change_password.save.complete', 'admin');
243
244 1
                return $app->redirect($app->url('admin_change_password'));
245
            }
246
247
            $app->addError('admin.change_password.save.error', 'admin');
248
        }
249
250 2
        return $app->render('change_password.twig', array(
251 2
            'form' => $form->createView(),
252 2
        ));
253
    }
254
255
    /**
256
     * 在庫なし商品の検索結果を表示する.
257
     *
258
     * @param Application $app
259
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
260
     * @return \Symfony\Component\HttpFoundation\Response
261
     */
262 2
    public function searchNonStockProducts(Application $app, Request $request)
263
    {
264
        // 商品マスター検索用フォーム
265 2
        $form = $app['form.factory']
266 2
            ->createBuilder('admin_search_product')
267 2
            ->getForm();
268
269 2
        if ('POST' === $request->getMethod()) {
270 2
            $form->handleRequest($request);
271
272 2
            if ($form->isValid()) {
273
                // 在庫なし商品の検索条件をセッションに付与し, 商品マスタへリダイレクトする.
274
                $searchData = array();
275
                $searchData['stock_status'] = Constant::DISABLED;
276
                $session = $request->getSession();
277
                $session->set('eccube.admin.product.search', $searchData);
278
279
                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...
280
                    'page_no' => 1,
281
                    '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...
282
            }
283 2
        }
284
285 2
        return $app->redirect($app->url('admin_homepage'));
286
    }
287
288 3
    protected function findOrderStatus($em, array $excludes)
289
    {
290
        $qb = $em
291 3
            ->getRepository('Eccube\Entity\Master\OrderStatus')
292 3
            ->createQueryBuilder('os');
293
294
        return $qb
295 3
            ->where($qb->expr()->notIn('os.id', $excludes))
296 3
            ->getQuery()
297 3
            ->getResult();
298
    }
299
300 3
    protected function getOrderEachStatus($em, array $excludes)
301
    {
302
        $sql = 'SELECT
303
                    t1.status as status,
304
                    COUNT(t1.order_id) as count
305
                FROM
306
                    dtb_order t1
307
                WHERE
308
                    t1.del_flg = 0
309
                    AND t1.status NOT IN (:excludes)
310
                GROUP BY
311
                    t1.status
312
                ORDER BY
313 3
                    t1.status';
314 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...
315 3
        $rsm->addScalarResult('status', 'status');
316 3
        $rsm->addScalarResult('count', 'count');
317 3
        $query = $em->createNativeQuery($sql, $rsm);
318 3
        $query->setParameters(array(':excludes' => $excludes));
319 3
        $result = $query->getResult();
320 3
        $orderArray = array();
321 3
        foreach ($result as $row) {
322 1
            $orderArray[$row['status']] = $row['count'];
323 3
        }
324
325 3
        return $orderArray;
326
    }
327
328 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...
329
    {
330
        // concat... for pgsql
331
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
332
        $dql = 'SELECT
333
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) AS order_month,
334
                  SUM(o.payment_total) AS order_amount,
335
                  COUNT(o) AS order_count
336
                FROM
337
                  Eccube\Entity\Order o
338
                WHERE
339
                    o.del_flg = 0
340
                    AND o.OrderStatus NOT IN (:excludes)
341
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) = SUBSTRING(:targetDate, 1, 7)
342
                GROUP BY
343 3
                  order_month';
344
345
        $q = $em
346 3
            ->createQuery($dql)
347 3
            ->setParameter(':excludes', $excludes)
348 3
            ->setParameter(':targetDate', $dateTime);
349
350 3
        $result = array();
351
        try {
352 3
            $result = $q->getSingleResult();
353 3
        } catch (NoResultException $e) {
354
            // 結果がない場合は空の配列を返す.
355
        }
356 3
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
357
    }
358
359 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...
360
    {
361
        // concat... for pgsql
362
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
363
        $dql = 'SELECT
364
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) AS order_day,
365
                  SUM(o.payment_total) AS order_amount,
366
                  COUNT(o) AS order_count
367
                FROM
368
                  Eccube\Entity\Order o
369
                WHERE
370
                    o.del_flg = 0
371
                    AND o.OrderStatus NOT IN (:excludes)
372
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) = SUBSTRING(:targetDate, 1, 10)
373
                GROUP BY
374 3
                  order_day';
375
376
        $q = $em
377 3
            ->createQuery($dql)
378 3
            ->setParameter(':excludes', $excludes)
379 3
            ->setParameter(':targetDate', $dateTime);
380
381 3
        $result = array();
382
        try {
383 3
            $result = $q->getSingleResult();
384 3
        } catch (NoResultException $e) {
385
            // 結果がない場合は空の配列を返す.
386
        }
387 3
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
388
    }
389
390 3
    protected function countNonStockProducts($em)
391
    {
392
        /** @var $qb \Doctrine\ORM\QueryBuilder */
393 3
        $qb = $em->getRepository('Eccube\Entity\Product')
394 3
            ->createQueryBuilder('p')
395 3
            ->select('count(p.id)')
396 3
            ->innerJoin('p.ProductClasses', 'pc')
397 3
            ->where('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
398 3
            ->setParameter('StockUnlimited', Constant::DISABLED);
399
400
        return $qb
401 3
            ->getQuery()
402 3
            ->getSingleScalarResult();
403
    }
404
405 3
    protected function countCustomers($em)
406
    {
407
        $Status = $em
408 3
            ->getRepository('Eccube\Entity\Master\CustomerStatus')
409 3
            ->find(2);
410
411
        /** @var $qb \Doctrine\ORM\QueryBuilder */
412 3
        $qb = $em->getRepository('Eccube\Entity\Customer')
413 3
            ->createQueryBuilder('c')
414 3
            ->select('count(c.id)')
415 3
            ->where('c.Status = :Status')
416 3
            ->setParameter('Status', $Status);
417
418
        return $qb
419 3
            ->getQuery()
420 3
            ->getSingleScalarResult();
421
    }
422
}