2016-03-05 00:18:10 +00:00
< ? php
2016-02-29 15:59:36 +00:00
namespace App\Http\Controllers ;
2016-03-11 12:04:09 +00:00
use Illuminate\Http\Request ;
2016-02-29 15:59:36 +00:00
use App\Models\Attendee ;
2016-03-05 00:18:10 +00:00
use App\Models\Event ;
2016-02-29 15:59:36 +00:00
use Carbon\Carbon ;
2016-03-05 00:18:10 +00:00
use DB ;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
class EventCheckInController extends MyBaseController
{
2016-02-29 15:59:36 +00:00
/**
2016-03-11 12:04:09 +00:00
* Show the check - in page
2016-03-05 00:18:10 +00:00
*
2016-03-11 12:04:09 +00:00
* @ param $event_id
* @ return \Illuminate\View\View
2016-02-29 15:59:36 +00:00
*/
2016-03-05 00:18:10 +00:00
public function showCheckIn ( $event_id )
{
2016-04-07 13:33:07 +00:00
2016-02-29 15:59:36 +00:00
$data [ 'event' ] = Event :: scope () -> findOrFail ( $event_id );
$data [ 'attendees' ] = $data [ 'event' ] -> attendees ;
2016-03-05 00:18:10 +00:00
2016-04-07 13:33:07 +00:00
2016-03-11 12:04:09 +00:00
return view ( 'ManageEvent.CheckIn' , $data );
2016-02-29 15:59:36 +00:00
}
2016-04-07 13:33:07 +00:00
public function showQRCodeModal ( Request $request , $event_id )
{
return view ( 'ManageEvent.Modals.QrcodeCheckIn' );
}
2016-03-11 12:04:09 +00:00
/**
* Search attendees
*
* @ param Request $request
* @ param $event_id
* @ return \Illuminate\Http\JsonResponse
*/
public function postCheckInSearch ( Request $request , $event_id )
2016-03-05 00:18:10 +00:00
{
2016-03-11 12:04:09 +00:00
$searchQuery = $request -> get ( 'q' );
2016-02-29 15:59:36 +00:00
$attendees = Attendee :: scope () -> withoutCancelled ()
-> join ( 'tickets' , 'tickets.id' , '=' , 'attendees.ticket_id' )
2016-03-05 00:18:10 +00:00
-> where ( function ( $query ) use ( $event_id ) {
2016-02-29 15:59:36 +00:00
$query -> where ( 'attendees.event_id' , '=' , $event_id );
2016-03-05 00:18:10 +00:00
}) -> where ( function ( $query ) use ( $searchQuery ) {
$query -> orWhere ( 'attendees.first_name' , 'like' , $searchQuery . '%' )
-> orWhere ( DB :: raw ( " CONCAT_WS(' ', first_name, last_name) " ), 'like' , $searchQuery . '%' )
2016-02-29 15:59:36 +00:00
//->orWhere('attendees.email', 'like', $searchQuery . '%')
2016-03-05 00:18:10 +00:00
-> orWhere ( 'attendees.reference' , 'like' , $searchQuery . '%' )
-> orWhere ( 'attendees.last_name' , 'like' , $searchQuery . '%' );
2016-02-29 15:59:36 +00:00
})
-> select ([
'attendees.id' ,
'attendees.first_name' ,
'attendees.last_name' ,
'attendees.email' ,
'attendees.reference' ,
'attendees.arrival_time' ,
'attendees.has_arrived' ,
2016-03-05 00:18:10 +00:00
'tickets.title as ticket' ,
2016-02-29 15:59:36 +00:00
])
-> orderBy ( 'attendees.first_name' , 'ASC' )
-> get ();
2016-03-11 12:04:09 +00:00
return response () -> json ( $attendees );
2016-02-29 15:59:36 +00:00
}
2016-03-11 12:04:09 +00:00
/**
* Check in / out an attendee
*
* @ param Request $request
* @ return \Illuminate\Http\JsonResponse
*/
public function postCheckInAttendee ( Request $request )
2016-03-05 00:18:10 +00:00
{
2016-03-11 12:04:09 +00:00
$attendee_id = $request -> get ( 'attendee_id' );
$checking = $request -> get ( 'checking' );
2016-02-29 15:59:36 +00:00
$attendee = Attendee :: scope () -> find ( $attendee_id );
/*
* Ugh
*/
if ((( $checking == 'in' ) && ( $attendee -> has_arrived == 1 )) || (( $checking == 'out' ) && ( $attendee -> has_arrived == 0 ))) {
2016-03-11 12:04:09 +00:00
return response () -> json ([
2016-03-05 00:18:10 +00:00
'status' => 'error' ,
'message' => 'Warning: This Attendee Has Already Been Checked ' . (( $checking == 'in' ) ? 'In (at ' . $attendee -> arrival_time -> format ( 'H:i A, F j' ) . ')' : 'Out' ) . '!' ,
2016-02-29 15:59:36 +00:00
'checked' => $checking ,
2016-03-05 00:18:10 +00:00
'id' => $attendee -> id ,
2016-02-29 15:59:36 +00:00
]);
}
$attendee -> has_arrived = ( $checking == 'in' ) ? 1 : 0 ;
$attendee -> arrival_time = Carbon :: now ();
$attendee -> save ();
2016-03-11 12:04:09 +00:00
return response () -> json ([
2016-03-05 00:18:10 +00:00
'status' => 'success' ,
2016-02-29 15:59:36 +00:00
'checked' => $checking ,
2016-03-05 00:18:10 +00:00
'message' => 'Attendee Successfully Checked ' . (( $checking == 'in' ) ? 'In' : 'Out' ),
'id' => $attendee -> id ,
2016-02-29 15:59:36 +00:00
]);
}
2016-04-11 11:52:50 +00:00
/**
* Check in an attendee
*
* @ param $event_id
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse
*/
public function postCheckInAttendeeQr ( $event_id , Request $request )
{
$event = Event :: scope () -> findOrFail ( $event_id );
$qrcodeToken = $request -> get ( 'qrcode_token' );
$attendee = Attendee :: scope () -> withoutCancelled ()
-> join ( 'tickets' , 'tickets.id' , '=' , 'attendees.ticket_id' )
-> where ( function ( $query ) use ( $event , $qrcodeToken ) {
$query -> where ( 'attendees.event_id' , $event -> id )
-> where ( 'attendees.private_reference_number' , $qrcodeToken );
}) -> select ([
'attendees.id' ,
'attendees.order_id' ,
'attendees.first_name' ,
'attendees.last_name' ,
'attendees.email' ,
'attendees.reference' ,
'attendees.arrival_time' ,
'attendees.has_arrived' ,
'tickets.title as ticket' ,
]) -> first ();
if ( is_null ( $attendee )){
return response () -> json ([ 'status' => 'error' , 'message' => " Invalid Ticket! Please try again. " ]);
}
$relatedAttendesCount = Attendee :: where ( 'id' , '!=' , $attendee -> id )
-> where ([
'order_id' => $attendee -> order_id ,
'has_arrived' => false
]) -> count ();
if ( $relatedAttendesCount >= 1 ){
$confirmOrderTicketsRoute = route ( 'confirmCheckInOrderTickets' , [ $event -> id , $attendee -> order_id ]);
$appendedText = '<br><br><form class="ajax" action="' . $confirmOrderTicketsRoute . '" method="POST">' . csrf_field () . '<button class="btn btn-primary btn-sm" type="submit"><i class="ico-ticket"></i> Check in all tickets associated to this order</button></form>' ;
} else {
$appendedText = '' ;
}
if ( $attendee -> has_arrived ) {
return response () -> json ([
'status' => 'error' ,
'message' => 'Warning: This attendee has already been checked in at ' . $attendee -> arrival_time -> format ( 'H:i A, F j' ) . '.' . $appendedText
]);
}
Attendee :: find ( $attendee -> id ) -> update ([ 'has_arrived' => true , 'arrival_time' => Carbon :: now ()]);
return response () -> json ([
'status' => 'success' ,
'message' => 'Success !<br>Name: ' . $attendee -> first_name . ' ' . $attendee -> last_name . '<br>Reference: ' . $attendee -> reference . '<br>Ticket: ' . $attendee -> ticket . '.' . $appendedText
]);
}
/**
* Confirm tickets of same order .
*
* @ param $event_id
* @ param $order_id
* @ return \Illuminate\Http\Response
*/
public function confirmOrderTicketsQr ( $event_id , $order_id )
{
$updateRowsCount = Attendee :: scope () -> where ([
'event_id' => $event_id ,
'order_id' => $order_id ,
'has_arrived' => 0 ,
]) -> update ([
'has_arrived' => 1 ,
'arrival_time' => Carbon :: now (),
]);
return response () -> json ([
'message' => $updateRowsCount . ' Attendee(s) Checked in.'
]);
}
2016-02-29 15:59:36 +00:00
}