Commit c130fad8e1f45575ea418e4d9f066af45901d3bd

Authored by Alexey Boroda
1 parent 1ac35977

-Yml fix

controllers/StatisticsController.php
@@ -2,15 +2,51 @@ @@ -2,15 +2,51 @@
2 2
3 namespace artweb\artbox\ecommerce\controllers; 3 namespace artweb\artbox\ecommerce\controllers;
4 4
  5 + use artweb\artbox\ecommerce\models\Label;
  6 + use artweb\artbox\ecommerce\models\Order;
  7 + use yii\db\Query;
  8 + use yii\helpers\ArrayHelper;
  9 + use yii\helpers\VarDumper;
5 use yii\web\Controller; 10 use yii\web\Controller;
6 11
7 - /**  
8 - * BrandController implements the CRUD actions for Brand model.  
9 - */  
10 class StatisticsController extends Controller 12 class StatisticsController extends Controller
11 { 13 {
12 - public function actionIndex() 14 + public function actionIndex($date_range = NULL, $label = NULL)
13 { 15 {
14 - return $this->render('index'); 16 + /**
  17 + * Get a dates range
  18 + */
  19 + if (!empty($date_range)) {
  20 + $arr = [];
  21 + preg_match('@(.*)\s:\s(.*)@', $date_range, $arr);
  22 + $date_from = strtotime($arr[1]);
  23 + $date_to = strtotime($arr[2]);
  24 + } else {
  25 + $date_from = 0;
  26 + $date_to = 0;
  27 + }
  28 +
  29 + $labels = ArrayHelper::map(
  30 + Label::find()
  31 + ->joinWith('lang')
  32 + ->asArray()
  33 + ->all(),
  34 + 'id',
  35 + 'lang.title'
  36 + );
  37 +
  38 + if (!empty($label)) {
  39 + $label = Label::findOne($label);
  40 + /**
  41 + * continue here!!!
  42 + */
  43 + }
  44 +
  45 + return $this->render(
  46 + 'index',
  47 + [
  48 + 'labels' => $labels,
  49 + ]
  50 + );
15 } 51 }
16 } 52 }
@@ -5,6 +5,8 @@ @@ -5,6 +5,8 @@
5 use artweb\artbox\language\behaviors\LanguageBehavior; 5 use artweb\artbox\language\behaviors\LanguageBehavior;
6 use yii\db\ActiveQuery; 6 use yii\db\ActiveQuery;
7 use yii\db\ActiveRecord; 7 use yii\db\ActiveRecord;
  8 + use yii\db\Query;
  9 + use yii\helpers\VarDumper;
8 use yii\web\Request; 10 use yii\web\Request;
9 11
10 /** 12 /**
@@ -62,4 +64,52 @@ @@ -62,4 +64,52 @@
62 ], 64 ],
63 ]; 65 ];
64 } 66 }
  67 +
  68 + public function getStatistics(int $dateFrom, int $dateTo)
  69 + {
  70 +
  71 + if ($dateTo != 0) {
  72 + $where = [
  73 + 'between',
  74 + 'created_at',
  75 + $dateFrom,
  76 + $dateTo,
  77 + ];
  78 + } else {
  79 + $where = [];
  80 + }
  81 + $query = ( new Query() )->select(
  82 + [
  83 + 'sum' => 'SUM(total)',
  84 + 'count' => 'COUNT(*)',
  85 + 'unique' => ( new Query() )->select('COUNT(*)')
  86 + ->from('order')
  87 + ->leftJoin(
  88 + 'order_product',
  89 + '"order"."id"="order_product"."order_id"'
  90 + )
  91 + ->where([ 'order.label' => $this->id ])
  92 + ->andFilterWhere(
  93 + $where
  94 + ),
  95 + 'products' => ( new Query() )->select('SUM(count)')
  96 + ->from('order')
  97 + ->leftJoin(
  98 + 'order_product',
  99 + '"order"."id"="order_product"."order_id"'
  100 + )
  101 + ->where([ 'order.label' => $this->id ])
  102 + ->andFilterWhere(
  103 + $where
  104 + ),
  105 + ]
  106 + )
  107 + ->from('order')
  108 + ->where([ 'label' => $this->id ])
  109 + ->andFilterWhere(
  110 + $where
  111 + );
  112 +
  113 + return $query->one();
  114 + }
65 } 115 }
views/statistics/index.php
1 <?php 1 <?php
  2 + use kartik\daterange\DateRangePicker;
  3 + use kartik\select2\Select2;
  4 + use yii\helpers\Html;
2 use yii\web\View; 5 use yii\web\View;
3 6
4 /** 7 /**
5 * @var View $this 8 * @var View $this
  9 + * @var array $labels
6 */ 10 */
7 11
8 ?> 12 ?>
@@ -15,6 +19,32 @@ @@ -15,6 +19,32 @@
15 </div><!-- /.box-tools --> 19 </div><!-- /.box-tools -->
16 </div><!-- /.box-header --> 20 </div><!-- /.box-header -->
17 <div class="box-body"> 21 <div class="box-body">
18 - The body of the box 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 +
  48 + <?= Html::endForm() ?>
19 </div><!-- /.box-body --> 49 </div><!-- /.box-body -->
20 </div><!-- /.box --> 50 </div><!-- /.box -->