diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 046395fa9..e051fb3e5 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -9,6 +9,18 @@ Thelia\Controller\Admin\AdminController::indexAction + + + + Thelia\Controller\Admin\HomeController::defaultAction + + + + Thelia\Controller\Admin\HomeController::loadStatsAjaxAction + + + + Thelia\Controller\Admin\SessionController::showLoginAction diff --git a/core/lib/Thelia/Controller/Admin/HomeController.php b/core/lib/Thelia/Controller/Admin/HomeController.php new file mode 100644 index 000000000..90b0c6655 --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/HomeController.php @@ -0,0 +1,102 @@ +. */ +/* */ +/*************************************************************************************/ + +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); + } +} diff --git a/core/lib/Thelia/Model/CustomerQuery.php b/core/lib/Thelia/Model/CustomerQuery.php index 359d072bd..5c225510b 100755 --- a/core/lib/Thelia/Model/CustomerQuery.php +++ b/core/lib/Thelia/Model/CustomerQuery.php @@ -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 diff --git a/core/lib/Thelia/Model/OrderQuery.php b/core/lib/Thelia/Model/OrderQuery.php index 0fff5dba1..6eee6a81b 100755 --- a/core/lib/Thelia/Model/OrderQuery.php +++ b/core/lib/Thelia/Model/OrderQuery.php @@ -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 diff --git a/templates/admin/default/home.html b/templates/admin/default/home.html index 22989d8fb..0081eeebf 100755 --- a/templates/admin/default/home.html +++ b/templates/admin/default/home.html @@ -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;