Commit ee0e1df535acc02bc75e94742ef3742ced2e7b49
1 parent
c130fad8
-Statistics half way done
Showing
4 changed files
with
200 additions
and
63 deletions
Show diff stats
controllers/StatisticsController.php
| ... | ... | @@ -4,7 +4,7 @@ |
| 4 | 4 | |
| 5 | 5 | use artweb\artbox\ecommerce\models\Label; |
| 6 | 6 | use artweb\artbox\ecommerce\models\Order; |
| 7 | - use yii\db\Query; | |
| 7 | + use yii\data\ActiveDataProvider; | |
| 8 | 8 | use yii\helpers\ArrayHelper; |
| 9 | 9 | use yii\helpers\VarDumper; |
| 10 | 10 | use yii\web\Controller; |
| ... | ... | @@ -19,33 +19,58 @@ |
| 19 | 19 | if (!empty($date_range)) { |
| 20 | 20 | $arr = []; |
| 21 | 21 | preg_match('@(.*)\s:\s(.*)@', $date_range, $arr); |
| 22 | - $date_from = strtotime($arr[1]); | |
| 23 | - $date_to = strtotime($arr[2]); | |
| 22 | + $dateFilter = [ | |
| 23 | + 'between', | |
| 24 | + 'created_at', | |
| 25 | + strtotime($arr[1]), | |
| 26 | + strtotime($arr[2]), | |
| 27 | + ]; | |
| 24 | 28 | } else { |
| 25 | - $date_from = 0; | |
| 26 | - $date_to = 0; | |
| 29 | + $dateFilter = []; | |
| 27 | 30 | } |
| 28 | - | |
| 29 | - $labels = ArrayHelper::map( | |
| 30 | - Label::find() | |
| 31 | - ->joinWith('lang') | |
| 32 | - ->asArray() | |
| 33 | - ->all(), | |
| 34 | - 'id', | |
| 35 | - 'lang.title' | |
| 36 | - ); | |
| 37 | 31 | |
| 38 | 32 | if (!empty($label)) { |
| 39 | - $label = Label::findOne($label); | |
| 40 | - /** | |
| 41 | - * continue here!!! | |
| 42 | - */ | |
| 33 | + $labelFilter = ['label' => $label]; | |
| 34 | + } else { | |
| 35 | + $labelFilter = []; | |
| 43 | 36 | } |
| 44 | 37 | |
| 38 | + /** | |
| 39 | + * Get labels | |
| 40 | + */ | |
| 41 | + $labels = Label::find() | |
| 42 | + ->with('lang') | |
| 43 | + ->all(); | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Generate statistics | |
| 47 | + */ | |
| 48 | + $labelStatistics = ArrayHelper::map( | |
| 49 | + $labels, | |
| 50 | + function($model) { | |
| 51 | + /** | |
| 52 | + * @var Label $model | |
| 53 | + */ | |
| 54 | + return $model->lang->title; | |
| 55 | + }, | |
| 56 | + function($model) use ($dateFilter) { | |
| 57 | + /** | |
| 58 | + * @var Label $model | |
| 59 | + */ | |
| 60 | + return $model->getStatistics($dateFilter);} | |
| 61 | + ); | |
| 62 | + | |
| 63 | + $dataProvider = new ActiveDataProvider([ | |
| 64 | + 'query' => Order::find()->filterWhere($dateFilter)->andFilterWhere($labelFilter), | |
| 65 | + ]); | |
| 66 | + | |
| 45 | 67 | return $this->render( |
| 46 | 68 | 'index', |
| 47 | 69 | [ |
| 48 | 70 | 'labels' => $labels, |
| 71 | + 'labelStatistics' => $labelStatistics, | |
| 72 | + 'rejectionStatistics' => Order::getRejectionStatistics($dateFilter), | |
| 73 | + 'dataProvider' => $dataProvider, | |
| 49 | 74 | ] |
| 50 | 75 | ); |
| 51 | 76 | } | ... | ... |
models/Label.php
| ... | ... | @@ -65,19 +65,8 @@ |
| 65 | 65 | ]; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - public function getStatistics(int $dateFrom, int $dateTo) | |
| 68 | + public function getStatistics(array $where = []) | |
| 69 | 69 | { |
| 70 | - | |
| 71 | - if ($dateTo != 0) { | |
| 72 | - $where = [ | |
| 73 | - 'between', | |
| 74 | - 'created_at', | |
| 75 | - $dateFrom, | |
| 76 | - $dateTo, | |
| 77 | - ]; | |
| 78 | - } else { | |
| 79 | - $where = []; | |
| 80 | - } | |
| 81 | 70 | $query = ( new Query() )->select( |
| 82 | 71 | [ |
| 83 | 72 | 'sum' => 'SUM(total)', | ... | ... |
models/Order.php
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | use Yii; |
| 8 | 8 | use yii\behaviors\TimestampBehavior; |
| 9 | 9 | use yii\db\ActiveRecord; |
| 10 | + use yii\db\Query; | |
| 10 | 11 | |
| 11 | 12 | /** |
| 12 | 13 | * Class Order |
| ... | ... | @@ -91,6 +92,32 @@ |
| 91 | 92 | { |
| 92 | 93 | return 'order'; |
| 93 | 94 | } |
| 95 | + | |
| 96 | + /** | |
| 97 | + * @param array $where | |
| 98 | + * | |
| 99 | + * @return array | |
| 100 | + */ | |
| 101 | + public static function getRejectionStatistics(array $where = []) | |
| 102 | + { | |
| 103 | + $result = []; | |
| 104 | + foreach (self::REASONS as $id => $reason) { | |
| 105 | + $result[ $reason ] = ( new Query() )->select( | |
| 106 | + [ | |
| 107 | + 'sum' => 'SUM(total)', | |
| 108 | + 'count' => 'COUNT(*)', | |
| 109 | + ] | |
| 110 | + ) | |
| 111 | + ->from(self::tableName()) | |
| 112 | + ->where( | |
| 113 | + [ | |
| 114 | + 'reason' => $id, | |
| 115 | + ] | |
| 116 | + )->andFilterWhere($where)->one(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $result; | |
| 120 | + } | |
| 94 | 121 | |
| 95 | 122 | public function behaviors() |
| 96 | 123 | { |
| ... | ... | @@ -185,7 +212,7 @@ |
| 185 | 212 | public function afterFind() |
| 186 | 213 | { |
| 187 | 214 | parent::afterFind(); |
| 188 | - $this->deadline = !empty( $this->deadline ) ? date('d.m.Y', $this->deadline) : ''; | |
| 215 | + $this->deadline = !empty($this->deadline) ? date('d.m.Y', $this->deadline) : ''; | |
| 189 | 216 | |
| 190 | 217 | } |
| 191 | 218 | |
| ... | ... | @@ -202,7 +229,7 @@ |
| 202 | 229 | |
| 203 | 230 | protected function convertDate() |
| 204 | 231 | { |
| 205 | - if (!empty( $this->deadline )) { | |
| 232 | + if (!empty($this->deadline)) { | |
| 206 | 233 | $date = new \DateTime(); |
| 207 | 234 | $date->setTimestamp(strtotime($this->deadline)); |
| 208 | 235 | $date->format("d.m.Y"); |
| ... | ... | @@ -291,8 +318,8 @@ |
| 291 | 318 | */ |
| 292 | 319 | public function getDeliveryString() |
| 293 | 320 | { |
| 294 | - if (!empty( $this->orderDelivery )) { | |
| 295 | - if (!empty( $this->orderDelivery->parent )) { | |
| 321 | + if (!empty($this->orderDelivery)) { | |
| 322 | + if (!empty($this->orderDelivery->parent)) { | |
| 296 | 323 | return $this->orderDelivery->parent->lang->title . ': ' . $this->orderDelivery->lang->title; |
| 297 | 324 | } else { |
| 298 | 325 | return $this->orderDelivery->lang->title; |
| ... | ... | @@ -310,7 +337,7 @@ |
| 310 | 337 | */ |
| 311 | 338 | public function getWasted() |
| 312 | 339 | { |
| 313 | - if (empty( $this->deadline )) { | |
| 340 | + if (empty($this->deadline)) { | |
| 314 | 341 | return false; |
| 315 | 342 | } else { |
| 316 | 343 | return time() > strtotime($this->deadline); | ... | ... |
views/statistics/index.php
| 1 | 1 | <?php |
| 2 | + use artweb\artbox\ecommerce\models\Label; | |
| 2 | 3 | use kartik\daterange\DateRangePicker; |
| 4 | + use kartik\grid\GridView; | |
| 3 | 5 | use kartik\select2\Select2; |
| 6 | + use yii\data\ActiveDataProvider; | |
| 7 | + use yii\helpers\ArrayHelper; | |
| 4 | 8 | use yii\helpers\Html; |
| 5 | 9 | use yii\web\View; |
| 6 | 10 | |
| 7 | 11 | /** |
| 8 | - * @var View $this | |
| 9 | - * @var array $labels | |
| 12 | + * @var View $this | |
| 13 | + * @var Label[] $labels | |
| 14 | + * @var array $labelStatistics | |
| 15 | + * @var array $rejectionStatistics | |
| 16 | + * @var ActiveDataProvider $dataProvider | |
| 10 | 17 | */ |
| 11 | 18 | |
| 12 | 19 | ?> |
| ... | ... | @@ -19,32 +26,121 @@ |
| 19 | 26 | </div><!-- /.box-tools --> |
| 20 | 27 | </div><!-- /.box-header --> |
| 21 | 28 | <div class="box-body"> |
| 22 | - <?= Html::beginForm(['/ecommerce/statistics'], 'get', [ | |
| 23 | - 'class' => 'form-inline' | |
| 24 | - ])?> | |
| 25 | - | |
| 26 | - <?= DateRangePicker::widget([ | |
| 27 | - 'name' => 'date_range', | |
| 28 | - 'pluginOptions' => [ | |
| 29 | - 'locale' => [ | |
| 30 | - 'format' => 'DD-MM-Y', | |
| 31 | - 'separator' => ' : ', | |
| 32 | - ], | |
| 33 | - ], | |
| 34 | - ])?> | |
| 35 | - | |
| 36 | - <?= Select2::widget([ | |
| 37 | - 'name' => 'label', | |
| 38 | - 'data' => $labels, | |
| 39 | - 'options' => [ | |
| 40 | - 'placeholder' => 'Все', | |
| 41 | - ], | |
| 42 | - ])?> | |
| 43 | - | |
| 44 | - <?= Html::submitButton('Go', [ | |
| 45 | - 'class' => 'btn btn-success', | |
| 46 | - ])?> | |
| 47 | - | |
| 29 | + <?= Html::beginForm( | |
| 30 | + [ '/ecommerce/statistics' ], | |
| 31 | + 'get' | |
| 32 | + ) ?> | |
| 33 | + <div class="row"> | |
| 34 | + <div class="col-md-4"> | |
| 35 | + <?= DateRangePicker::widget( | |
| 36 | + [ | |
| 37 | + 'name' => 'date_range', | |
| 38 | + 'pluginOptions' => [ | |
| 39 | + 'locale' => [ | |
| 40 | + 'format' => 'DD-MM-Y', | |
| 41 | + 'separator' => ' : ', | |
| 42 | + ], | |
| 43 | + ], | |
| 44 | + ] | |
| 45 | + ) ?> | |
| 46 | + </div> | |
| 47 | + <div class="col-md-6"> | |
| 48 | + <?= Select2::widget( | |
| 49 | + [ | |
| 50 | + 'name' => 'label', | |
| 51 | + 'data' => ArrayHelper::map( | |
| 52 | + $labels, | |
| 53 | + function($model) { | |
| 54 | + return $model->id; | |
| 55 | + }, | |
| 56 | + function($model) { | |
| 57 | + return $model->lang->title; | |
| 58 | + } | |
| 59 | + ), | |
| 60 | + 'options' => [ | |
| 61 | + 'placeholder' => 'Все', | |
| 62 | + ], | |
| 63 | + ] | |
| 64 | + ) ?> | |
| 65 | + </div> | |
| 66 | + <div class="col-md-2"> | |
| 67 | + <?= Html::submitButton( | |
| 68 | + 'Go', | |
| 69 | + [ | |
| 70 | + 'class' => 'btn btn-success', | |
| 71 | + ] | |
| 72 | + ) ?> | |
| 73 | + </div> | |
| 74 | + </div> | |
| 48 | 75 | <?= Html::endForm() ?> |
| 49 | 76 | </div><!-- /.box-body --> |
| 50 | 77 | </div><!-- /.box --> |
| 78 | + | |
| 79 | +<div class="box box-default"> | |
| 80 | + <div class="box-header with-border"> | |
| 81 | + <h3 class="box-title">Метки, статистика за </h3> | |
| 82 | + <div class="box-tools pull-right"> | |
| 83 | + <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button> | |
| 84 | + </div><!-- /.box-tools --> | |
| 85 | + </div><!-- /.box-header --> | |
| 86 | + <div class="box-body table-responsive no-padding"> | |
| 87 | + <table class="table table-hover"> | |
| 88 | + <tr> | |
| 89 | + <td><b>Метка</b></td> | |
| 90 | + <td><b>Заказов, шт.</b></td> | |
| 91 | + <td><b>На сумму, грн.</b></td> | |
| 92 | + <td><b>Заказано товаров, шт.</b></td> | |
| 93 | + <td><b>Уникальных товаров, шт.</b></td> | |
| 94 | + </tr> | |
| 95 | + <?php | |
| 96 | + 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 } ?> | |
| 106 | + </table> | |
| 107 | + </div><!-- /.box-body --> | |
| 108 | +</div><!-- /.box --> | |
| 109 | + | |
| 110 | +<div class="box box-default"> | |
| 111 | + <div class="box-header with-border"> | |
| 112 | + <h3 class="box-title">Метки, статистика за </h3> | |
| 113 | + <div class="box-tools pull-right"> | |
| 114 | + <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button> | |
| 115 | + </div><!-- /.box-tools --> | |
| 116 | + </div><!-- /.box-header --> | |
| 117 | + <div class="box-body table-responsive no-padding"> | |
| 118 | + <table class="table table-hover"> | |
| 119 | + <tr> | |
| 120 | + <td><b>Причина</b></td> | |
| 121 | + <td><b>Заказов, шт.</b></td> | |
| 122 | + <td><b>На сумму, грн.</b></td> | |
| 123 | + </tr> | |
| 124 | + <?php | |
| 125 | + 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 } ?> | |
| 133 | + </table> | |
| 134 | + </div><!-- /.box-body --> | |
| 135 | +</div><!-- /.box --> | |
| 136 | + | |
| 137 | +<?=GridView::widget([ | |
| 138 | + 'dataProvider' => $dataProvider, | |
| 139 | + 'columns' => [ | |
| 140 | + 'id', | |
| 141 | + 'created_at:datetime', | |
| 142 | + 'name', | |
| 143 | + 'city', | |
| 144 | + | |
| 145 | + ], | |
| 146 | + ])?> | ... | ... |