This commit is contained in:
Daniel Camargo 2014-05-21 18:53:34 -03:00
parent f36b4f7116
commit e3c095ad08
5 changed files with 182 additions and 12 deletions

View File

@ -4,13 +4,93 @@ class Controller_api_driver_shifts extends Crunchbutton_Controller_RestAccount {
public function init() {
switch ( c::getPagePiece( 3 ) ) {
case 'schedule':
$this->_schedule();
break;
default:
$this->_list();
break;
}
}
private function _communities(){
$communities = c::user()->communitiesHeDeliveriesFor();
$_communities = [];
foreach( $communities as $community ){
$_communities[] = $community->id_community;
}
return $_communities;
}
private function _schedule(){
$year = date( 'Y', strtotime( '- 1 day' ) );
$week = date( 'W', strtotime( '- 1 day' ) );
$firstDay = new DateTime( date( 'Y-m-d', strtotime( $year . 'W' . $week . 1 ) ), new DateTimeZone( c::config()->timezone ) );
if( date( 'l' ) == 'Monday' ){
$firstDay->modify( '+ 2 week' );
} else {
$firstDay->modify( '+ 1 week' );
}
// todo: remove it -> get the current week
$firstDay->modify( '- 1 week' );
$week = $firstDay->format( 'W' );
$year = $firstDay->format( 'Y' );
$days = [];
for( $i = 0; $i <= 6; $i++ ){
$days[] = new DateTime( $firstDay->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
$firstDay->modify( '+ 1 day' );
}
$wantToWork = [];
$donWantToWork = [];
$id_admin = c::user()->id_admin;
$from = new DateTime( $days[ 0 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
$to = new DateTime( $days[ 6 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
$preferences = Crunchbutton_Admin_Shift_Preference::shiftsByPeriod( $id_admin, $from->format( 'Y-m-d' ), $to->format( 'Y-m-d' ) );
$ranking = 1;
foreach ( $preferences as $preference ) {
$wantToWork[ $preference->id_community_shift ] = $ranking;
$ranking++;
}
$preferences = Crunchbutton_Admin_Shift_Preference::shiftsByPeriod( $id_admin, $from->format( 'Y-m-d' ), $to->format( 'Y-m-d' ), true );
foreach ( $preferences as $preference ) {
$donWantToWork[ $preference->id_community_shift ] = true;;
}
$communities = $this->_communities();
foreach( $communities as $community ) {
foreach( $days as $day ){
$segments = Crunchbutton_Community_Shift::shiftByCommunityDay( $community, $day->format( 'Y-m-d' ) );
foreach ( $segments as $segment ) {
$export = $segment->export();
$data = array( 'id_community_shift' => $segment->id_community_shift, 'day' => $export[ 'period' ][ 'day_start' ], 'period' => $export[ 'period' ][ 'toString' ], 'tz' => $export[ 'period' ][ 'timezone_abbr' ] );
$data[ 'community' ] = $export[ 'community' ][ 'name' ];
$data[ 'assigned' ] = Crunchbutton_Admin_Shift_Preference::adminHasShift( $id_admin, $segment->id_community_shift );
if( $wantToWork[ $segment->id_community_shift ] ){
$data[ 'ranking' ] = $wantToWork[ $segment->id_community_shift ];
} else if( $donWantToWork[ $segment->id_community_shift ] ){
$data[ 'ranking' ] = 0;
}
$_shifts[] = $data;
}
}
}
echo json_encode( $_shifts );
}
private function _list(){
// this method returns the shifts for the next 7 days
$shifts = Crunchbutton_Community_Shift::nextShiftsByCommunities( $_communities );
$shifts = Crunchbutton_Community_Shift::nextShiftsByCommunities( $this->_communities() );
$export = [];
foreach ( $shifts as $shift ) {
@ -29,4 +109,5 @@ class Controller_api_driver_shifts extends Crunchbutton_Controller_RestAccount {
}
echo json_encode( $export );
}
}

View File

@ -0,0 +1,56 @@
<div class="top-pad"></div>
<h1 class="title left"><i class="fa fa-calendar"></i><span>Shifts</span></h1>
<div class="content-padding" ng-show="ready">
<div class="divider"></div>
<div class="drivers-shifts-list">
<div ng-if="drivershifts.length">
<div class="drivers-shift" ng-repeat="shift in drivershifts | filter:filterShifts">
<span class="day">{{shift.day}}</span>
<div ng-if="shift.drivers.length > 0" class="box-content">
<table class="drivers">
<tr class="driver" ng-repeat="driver in shift.drivers" ng-class="{me: (driver.id == account.user.id_admin)}">
<td class="start-end"><div class="start-end-brace">{{driver.hour}}</div></td>
<td class="driver-spacer"></td>
<td class="driver-details">
<span ng-if="!driver.name" class="no-drivers">
No drivers
</span>
<span ng-if="driver.name">
<span class="driver-name">{{driver.name}}</span><br>
<a href="tel:{{driver.phone | formatPhone}}" class="driver-phone">{{driver.phone | formatPhone}}</a>
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
<div ng-if="!drivershifts.length" class="box-content">
You currently have no shifts.
</div>
</div>
</div>
<spinner-loading></spinner-loading>
<hack-expand-content></hack-expand-content>

View File

@ -93,6 +93,12 @@ NGApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $l
controller: 'DriversShiftsCtrl',
templateUrl: 'assets/view/drivers-shifts.html'
})
/* temp */
.when('/drivers/shifts/schedule', {
action: 'drivers-shifts',
controller: 'DriversShiftsScheduleCtrl',
templateUrl: 'assets/view/drivers-shifts-schedule.html'
})
.when('/login', {
action: 'login',
controller: 'LoginCtrl',

View File

@ -139,6 +139,37 @@ NGApp.controller( 'DriversShiftsCtrl', function ( $scope, DriverShiftsService )
} );
NGApp.controller( 'DriversShiftsScheduleCtrl', function ( $scope, DriverShiftsService ) {
$scope.show = { all : true };
$scope.ready = false;
$scope.filterShifts = function( shift ){
if( $scope.show.all ){
return true;
} else {
if( shift.mine ){
return true;
}
}
return false;
}
$scope.list = function(){
DriverShiftsService.list( function( data ){
DriverShiftsService.groupByDay( data, function( data ){
$scope.drivershifts = data;
$scope.ready = true;
} );
} );
}
if( $scope.account.isLoggedIn() ){
$scope.list();
}
} );
NGApp.controller( 'DriversOnboardingCtrl', function ( $scope, $timeout, DriverOnboardingService ) {
$scope.ready = false;

View File

@ -3,8 +3,9 @@ NGApp.factory( 'DriverShiftsService', function( $rootScope, $resource ) {
var service = {};
// Create a private resource 'shifts'
var shifts = $resource( App.service + 'driver/shifts/:action/:id_community_shift', { action: '@action', id_admin: '@id_admin' }, {
'assign': { 'method': 'POST', params: { action: 'assign' } }
var shifts = $resource( App.service + 'driver/shifts', {}, {} );
var schedules = $resource( App.service + 'driver/shifts/', { }, {
}
);
@ -13,12 +14,7 @@ NGApp.factory( 'DriverShiftsService', function( $rootScope, $resource ) {
callback( data ); } );
}
service.wantWork = function(){
}
service.groupByDay = function( data, callback ){
var groups = {};
var order = 0;
@ -26,12 +22,12 @@ NGApp.factory( 'DriverShiftsService', function( $rootScope, $resource ) {
var day = data[ i ].date.day;
var segment = data[ i ].date.start_end;
if( !groups[ day ] ){
groups[ day ] = { 'day': day, 'order': order, 'drivers': [] };
groups[ day ] = { 'day' : day, 'order' : order, 'drivers' : [] };
order++;
}
if( data[ i ].drivers && data[ i ].drivers.length ){
for( var j = 0; j < data[ i ].drivers.length; j++ ){
groups[ day ][ 'drivers' ].push( { 'hour': segment, 'id': data[ i ].drivers[j].id, 'name': data[ i ].drivers[j].name, 'phone': data[ i ].drivers[j].phone } );
groups[ day ][ 'drivers' ].push( { 'hour': segment, 'id' : data[ i ].drivers[j].id, 'name' : data[ i ].drivers[j].name, 'phone' : data[ i ].drivers[j].phone } );
if( data[ i ].drivers[j].id == $rootScope.account.user.id_admin ){
groups[ day ][ 'mine' ] = true;
}