Blame view

controllers/StatisticsController.php 10.7 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;

29257441   Alexey Boroda   -Manager filter o...
7
      use common\models\User;

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

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

d7331490   Alexey Boroda   -Export ready
10
      use yii\helpers\Html;

c130fad8   Alexey Boroda   -Yml fix
11
      use yii\helpers\VarDumper;

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

2f4b4e77   Alexey Boroda   -Statistics expor...
13
      use yii\web\Response;

1ac35977   Alexey Boroda   -Staistics started
14
      

1ac35977   Alexey Boroda   -Staistics started
15
16
      class StatisticsController extends Controller

      {

29257441   Alexey Boroda   -Manager filter o...
17
          public function actionIndex($date_range = NULL, $label = NULL, $manager = NULL)

1ac35977   Alexey Boroda   -Staistics started
18
          {

c130fad8   Alexey Boroda   -Yml fix
19
20
21
22
23
24
              /**

               * Get a dates range

               */

              if (!empty($date_range)) {

                  $arr = [];

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

44747138   Alexey Boroda   -Default statisti...
25
26
27
28
29
30
                  $dateFilter = [

                      'between',

                      'created_at',

                      strtotime($arr[ 1 ]),

                      strtotime($arr[ 2 ]),

                  ];

c130fad8   Alexey Boroda   -Yml fix
31
              } else {

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

c130fad8   Alexey Boroda   -Yml fix
33
              }

c130fad8   Alexey Boroda   -Yml fix
34
35
              

              if (!empty($label)) {

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

ee0e1df5   Alexey Boroda   -Statistics half ...
37
38
              } else {

                  $labelFilter = [];

c130fad8   Alexey Boroda   -Yml fix
39
              }

2f4b4e77   Alexey Boroda   -Statistics expor...
40
              

29257441   Alexey Boroda   -Manager filter o...
41
42
43
44
45
              if (!empty($manager)) {

                  $managerFilter = [ 'manager_id' => $manager ];

              } else {

                  $managerFilter = [];

              }

c130fad8   Alexey Boroda   -Yml fix
46
              

ee0e1df5   Alexey Boroda   -Statistics half ...
47
48
49
50
51
52
              /**

               * Get labels

               */

              $labels = Label::find()

                             ->with('lang')

                             ->all();

2f4b4e77   Alexey Boroda   -Statistics expor...
53
              

29257441   Alexey Boroda   -Manager filter o...
54
55
56
57
58
              /**

               * Get user

               */

              $managers = User::find()

                              ->all();

ee0e1df5   Alexey Boroda   -Statistics half ...
59
60
61
62
63
64
              

              /**

               * Generate statistics

               */

              $labelStatistics = ArrayHelper::map(

                  $labels,

29257441   Alexey Boroda   -Manager filter o...
65
                  function(Label $model) {

ee0e1df5   Alexey Boroda   -Statistics half ...
66
67
                      return $model->lang->title;

                  },

29257441   Alexey Boroda   -Manager filter o...
68
69
                  function(Label $model) use ($dateFilter, $managerFilter) {

                      return $model->getStatistics($dateFilter, $managerFilter);

44747138   Alexey Boroda   -Default statisti...
70
                  }

ee0e1df5   Alexey Boroda   -Statistics half ...
71
              );

2f4b4e77   Alexey Boroda   -Statistics expor...
72
              

29257441   Alexey Boroda   -Manager filter o...
73
74
75
              /**

               * Data provider for table

               */

44747138   Alexey Boroda   -Default statisti...
76
77
78
79
              $dataProvider = new ActiveDataProvider(

                  [

                      'query' => Order::find()

                                      ->filterWhere($dateFilter)

2f4b4e77   Alexey Boroda   -Statistics expor...
80
                                      ->andFilterWhere($managerFilter)

44747138   Alexey Boroda   -Default statisti...
81
82
83
                                      ->andFilterWhere($labelFilter),

                  ]

              );

2f4b4e77   Alexey Boroda   -Statistics expor...
84
              

29257441   Alexey Boroda   -Manager filter o...
85
86
87
              /**

               * Creating charts data

               */

cbc8daa9   Alexey Boroda   -Order history ready
88
89
90
91
92
93
94
95
96
97
98
99
              $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 ...
100
              

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

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

44747138   Alexey Boroda   -Default statisti...
103
104
                  'datasets' => [

                      [

3bee3445   Alexey Boroda   -Order history ready
105
106
107
108
109
                          '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...
110
111
112
                      ],

                  ],

              ];

29257441   Alexey Boroda   -Manager filter o...
113
              

cbc8daa9   Alexey Boroda   -Order history ready
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
              $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,

                      ],

                  ],

              ];

2f4b4e77   Alexey Boroda   -Statistics expor...
133
              

29257441   Alexey Boroda   -Manager filter o...
134
135
136
137
              /**

               * Getting rejection statistics

               */

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

2f4b4e77   Alexey Boroda   -Statistics expor...
138
              

29257441   Alexey Boroda   -Manager filter o...
139
140
141
              /**

               * Charts data for rejects

               */

cbc8daa9   Alexey Boroda   -Order history ready
142
143
144
145
146
147
148
149
150
151
152
153
              $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,

                      ],

                  ],

              ];

29257441   Alexey Boroda   -Manager filter o...
154
              

