From b51c75415aae975acdcd32e5ad757f8e60f2c4e4 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 22 Feb 2014 18:25:08 -0300 Subject: [PATCH] Report - orders for the last past 14 days --- .../default/cockpit/community/index.php | 24 +++++++++++++++++ include/library/Crunchbutton/Restaurant.php | 7 +++++ .../views/default/cockpit/community/csv.phtml | 26 +++++++++++++++++++ .../default/cockpit/community/index.phtml | 4 +++ 4 files changed, 61 insertions(+) create mode 100644 include/views/default/cockpit/community/csv.phtml diff --git a/include/controllers/default/cockpit/community/index.php b/include/controllers/default/cockpit/community/index.php index f5a5a472b..8ace3dc18 100644 --- a/include/controllers/default/cockpit/community/index.php +++ b/include/controllers/default/cockpit/community/index.php @@ -12,6 +12,12 @@ class Controller_community extends Crunchbutton_Controller_Account { c::view()->page = 'community'; + // Report with the orders from the last 14 days + if( $slug == 'report' ){ + $this->report(); + exit; + } + if( $slug ){ $permission = "community-communities-{$slug}"; @@ -128,5 +134,23 @@ class Controller_community extends Crunchbutton_Controller_Account { return $info; } + public function report(){ + $interval = 14; + $communities = Restaurant::getCommunities(); + $orders = []; + $days = []; + foreach ( $communities as $community ) { + $orders[ $community ] = Restaurant::getOrdersFromLastDaysByCommunity( $community, $interval ); + } + $today = new DateTime( $time, new DateTimeZone( 'America/Los_Angeles' ) ); + for( $i = 0; $i <= $interval; $i++ ){ + $days[] = $today->format( 'm/d/Y' ); + $today->modify( '-1 day' ); + } + c::view()->days = $days; + c::view()->orders = $orders; + c::view()->layout('layout/csv'); + c::view()->display('community/csv', ['display' => true, 'filter' => false]); + } } \ No newline at end of file diff --git a/include/library/Crunchbutton/Restaurant.php b/include/library/Crunchbutton/Restaurant.php index a26fb8ba2..2627c7e52 100644 --- a/include/library/Crunchbutton/Restaurant.php +++ b/include/library/Crunchbutton/Restaurant.php @@ -1627,6 +1627,13 @@ class Crunchbutton_Restaurant extends Cana_Table_Trackchange { return Hour::restaurantClosesIn( $this ); } + public function getOrdersFromLastDaysByCommunity( $community, $days = 14 ){ + $query = "SELECT SUM(1) orders, DATE_FORMAT( o.date, '%m/%d/%Y' ) day FROM `order` o + INNER JOIN restaurant r ON r.id_restaurant = o.id_restaurant AND r.community = '$community' + WHERE o.date > DATE_SUB(CURDATE(), INTERVAL $days DAY) AND o.name NOT LIKE '%test%' GROUP BY day ORDER BY o.date ASC"; + return c::db()->get( $query ); + } + // Return minutes left to open public function opensIn( $dt = null ) { if( $this->open_for_business ){ diff --git a/include/views/default/cockpit/community/csv.phtml b/include/views/default/cockpit/community/csv.phtml new file mode 100644 index 000000000..3ead6c1aa --- /dev/null +++ b/include/views/default/cockpit/community/csv.phtml @@ -0,0 +1,26 @@ +orders as $communities => $day ){ + $headers[] = $communities; +} +echo implode( $separator, $headers ) . "\n"; + +// print dates +$content = []; +foreach( $this->days as $day ){ + ob_start(); + echo $day . $separator; + foreach( $this->orders as $communities => $days ){ + foreach( $days as $orders ){ + if( $orders->day == $day ){ + echo $orders->orders; + } + } + echo $separator; + } + $output = ob_get_clean(); + $output = preg_replace('/\n|\r|\t/i', '', $output); + echo "$output\n"; +} +?> \ No newline at end of file diff --git a/include/views/default/cockpit/community/index.phtml b/include/views/default/cockpit/community/index.phtml index f0214c8de..8137c6523 100644 --- a/include/views/default/cockpit/community/index.phtml +++ b/include/views/default/cockpit/community/index.phtml @@ -19,6 +19,10 @@ } ?> +


+ + Report (orders for the past 14 days by community) +