Blame view

controllers/StatisticsController.php 3.37 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
              $chartData = [];

              foreach ($labelStatistics as $statistic) {

                  $chartData[] = $statistic[ 'sum' ];

              }

              $chartLabels = [];

              foreach ($labelStatistics as $name => $statistic) {

                  $chartLabels[] = $name;

              }

              $labelChartData = [

                  'labels'   => $chartLabels,

                  'datasets' => [

                      [

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

                          'data'  => $chartData,

                      ],

                  ],

              ];

c130fad8   Alexey Boroda   -Yml fix
89
90
91
              return $this->render(

                  'index',

                  [

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

                      'labelStatistics'     => $labelStatistics,

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

44747138   Alexey Boroda   -Default statisti...
95
96
97
98
                      'dataProvider'        => $dataProvider,

                      'labelChartData'      => $labelChartData,

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

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

c130fad8   Alexey Boroda   -Yml fix
99
100
                  ]

              );

1ac35977   Alexey Boroda   -Staistics started
101
102
          }

      }