From 894d945a1b996af2ccb02a1ee459843c7b058fa6 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 30 Jan 2017 16:01:15 +0200 Subject: [PATCH] -Logs bu fix and Slava's task --- components/OrderProductLogger.php | 8 +++++++- controllers/StatisticsController.php | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- models/Label.php | 2 ++ models/OrderProduct.php | 4 ++-- views/order/log.php | 40 ++++++++++++++++++++-------------------- views/statistics/index.php | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------- views/statistics/view.php | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 331 insertions(+), 71 deletions(-) create mode 100644 views/statistics/view.php diff --git a/components/OrderProductLogger.php b/components/OrderProductLogger.php index c9dd58a..e839ac6 100644 --- a/components/OrderProductLogger.php +++ b/components/OrderProductLogger.php @@ -2,6 +2,7 @@ namespace artweb\artbox\ecommerce\components; use artweb\artbox\ecommerce\models\OrderProductLog; + use common\models\User; use yii\base\Object; use yii\helpers\Json; @@ -40,9 +41,14 @@ $log = new OrderProductLog(); $log->order_product_id = (integer) $identityId; $log->order_id = $params['order_id']; - $log->user_id = (integer) \Yii::$app->user->identity->id; $log->data = Json::encode($data); + if (\Yii::$app->user instanceof User) { + $log->user_id = (integer) \Yii::$app->user->identity->id; + } else { + $log->user_id = NULL; + } + $log->save(); } } diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index 64cc6d7..5e53802 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -8,7 +8,6 @@ use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; use yii\helpers\Html; - use yii\helpers\VarDumper; use yii\web\Controller; use yii\web\Response; @@ -185,6 +184,56 @@ ); } + public function actionView($id = NULL, $manager = NULL, $date = NULL) + { + /** + * Get a dates range + */ + if (!empty($date)) { + $arr = []; + preg_match('@(.*)\s:\s(.*)@', $date, $arr); + $dateFilter = [ + 'between', + 'created_at', + strtotime($arr[ 1 ]), + strtotime($arr[ 2 ]), + ]; + } else { + $dateFilter = []; + } + + if (!empty($id)) { + $labelFilter = [ 'label' => $id ]; + } else { + $labelFilter = []; + } + + if (!empty($manager)) { + $managerFilter = [ 'manager_id' => $manager ]; + } else { + $managerFilter = []; + } + + $dataProvider = new ActiveDataProvider( + [ + 'query' => Order::find() + ->filterWhere($labelFilter) + ->andFilterWhere($dateFilter) + ->andFilterWhere($managerFilter), + ] + ); + + $label = Label::find()->with('lang')->where(['id' => $id])->one(); + + return $this->render( + 'view', + [ + 'dataProvider' => $dataProvider, + 'label' => $label, + ] + ); + } + public function actionExport($date_range = NULL, $label = NULL, $manager = NULL) { \Yii::$app->response->format = Response::FORMAT_JSON; @@ -246,7 +295,7 @@ $i = 0; $products = ''; foreach ($order->products as $product) { - $i ++; + $i++; $products .= $product->sku; if (count($order->products) != $i) { $products .= ' ,'; @@ -267,12 +316,19 @@ return [ 'message' => 'Файл успешно сгенерирован', - 'button' => Html::a( - Html::tag('i', '', [ - 'class' => 'glyphicon glyphicon-download-alt' - ]) . ' Скачать', '/storage/statistics_export.csv', [ - 'class' => 'btn bg-olive', - ]) + 'button' => Html::a( + Html::tag( + 'i', + '', + [ + 'class' => 'glyphicon glyphicon-download-alt', + ] + ) . ' Скачать', + '/storage/statistics_export.csv', + [ + 'class' => 'btn bg-olive', + ] + ), ]; } } diff --git a/models/Label.php b/models/Label.php index b56e5e5..0048c0f 100755 --- a/models/Label.php +++ b/models/Label.php @@ -69,6 +69,7 @@ { $query = ( new Query() )->select( [ + 'label', 'sum' => 'SUM(total)', 'count' => 'COUNT(*)', 'unique' => ( new Query() )->select('COUNT(*)') @@ -96,6 +97,7 @@ ] ) ->from('order') + ->groupBy('label') ->where([ 'label' => $this->id ]) ->andFilterWhere( $date diff --git a/models/OrderProduct.php b/models/OrderProduct.php index da057b1..15fe2a8 100755 --- a/models/OrderProduct.php +++ b/models/OrderProduct.php @@ -37,8 +37,8 @@ public function afterSave($insert, $changedAttributes) { -// $data = OrderProductLogger::generateData($changedAttributes, $this->oldAttributes, $insert); -// OrderProductLogger::saveData($data, $this->id, [ 'order_id' => $this->order_id ]); + $data = OrderProductLogger::generateData($changedAttributes, $this->oldAttributes, $insert); + OrderProductLogger::saveData($data, $this->id, [ 'order_id' => $this->order_id ]); parent::afterSave($insert, $changedAttributes); } diff --git a/views/order/log.php b/views/order/log.php index b0bf19a..5b4d6de 100755 --- a/views/order/log.php +++ b/views/order/log.php @@ -70,26 +70,26 @@ $productLogData, -// 'layout' => '{items}', -// 'itemView' => '_log_product_item', -// 'itemOptions' => [ -// 'tag' => false, -// ], -// 'viewParams' => [ -// 'order' => $model, -// ], -// 'options' => [ -// 'class' => 'list-view', -// ], -// 'emptyText' => 'У этого заказа нет товаров', -// 'emptyTextOptions' => [ -// 'class' => 'callout callout-info', -// ], -// ] -// ); + echo ListView::widget( + [ + 'dataProvider' => $productLogData, + 'layout' => '{items}', + 'itemView' => '_log_product_item', + 'itemOptions' => [ + 'tag' => false, + ], + 'viewParams' => [ + 'order' => $model, + ], + 'options' => [ + 'class' => 'list-view', + ], + 'emptyText' => 'У этого заказа нет товаров', + 'emptyTextOptions' => [ + 'class' => 'callout callout-info', + ], + ] + ); ?> diff --git a/views/statistics/index.php b/views/statistics/index.php index 110d0d1..ffb8b19 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -30,11 +30,18 @@ * @var int | boolean $dataLabel * @var int | boolean $dataManager */ - - $this->registerJsFile('/admin/js/statistics.js', [ - 'position' => View::POS_END, - 'depends' => 'yii\web\JqueryAsset' - ]); + + $this->registerJsFile( + '/admin/js/statistics.js', + [ + 'position' => View::POS_END, + 'depends' => 'yii\web\JqueryAsset', + ] + ); + + $this->title = 'Статистика заказов'; + + $this->params['breadcrumbs'][] = $this->title; ?> @@ -141,30 +148,49 @@ Уникальных товаров, шт. $statistic) { + if ($statistic) { + $link = Html::a( + $name, + [ + 'view', + 'id' => $statistic[ 'label' ], + 'manager' => $dataManager, + 'date' => $dateValue, + ], + [ + 'target' => '_blank', + ] + ); + } else { + $link = $name; + } $total_count += $statistic[ 'count' ]; $total_sum += $statistic[ 'sum' ]; $total_products += $statistic[ 'products' ]; $total_unique += $statistic[ 'unique' ]; echo Html::tag( 'tr', - Html::tag('td', $name) . Html::tag('td', $statistic[ 'count' ]) . Html::tag( + Html::tag( + 'td', + $link + ) . Html::tag('td', $statistic[ 'count' ]) . Html::tag( 'td', $statistic[ 'sum' ] ) . Html::tag('td', $statistic[ 'products' ]) . Html::tag('td', $statistic[ 'unique' ]) ); } - ?> + ?> Всего - - - - + + + + @@ -362,25 +388,34 @@

