Blame view

controllers/StatisticsController.php 3.34 KB
1ac35977   Alexey Boroda   -Staistics started
1
2
3
4
  <?php

      

      namespace artweb\artbox\ecommerce\controllers;

      

c130fad8   Alexey Boroda   -Yml fix
5
6
      use artweb\artbox\ecommerce\models\Label;

      use artweb\artbox\ecommerce\models\Order;

ee0e1df5   Alexey Boroda   -Statistics half ...
7
      use yii\data\ActiveDataProvider;

c130fad8   Alexey Boroda   -Yml fix
8
9
      use yii\helpers\ArrayHelper;

      use yii\helpers\VarDumper;

1ac35977   Alexey Boroda   -Staistics started
10
11
      use yii\web\Controller;

      

1ac35977   Alexey Boroda   -Staistics started
12
13
      class StatisticsController extends Controller

      {

c130fad8   Alexey Boroda   -Yml fix
14
          public function actionIndex($date_range = NULL, $label = NULL)

1ac35977   Alexey Boroda   -Staistics started
15
          {

c130fad8   Alexey Boroda   -Yml fix
16
17
18
19
20
21
              /**

               * Get a dates range

               */

              if (!empty($date_range)) {

                  $arr = [];

                  preg_match('@(.*)\s:\s(.*)@', $date_range, $arr);

44747138   Alexey Boroda   -Default statisti...
22
23
24
25
26
27
                  $dateFilter = [

                      'between',

                      'created_at',

                      strtotime($arr[ 1 ]),

                      strtotime($arr[ 2 ]),

                  ];

c130fad8   Alexey Boroda   -Yml fix
28
              } else {

ee0e1df5   Alexey Boroda   -Statistics half ...
29
                  $dateFilter = [];

c130fad8   Alexey Boroda   -Yml fix
30
              }

c130fad8   Alexey Boroda   -Yml fix
31
32
              

              if (!empty($label)) {

44747138   Alexey Boroda   -Default statisti...
33
                  $labelFilter = [ 'label' => $label ];

ee0e1df5   Alexey Boroda   -Statistics half ...
34
35
              } else {

                  $labelFilter = [];

c130fad8   Alexey Boroda   -Yml fix
36
37
              }

              

ee0e1df5   Alexey Boroda   -Statistics half ...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
              /**

               * Get labels

               */

              $labels = Label::find()

                             ->with('lang')

                             ->all();

              

              /**

               * Generate statistics

               */

              $labelStatistics = ArrayHelper::map(

                  $labels,

                  function($model) {

                      /**

                       * @var Label $model

                       */

                      return $model->lang->title;

                  },

                  function($model) use ($dateFilter) {

                      /**

                       * @var Label $model

                       */

44747138   Alexey Boroda   -Default statisti...
60
61
                      return $model->getStatistics($dateFilter);

                  }

ee0e1df5   Alexey Boroda   -Statistics half ...
62
63
              );

              

44747138   Alexey Boroda   -Default statisti...
64
65
66
67
68
69
70
              $dataProvider = new ActiveDataProvider(

                  [

                      'query' => Order::find()

                                      ->filterWhere($dateFilter)

                                      ->andFilterWhere($labelFilter),

                  ]

              );

ee0e1df5   Alexey Boroda   -Statistics half ...
71
              

44747138   Alexey Boroda   -Default statisti...
72
              $labelChartData = [

3bee3445   Alexey Boroda   -Order history ready
73
                  'labels'   => array_keys($labelStatistics),

44747138   Alexey Boroda   -Default statisti...
74
75
                  'datasets' => [

                      [

3bee3445   Alexey Boroda   -Order history ready
76
77
78
79
80
                          'label'           => 'На сумму, грн.',

                          'data'            => ArrayHelper::getColumn($labelStatistics, 'sum', false),

                          'backgroundColor' => 'rgba(255, 99, 132, 0.2)',

                          'borderColor'     => 'rgba(255,99,132,1)',

                          'borderWidth'     => 1,

44747138   Alexey Boroda   -Default statisti...
81
82
83
                      ],

                  ],

              ];

c130fad8   Alexey Boroda   -Yml fix
84
85
86
              return $this->render(

                  'index',

                  [

44747138   Alexey Boroda   -Default statisti...
87
88
                      'labels'              => $labels,

                      'labelStatistics'     => $labelStatistics,

ee0e1df5   Alexey Boroda   -Statistics half ...
89
                      'rejectionStatistics' => Order::getRejectionStatistics($dateFilter),

44747138   Alexey Boroda   -Default statisti...
90
91
92
93
                      'dataProvider'        => $dataProvider,

                      'labelChartData'      => $labelChartData,

                      'dateValue'           => empty($date_range) ? '' : $date_range,

                      'dataLabel'           => empty($label) ? false : $label,

c130fad8   Alexey Boroda   -Yml fix
94
95
                  ]

              );

1ac35977   Alexey Boroda   -Staistics started
96
97
          }

      }