Blame view

controllers/StatisticsController.php 11.1 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
187
188
          

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

          {

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

              

2f4b4e77   Alexey Boroda   -Statistics expor...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
              $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
207
              

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

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

              } else {

                  $labelFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
213
              

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

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

              } else {

                  $managerFilter = [];

              }

d7331490   Alexey Boroda   -Export ready
219
              

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

                             ->with(

                                 [

                                     'products',

                                     'orderLabel',

                                     'manager',

                                 ]

                             )

                             ->filterWhere($dateFilter)

                             ->andFilterWhere($labelFilter)

                             ->andFilterWhere($managerFilter)

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

              

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

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

                  $line = [];

                  /**

                   * @var Order $order

                   */

29739088   Alexey Boroda   -Statistics expor...
239
                  $line[] = (string) $order->id;

2f4b4e77   Alexey Boroda   -Statistics expor...
240
                  $line[] = $formatter->asDatetime($order->created_at);

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

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

                      $line[] = '';

                  } else {

                      $i = 0;

                      $products = '';

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

e83bcda3   Alexey Boroda   -Replace line breaks
248
                          $i++;

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

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

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

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

                      }

                      $line[] = $products;

                  }

d7331490   Alexey Boroda   -Export ready
256
257
258
259
                  $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...
260
                  $line[] = empty($order->manager) ? '' : $formatter->asText($order->manager->username);

e83bcda3   Alexey Boroda   -Replace line breaks
261
262
263
264
265
266
267
268
269
                  $line[] = str_replace(

                      [

                          "\r\n",

                          "\n",

                          "\r",

                      ],

                      ' ',

                      $order->body

                  );

2f4b4e77   Alexey Boroda   -Statistics expor...
270
                  

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

2f4b4e77   Alexey Boroda   -Statistics expor...
272
273
              }

              fclose($file);

d7331490   Alexey Boroda   -Export ready
274
275
276
              

              return [

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

e83bcda3   Alexey Boroda   -Replace line breaks
277
278
279
280
281
282
283
284
285
286
287
288
289
                  'button'  => Html::a(

                      Html::tag(

                          'i',

                          '',

                          [

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

                          ]

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

                      '/storage/statistics_export.csv',

                      [

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

                      ]

                  ),

d7331490   Alexey Boroda   -Export ready
290
              ];

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

1ac35977   Alexey Boroda   -Staistics started
292
      }