Commit 447471387b26a2ffd20c2ed1fcb51ac86f2a94c0

Authored by Alexey Boroda
1 parent ee0e1df5

-Default statistics done

controllers/StatisticsController.php
@@ -19,18 +19,18 @@ @@ -19,18 +19,18 @@
19 if (!empty($date_range)) { 19 if (!empty($date_range)) {
20 $arr = []; 20 $arr = [];
21 preg_match('@(.*)\s:\s(.*)@', $date_range, $arr); 21 preg_match('@(.*)\s:\s(.*)@', $date_range, $arr);
22 - $dateFilter = [  
23 - 'between',  
24 - 'created_at',  
25 - strtotime($arr[1]),  
26 - strtotime($arr[2]),  
27 - ]; 22 + $dateFilter = [
  23 + 'between',
  24 + 'created_at',
  25 + strtotime($arr[ 1 ]),
  26 + strtotime($arr[ 2 ]),
  27 + ];
28 } else { 28 } else {
29 $dateFilter = []; 29 $dateFilter = [];
30 } 30 }
31 31
32 if (!empty($label)) { 32 if (!empty($label)) {
33 - $labelFilter = ['label' => $label]; 33 + $labelFilter = [ 'label' => $label ];
34 } else { 34 } else {
35 $labelFilter = []; 35 $labelFilter = [];
36 } 36 }
@@ -57,20 +57,45 @@ @@ -57,20 +57,45 @@
57 /** 57 /**
58 * @var Label $model 58 * @var Label $model
59 */ 59 */
60 - return $model->getStatistics($dateFilter);} 60 + return $model->getStatistics($dateFilter);
  61 + }
61 ); 62 );
62 63
63 - $dataProvider = new ActiveDataProvider([  
64 - 'query' => Order::find()->filterWhere($dateFilter)->andFilterWhere($labelFilter),  
65 - ]); 64 + $dataProvider = new ActiveDataProvider(
  65 + [
  66 + 'query' => Order::find()
  67 + ->filterWhere($dateFilter)
  68 + ->andFilterWhere($labelFilter),
  69 + ]
  70 + );
66 71
  72 + $chartData = [];
  73 + foreach ($labelStatistics as $statistic) {
  74 + $chartData[] = $statistic[ 'sum' ];
  75 + }
  76 + $chartLabels = [];
  77 + foreach ($labelStatistics as $name => $statistic) {
  78 + $chartLabels[] = $name;
  79 + }
  80 + $labelChartData = [
  81 + 'labels' => $chartLabels,
  82 + 'datasets' => [
  83 + [
  84 + 'label' => 'На сумму, грн.',
  85 + 'data' => $chartData,
  86 + ],
  87 + ],
  88 + ];
67 return $this->render( 89 return $this->render(
68 'index', 90 'index',
69 [ 91 [
70 - 'labels' => $labels,  
71 - 'labelStatistics' => $labelStatistics, 92 + 'labels' => $labels,
  93 + 'labelStatistics' => $labelStatistics,
72 'rejectionStatistics' => Order::getRejectionStatistics($dateFilter), 94 'rejectionStatistics' => Order::getRejectionStatistics($dateFilter),
73 - 'dataProvider' => $dataProvider, 95 + 'dataProvider' => $dataProvider,
  96 + 'labelChartData' => $labelChartData,
  97 + 'dateValue' => empty($date_range) ? '' : $date_range,
  98 + 'dataLabel' => empty($label) ? false : $label,
74 ] 99 ]
75 ); 100 );
76 } 101 }
views/statistics/index.php
1 <?php 1 <?php
2 use artweb\artbox\ecommerce\models\Label; 2 use artweb\artbox\ecommerce\models\Label;
  3 + use artweb\artbox\ecommerce\models\Order;
