admin home stats

This commit is contained in:
Etienne Roudeix
2013-10-30 16:32:24 +01:00
parent a540c50395
commit 083dde5e93
5 changed files with 209 additions and 10 deletions

View File

@@ -9,6 +9,18 @@
<default key="_controller">Thelia\Controller\Admin\AdminController::indexAction</default>
</route>
<!-- home -->
<route id="admin.home.view" path="/admin/home">
<default key="_controller">Thelia\Controller\Admin\HomeController::defaultAction</default>
</route>
<route id="admin.home.stats" path="/admin/home/stats">
<default key="_controller">Thelia\Controller\Admin\HomeController::loadStatsAjaxAction</default>
</route>
<!-- end home -->
<!-- Route to the administration login page -->
<route id="admin.login" path="/admin/login">
<default key="_controller">Thelia\Controller\Admin\SessionController::showLoginAction</default>

View File

@@ -0,0 +1,102 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Core\Security\AccessManager;
use Thelia\Model\CustomerQuery;
use Thelia\Model\OrderQuery;
class HomeController extends BaseAdminController
{
const RESOURCE_CODE = "admin.home";
public function defaultAction()
{
if (null !== $response = $this->checkAuth(self::RESOURCE_CODE, AccessManager::VIEW)) return $response;
// Render the edition template.
return $this->render('home');
}
public function loadStatsAjaxAction()
{
$data = new \stdClass();
$data->title = "Stats on [...]";
/* sales */
$saleSeries = new \stdClass();
$saleSeries->color = $this->getRequest()->request->get('sales_color', '#adadad');
$saleSeries->data = OrderQuery::getSaleStats(
$this->getRequest()->request->get('month', date('m')),
$this->getRequest()->request->get('year', date('Y'))
);
/* new customers */
$newCustomerSeries = new \stdClass();
$newCustomerSeries->color = $this->getRequest()->request->get('customers_color', '#f39922');
$newCustomerSeries->data = CustomerQuery::getNewCustomersStats(
$this->getRequest()->request->get('month', date('m')),
$this->getRequest()->request->get('year', date('Y'))
);
/* orders */
$orderSeries = new \stdClass();
$orderSeries->color = $this->getRequest()->request->get('orders_color', '#5cb85c');
$orderSeries->data = OrderQuery::getOrdersStats(
$this->getRequest()->request->get('month', date('m')),
$this->getRequest()->request->get('year', date('Y'))
);
/* first order */
$firstOrderSeries = new \stdClass();
$firstOrderSeries->color = $this->getRequest()->request->get('first_orders_color', '#5bc0de');
$firstOrderSeries->data = OrderQuery::getFirstOrdersStats(
$this->getRequest()->request->get('month', date('m')),
$this->getRequest()->request->get('year', date('Y'))
);
/* cancelled orders */
$cancelledOrderSeries = new \stdClass();
$cancelledOrderSeries->color = $this->getRequest()->request->get('cancelled_orders_color', '#d9534f');
$cancelledOrderSeries->data = OrderQuery::getOrdersStats(
$this->getRequest()->request->get('month', date('m')),
$this->getRequest()->request->get('year', date('Y')),
array(5)
);
$data->series = array(
$saleSeries,
$newCustomerSeries,
$orderSeries,
$firstOrderSeries,
$cancelledOrderSeries,
);
$json = json_encode($data);
return $this->jsonResponse($json);
}
}

View File

@@ -2,6 +2,7 @@
namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Model\Base\CustomerQuery as BaseCustomerQuery;
@@ -21,4 +22,20 @@ class CustomerQuery extends BaseCustomerQuery {
{
return self::create()->findOneByEmail($email);
}
public static function getNewCustomersStats($month, $year)
{
$numberOfDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$stats = array();
for($day=1; $day<=$numberOfDay; $day++) {
$dayCustomers = self::create()
->filterByCreatedAt(sprintf("%s-%s-%s 00:00:00", $year, $month, $day), Criteria::GREATER_EQUAL)
->filterByCreatedAt(sprintf("%s-%s-%s 23:59:59", $year, $month, $day), Criteria::LESS_EQUAL)
->count();
$stats[] = array($day-1, $dayCustomers);
}
return $stats;
}
} // CustomerQuery

View File

