Blame view

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

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

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

1ac35977   Alexey Boroda   -Staistics started
13
      

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

      {

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

1ac35977   Alexey Boroda   -Staistics started
17
          {

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

               * Get a dates range

               */

              if (!empty($date_range)) {

                  $arr = [];

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

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

                      'between',

                      'created_at',

                      strtotime($arr[ 1 ]),

                      strtotime($arr[ 2 ]),

                  ];

c130fad8   Alexey Boroda   -Yml fix
30
              } else {

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

c130fad8   Alexey Boroda   -Yml fix
32
              }

c130fad8   Alexey Boroda   -Yml fix
33
34
              

              if (!empty($label)) {

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

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

                  $labelFilter = [];

c130fad8   Alexey Boroda   -Yml fix
38
              }

2f4b4e77   Alexey Boroda   -Statistics expor...
39
              

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

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

              } else {

                  $managerFilter = [];

              }

c130fad8   Alexey Boroda   -Yml fix
45
              

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

               * Get labels

               */

              $labels = Label::find()

                             ->with('lang')

                             ->all();

2f4b4e77   Alexey Boroda   -Statistics expor...
52
              

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

               * Get user

               */

              $managers = User::find()

                              ->all();

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

              /**

               * Generate statistics

               */

              $labelStatistics = ArrayHelper::map(

                  $labels,

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

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

                  },

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

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

44747138   Alexey Boroda   -Default statisti...
69
                  }

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

2f4b4e77   Alexey Boroda   -Statistics expor...
71
              

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

               * Data provider for table

               */

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

                  [

                      'query' => Order::find()

                                      ->filterWhere($dateFilter)

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

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

                  ]

              );

2f4b4e77   Alexey Boroda   -Statistics expor...
83
              

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

               * Creating charts data

               */

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

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

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

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

                      [

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

                  ],

              ];

29257441   Alexey Boroda   -Manager filter o...
112
              

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

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

               * Getting rejection statistics

               */

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

2f4b4e77   Alexey Boroda   -Statistics expor...
137
              

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

               * Charts data for rejects

               */

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

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

                  'index',

                  [

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

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

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

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

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

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

                      'labelChartData2'     => $labelChartData2,

                      'labelChartData3'     => $labelChartData3,

                      'rejectChartData1'    => $rejectChartData1,

                      'rejectChartData2'    => $rejectChartData2,

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

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

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

c130fad8   Alexey Boroda   -Yml fix
183
184
                  ]

              );

1ac35977   Alexey Boroda   -Staistics started
185
          }

2f4b4e77   Alexey Boroda   -Statistics expor...
186
          

894d945a   Alexey Boroda   -Logs bu fix and ...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
          public function actionView($id = NULL, $manager = NULL, $date = NULL)

          {

              /**

               * Get a dates range

               */

              if (!empty($date)) {

                  $arr = [];

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

                  $dateFilter = [

                      'between',

                      'created_at',

                      strtotime($arr[ 1 ]),

                      strtotime($arr[ 2 ]),

                  ];

              } else {

                  $dateFilter = [];

              }

              

              if (!empty($id)) {

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

              } else {

                  $labelFilter = [];

              }

              

              if (!empty($manager)) {

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

              } else {

                  $managerFilter = [];

              }

              

              $dataProvider = new ActiveDataProvider(

                  [

                      'query' => Order::find()

                                      ->filterWhere($labelFilter)

                                      ->andFilterWhere($dateFilter)

                                      ->andFilterWhere($managerFilter),

                  ]

              );

              

              $label = Label::find()->with('lang')->where(['id' => $id])->one();

              

              return $this->render(

                  'view',

                  [

                      'dataProvider' => $dataProvider,

                      'label' => $label,

                  ]

              );

          }

          

2f4b4e77   Alexey Boroda   -Statistics expor...
237
238
          public function actionExport($date_range = NULL, $label = NULL, $manager = NULL)

          {

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

              

2f4b4e77   Alexey Boroda   -Statistics expor...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
              $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
257
              

2f4b4e77   Alexey Boroda   -Statistics expor...
258
259
260
261
262
              if (!empty($label)) {

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

              } else {

                  $labelFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
263
              

2f4b4e77   Alexey Boroda   -Statistics expor...
264
265
266
267
268
              if (!empty($manager)) {

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

              } else {

                  $managerFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
269
              

2f4b4e77   Alexey Boroda   -Statistics expor...
270
271
272
273
274
275
276
277
278
279
280
              $orders = Order::find()

                             ->with(

                                 [

                                     'products',

                                     'orderLabel',

                                     'manager',

                                 ]

                             )

                             ->filterWhere($dateFilter)

                             ->andFilterWhere($labelFilter)

                             ->andFilterWhere($managerFilter)

2f4b4e77   Alexey Boroda   -Statistics expor...
281
282
                             ->all();

              

ce03dacb   Alexey Boroda   -Export to storag...
283
              $file = fopen(\Yii::getAlias('@storage/') . 'statistics_export.csv', 'w');

2f4b4e77   Alexey Boroda   -Statistics expor...
284
285
286
287
288
289
290
              foreach ($orders as $order) {

                  $line = [];

                  /**

                   * @var Order $order

                   */

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

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

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

2f4b4e77   Alexey Boroda   -Statistics expor...
292
293
294
295
296
297
                  if (empty($order->products)) {

                      $line[] = '';

                  } else {

                      $i = 0;

                      $products = '';

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

894d945a   Alexey Boroda   -Logs bu fix and ...
298
                          $i++;

2f4b4e77   Alexey Boroda   -Statistics expor...
299
300
                          $products .= $product->sku;

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

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

2f4b4e77   Alexey Boroda   -Statistics expor...
302
303
304
305
                          }

                      }

                      $line[] = $products;

                  }

d7331490   Alexey Boroda   -Export ready
306
307
308
309
                  $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...
310
                  $line[] = empty($order->manager) ? '' : $formatter->asText($order->manager->username);

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

2f4b4e77   Alexey Boroda   -Statistics expor...
312
                  

44caf85a   Alexey Boroda   -Export file deli...
313
                  fputcsv($file, $line, ";");

2f4b4e77   Alexey Boroda   -Statistics expor...
314
315
              }

              fclose($file);

d7331490   Alexey Boroda   -Export ready
316
317
318
              

              return [

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

894d945a   Alexey Boroda   -Logs bu fix and ...
319
320
321
322
323
324
325
326
327
328
329
330
331
                  'button'  => Html::a(

                      Html::tag(

                          'i',

                          '',

                          [

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

                          ]

                      ) . ' Скачать',

                      '/storage/statistics_export.csv',

                      [

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

                      ]

                  ),

d7331490   Alexey Boroda   -Export ready
332
              ];

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

1ac35977   Alexey Boroda   -Staistics started
334
      }