2016-03-30 01:37:38 +00:00
< ? php
namespace App\Http\Controllers ;
use App\Http\Requests ;
use App\Models\Attendee ;
use App\Models\Event ;
use Carbon\Carbon ;
use Illuminate\Http\Request ;
use JavaScript ;
class EventQrcodeCheckInController extends Controller
{
/**
* Show the check - in page
*
* @ param $event_id
* @ return \Illuminate\View\View
*/
public function showCheckIn ( $event_id )
{
$event = Event :: scope () -> findOrFail ( $event_id );
JavaScript :: put ([
'qrcodeCheckInRoute' => route ( 'postQRCodeCheckInAttendee' , [ 'event_id' => $event -> id ])
]);
return view ( 'ManageEvent.QrcodeCheckIn' , compact ( 'event' ));
}
2016-09-06 20:39:27 +00:00
/**
2016-03-30 01:37:38 +00:00
* Check in an attendee
*
* @ param $event_id
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse
*/
public function postCheckInAttendee ( $event_id , Request $request )
{
$event = Event :: scope () -> findOrFail ( $event_id );
$qrcodeToken = $request -> get ( 'qrcode_token' );
$attendee = Attendee :: scope () -> withoutCancelled ()
2016-09-06 20:39:27 +00:00
-> 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. " ]);
2016-03-30 01:37:38 +00:00
}
2016-03-30 04:57:08 +00:00
$relatedAttendesCount = Attendee :: where ( 'id' , '!=' , $attendee -> id )
2016-09-06 20:39:27 +00:00
-> where ([
'order_id' => $attendee -> order_id ,
'has_arrived' => false
]) -> count ();
2016-03-30 01:37:38 +00:00
2016-06-15 02:31:24 +00:00
$appendedText = '' ;
2016-09-06 20:39:27 +00:00
if ( $relatedAttendesCount >= 1 ) {
2016-03-30 01:37:38 +00:00
$confirmOrderTicketsRoute = route ( 'confirmCheckInOrderTickets' , [ $event -> id , $attendee -> order_id ]);
2016-09-06 20:39:27 +00:00
$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>' ;
2016-03-30 01:37:38 +00:00
}
if ( $attendee -> has_arrived ) {
return response () -> json ([
2016-06-15 02:31:24 +00:00
'status' => 'error' ,
2016-09-06 20:39:27 +00:00
'message' => 'Warning: This attendee has already been checked in at ' . $attendee -> arrival_time -> format ( 'H:i A, F j' ) . '.' . $appendedText
2016-03-30 01:37:38 +00:00
]);
}
Attendee :: find ( $attendee -> id ) -> update ([ 'has_arrived' => true , 'arrival_time' => Carbon :: now ()]);
return response () -> json ([
2016-06-15 02:31:24 +00:00
'status' => 'success' ,
2016-09-06 20:39:27 +00:00
'message' => 'Success !<br>Name: ' . $attendee -> first_name . ' ' . $attendee -> last_name . '<br>Reference: ' . $attendee -> reference . '<br>Ticket: ' . $attendee -> ticket . '.' . $appendedText
2016-06-15 02:31:24 +00:00
]);
2016-03-30 01:37:38 +00:00
}
/**
* Confirm tickets of same order .
*
* @ param $event_id
* @ param $order_id
* @ return \Illuminate\Http\Response
*/
public function confirmOrderTickets ( $event_id , $order_id )
{
2016-09-06 20:39:27 +00:00
$updateRowsCount = Attendee :: scope () -> where ([
'event_id' => $event_id ,
'order_id' => $order_id ,
'has_arrived' => false ,
'arrival_time' => Carbon :: now (),
])
-> update ([ 'has_arrived' => true , 'arrival_time' => Carbon :: now ()]);
2016-03-30 01:37:38 +00:00
2016-04-11 11:52:50 +00:00
return response () -> json ([
2016-06-15 02:31:24 +00:00
'message' => $updateRowsCount . ' Attendee(s) Checked in.'
2016-04-11 11:52:50 +00:00
]);
2016-03-30 01:37:38 +00:00
}
}