From ed616e4b3f1b935d62e4e59796ba73eecec60e44 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Mon, 8 Jul 2013 16:26:58 -0300 Subject: [PATCH] partial #1476 --- .../default/cockpit/home/charts.php | 24 +- .../default/cockpit/home/index.php | 2 +- include/library/Crunchbutton/Chart/Order.php | 246 +++++++++++++++++- include/library/Crunchbutton/Chart/User.php | 27 +- 4 files changed, 289 insertions(+), 10 deletions(-) diff --git a/include/controllers/default/cockpit/home/charts.php b/include/controllers/default/cockpit/home/charts.php index 87462cc4a..d98ad77ab 100644 --- a/include/controllers/default/cockpit/home/charts.php +++ b/include/controllers/default/cockpit/home/charts.php @@ -107,6 +107,11 @@ class Controller_home_charts extends Crunchbutton_Controller_Account { $this->renderColumn( $chart->byUsersPerMonth( true ), $chart->getGroupedCharts() ); break; + case 'orders-per-day': + $chart = new Crunchbutton_Chart_Order(); + $this->renderColumn( $chart->byDay( true ), $chart->getGroupedCharts() ); + break; + case 'orders-per-week': $chart = new Crunchbutton_Chart_Order(); $this->renderColumn( $chart->byWeek( true ), $chart->getGroupedCharts() ); @@ -137,9 +142,24 @@ class Controller_home_charts extends Crunchbutton_Controller_Account { $this->renderPieCommunities( $chart->perRestaurantPerCommunity( true ), $chart->getGroupedCharts() ); break; - case 'orders-repeat-vs-news': + case 'orders-repeat-vs-news-per-week': $chart = new Crunchbutton_Chart_Order(); - $this->renderColumn( $chart->repeatVsNew( true ), $chart->getGroupedCharts() ); + $this->renderColumn( $chart->repeatVsNewPerWeek( true ), $chart->getGroupedCharts() ); + break; + + case 'orders-repeat-vs-news-per-month': + $chart = new Crunchbutton_Chart_Order(); + $this->renderColumn( $chart->repeatVsNewPerMonth( true ), $chart->getGroupedCharts() ); + break; + + case 'orders-repeat-vs-news-per-day': + $chart = new Crunchbutton_Chart_Order(); + $this->renderColumn( $chart->repeatVsNewPerDay( true ), $chart->getGroupedCharts() ); + break; + + case 'orders-repeat-vs-news-per-week-per-community': + $chart = new Crunchbutton_Chart_Order(); + $this->renderColumn( $chart->repeatVsNewPerWeekPerCommunity( true ), $chart->getGroupedCharts() ); break; /* Churn */ diff --git a/include/controllers/default/cockpit/home/index.php b/include/controllers/default/cockpit/home/index.php index 597b66bfa..19b5f9a4f 100644 --- a/include/controllers/default/cockpit/home/index.php +++ b/include/controllers/default/cockpit/home/index.php @@ -35,7 +35,7 @@ class Controller_home extends Crunchbutton_Controller_Account { $graphs = array( '' => array( // Groups - array( 'divId' => 'chart-orders', 'title-group' => 'Orders', 'chart-url' => 'orders-per-week' ), + array( 'divId' => 'chart-orders', 'title-group' => 'Orders', 'chart-url' => 'orders-per-day' ), array( 'divId' => 'chart-users', 'title-group' => 'Users', 'chart-url' => 'users-new-per-day' ), array( 'divId' => 'chart-gross', 'title-group' => 'Gross Revenue', 'chart-url' => 'gross-revenue-per-week' ), array( 'divId' => 'chart-churn', 'title-group' => 'Churn Rate', 'chart-url' => 'churn-rate-per-week' ), diff --git a/include/library/Crunchbutton/Chart/Order.php b/include/library/Crunchbutton/Chart/Order.php index 6aaadaf40..790051e48 100644 --- a/include/library/Crunchbutton/Chart/Order.php +++ b/include/library/Crunchbutton/Chart/Order.php @@ -8,6 +8,7 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { public $groups = array( 'group1' => array( + 'orders-per-day' => 'Orders per Day', 'orders-per-week' => 'Orders per Week', 'orders-per-month' => 'Orders per Month', 'orders-by-user-per-week' => 'Orders by User per Week', @@ -15,7 +16,11 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { 'orders-per-week-by-community' => 'Orders per Week by Community', 'orders-repeat-per-active-user' => 'Repeat Orders per Active User', 'orders-by-weekday-by-community' => 'Orders by Weekday by Community', - 'orders-per-restaurant-by-community' => 'Orders per Restaurant by Community', + 'orders-per-restaurant-by-community' => 'Orders per Restaurant by Community', + 'orders-repeat-vs-news-per-day' => 'Repeat vs. New Orders per Day', + 'orders-repeat-vs-news-per-week' => 'Repeat vs. New Orders per Week', + 'orders-repeat-vs-news-per-month' => 'Repeat vs. New Orders per Month', + 'orders-repeat-vs-news-per-week-per-community' => 'Repeat vs. New Orders per Week per Community', ) ); @@ -86,6 +91,26 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { return $parsedData; } + public function byDay( $render = false ){ + + $query = "SELECT DATE_FORMAT(o.date ,'%Y-%m-%d') AS Day, + COUNT(*) AS Total + FROM `order` o + INNER JOIN user u ON u.id_user = o.id_user + LEFT JOIN community c ON o.id_community = c.id_community + WHERE + 1 = 1 + {$this->queryExcludeCommunties} + {$this->queryExcludeUsers} + GROUP BY DATE_FORMAT(o.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 byWeekPerCommunity( $render = false ){ $query = "SELECT YEARWEEK(date) AS Week, @@ -106,6 +131,26 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { return $parsedData; } + public function byDayPerCommunity( $render = false ){ + + $query = "SELECT DATE_FORMAT(o.date ,'%Y-%m-%d') AS Day, + COUNT(*) AS Total, + r.community AS 'Group' + FROM `order` o + INNER JOIN user u ON u.id_user = o.id_user + LEFT JOIN restaurant r ON r.id_restaurant = o.id_restaurant + WHERE 1 = 1 + AND r.community IS NOT NULL + {$this->queryExcludeUsers} + GROUP BY DATE_FORMAT(o.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 ); + } + return $parsedData; + } + public function perRestaurantPerCommunity( $render = false ){ $query = "SELECT @@ -116,7 +161,7 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { (SELECT count(*) AS orders, o.id_restaurant FROM `order` o - -- WHERE o.date BETWEEN CURDATE() - INTERVAL 60 DAY AND CURDATE() + WHERE o.date BETWEEN CURDATE() - INTERVAL 14 DAY AND CURDATE() GROUP BY o.id_restaurant) orders INNER JOIN restaurant r ON r.id_restaurant = orders.id_restaurant"; @@ -172,16 +217,205 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart { return $parsedData; } - public function repeatVsNew( $render = false ){ + public function repeatVsNewPerDay( $render = false ){ + + $user = new Crunchbutton_Chart_User(); + $newUsers = $user->newByDay(); + $orders = $this->byDay(); + + $data = []; + + $days = []; + + foreach ( $orders as $order ) { + $days[ $order->Label ] = []; + $days[ $order->Label ][ 'Order' ] = $order->Total; + } + + foreach ( $newUsers as $new ) { + if( !$days[ $new->Label ] ){ + $days[ $new->Label ] = []; + } + $days[ $new->Label ][ 'New' ] = $new->Total; + } + + $data = []; + + foreach ( $days as $label => $values ) { + $new = $values[ 'New' ]; + $repeat = $values[ 'Order' ] - $new; + $data[] = ( object ) array( 'Label' => $label, 'Total' => $new, 'Type' => 'New' ); + $data[] = ( object ) array( 'Label' => $label, 'Total' => $repeat, 'Type' => 'Repeated' ); + } + + if( $render ){ + return array( 'data' => $data, 'unit' => $this->unity, 'interval' => 'day' ); + } + return $data; + } + + public function repeatVsNewPerDayPerCommunity( $render = false ){ + + $user = new Crunchbutton_Chart_User(); + $newUsers = $user->newByDayByCommunity(); + $orders = $this->byDayPerCommunity(); + + $data = []; + + $days = []; + $groups = []; + + foreach ( $orders as $order ) { + if( !$days[ $order->Label ] ){ + $days[ $order->Label ] = []; + } + $days[ $order->Label ][ 'Order' ][ $order->Type ] = $order->Total; + $groups[ $order->Type ] = true; + } + + foreach ( $newUsers as $new ) { + if( !$days[ $new->Label ] ){ + $days[ $new->Label ] = []; + } + $days[ $new->Label ][ 'New' ][ $new->Type ] = $new->Total; + $groups[ $new->Type ] = true; + } + + $data = []; + + foreach ( $days as $label => $values ) { + foreach( $groups as $group => $val ){ + $new = ( $values[ 'New' ][ $group ] ) ? $values[ 'New' ][ $group ] : 0; + $orders = ( $values[ 'Order' ][ $group ] ) ? $values[ 'Order' ][ $group ] : 0; + $repeat = $orders - $new; + $data[] = ( object ) array( 'Label' => $label, 'Total' => $new, 'Type' => "$group New" ); + $data[] = ( object ) array( 'Label' => $label, 'Total' => $repeat, 'Type' => "$group Repeated" ); + } + } + + if( $render ){ + return array( 'data' => $data, 'unit' => $this->unity, 'interval' => 'day' ); + } + return $data; + } + + public function repeatVsNewPerWeekPerCommunity( $render = false ){ + + $user = new Crunchbutton_Chart_User(); + $newUsers = $user->newByWeekByCommunity(); + $orders = $this->byWeekPerCommunity(); + + $data = []; + + $weeks = []; + $groups = []; + + foreach ( $orders as $order ) { + if( !$weeks[ $order->Label ] ){ + $weeks[ $order->Label ] = []; + } + $weeks[ $order->Label ][ 'Order' ][ $order->Type ] = $order->Total; + $groups[ $order->Type ] = true; + } + + foreach ( $newUsers as $new ) { + if( !$weeks[ $new->Label ] ){ + $weeks[ $new->Label ] = []; + } + $weeks[ $new->Label ][ 'New' ][ $new->Type ] = $new->Total; + $groups[ $new->Type ] = true; + } + + $data = []; + + foreach ( $weeks as $label => $values ) { + foreach( $groups as $group => $val ){ + $new = ( $values[ 'New' ][ $group ] ) ? $values[ 'New' ][ $group ] : 0; + $orders = ( $values[ 'Order' ][ $group ] ) ? $values[ 'Order' ][ $group ] : 0; + $repeat = $orders - $new; + $data[] = ( object ) array( 'Label' => $label, 'Total' => $new, 'Type' => "$group New" ); + $data[] = ( object ) array( 'Label' => $label, 'Total' => $repeat, 'Type' => "$group Repeated" ); + } + } + + if( $render ){ + return array( 'data' => $data, 'unit' => $this->unity ); + } + return $data; + } + + public function repeatVsNewPerWeek( $render = false ){ $user = new Crunchbutton_Chart_User(); $newUsers = $user->newByWeek(); + $orders = $this->byWeek(); - echo '
';
-		var_dump($newUsers);
+		$data = [];
 
