diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index d7dc58f..6b92b1f 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -2,15 +2,51 @@ namespace artweb\artbox\ecommerce\controllers; + use artweb\artbox\ecommerce\models\Label; + use artweb\artbox\ecommerce\models\Order; + use yii\db\Query; + use yii\helpers\ArrayHelper; + use yii\helpers\VarDumper; use yii\web\Controller; - /** - * BrandController implements the CRUD actions for Brand model. - */ class StatisticsController extends Controller { - public function actionIndex() + public function actionIndex($date_range = NULL, $label = NULL) { - return $this->render('index'); + /** + * Get a dates range + */ + if (!empty($date_range)) { + $arr = []; + preg_match('@(.*)\s:\s(.*)@', $date_range, $arr); + $date_from = strtotime($arr[1]); + $date_to = strtotime($arr[2]); + } else { + $date_from = 0; + $date_to = 0; + } + + $labels = ArrayHelper::map( + Label::find() + ->joinWith('lang') + ->asArray() + ->all(), + 'id', + 'lang.title' + ); + + if (!empty($label)) { + $label = Label::findOne($label); + /** + * continue here!!! + */ + } + + return $this->render( + 'index', + [ + 'labels' => $labels, + ] + ); } } diff --git a/models/Label.php b/models/Label.php index bd2128d..3fb94dc 100755 --- a/models/Label.php +++ b/models/Label.php @@ -5,6 +5,8 @@ use artweb\artbox\language\behaviors\LanguageBehavior; use yii\db\ActiveQuery; use yii\db\ActiveRecord; + use yii\db\Query; + use yii\helpers\VarDumper; use yii\web\Request; /** @@ -62,4 +64,52 @@ ], ]; } + + public function getStatistics(int $dateFrom, int $dateTo) + { + + if ($dateTo != 0) { + $where = [ + 'between', + 'created_at', + $dateFrom, + $dateTo, + ]; + } else { + $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( + $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 + ); + + return $query->one(); + } } diff --git a/views/statistics/index.php b/views/statistics/index.php index e4e464d..0784a93 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -1,8 +1,12 @@ @@ -15,6 +19,32 @@
- The body of the box + 'form-inline' + ])?> + + 'date_range', + 'pluginOptions' => [ + 'locale' => [ + 'format' => 'DD-MM-Y', + 'separator' => ' : ', + ], + ], + ])?> + + 'label', + 'data' => $labels, + 'options' => [ + 'placeholder' => 'Все', + ], + ])?> + + 'btn btn-success', + ])?> + +
-- libgit2 0.21.4