diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index 6b92b1f..0c4cb96 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -4,7 +4,7 @@ use artweb\artbox\ecommerce\models\Label; use artweb\artbox\ecommerce\models\Order; - use yii\db\Query; + use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; use yii\web\Controller; @@ -19,33 +19,58 @@ if (!empty($date_range)) { $arr = []; preg_match('@(.*)\s:\s(.*)@', $date_range, $arr); - $date_from = strtotime($arr[1]); - $date_to = strtotime($arr[2]); + $dateFilter = [ + 'between', + 'created_at', + strtotime($arr[1]), + strtotime($arr[2]), + ]; } else { - $date_from = 0; - $date_to = 0; + $dateFilter = []; } - - $labels = ArrayHelper::map( - Label::find() - ->joinWith('lang') - ->asArray() - ->all(), - 'id', - 'lang.title' - ); if (!empty($label)) { - $label = Label::findOne($label); - /** - * continue here!!! - */ + $labelFilter = ['label' => $label]; + } else { + $labelFilter = []; } + /** + * 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 + */ + return $model->getStatistics($dateFilter);} + ); + + $dataProvider = new ActiveDataProvider([ + 'query' => Order::find()->filterWhere($dateFilter)->andFilterWhere($labelFilter), + ]); + return $this->render( 'index', [ 'labels' => $labels, + 'labelStatistics' => $labelStatistics, + 'rejectionStatistics' => Order::getRejectionStatistics($dateFilter), + 'dataProvider' => $dataProvider, ] ); } diff --git a/models/Label.php b/models/Label.php index 3fb94dc..2b25476 100755 --- a/models/Label.php +++ b/models/Label.php @@ -65,19 +65,8 @@ ]; } - public function getStatistics(int $dateFrom, int $dateTo) + public function getStatistics(array $where = []) { - - if ($dateTo != 0) { - $where = [ - 'between', - 'created_at', - $dateFrom, - $dateTo, - ]; - } else { - $where = []; - } $query = ( new Query() )->select( [ 'sum' => 'SUM(total)', diff --git a/models/Order.php b/models/Order.php index 244f1ba..5e54089 100755 --- a/models/Order.php +++ b/models/Order.php @@ -7,6 +7,7 @@ use Yii; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; + use yii\db\Query; /** * Class Order @@ -91,6 +92,32 @@ { return 'order'; } + + /** + * @param array $where + * + * @return array + */ + public static function getRejectionStatistics(array $where = []) + { + $result = []; + foreach (self::REASONS as $id => $reason) { + $result[ $reason ] = ( new Query() )->select( + [ + 'sum' => 'SUM(total)', + 'count' => 'COUNT(*)', + ] + ) + ->from(self::tableName()) + ->where( + [ + 'reason' => $id, + ] + )->andFilterWhere($where)->one(); + } + + return $result; + } public function behaviors() { @@ -185,7 +212,7 @@ public function afterFind() { parent::afterFind(); - $this->deadline = !empty( $this->deadline ) ? date('d.m.Y', $this->deadline) : ''; + $this->deadline = !empty($this->deadline) ? date('d.m.Y', $this->deadline) : ''; } @@ -202,7 +229,7 @@ protected function convertDate() { - if (!empty( $this->deadline )) { + if (!empty($this->deadline)) { $date = new \DateTime(); $date->setTimestamp(strtotime($this->deadline)); $date->format("d.m.Y"); @@ -291,8 +318,8 @@ */ public function getDeliveryString() { - if (!empty( $this->orderDelivery )) { - if (!empty( $this->orderDelivery->parent )) { + if (!empty($this->orderDelivery)) { + if (!empty($this->orderDelivery->parent)) { return $this->orderDelivery->parent->lang->title . ': ' . $this->orderDelivery->lang->title; } else { return $this->orderDelivery->lang->title; @@ -310,7 +337,7 @@ */ public function getWasted() { - if (empty( $this->deadline )) { + if (empty($this->deadline)) { return false; } else { return time() > strtotime($this->deadline); diff --git a/views/statistics/index.php b/views/statistics/index.php index 0784a93..27dbc71 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -1,12 +1,19 @@ @@ -19,32 +26,121 @@
- 'form-inline' - ])?> - - 'date_range', - 'pluginOptions' => [ - 'locale' => [ - 'format' => 'DD-MM-Y', - 'separator' => ' : ', - ], - ], - ])?> - - 'label', - 'data' => $labels, - 'options' => [ - 'placeholder' => 'Все', - ], - ])?> - - 'btn btn-success', - ])?> - + +
+
+ 'date_range', + 'pluginOptions' => [ + 'locale' => [ + 'format' => 'DD-MM-Y', + 'separator' => ' : ', + ], + ], + ] + ) ?> +
+
+ 'label', + 'data' => ArrayHelper::map( + $labels, + function($model) { + return $model->id; + }, + function($model) { + return $model->lang->title; + } + ), + 'options' => [ + 'placeholder' => 'Все', + ], + ] + ) ?> +
+
+ 'btn btn-success', + ] + ) ?> +
+
+ +
+
+

Метки, статистика за

+
+ +
+
+
+ + + + + + + + + $statistic) { + ?> + + + + + + + + +
МеткаЗаказов, шт.На сумму, грн.Заказано товаров, шт.Уникальных товаров, шт.
+
+
+ +
+
+

Метки, статистика за

+
+ +
+
+
+ + + + + + + $statistic) { + ?> + + + + + + +
ПричинаЗаказов, шт.На сумму, грн.
+
+
+ + $dataProvider, + 'columns' => [ + 'id', + 'created_at:datetime', + 'name', + 'city', + + ], + ])?> -- libgit2 0.21.4