- 'glyphicon glyphicon-cog', - ]) . ' Создать выгрузку', [ - 'class' => 'btn bg-navy', - 'id' => 'generate-button', - 'data-link' => Url::to([ - 'export', - 'date_range' => $dateValue, - 'label' => $dataLabel, - 'manager' => $dataManager, - ]) - ]) -?> + 'glyphicon glyphicon-cog', + ] + ) . ' Создать выгрузку', + [ + 'class' => 'btn bg-navy', + 'id' => 'generate-button', + 'data-link' => Url::to( + [ + 'export', + 'date_range' => $dateValue, + 'label' => $dataLabel, + 'manager' => $dataManager, + ] + ), + ] + ) + ?>

Заказы

- +
@@ -392,11 +427,12 @@ 'columns' => [ 'id', [ - 'label' => 'Дата добавления', - 'content' => function(Order $model) { - return \Yii::$app->formatter->asDate($model->created_at) . - '
' . \Yii::$app->formatter->asTime($model->created_at); - }, + 'label' => 'Дата добавления', + 'content' => function(Order $model) { + return \Yii::$app->formatter->asDate( + $model->created_at + ) . '
' . \Yii::$app->formatter->asTime($model->created_at); + }, ], 'name', [ @@ -408,11 +444,11 @@ $content = ''; $i = 0; foreach ($model->products as $product) { - if(empty($product->productVariant)){ - $image = ''; - } else { - $image = $product->productVariant->imageUrl; - } + if (empty($product->productVariant)) { + $image = ''; + } else { + $image = $product->productVariant->imageUrl; + } $i++; $content .= Html::a( $product->sku, diff --git a/views/statistics/view.php b/views/statistics/view.php new file mode 100644 index 0000000..86464f3 --- /dev/null +++ b/views/statistics/view.php @@ -0,0 +1,160 @@ +title = 'Статистика по метке: ' . $label->lang->title; + + $this->params['breadcrumbs'][] = [ + 'url' => ['index'], + 'label' => 'Статистика заказов', + ]; + $this->params['breadcrumbs'][] = $this->title; + + $js = <<< JS + $('[data-toggle="popover"]').popover(); +JS; +$this->registerJs($js, View::POS_READY); + +?> + +
+
+

title?>

+
+
+ + $dataProvider, + 'columns' => [ + 'id', + [ + 'label' => 'Дата добавления', + 'content' => function(Order $model) { + return \Yii::$app->formatter->asDate( + $model->created_at + ) . '
' . \Yii::$app->formatter->asTime($model->created_at); + }, + ], + 'name', + [ + 'label' => 'Товары', + 'content' => function(Order $model) { + if (empty($model->products)) { + return ''; + } else { + $content = ''; + $i = 0; + foreach ($model->products as $product) { + if (empty($product->productVariant)) { + $image = ''; + } else { + $image = $product->productVariant->imageUrl; + } + $i++; + $content .= Html::a( + $product->sku, + '#', + [ + 'onclick' => 'event.preventDefault();', + 'data-toggle' => 'popover', + 'data-placement' => 'right', + 'data-html' => 'true', + 'data-content' => Html::img( + $image, + [ + 'class' => 'img-rounded', + ] + ) . Html::tag('p', $product->product_name), + ] + ); + if ($i != count($model->products)) { + $content .= ', '; + } + if ($i % 2 == 0) { + $content .= '
'; + } + } + return $content; + } + }, + ], + 'city', + [ + 'attribute' => 'orderLabel.label', + 'label' => 'Метка', + ], + 'total', + [ + 'attribute' => 'reason', + 'content' => function($model) { + /** + * @var Order $model + */ + if (empty($model->reason)) { + return ''; + } else { + return Order::REASONS[ $model->reason ]; + } + }, + ], + [ + 'attribute' => 'manager.username', + 'label' => 'Менеджер', + ], + [ + 'attribute' => 'body', + 'content' => function($model) { + /** + * @var Order $model + */ + if (empty($model->body)) { + return ''; + } else { + return Html::a( + StringHelper::truncate($model->body, 10, '...'), + '#', + [ + 'data-toggle' => 'tooltip', + 'title' => $model->body, + 'onclick' => 'event.preventDefault();', + ] + ); + } + }, + ], + [ + 'content' => function($model) { + /** + * @var Order $model + */ + return Html::a( + Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-eye-open' ]), + [ + '/ecommerce/order/view', + 'id' => $model->id, + ], + [ + 'target' => '_blank', + 'data-pjax' => '0', + ] + ); + }, + ], + ], + ] + ) ?> + +
+
-- libgit2 0.21.4