This commit is contained in:
Daniel Camargo 2013-06-28 17:44:30 -03:00
parent 1ec5e09d29
commit a052e96cd8
5 changed files with 267 additions and 2 deletions

View File

@ -156,7 +156,55 @@ class Controller_home_charts extends Crunchbutton_Controller_Account {
$this->renderColumn( $chart->byMonth( true ) );
break;
/* Gift card */
case 'gift-cards-created-per-day':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->createdByDay( true ) );
break;
case 'gift-cards-created-per-week':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->createdByWeek( true ) );
break;
case 'gift-cards-created-per-month':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->createdByMonth( true ) );
break;
case 'gift-cards-redeemed-per-day':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedByDay( true ) );
break;
case 'gift-cards-redeemed-per-week':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedByWeek( true ) );
break;
case 'gift-cards-redeemed-per-month':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedByMonth( true ) );
break;
case 'gift-cards-redeemed-per-group-per-day':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedPerGroupByDay( true ) );
break;
case 'gift-cards-redeemed-per-group-per-week':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedPerGroupByWeek( true ) );
break;
case 'gift-cards-redeemed-per-group-per-month':
$chart = new Crunchbutton_Chart_Giftcard();
$this->renderColumn( $chart->redeemedPerGroupByMonth( true ) );
break;
/* Others */
case 'weeks':
echo $this->chart->weeksToJson();
break;

View File

