diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index 36486e4..58dd2c8 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -4,6 +4,7 @@ use artweb\artbox\ecommerce\models\Label; use artweb\artbox\ecommerce\models\Order; + use common\models\User; use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; @@ -11,7 +12,7 @@ class StatisticsController extends Controller { - public function actionIndex($date_range = NULL, $label = NULL) + public function actionIndex($date_range = NULL, $label = NULL, $manager = NULL) { /** * Get a dates range @@ -34,6 +35,12 @@ } else { $labelFilter = []; } + + if (!empty($manager)) { + $managerFilter = [ 'manager_id' => $manager ]; + } else { + $managerFilter = []; + } /** * Get labels @@ -41,34 +48,41 @@ $labels = Label::find() ->with('lang') ->all(); + + /** + * Get user + */ + $managers = User::find() + ->all(); /** * Generate statistics */ $labelStatistics = ArrayHelper::map( $labels, - function($model) { - /** - * @var Label $model - */ + function(Label $model) { return $model->lang->title; }, - function($model) use ($dateFilter) { - /** - * @var Label $model - */ - return $model->getStatistics($dateFilter); + function(Label $model) use ($dateFilter, $managerFilter) { + return $model->getStatistics($dateFilter, $managerFilter); } ); - + + /** + * Data provider for table + */ $dataProvider = new ActiveDataProvider( [ 'query' => Order::find() ->filterWhere($dateFilter) + ->andFilterWhere($managerFilter) ->andFilterWhere($labelFilter), ] ); + /** + * Creating charts data + */ $labelChartData1 = [ 'labels' => array_keys($labelStatistics), 'datasets' => [ @@ -94,7 +108,7 @@ ], ], ]; - + $labelChartData3 = [ 'labels' => array_keys($labelStatistics), 'datasets' => [ @@ -115,8 +129,14 @@ ], ]; - $rejectStatistics = Order::getRejectionStatistics($dateFilter); - + /** + * Getting rejection statistics + */ + $rejectStatistics = Order::getRejectionStatistics($dateFilter, $managerFilter); + + /** + * Charts data for rejects + */ $rejectChartData1 = [ 'labels' => array_keys($rejectStatistics), 'datasets' => [ @@ -129,7 +149,7 @@ ], ], ]; - + $rejectChartData2 = [ 'labels' => array_keys($rejectStatistics), 'datasets' => [ @@ -147,16 +167,18 @@ 'index', [ 'labels' => $labels, + 'managers' => $managers, 'labelStatistics' => $labelStatistics, 'rejectionStatistics' => $rejectStatistics, 'dataProvider' => $dataProvider, - 'labelChartData1' => $labelChartData1, - 'labelChartData2' => $labelChartData2, - 'labelChartData3' => $labelChartData3, - 'rejectChartData1' => $rejectChartData1, - 'rejectChartData2' => $rejectChartData2, + 'labelChartData1' => $labelChartData1, + 'labelChartData2' => $labelChartData2, + 'labelChartData3' => $labelChartData3, + 'rejectChartData1' => $rejectChartData1, + 'rejectChartData2' => $rejectChartData2, 'dateValue' => empty($date_range) ? '' : $date_range, 'dataLabel' => empty($label) ? false : $label, + 'dataManager' => empty($manager) ? false : $manager, ] ); } diff --git a/models/Label.php b/models/Label.php index 2b25476..b56e5e5 100755 --- a/models/Label.php +++ b/models/Label.php @@ -65,40 +65,43 @@ ]; } - public function getStatistics(array $where = []) + public function getStatistics(array $date = [], array $manager = []) { - $query = ( new Query() )->select( - [ - 'sum' => 'SUM(total)', - 'count' => 'COUNT(*)', - 'unique' => ( new Query() )->select('COUNT(*)') - ->from('order') - ->leftJoin( - 'order_product', - '"order"."id"="order_product"."order_id"' - ) - ->where([ 'order.label' => $this->id ]) - ->andFilterWhere( - $where - ), - 'products' => ( new Query() )->select('SUM(count)') - ->from('order') - ->leftJoin( - 'order_product', - '"order"."id"="order_product"."order_id"' - ) - ->where([ 'order.label' => $this->id ]) - ->andFilterWhere( - $where - ), - ] - ) - ->from('order') - ->where([ 'label' => $this->id ]) - ->andFilterWhere( - $where - ); - + $query = ( new Query() )->select( + [ + 'sum' => 'SUM(total)', + 'count' => 'COUNT(*)', + 'unique' => ( new Query() )->select('COUNT(*)') + ->from('order') + ->leftJoin( + 'order_product', + '"order"."id"="order_product"."order_id"' + ) + ->where([ 'order.label' => $this->id ]) + ->andFilterWhere( + $date + ) + ->andFilterWhere($manager), + 'products' => ( new Query() )->select('SUM(count)') + ->from('order') + ->leftJoin( + 'order_product', + '"order"."id"="order_product"."order_id"' + ) + ->where([ 'order.label' => $this->id ]) + ->andFilterWhere( + $date + ) + ->andFilterWhere($manager), + ] + ) + ->from('order') + ->where([ 'label' => $this->id ]) + ->andFilterWhere( + $date + ) + ->andFilterWhere($manager); + return $query->one(); } } diff --git a/models/Order.php b/models/Order.php index 1aae862..e3e6bdf 100755 --- a/models/Order.php +++ b/models/Order.php @@ -102,11 +102,12 @@ } /** - * @param array $where + * @param array $date + * @param array $manager * * @return array */ - public static function getRejectionStatistics(array $where = []) + public static function getRejectionStatistics(array $date = [], array $manager = []) { $result = []; foreach (self::REASONS as $id => $reason) { @@ -122,7 +123,8 @@ 'reason' => $id, ] ) - ->andFilterWhere($where) + ->andFilterWhere($date) + ->andFilterWhere($manager) ->one(); } @@ -190,8 +192,8 @@ ]; } elseif ($attr == 'pay') { return [ - 'old' => ($values[ 'old' ] == true) ? 'Оплачен' : 'Не оплачен', - 'new' => ($values[ 'new' ] == true) ? 'Оплачен' : 'Не оплачен', + 'old' => ( $values[ 'old' ] == true ) ? 'Оплачен' : 'Не оплачен', + 'new' => ( $values[ 'new' ] == true ) ? 'Оплачен' : 'Не оплачен', ]; } else { return $values; diff --git a/views/statistics/index.php b/views/statistics/index.php index 32448f0..5bdc139 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -1,6 +1,8 @@ registerJs($js, View::POS_READY); - + $this->registerJs($js, View::POS_READY); + ?>
@@ -46,7 +50,7 @@ $this->registerJs($js, View::POS_READY); 'get' ) ?>
-
+
'date_range', @@ -60,7 +64,7 @@ $this->registerJs($js, View::POS_READY); ] ) ?>
-
+
'label', @@ -75,7 +79,30 @@ $this->registerJs($js, View::POS_READY); } ), 'options' => [ - 'placeholder' => 'Все', + 'placeholder' => 'Все метки', + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ] + ) ?> +
+
+ 'manager', + 'value' => $dataManager, + 'data' => ArrayHelper::map( + $managers, + function(User $model) { + return $model->id; + }, + function(User $model) { + return $model->username; + } + ), + 'options' => [ + 'placeholder' => 'Все менеджеры', ], 'pluginOptions' => [ 'allowClear' => true, @@ -98,7 +125,7 @@ $this->registerJs($js, View::POS_READY);
-

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

+

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

@@ -124,15 +151,17 @@ $this->registerJs($js, View::POS_READY); } ?>
- +