@@ -3,6 +3,7 @@
namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Model\Base\OrderQuery as BaseOrderQuery;
@@ -54,4 +55,71 @@ class OrderQuery extends BaseOrderQuery
return $obj;
}
public static function getSaleStats($month, $year)
{
$numberOfDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$stats = array();
for($day=1; $day<=$numberOfDay; $day++) {
$dayAmount = 0;
foreach(self::create()
->filterByStatusId(array(2,3,4), Criteria::IN)
->filterByCreatedAt(sprintf("%s-%s-%s 00:00:00", $year, $month, $day), Criteria::GREATER_EQUAL)
->filterByCreatedAt(sprintf("%s-%s-%s 23:59:59", $year, $month, $day), Criteria::LESS_EQUAL)
->find() as $dayOrders) {
$dayAmount += $dayOrders->getTotalAmount();
}
$stats[] = array($day-1, $dayAmount);
}
return $stats;
}
public static function getOrdersStats($month, $year, $status = null)
{
$numberOfDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$stats = array();
for($day=1; $day<=$numberOfDay; $day++) {
$dayOrdersQuery = self::create()
->filterByCreatedAt(sprintf("%s-%s-%s 00:00:00", $year, $month, $day), Criteria::GREATER_EQUAL)
->filterByCreatedAt(sprintf("%s-%s-%s 23:59:59", $year, $month, $day), Criteria::LESS_EQUAL);
if(null !== $status) {
$dayOrdersQuery->filterByStatusId($status, Criteria::IN);
}
$dayOrders = $dayOrdersQuery->count();
$stats[] = array($day-1, $dayOrders);
}
return $stats;
}
public static function getFirstOrdersStats($month, $year)
{
$numberOfDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$stats = array();
for($day=1; $day<=$numberOfDay; $day++) {
$dayOrdersQuery = self::create('matching_order')
->filterByCreatedAt(sprintf("%s-%s-%s 00:00:00", $year, $month, $day), Criteria::GREATER_EQUAL)
->filterByCreatedAt(sprintf("%s-%s-%s 23:59:59", $year, $month, $day), Criteria::LESS_EQUAL);
$otherOrderJoin = new Join();
$otherOrderJoin->addExplicitCondition(OrderTableMap::TABLE_NAME, 'CUSTOMER_ID', 'matching_order', OrderTableMap::TABLE_NAME, 'CUSTOMER_ID', 'other_order');
$otherOrderJoin->setJoinType(Criteria::LEFT_JOIN);
$dayOrdersQuery->addJoinObject($otherOrderJoin, 'other_order_join')
->addJoinCondition('other_order_join', '`matching_order`.`ID` <> `other_order`.`ID`')
->addJoinCondition('other_order_join', '`matching_order`.`CREATED_AT` > `other_order`.`CREATED_AT`');
$dayOrdersQuery->where('ISNULL(`other_order`.`ID`)');
$dayOrders = $dayOrdersQuery->count();
$stats[] = array($day-1, $dayOrders);
}
return $stats;
}
} // OrderQuery

View File

@@ -274,7 +274,7 @@
var $elem = $('#jqplot');
var url = "{url file='/test_to_remove/admin-stats.json'}",
var url = "{url path='/admin/home/stats'}",
series = [],
seriesColors = [],
ticks = [],
@@ -318,12 +318,12 @@
}
};
// Get datas Json
// Get data Json
$.getJSON(url)
.done(function(data) {
// Init series datas and colors
initJqplotDatas(series, seriesColors, options, data);
// Init series data and colors
initJqplotData(series, seriesColors, options, data);
// Add days to xaxis
for(var i = 1; i < (days+1); i++){
@@ -350,8 +350,8 @@
series = [];
seriesColors = [];
// Init series datas and colors
initJqplotDatas(series, seriesColors, options, data);
// Init series data and colors
initJqplotData(series, seriesColors, options, data);
// Restart jqplot
jqplot.destroy();
@@ -372,15 +372,15 @@
});
function initJqplotDatas(series, seriesColors, options, json){
function initJqplotData(series, seriesColors, options, json){
$('[data-toggle="jqplot-serie"].active').each(function(i){
var position = $(this).index() - 1;
series.push(json.series[position].datas);
series.push(json.series[position].data);
seriesColors.push(json.series[position].color);
});
// Number of days to display ( = datas.length in one serie)
days = json.series[0].datas.length;
// Number of days to display ( = data.length in one serie)
days = json.series[0].data.length;
// Graph title
options.title = json.title;