partial #2494 - driver shift preferences

This commit is contained in:
Daniel Camargo 2014-03-19 00:15:50 -03:00
parent 93ecb8962e
commit 8681278500
8 changed files with 148 additions and 35 deletions

View File

@ -1,9 +0,0 @@
CREATE TABLE `community_shift` (
`id_community_shift` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_community` int(11) unsigned DEFAULT NULL,
`date_start` datetime DEFAULT NULL,
`date_end` datetime DEFAULT NULL,
PRIMARY KEY (`id_community_shift`),
KEY `community_shift_ibfk_1` (`id_community`),
CONSTRAINT `community_shift_ibfk_1` FOREIGN KEY (`id_community`) REFERENCES `community` (`id_community`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

View File

@ -0,0 +1,34 @@
CREATE TABLE `community_shift` (
`id_community_shift` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_community` int(11) unsigned DEFAULT NULL,
`date_start` datetime DEFAULT NULL,
`date_end` datetime DEFAULT NULL,
PRIMARY KEY (`id_community_shift`),
KEY `community_shift_ibfk_1` (`id_community`),
CONSTRAINT `community_shift_ibfk_1` FOREIGN KEY (`id_community`) REFERENCES `community` (`id_community`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `admin_shift_preference` (
`id_admin_shift_preference` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_community_shift` int(11) unsigned DEFAULT NULL,
`id_admin` int(11) unsigned DEFAULT NULL,
`ranking` TINYINT( 2 ) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_admin_shift_preference`),
KEY `admin_shift_preference_ibfk_1` (`id_community_shift`),
KEY `admin_shift_preference_ibfk_2` (`id_admin`),
CONSTRAINT `admin_shift_preference_ibfk_1` FOREIGN KEY (`id_community_shift`) REFERENCES `community_shift` (`id_community_shift`) ON DELETE SET NULL ON UPDATE SET NULL,
CONSTRAINT `admin_shift_preference_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `admin_shift_status` (
`id_admin_shift_status` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_admin` int(11) unsigned DEFAULT NULL,
`year` int(4) unsigned DEFAULT NULL,
`week` int(2) unsigned DEFAULT NULL,
`completed` TINYINT( 1 ) NOT NULL DEFAULT '0',
`shifts` TINYINT( 2 ) NOT NULL DEFAULT '0',
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id_admin_shift_status`),
KEY `admin_shift_status_ibfk_1` (`id_admin`),
CONSTRAINT `admin_shift_status_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

View File

@ -1,10 +0,0 @@
CREATE TABLE `admin_shift_preference` (
`id_admin_shift_preference` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_community_shift` int(11) unsigned DEFAULT NULL,
`id_admin` int(11) unsigned DEFAULT NULL,
`ranking` TINYINT( 2 ) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_admin_shift_preference`),
KEY `admin_shift_preference_ibfk_1` (`id_community_shift`),
CONSTRAINT `admin_shift_preference_ibfk_1` FOREIGN KEY (`id_community_shift`) REFERENCES `community_shift` (`id_community_shift`) ON DELETE SET NULL ON UPDATE SET NULL,
CONSTRAINT `admin_shift_preference_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;

View File

@ -24,9 +24,18 @@ class Controller_api_drivers_shift extends Crunchbutton_Controller_RestAccount {
$allItems = $this->request()[ 'allItems' ]; $allItems = $this->request()[ 'allItems' ];
$wantWorkItems = $this->request()[ 'wantWorkItems' ]; $wantWorkItems = $this->request()[ 'wantWorkItems' ];
$dontWantWorkItems = $this->request()[ 'dontWantWorkItems' ]; $dontWantWorkItems = $this->request()[ 'dontWantWorkItems' ];
$completed = $this->request()[ 'completed' ];
$shifts = $this->request()[ 'shifts' ];
if( count( $allItems ) > 0 ){ if( count( $allItems ) > 0 ){
$id_admin = c::admin()->id_admin; $id_admin = c::admin()->id_admin;
$status = Crunchbutton_Admin_Shift_Status::currentStatus( $id_admin );
$status->completed = $completed;
$status->shifts = $shifts;
$status->date = date('Y-m-d H:i:s');
$status->save();
// remove all items // remove all items
if( count( $allItems ) > 0 ){ if( count( $allItems ) > 0 ){

View File

@ -54,7 +54,9 @@ class Controller_drivers_shift extends Crunchbutton_Controller_Account {
$days[] = new DateTime( $firstDay->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) ); $days[] = new DateTime( $firstDay->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
$firstDay->modify( '+ 1 day' ); $firstDay->modify( '+ 1 day' );
} }
c::view()->days = $days;
c::view()->status = Crunchbutton_Admin_Shift_Status::currentStatus( $admin->id_admin );
c::view()->days = $days;
c::view()->from = new DateTime( $days[ 0 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) ); c::view()->from = new DateTime( $days[ 0 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
c::view()->to = new DateTime( $days[ 6 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) ); c::view()->to = new DateTime( $days[ 6 ]->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone ) );
c::view()->communities = $admin->communitiesHeDeliveriesFor(); c::view()->communities = $admin->communitiesHeDeliveriesFor();

View File

@ -0,0 +1,33 @@
<?php
class Crunchbutton_Admin_Shift_Status extends Cana_Table {
public function __construct($id = null) {
parent::__construct();
$this
->table('admin_shift_status')
->idVar('id_admin_shift_status')
->load($id);
}
public function currentStatus( $id_admin ){
$year = date( 'Y' );
$week = date( 'W' );
$date = new DateTime( date( 'Y-m-d', strtotime( $year . 'W' . $week . 1 ) ), new DateTimeZone( c::config()->timezone ) );
$date->modify( '+ 1 week' );
return Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $id_admin, $date->format( 'W' ), $date->format( 'Y' ) );
}
public function getByAdminWeekYear( $id_admin, $week, $year ){
$status = Crunchbutton_Admin_Shift_Status::q( 'SELECT * FROM admin_shift_status WHERE id_admin = ' . $id_admin . ' AND year = ' . $year . ' AND week = ' . $week );
if( !$status->id_admin_shift_status ){
$status = new Crunchbutton_Admin_Shift_Status();
$status->id_admin = $id_admin;
$status->week = $week;
$status->year = $year;
$status->date = date('Y-m-d H:i:s');
$status->save();
}
return $status;
}
}

View File

@ -11,7 +11,7 @@
<div class="row-fluid"> <div class="row-fluid">
<div class="box"> <div class="box">
<div class="box-header"><span class="title">Sort your shift preferences</span></div> <div class="box-header"><span class="title">Sort your shift preferences</span></div>
<div class="padded sort-area"> <div class="padded">
<div class="row-fluid"> <div class="row-fluid">
<center> <center>
<h4> <h4>
@ -19,9 +19,9 @@
</h4> </h4>
</center> </center>
<hr/> <hr/>
<div class="row-fluid padded"> <div class="row-fluid sort-area">
<div class="span4"> <div class="span4">
<strong>Available shifts</strong> <strong>Available shifts:</strong>
<br/><br/> <br/><br/>
<ul class="available connected"> <ul class="available connected">
<?php <?php
@ -46,7 +46,7 @@
</ul> </ul>
</div> </div>
<div class="span4"> <div class="span4">
<strong>Shifts you want to work</strong> <strong>Shifts you want to work:</strong>
<br/><br/> <br/><br/>
<ul class="wantwork connected"> <ul class="wantwork connected">
<?php <?php
@ -64,7 +64,7 @@
</ul> </ul>
</div> </div>
<div class="span4"> <div class="span4">
<strong>Shifts you DONT want to work</strong> <strong>Shifts you DONT want to work:</strong>
<br/><br/> <br/><br/>
<ul class="dontwantwork connected"> <ul class="dontwantwork connected">
<?php <?php
@ -83,9 +83,34 @@
</div> </div>
</div> </div>
</div> </div>
<hr/>
<div class="row-fluid">
<div class="span4">
<strong>How many shifts would you like to work this week?</strong>
</div>
<div class="span4">
<select id="shifts" class="chosen-select">
<option value=""></option>
<?php for( $i = 1; $i <= 10; $i++ ) {
$selected = ( $this->status->shifts == $i ) ? 'selected="selected"' : '';
?>
<option <?php echo $selected; ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</div>
</div>
</div> </div>
<div class="form-actions"> <div class="form-actions">
<button type="button" class="save-shift-driver btn btn-blue">Save changes</button> <button type="button" class="update-shift-driver btn btn-blue"><i class="icon-save"></i> Update</button>&nbsp;&nbsp;
<button type="button" class="complete-shift-driver btn btn-green">
<?php if( $this->status->completed == '1' ) { ?>
<i class="icon-thumbs-up"></i> Already completed
<?php } else { ?>
<i class="icon-ok"></i> Complete
<?php } ?>
</button>
</div> </div>
<div class="divider"></div> <div class="divider"></div>
</div> </div>

View File

@ -169,35 +169,64 @@ shift.drivers.init = function(){
return false; return false;
} ); } );
$( '.save-shift-driver' ).click( function() { $( '.chosen-select' ).select2();
shift.drivers.save();
$( '.update-shift-driver' ).click( function() {
shift.drivers.update( false );
} ); } );
$( '.complete-shift-driver' ).click( function() {
shift.drivers.update( true );
} );
$('.available, .wantwork, .dontwantwork').sortable( { connectWith: '.connected' } ); $('.available, .wantwork, .dontwantwork').sortable( { connectWith: '.connected' } );
} }
shift.drivers.save = function(){ shift.drivers.update = function( completed ){
var wantWorkItems = []; var wantWorkItems = [];
var dontWantWorkItems = []; var dontWantWorkItems = [];
var availableItems = []; var availableItems = [];
var allItems = []; var allItems = [];
var hasAvailableItem = false;
var shifts = $( '#shifts' ).val();
$('ul.available li').each( function() {
hasAvailableItem = true;
availableItems.push( $( this ).attr( 'id' ) );
allItems.push( $( this ).attr( 'id' ) );
} );
if( completed ){
if( hasAvailableItem ){
alert( 'Ops, it seems you still have available items!' );
return;
}
if( $.trim( shifts ) == '' ){
alert( 'Ops, you need to answer: "How many shifts would you like to work this week?" !' );
$( '#shifts' ).focus();
return;
}
}
if( parseInt( shifts ) > 0 && !hasAvailableItem ){
completed = true;
}
completed = ( completed ) ? 1 : 0;
$('ul.wantwork li').each( function() { $('ul.wantwork li').each( function() {
wantWorkItems.push( $( this ).attr( 'id' ) ); wantWorkItems.push( $( this ).attr( 'id' ) );
allItems.push( $( this ).attr( 'id' ) ); allItems.push( $( this ).attr( 'id' ) );
} ); } );
$('ul.dontwantwork li').each( function() { $('ul.dontwantwork li').each( function() {
dontWantWorkItems.push( $( this ).attr( 'id' ) ); dontWantWorkItems.push( $( this ).attr( 'id' ) );
allItems.push( $( this ).attr( 'id' ) ); allItems.push( $( this ).attr( 'id' ) );
} ); } );
$('ul.available li').each( function() {
availableItems.push( $( this ).attr( 'id' ) );
allItems.push( $( this ).attr( 'id' ) );
} );
$.ajax( { $.ajax( {
url: '/api/drivers/shift/driver/', url: '/api/drivers/shift/driver/',
method: 'POST', method: 'POST',
data: { 'allItems' : allItems, 'dontWantWorkItems' : dontWantWorkItems, 'wantWorkItems' : wantWorkItems, 'availableItems' : availableItems }, data: { 'allItems' : allItems, 'dontWantWorkItems' : dontWantWorkItems, 'wantWorkItems' : wantWorkItems, 'availableItems' : availableItems, 'completed' : completed, 'shifts' : shifts },
dataType: 'json', dataType: 'json',
} ).done( function( data ) { } ).done( function( data ) {
if( data.success ){ if( data.success ){
@ -206,4 +235,4 @@ shift.drivers.save = function(){
alert( 'Ops, error! ' + data.error ); alert( 'Ops, error! ' + data.error );
} }
} ); } );
} }