@ -99,6 +99,25 @@ class Controller_home extends Crunchbutton_Controller_Account {
'users-reclaimed-per-week' => 'Reclaimed Users per Week',
),
'Tracking Marketing Efforts' => array(
'gift-cards-created-per-' => array(
'_title' => 'Gift Cards Created per',
'day' => 'Day',
'week' => 'Week',
'month' => 'Month',
),
'gift-cards-redeemed-per-' => array(
'_title' => 'Gift Cards Redeemed per',
'day' => 'Day',
'week' => 'Week',
'month' => 'Month',
),
'gift-cards-redeemed-per-group-per-' => array(
'_title' => 'Gift Cards Redeemed per Group per',
'day' => 'Day',
'week' => 'Week',
'month' => 'Month',
),
'active-users-per-week' => 'Active Users per Week',
'active-users-per-week-by-community' => 'Active Users per Week by Community',
'orders-by-weekday-by-community' => 'Orders by Weekday by Community',

View File

@ -278,10 +278,35 @@ class Crunchbutton_Chart extends Cana_Model {
return $data;
}
public function parseDataWeeksGroup( $query ){
public function parseDataDaysGroup( $query ){
$data = c::db()->get( $query );
$_days = [];
$groups = [];
foreach ( $data as $item ) {
$groups[ $item->Group ] = $item->Group;
$_days[ $item->Day ][ $item->Group ] = $item->Total;
}
$data = [];
$allDays = $this->allDays();
for( $i = $this->from_day -1 ; $i < $this->to_day; $i++ ){
$days = $allDays[ $i ];
foreach( $groups as $group ){
$total = ( $_days[ $days ][ $group ] ) ? $_days[ $days ][ $group ] : 0;
$data[] = ( object ) array( 'Label' => $this->parseDay( $days ), 'Total' => $total, 'Type' => $group );
}
}
return $data;
}
public function parseDataWeeksGroup( $query ){
$data = c::db()->get( $query );
$_weeks = [];
$groups = [];
foreach ( $data as $item ) {
@ -300,7 +325,32 @@ class Crunchbutton_Chart extends Cana_Model {
$data[] = ( object ) array( 'Label' => $this->parseWeek( $week ), 'Total' => $total, 'Type' => $group );
}
}
return $data;
}
public function parseDataMonthGroup( $query ){
$data = c::db()->get( $query );
$_months = [];
$groups = [];
foreach ( $data as $item ) {
$groups[ $item->Group ] = $item->Group;
$_months[ $item->Month ][ $item->Group ] = $item->Total;
}
$data = [];
$allMonths = $this->allMonths();
for( $i = $this->from -1 ; $i < $this->to; $i++ ){
$month = $allMonths[ $i ];
foreach( $groups as $group ){
$total = ( $_months[ $month ][ $group ] ) ? $_months[ $month ][ $group ] : 0;
$data[] = ( object ) array( 'Label' => $this->parseMonth( $month, true ), 'Total' => $total, 'Type' => $group );
}
}
return $data;
}
}

View File

@ -1,4 +1,5 @@
<?php
class Crunchbutton_Chart_Churn extends Crunchbutton_Chart {
public $unit = 'users';

View File

@ -0,0 +1,147 @@
<?php
class Crunchbutton_Chart_Giftcard extends Crunchbutton_Chart {
public $unit = 'gift cards';
public $description = 'Gift cards';
public function __construct() {
parent::__construct();
}
public function createdByDay( $render = false ){
$query = "SELECT
DATE_FORMAT( p.date ,'%Y-%m-%d' ) AS Day,
COUNT(*) AS Total
FROM promo p
WHERE DATE_FORMAT( p.date ,'%Y-%m-%d' )
GROUP BY DATE_FORMAT( p.date ,'%Y-%m-%d' ) HAVING Day BETWEEN '{$this->dayFrom}' AND '{$this->dayTo}'";
$parsedData = $this->parseDataDaysSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'day' );
}
return $parsedData;
}
public function redeemedByDay( $render = false ){
$query = "SELECT
DATE_FORMAT(c.date ,'%Y-%m-%d') AS Day,
COUNT(*) AS Total
FROM credit c
WHERE id_promo
GROUP BY DATE_FORMAT(c.date ,'%Y-%m-%d') HAVING Day BETWEEN '{$this->dayFrom}' AND '{$this->dayTo}'";
$parsedData = $this->parseDataDaysSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'day' );
}
return $parsedData;
}
public function redeemedPerGroupByDay( $render = false ){
$query = "SELECT DATE_FORMAT(c.date ,'%Y-%m-%d') AS Day,
COUNT(*) AS Total,
promos.Name AS 'Group'
FROM credit c
INNER JOIN ( SELECT pgp.id_promo, pg.name FROM promo_group pg
INNER JOIN promo_group_promo pgp ON pgp.id_promo_group = pg.id_promo_group ) promos ON promos.id_promo = c.id_promo
GROUP BY promos.Name, DATE_FORMAT(c.date ,'%Y-%m-%d') HAVING Day BETWEEN '{$this->dayFrom}' AND '{$this->dayTo}'";
$parsedData = $this->parseDataDaysGroup( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'day' );
}
return $parsedData;
}
public function createdByWeek( $render = false ){
$query = "SELECT
YEARWEEK(p.date) AS Week,
COUNT(*) AS Total
FROM promo p
WHERE YEARWEEK(p.date)
GROUP BY YEARWEEK(p.date) HAVING Week BETWEEN '{$this->weekFrom}' AND '{$this->weekTo}'";
$parsedData = $this->parseDataWeeksSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity );
}
return $parsedData;
}
public function redeemedByWeek( $render = false ){
$query = "SELECT
YEARWEEK(c.date) AS Week,
COUNT(*) AS Total
FROM credit c
WHERE YEARWEEK(c.date)
GROUP BY YEARWEEK(c.date) HAVING Week BETWEEN '{$this->weekFrom}' AND '{$this->weekTo}'";
$parsedData = $this->parseDataWeeksSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity );
}
return $parsedData;
}
public function redeemedPerGroupByWeek( $render = false ){
$query = "SELECT YEARWEEK(c.date) AS Week,
COUNT(*) AS Total,
promos.Name AS 'Group'
FROM credit c
INNER JOIN ( SELECT pgp.id_promo, pg.name FROM promo_group pg
INNER JOIN promo_group_promo pgp ON pgp.id_promo_group = pg.id_promo_group ) promos ON promos.id_promo = c.id_promo
GROUP BY promos.Name, YEARWEEK(c.date) HAVING Week BETWEEN '{$this->weekFrom}' AND '{$this->weekTo}'";
$parsedData = $this->parseDataWeeksGroup( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity );
}
return $parsedData;
}
public function createdByMonth( $render = false ){
$query = "SELECT
DATE_FORMAT( p.date ,'%Y-%m' ) AS Month,
COUNT(*) AS Total
FROM promo p
WHERE DATE_FORMAT( p.date ,'%Y-%m' )
GROUP BY DATE_FORMAT( p.date ,'%Y-%m' ) HAVING Month BETWEEN '{$this->monthFrom}' AND '{$this->monthTo}'";
$parsedData = $this->parseDataMonthSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'month' );
}
return $parsedData;
}
public function redeemedByMonth( $render = false ){
$query = "SELECT
DATE_FORMAT(c.date ,'%Y-%m') AS Month,
COUNT(*) AS Total
FROM credit c
WHERE id_promo
GROUP BY DATE_FORMAT( c.date ,'%Y-%m' ) HAVING Month BETWEEN '{$this->monthFrom}' AND '{$this->monthTo}'";
$parsedData = $this->parseDataMonthSimple( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'month' );
}
return $parsedData;
}
public function redeemedPerGroupByMonth( $render = false ){
$query = "SELECT DATE_FORMAT(c.date ,'%Y-%m') AS Month,
COUNT(*) AS Total,
promos.Name AS 'Group'
FROM credit c
INNER JOIN ( SELECT pgp.id_promo, pg.name FROM promo_group pg
INNER JOIN promo_group_promo pgp ON pgp.id_promo_group = pg.id_promo_group ) promos ON promos.id_promo = c.id_promo
GROUP BY promos.Name, DATE_FORMAT(c.date ,'%Y-%m') HAVING Month BETWEEN '{$this->monthFrom}' AND '{$this->monthTo}'";
$parsedData = $this->parseDataMonthGroup( $query, $this->description );
if( $render ){
return array( 'data' => $parsedData, 'unit' => $this->unity, 'interval' => 'month' );
}
return $parsedData;
}
}