From a052e96cd86aa71e47fa7b2551a559fae9f4aa3f Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Fri, 28 Jun 2013 17:44:30 -0300 Subject: [PATCH] partial #1465 --- .../default/cockpit/home/charts.php | 48 ++++++ .../default/cockpit/home/index.php | 19 +++ include/library/Crunchbutton/Chart.php | 54 ++++++- include/library/Crunchbutton/Chart/Churn.php | 1 + .../library/Crunchbutton/Chart/Giftcard.php | 147 ++++++++++++++++++ 5 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 include/library/Crunchbutton/Chart/Giftcard.php diff --git a/include/controllers/default/cockpit/home/charts.php b/include/controllers/default/cockpit/home/charts.php index b13f618d0..9ba0d1bfc 100644 --- a/include/controllers/default/cockpit/home/charts.php +++ b/include/controllers/default/cockpit/home/charts.php @@ -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; diff --git a/include/controllers/default/cockpit/home/index.php b/include/controllers/default/cockpit/home/index.php index 4c366a6ae..47024ea13 100644 --- a/include/controllers/default/cockpit/home/index.php +++ b/include/controllers/default/cockpit/home/index.php @@ -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', diff --git a/include/library/Crunchbutton/Chart.php b/include/library/Crunchbutton/Chart.php index 57e2f588e..a41f99b98 100644 --- a/include/library/Crunchbutton/Chart.php +++ b/include/library/Crunchbutton/Chart.php @@ -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; + } + } \ No newline at end of file diff --git a/include/library/Crunchbutton/Chart/Churn.php b/include/library/Crunchbutton/Chart/Churn.php index 704400073..8396c3a54 100644 --- a/include/library/Crunchbutton/Chart/Churn.php +++ b/include/library/Crunchbutton/Chart/Churn.php @@ -1,4 +1,5 @@ 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; + } + +} \ No newline at end of file