Commit d733149067386d4baa75e51a9a1a9b4b1c1cf9c1
1 parent
60fa8910
-Export ready
Showing
2 changed files
with
48 additions
and
25 deletions
Show diff stats
controllers/StatisticsController.php
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | use common\models\User; |
| 8 | 8 | use yii\data\ActiveDataProvider; |
| 9 | 9 | use yii\helpers\ArrayHelper; |
| 10 | + use yii\helpers\Html; | |
| 10 | 11 | use yii\helpers\VarDumper; |
| 11 | 12 | use yii\web\Controller; |
| 12 | 13 | use yii\web\Response; |
| ... | ... | @@ -186,7 +187,8 @@ |
| 186 | 187 | |
| 187 | 188 | public function actionExport($date_range = NULL, $label = NULL, $manager = NULL) |
| 188 | 189 | { |
| 189 | - \Yii::$app->response = Response::FORMAT_JSON; | |
| 190 | + \Yii::$app->response->format = Response::FORMAT_JSON; | |
| 191 | + | |
| 190 | 192 | $formatter = \Yii::$app->formatter; |
| 191 | 193 | /** |
| 192 | 194 | * Get export options |
| ... | ... | @@ -203,19 +205,19 @@ |
| 203 | 205 | } else { |
| 204 | 206 | $dateFilter = []; |
| 205 | 207 | } |
| 206 | - | |
| 208 | + | |
| 207 | 209 | if (!empty($label)) { |
| 208 | 210 | $labelFilter = [ 'label' => $label ]; |
| 209 | 211 | } else { |
| 210 | 212 | $labelFilter = []; |
| 211 | 213 | } |
| 212 | - | |
| 214 | + | |
| 213 | 215 | if (!empty($manager)) { |
| 214 | 216 | $managerFilter = [ 'manager_id' => $manager ]; |
| 215 | 217 | } else { |
| 216 | 218 | $managerFilter = []; |
| 217 | 219 | } |
| 218 | - | |
| 220 | + | |
| 219 | 221 | $orders = Order::find() |
| 220 | 222 | ->with( |
| 221 | 223 | [ |
| ... | ... | @@ -227,10 +229,9 @@ |
| 227 | 229 | ->filterWhere($dateFilter) |
| 228 | 230 | ->andFilterWhere($labelFilter) |
| 229 | 231 | ->andFilterWhere($managerFilter) |
| 230 | - ->limit(10) | |
| 231 | 232 | ->all(); |
| 232 | 233 | |
| 233 | - $file = fopen(\Yii::getAlias('@backend') . '/web/export/' . time() . '_export.csv', 'a'); | |
| 234 | + $file = fopen(\Yii::getAlias('@frontend/web/') . 'statistics_export.csv', 'w'); | |
| 234 | 235 | foreach ($orders as $order) { |
| 235 | 236 | $line = []; |
| 236 | 237 | /** |
| ... | ... | @@ -238,35 +239,40 @@ |
| 238 | 239 | */ |
| 239 | 240 | $line[] = $formatter->asDecimal($order->id); |
| 240 | 241 | $line[] = $formatter->asDatetime($order->created_at); |
| 241 | - $line[] = $formatter->asText($order->name); | |
| 242 | + $line[] = (string) $order->name; | |
| 242 | 243 | if (empty($order->products)) { |
| 243 | 244 | $line[] = ''; |
| 244 | 245 | } else { |
| 245 | 246 | $i = 0; |
| 246 | 247 | $products = ''; |
| 247 | 248 | foreach ($order->products as $product) { |
| 249 | + $i ++; | |
| 248 | 250 | $products .= $product->sku; |
| 249 | 251 | if (count($order->products) != $i) { |
| 250 | - $products .= ' ;'; | |
| 252 | + $products .= ' ,'; | |
| 251 | 253 | } |
| 252 | 254 | } |
| 253 | 255 | $line[] = $products; |
| 254 | 256 | } |
| 255 | - $line[] = $formatter->asText($order->city); | |
| 256 | - $line[] = $formatter->asText($order->orderLabel->label); | |
| 257 | - $line[] = $formatter->asDecimal($order->total); | |
| 258 | - $line[] = empty($order->reason) ? '' : $formatter->asText(Order::REASONS[$order->reason]); | |
| 257 | + $line[] = (string) $order->city; | |
| 258 | + $line[] = (string) $order->orderLabel->label; | |
| 259 | + $line[] = (string) $order->total; | |
| 260 | + $line[] = empty($order->reason) ? '' : $formatter->asText(Order::REASONS[ $order->reason ]); | |
| 259 | 261 | $line[] = empty($order->manager) ? '' : $formatter->asText($order->manager->username); |
| 260 | - $line[] = $formatter->asText($order->body); | |
| 262 | + $line[] = (string) $order->body; | |
| 261 | 263 | |
| 262 | 264 | fputcsv($file, $line); |
| 263 | 265 | } |
| 264 | 266 | fclose($file); |
| 265 | - | |
| 266 | - /** | |
| 267 | - * Start here | |
| 268 | - * @todo Ajax download button generation | |
| 269 | - * change ->asText - it generates html | |
| 270 | - */ | |
| 267 | + | |
| 268 | + return [ | |
| 269 | + 'message' => 'Файл успешно сгенерирован', | |
| 270 | + 'button' => Html::a( | |
| 271 | + Html::tag('i', '', [ | |
| 272 | + 'class' => 'glyphicon glyphicon-download-alt' | |
| 273 | + ]) . ' Скачать', '/statistics_export.csv', [ | |
| 274 | + 'class' => 'btn bg-olive', | |
| 275 | + ]) | |
| 276 | + ]; | |
| 271 | 277 | } |
| 272 | 278 | } | ... | ... |
views/statistics/index.php
| ... | ... | @@ -10,6 +10,7 @@ |
| 10 | 10 | use yii\helpers\ArrayHelper; |
| 11 | 11 | use yii\helpers\Html; |
| 12 | 12 | use yii\helpers\StringHelper; |
| 13 | + use yii\helpers\Url; | |
| 13 | 14 | use yii\web\View; |
| 14 | 15 | use yiier\chartjs\ChartJs; |
| 15 | 16 | |
| ... | ... | @@ -29,11 +30,11 @@ |
| 29 | 30 | * @var int | boolean $dataLabel |
| 30 | 31 | * @var int | boolean $dataManager |
| 31 | 32 | */ |
| 32 | - | |
| 33 | - $js = <<< JS | |
| 34 | -$('[data-toggle="popover"]').popover(); | |
| 35 | -JS; | |
| 36 | - $this->registerJs($js, View::POS_READY); | |
| 33 | + | |
| 34 | + $this->registerJsFile('/admin/js/statistics.js', [ | |
| 35 | + 'position' => View::POS_END, | |
| 36 | + 'depends' => 'yii\web\JqueryAsset' | |
| 37 | + ]); | |
| 37 | 38 | |
| 38 | 39 | ?> |
| 39 | 40 | |
| ... | ... | @@ -360,10 +361,26 @@ JS; |
| 360 | 361 | |
| 361 | 362 | </div><!-- /.box --> |
| 362 | 363 | |
| 363 | - | |
| 364 | +<p> | |
| 365 | +<?php | |
| 366 | + echo Html::button(Html::tag('i', '', [ | |
| 367 | + 'class' => 'glyphicon glyphicon-cog', | |
| 368 | + ]) . ' Создать выгрузку', [ | |
| 369 | + 'class' => 'btn bg-navy', | |
| 370 | + 'id' => 'generate-button', | |
| 371 | + 'data-link' => Url::to([ | |
| 372 | + 'export', | |
| 373 | + 'date_range' => $dateValue, | |
| 374 | + 'label' => $dataLabel, | |
| 375 | + 'manager' => $dataManager, | |
| 376 | + ]) | |
| 377 | + ]) | |
| 378 | +?> | |
| 379 | +</p> | |
| 364 | 380 | <div class="box box-default"> |
| 365 | 381 | <div class="box-header with-border"> |
| 366 | 382 | <h3 class="box-title">Заказы</h3> |
| 383 | + | |
| 367 | 384 | <div class="box-tools pull-right"> |
| 368 | 385 | <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button> |
| 369 | 386 | </div><!-- /.box-tools --> | ... | ... |