-		exit;
+		$weeks = [];
 
+		foreach ( $orders as $order ) {
+			$weeks[ $order->Label ] = [];
+			$weeks[ $order->Label ][ 'Order' ] = $order->Total;
+		}
+
+		foreach ( $newUsers as $new ) {
+			if( !$weeks[ $new->Label ] ){
+				$weeks[ $new->Label ] = [];	
+			}
+			$weeks[ $new->Label ][ 'New' ] = $new->Total;
+		}
+
+		$data = [];
+
+		foreach ( $weeks as $label => $values ) {
+			$new = $values[ 'New' ];
+			$repeat = $values[ 'Order' ] - $new;
+			$data[] = ( object ) array( 'Label' => $label, 'Total' => $new, 'Type' => 'New'  ); 
+			$data[] = ( object ) array( 'Label' => $label, 'Total' => $repeat, 'Type' => 'Repeated'  ); 
+		}
+
+		if( $render ){
+			return array( 'data' => $data, 'unit' => $this->unity );
+		}
+		return $data;
+	}
+
+	public function repeatVsNewPerMonth( $render = false ){
+
+		$user = new Crunchbutton_Chart_User();
+		$newUsers = $user->newByMonth();
+		$orders = $this->byMonth();
+
+		$data = [];
+
+		$months = [];
+
+		foreach ( $orders as $order ) {
+			$months[ $order->Label ] = [];
+			$months[ $order->Label ][ 'Order' ] = $order->Total;
+		}
+
+		foreach ( $newUsers as $new ) {
+			if( !$months[ $new->Label ] ){
+				$months[ $new->Label ] = [];	
+			}
+			$months[ $new->Label ][ 'New' ] = $new->Total;
+		}
+
+		$data = [];
+
+		foreach ( $months as $label => $values ) {
+			$new = $values[ 'New' ];
+			$repeat = $values[ 'Order' ] - $new;
+			$data[] = ( object ) array( 'Label' => $label, 'Total' => $new, 'Type' => 'New'  ); 
+			$data[] = ( object ) array( 'Label' => $label, 'Total' => $repeat, 'Type' => 'Repeated'  ); 
+		}
+
+		if( $render ){
+			return array( 'data' => $data, 'unit' => $this->unity, 'interval' => 'month' );
+		}
+		return $data;
 	}
 
 	public function repeatByActiveuserByWeek( $render = false ){
diff --git a/include/library/Crunchbutton/Chart/User.php b/include/library/Crunchbutton/Chart/User.php
index 98c251ff5..cc6ee60d2 100644
--- a/include/library/Crunchbutton/Chart/User.php
+++ b/include/library/Crunchbutton/Chart/User.php
@@ -181,6 +181,31 @@ class Crunchbutton_Chart_User extends Crunchbutton_Chart {
 		return $parsedData;
 	}
 
+	public function newByDayByCommunity( $render = false ){
+
+		$query = "SELECT SUM(1) AS Total,
+										 DATE_FORMAT(o.date ,'%Y-%m-%d') AS Day,
+										 community AS `Group`
+							FROM `order` o
+							INNER JOIN
+								(SELECT min(id_order) id_order,
+												u.phone,
+												r.community
+								 FROM `order` o
+								 INNER JOIN user u ON u.id_user = o.id_user
+								 LEFT JOIN restaurant r ON r.id_restaurant = o.id_restaurant 
+								 WHERE r.community IS NOT NULL
+										{$this->queryExcludeUsers}
+								 GROUP BY u.phone) orders ON o.id_order = orders.id_order
+							GROUP BY DATE_FORMAT(o.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 newByDay( $render = false ){
 
 		$query = "SELECT SUM(1) AS Total,
@@ -221,7 +246,7 @@ class Crunchbutton_Chart_User extends Crunchbutton_Chart {
 										{$this->queryExcludeUsers}
 								 GROUP BY u.phone) orders ON o.id_order = orders.id_order
 							GROUP BY YEARWEEK(o.date) HAVING Week BETWEEN '{$this->weekFrom}' AND '{$this->weekTo}'";
-	echo $query;exit;
+
 		$parsedData = $this->parseDataWeeksSimple( $query, $this->description );
 		if( $render ){
 			return array( 'data' => $parsedData, 'unit' => $this->unity );