Blame view

controllers/StatisticsController.php 6.41 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),

                  ]

              );

cbc8daa9   Alexey Boroda   -Order history ready
71
72
73
74
75
76
77
78
79
80
81
82
83
      

              $labelChartData1 = [

                  'labels'   => array_keys($labelStatistics),

                  'datasets' => [

                      [

                          'label'           => 'Заказов, шт.',

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

                          'backgroundColor' => 'rgba(54, 162, 235, 0.2)',

                          'borderColor'     => 'rgba(54, 162, 235, 1)',

                          'borderWidth'     => 1,

                      ],

                  ],

              ];

ee0e1df5   Alexey Boroda   -Statistics half ...
84
              

cbc8daa9   Alexey Boroda   -Order history ready
85
              $labelChartData2 = [

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

44747138   Alexey Boroda   -Default statisti...
87
88
                  'datasets' => [

                      [

3bee3445   Alexey Boroda   -Order history ready
89
90
91
92
93
                          '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...
94
95
96
                      ],

                  ],

              ];

cbc8daa9   Alexey Boroda   -Order history ready
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      

              $labelChartData3 = [

                  'labels'   => array_keys($labelStatistics),

                  'datasets' => [

                      [

                          'label'           => 'Заказано товаров, шт.',

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

                          'backgroundColor' => 'rgba(255, 206, 86, 0.2)',

                          'borderColor'     => 'rgba(255, 206, 86, 1)',

                          'borderWidth'     => 1,

                      ],

                      [

                          'label'           => 'Уникальных товаров, шт.',

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

                          'backgroundColor' => 'rgba(75, 192, 192, 0.2)',

                          'borderColor'     => 'rgba(75, 192, 192, 1)',

                          'borderWidth'     => 1,

                      ],

                  ],

              ];

      

              $rejectStatistics = Order::getRejectionStatistics($dateFilter);

              

              $rejectChartData1 = [

                  'labels'   => array_keys($rejectStatistics),

                  'datasets' => [

                      [

                          'label'           => 'Заказов, шт.',

                          'data'            => ArrayHelper::getColumn($rejectStatistics, 'count', false),

                          'backgroundColor' => 'rgba(153, 102, 255, 0.2)',

                          'borderColor'     => 'rgba(153, 102, 255, 1)',

                          'borderWidth'     => 1,

                      ],

                  ],

              ];

      

              $rejectChartData2 = [

                  'labels'   => array_keys($rejectStatistics),

                  'datasets' => [

                      [

                          'label'           => 'На сумму, грн.',

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

                          'backgroundColor' => 'rgba(255, 159, 64, 0.2)',

                          'borderColor'     => 'rgba(255, 159, 64, 1)',

                          'borderWidth'     => 1,

                      ],

                  ],

              ];

              

c130fad8   Alexey Boroda   -Yml fix
146
147
148
              return $this->render(

                  'index',

                  [

44747138   Alexey Boroda   -Default statisti...
149
150
                      'labels'              => $labels,

                      'labelStatistics'     => $labelStatistics,

cbc8daa9   Alexey Boroda   -Order history ready
151
                      'rejectionStatistics' => $rejectStatistics,

44747138   Alexey Boroda   -Default statisti...
152
                      'dataProvider'        => $dataProvider,

cbc8daa9   Alexey Boroda   -Order history ready
153
154
155
156
157
                      'labelChartData1'      => $labelChartData1,

                      'labelChartData2'      => $labelChartData2,

                      'labelChartData3'      => $labelChartData3,

                      'rejectChartData1' => $rejectChartData1,

                      'rejectChartData2' => $rejectChartData2,

44747138   Alexey Boroda   -Default statisti...
158
159
                      'dateValue'           => empty($date_range) ? '' : $date_range,

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

c130fad8   Alexey Boroda   -Yml fix
160
161
                  ]

              );

1ac35977   Alexey Boroda   -Staistics started
162
163
          }

      }