Make graphs of orders each hour each day per community #3036

This commit is contained in:
Daniel Camargo 2014-04-23 11:11:59 -03:00
parent 440f1efd3c
commit 104a834201
6 changed files with 111 additions and 7 deletions

View File

@ -13,6 +13,11 @@ class Controller_charts_community extends Crunchbutton_Controller_Account {
c::view()->display( 'charts/community/delivery_tips' );
break;
case 'delivered-orders':
c::view()->layout( 'layout/ajax' );
c::view()->display( 'charts/community/delivered_orders' );
break;
case 'orders-per-day':
$hasPermissionFullPermission = c::admin()->permission()->check( [ 'global', 'metrics-all', 'metrics-communities-all' ] );

View File

@ -39,6 +39,13 @@ class Crunchbutton_Chart_Order extends Crunchbutton_Chart {
'orders-per-day-per-community' => array( 'title' => 'Last 14 Days', 'interval' => 'day', 'type' => 'column', 'method' => 'byDayPerCommunitySelective' ),
)
),
'group-delivered-orders-by-community' => array(
'title' => 'Delivered',
'tags' => array( 'especial' ),
'charts' => array(
'delivered-orders-per-day-per-community' => array( 'title' => 'Orders', 'interval' => 'hour', 'type' => 'column', 'method' => 'deliveredByDayPerCommunity' ),
)
),
'group-orders-community' => array(
'title' => 'Orders',
'tags' => array( 'reps' ),
@ -531,6 +538,51 @@ public function byDayPerRestaurant( $render = false ){
return $parsedData;
}
public function deliveredByDayPerCommunity(){
$id_community = $_REQUEST[ 'id_community' ];
$day = $_REQUEST[ 'day' ];
$where_day = ( $day != 'All' ) ? "AND DATE_FORMAT( o.date, '%W' ) = '{$day}' " : "";
$query = "SELECT COUNT(*) AS Total,
DATE_FORMAT( o.date, '%H' ) AS Hour
FROM `order` o
INNER JOIN restaurant r ON r.id_restaurant = o.id_restaurant
INNER JOIN restaurant_community rc ON rc.id_restaurant = r.id_restaurant AND rc.id_community = {$id_community}
WHERE o.delivery_service = 1 {$where_day}
GROUP BY Hour";
$data = c::db()->get( $query );
$_hours = [];
for( $i = 0; $i <= 12; $i++ ){
$hour = $i . ( $i == 12 ? ' pm' : ' am' );
$_hours[ $hour ] = 0;
}
for( $i = 1; $i <= 12; $i++ ){
$hour = $i . ' pm';
$_hours[ $hour ] = 0;
}
$community = Crunchbutton_Community::o( $id_community );
foreach ( $data as $item ) {
$date = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
$date->setTime( $item->Hour, 00 );
$date->setTimezone( new DateTimeZone( $community->timezone ) );
$_hours[ $date->format( 'g a' ) ] = $item->Total;
}
$data = [];
foreach( $_hours as $hour => $value ){
$data[] = ( object ) array( 'Label' => $hour, 'Total' => $value, 'Type' => 'Hour' );
}
return array( 'data' => $data, 'unit' => $this->unit, 'interval' => 'hour' );
}
public function byDayPerCommunitySelective(){
$communities = $_REQUEST[ 'communities' ];

View File

@ -18,7 +18,7 @@ class Crunchbutton_Community_Shift extends Cana_Table {
if( $order->restaurant()->timezone ){
$now = new DateTime( 'now', new DateTimeZone( $order->restaurant()->timezone ) );
} else {
$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
}
return Admin::q( 'SELECT a.* FROM admin a

View File

@ -114,11 +114,14 @@ if( $isPopup ){
];
}
?>
<?php if( $interval == 'hour' && $_GET[ 'day' ] ){
$chartId .= $_GET[ 'day' ];
} ?>
<div class="row-fluid">
<div class="row-fluid">
<div class="span12">
<h4 style="text-align:center;">
<?php echo $title; ?>
<?php echo $title; ?> <?php if( $interval == 'hour' && $_GET[ 'day' ] ){ echo $_GET[ 'day' ]; } ?>
<?php if( !$isPopup ){
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&popup=1";
echo '&nbsp;&nbsp;<a href="' . $url . '" target="_blank" title="Zoom this chart"><i class="icon-zoom-in"></i></a>';
@ -134,7 +137,7 @@ if( $isPopup ){
<?php } ?>
<?php echo $description; ?>
</center>
<div id="options-<?=$divId?>" style="height:110px;background:#F5F5F5;overflow-x:auto;width:99%;overflow-y:hidden;<?php if( $isPopup ){ echo 'display:none'; } ?>">
<div id="options-<?=$divId?>" style="height:110px;background:#F5F5F5;overflow-x:auto;width:99%;overflow-y:hidden;<?php if( $isPopup || $interval == 'hour' ){ echo 'display:none'; } ?>">
<table>
<tr>
<?php
@ -280,7 +283,6 @@ if( $isPopup ){
</div>
<?php if( $hasResults ){ ?>
<script type="text/javascript">
$(function () {
var data = <?=json_encode($jsData, JSON_NUMERIC_CHECK)?>;
@ -301,6 +303,7 @@ $(function () {
labels : {
rotation: -45,
align: 'right'
<?php if( $interval == 'hour' ){ ?>,step:1<?php } ?>
<?php if( $interval == 'day' ){ ?>,step:10<?php } ?>
<?php if( $interval == 'week' ){ ?>,step:2<?php } ?>
}

View File

@ -0,0 +1,28 @@
<?php
$count = 0;
$weekdays = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'All' ];
foreach( $weekdays as $day ){
if( $count == 0 ){
echo '<div class="row-fluid">';
}
$count++;
?>
<div class="span3">
<!-- <h5><?php echo $day; ?></h5> -->
<div id="chart-<?php echo $day; ?>"></div>
</div>
<?php
if( $count == 4 ){
echo '</div>';
$count = 0;
}
}
?>
<script type="text/javascript">
$( document ).ready( function(){
<?php foreach( $weekdays as $day ){ ?>
var url = '/home/charts/delivered-orders-per-day-per-community/?day=<?php echo $day; ?>&=1&id_community=<?php echo $_GET[ 'id_community' ] ?>';
$.ajax( { url: url, } ).done( function( data ) { $( '#chart-<?php echo $day; ?>' ).html( data ); } );
<?php } ?>
} );
</script>

View File

@ -83,7 +83,8 @@
<li <?php echo $active; ?>><a href="#drivers" data-toggle="tab"><i class="icon-road"></i> <span>Drivers</span></a></li>
<?php } ?>
<? if ( c::admin()->permission()->check( [ 'global','drivers-all', 'drivers-working-hours' ] ) ) { ?>
<li><a href="#shifts" data-toggle="tab"><i class="icon-truck"></i> <span>Shifts (Beta)</span></a></li>
<li><a href="#shifts" data-toggle="tab"><i class="icon-truck"></i> <span>Shifts</span></a></li>
<li><a href="#charts" data-toggle="tab"><i class="icon-bar-chart"></i> <span>Delivery Report</span></a></li>
<?php } ?>
<li><a href="#more" data-toggle="tab"><i class="icon-cog"></i> <span>More actions</span></a></li>
</ul>
@ -120,7 +121,13 @@
<div class="row-fluid">
<div id="shifts-content"></div>
</div>
</div>
<div class="tab-pane" id="charts">
<div class="row-fluid">
<div id="charts-content">
<button class="btn btn-default" onclick="community.deliveryChats();">Load charts</button>
</div>
</div>
</div>
</div>
</div>
@ -154,7 +161,6 @@ $( document ).ready( function(){
} );
community.restaurants();
community.drivers();
<?php
if ( c::admin()->permission()->check( [ 'global','drivers-all', 'drivers-working-hours' ] ) ) {
@ -169,6 +175,16 @@ $( document ).ready( function(){
} );
community.deliveryChats = function(){
var url = '/charts/community/delivered-orders?id_community=' + community.id_community;
$.ajax( {
url: url,
complete: function(content) {
$('#charts-content').html( content.responseText );
}
} );
};
community.shifts = function( url ){
$.ajax( {
url: url,