cbc8daa9   Alexey Boroda   -Order history ready
155
156
157
158
159
160
161
162
163
164
165
166
167
              $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
168
169
170
              return $this->render(

                  'index',

                  [

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

29257441   Alexey Boroda   -Manager filter o...
172
                      'managers'            => $managers,

44747138   Alexey Boroda   -Default statisti...
173
                      'labelStatistics'     => $labelStatistics,

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

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

29257441   Alexey Boroda   -Manager filter o...
176
177
178
179
180
                      'labelChartData1'     => $labelChartData1,

                      'labelChartData2'     => $labelChartData2,

                      'labelChartData3'     => $labelChartData3,

                      'rejectChartData1'    => $rejectChartData1,

                      'rejectChartData2'    => $rejectChartData2,

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

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

2f4b4e77   Alexey Boroda   -Statistics expor...
183
                      'dataManager'         => empty($manager) ? false : $manager,

c130fad8   Alexey Boroda   -Yml fix
184
185
                  ]

              );

1ac35977   Alexey Boroda   -Staistics started
186
          }

2f4b4e77   Alexey Boroda   -Statistics expor...
187
188
189
          

          public function actionExport($date_range = NULL, $label = NULL, $manager = NULL)

          {

d7331490   Alexey Boroda   -Export ready
190
191
              \Yii::$app->response->format = Response::FORMAT_JSON;

              

2f4b4e77   Alexey Boroda   -Statistics expor...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
              $formatter = \Yii::$app->formatter;

              /**

               * Get export options

               */

              if (!empty($date_range)) {

                  $arr = [];

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

                  $dateFilter = [

                      'between',

                      'created_at',

                      strtotime($arr[ 1 ]),

                      strtotime($arr[ 2 ]),

                  ];

              } else {

                  $dateFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
208
              

2f4b4e77   Alexey Boroda   -Statistics expor...
209
210
211
212
213
              if (!empty($label)) {

                  $labelFilter = [ 'label' => $label ];

              } else {

                  $labelFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
214
              

2f4b4e77   Alexey Boroda   -Statistics expor...
215
216
217
218
219
              if (!empty($manager)) {

                  $managerFilter = [ 'manager_id' => $manager ];

              } else {

                  $managerFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
220
              

2f4b4e77   Alexey Boroda   -Statistics expor...
221
222
223
224
225
226
227
228
229
230
231
              $orders = Order::find()

                             ->with(

                                 [

                                     'products',

                                     'orderLabel',

                                     'manager',

                                 ]

                             )

                             ->filterWhere($dateFilter)

                             ->andFilterWhere($labelFilter)

                             ->andFilterWhere($managerFilter)

2f4b4e77   Alexey Boroda   -Statistics expor...
232
233
                             ->all();

              

d7331490   Alexey Boroda   -Export ready
234
              $file = fopen(\Yii::getAlias('@frontend/web/') . 'statistics_export.csv', 'w');

2f4b4e77   Alexey Boroda   -Statistics expor...
235
236
237
238
239
240
241
              foreach ($orders as $order) {

                  $line = [];

                  /**

                   * @var Order $order

                   */

                  $line[] = $formatter->asDecimal($order->id);

                  $line[] = $formatter->asDatetime($order->created_at);

d7331490   Alexey Boroda   -Export ready
242
                  $line[] = (string) $order->name;

2f4b4e77   Alexey Boroda   -Statistics expor...
243
244
245
246
247
248
                  if (empty($order->products)) {

                      $line[] = '';

                  } else {

                      $i = 0;

                      $products = '';

                      foreach ($order->products as $product) {

d7331490   Alexey Boroda   -Export ready
249
                          $i ++;

2f4b4e77   Alexey Boroda   -Statistics expor...
250
251
                          $products .= $product->sku;

                          if (count($order->products) != $i) {

d7331490   Alexey Boroda   -Export ready
252
                              $products .= ' ,';

2f4b4e77   Alexey Boroda   -Statistics expor...
253
254
255
256
                          }

                      }

                      $line[] = $products;

                  }

d7331490   Alexey Boroda   -Export ready
257
258
259
260
                  $line[] = (string) $order->city;

                  $line[] = (string) $order->orderLabel->label;

                  $line[] = (string) $order->total;

                  $line[] = empty($order->reason) ? '' : $formatter->asText(Order::REASONS[ $order->reason ]);

2f4b4e77   Alexey Boroda   -Statistics expor...
261
                  $line[] = empty($order->manager) ? '' : $formatter->asText($order->manager->username);

d7331490   Alexey Boroda   -Export ready
262
                  $line[] = (string) $order->body;

2f4b4e77   Alexey Boroda   -Statistics expor...
263
264
265
266
                  

                  fputcsv($file, $line);

              }

              fclose($file);

d7331490   Alexey Boroda   -Export ready
267
268
269
270
271
272
273
274
275
276
              

              return [

                  'message' => 'Файл успешно сгенерирован',

                  'button' => Html::a(

                      Html::tag('i', '', [

                      'class' => 'glyphicon glyphicon-download-alt'

                  ]) . ' Скачать', '/statistics_export.csv', [

                      'class' => 'btn bg-olive',

                  ])

              ];

2f4b4e77   Alexey Boroda   -Statistics expor...
277
          }

1ac35977   Alexey Boroda   -Staistics started
278
      }