3 use kartik\daterange\DateRangePicker; 4 use kartik\daterange\DateRangePicker;
4 use kartik\grid\GridView; 5 use kartik\grid\GridView;
5 use kartik\select2\Select2; 6 use kartik\select2\Select2;
6 use yii\data\ActiveDataProvider; 7 use yii\data\ActiveDataProvider;
7 use yii\helpers\ArrayHelper; 8 use yii\helpers\ArrayHelper;
8 use yii\helpers\Html; 9 use yii\helpers\Html;
  10 + use yii\helpers\StringHelper;
9 use yii\web\View; 11 use yii\web\View;
  12 + use yiier\chartjs\ChartJs;
10 13
11 /** 14 /**
12 - * @var View $this  
13 - * @var Label[] $labels  
14 - * @var array $labelStatistics  
15 - * @var array $rejectionStatistics 15 + * @var View $this
  16 + * @var Label[] $labels
  17 + * @var array $labelStatistics
  18 + * @var array $rejectionStatistics
16 * @var ActiveDataProvider $dataProvider 19 * @var ActiveDataProvider $dataProvider
  20 + * @var array $labelChartData
  21 + * @var string $dateValue
  22 + * @var int | boolean $dataLabel
17 */ 23 */
18 24
19 ?> 25 ?>
@@ -35,6 +41,7 @@ @@ -35,6 +41,7 @@
35 <?= DateRangePicker::widget( 41 <?= DateRangePicker::widget(
36 [ 42 [
37 'name' => 'date_range', 43 'name' => 'date_range',
  44 + 'value' => $dateValue,
38 'pluginOptions' => [ 45 'pluginOptions' => [
39 'locale' => [ 46 'locale' => [
40 'format' => 'DD-MM-Y', 47 'format' => 'DD-MM-Y',
@@ -48,6 +55,7 @@ @@ -48,6 +55,7 @@
48 <?= Select2::widget( 55 <?= Select2::widget(
49 [ 56 [
50 'name' => 'label', 57 'name' => 'label',
  58 + 'value' => $dataLabel,
51 'data' => ArrayHelper::map( 59 'data' => ArrayHelper::map(
52 $labels, 60 $labels,
53 function($model) { 61 function($model) {
@@ -60,6 +68,9 @@ @@ -60,6 +68,9 @@
60 'options' => [ 68 'options' => [
61 'placeholder' => 'Все', 69 'placeholder' => 'Все',
62 ], 70 ],
  71 + 'pluginOptions' => [
  72 + 'allowClear' => true,
  73 + ],
63 ] 74 ]
64 ) ?> 75 ) ?>
65 </div> 76 </div>
@@ -94,16 +105,40 @@ @@ -94,16 +105,40 @@
94 </tr> 105 </tr>
95 <?php 106 <?php
96 foreach ($labelStatistics as $name => $statistic) { 107 foreach ($labelStatistics as $name => $statistic) {
97 - ?>  
98 - <tr>  
99 - <td><?= $name ?></td>  
100 - <td><?= $statistic[ 'count' ] ?></td>  
101 - <td><?= $statistic[ 'sum' ] ?></td>  
102 - <td><?= $statistic[ 'products' ] ?></td>  
103 - <td><?= $statistic[ 'unique' ] ?></td>  
104 - </tr>  
105 - <?php } ?> 108 + echo Html::tag(
  109 + 'tr',
  110 + Html::tag('td', $name) . Html::tag('td', $statistic[ 'count' ]) . Html::tag(
  111 + 'td',
  112 + $statistic[ 'sum' ]
  113 + ) . Html::tag('td', $statistic[ 'products' ]) . Html::tag('td', $statistic[ 'unique' ])
  114 + );
  115 + } ?>
106 </table> 116 </table>
  117 +
  118 + <?= ChartJs::widget(
  119 + [
  120 + 'type' => 'bar',
  121 + 'options' => [
  122 + 'height' => 200,
  123 + 'width' => 600,
  124 + ],
  125 + 'data' => $labelChartData,
  126 + 'clientOptions' => [
  127 + 'title' => [
  128 + 'display' => true,
  129 + 'text' => 'Custom Chart Title',
  130 + ],
  131 + 'scales' => [
  132 + 'xAxes' => [
  133 + [
  134 + 'display' => false,
  135 + ],
  136 + ],
  137 + ],
  138 + ],
  139 + ]
  140 + ); ?>
  141 +
107 </div><!-- /.box-body --> 142 </div><!-- /.box-body -->
108 </div><!-- /.box --> 143 </div><!-- /.box -->
109 144
@@ -123,24 +158,88 @@ @@ -123,24 +158,88 @@
123 </tr> 158 </tr>
124 <?php 159 <?php
125 foreach ($rejectionStatistics as $name => $statistic) { 160 foreach ($rejectionStatistics as $name => $statistic) {
126 - ?>  
127 - <tr>  
128 - <td><?= $name ?></td>  
129 - <td><?= $statistic[ 'count' ] ?></td>  
130 - <td><?= $statistic[ 'sum' ] ?></td>  
131 - </tr>  
132 - <?php } ?> 161 + echo Html::tag(
  162 + 'tr',
  163 + Html::tag('td', $name) . Html::tag('td', $statistic[ 'count' ]) . Html::tag(
  164 + 'td',
  165 + $statistic[ 'sum' ]
  166 + )
  167 + );
  168 + }
  169 + ?>
133 </table> 170 </table>
134 </div><!-- /.box-body --> 171 </div><!-- /.box-body -->
135 </div><!-- /.box --> 172 </div><!-- /.box -->
136 173
137 -<?=GridView::widget([  
138 - 'dataProvider' => $dataProvider,  
139 - 'columns' => [  
140 - 'id',  
141 - 'created_at:datetime',  
142 - 'name',  
143 - 'city',  
144 - 174 +<?= GridView::widget(
  175 + [
  176 + 'dataProvider' => $dataProvider,
  177 + 'columns' => [
  178 + 'id',
  179 + 'created_at:datetime',
  180 + 'name',
  181 + 'city',
  182 + [
  183 + 'attribute' => 'orderLabel.label',
  184 + 'label' => 'Метка',
  185 + ],
  186 + 'total',
  187 + [
  188 + 'attribute' => 'reason',
  189 + 'content' => function($model) {
  190 + /**
  191 + * @var Order $model
  192 + */
  193 + if (empty($model->reason)) {
  194 + return '';
  195 + } else {
  196 + return Order::REASONS[ $model->reason ];
  197 + }
  198 + },
  199 + ],
  200 + [
  201 + 'attribute' => 'manager.username',
  202 + 'label' => 'Менеджер',
  203 + ],
  204 + [
  205 + 'attribute' => 'body',
  206 + 'content' => function($model) {
  207 + /**
  208 + * @var Order $model
  209 + */
  210 + if (empty($model->body)) {
  211 + return '';
  212 + } else {
  213 + return Html::a(
  214 + StringHelper::truncate($model->body, 10, '...'),
  215 + '#',
  216 + [
  217 + 'data-toggle' => 'tooltip',
  218 + 'title' => $model->body,
  219 + 'onclick' => 'event.preventDefault();',
  220 + ]
  221 + );
  222 + }
  223 + },
  224 + ],
  225 + [
  226 + 'content' => function($model) {
  227 + /**
  228 + * @var Order $model
  229 + */
  230 + return Html::a(
  231 + Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-eye-open' ]),
  232 + [
  233 + '/ecommerce/order/view',
  234 + 'id' => $model->id,
145 ], 235 ],
146 - ])?> 236 + [
  237 + 'target' => '_blank',
  238 + 'data-pjax' => '0',
  239 + ]
  240 + );
  241 + },
  242 + ],
  243 + ],
  244 + ]
  245 +) ?>