Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 129 |
| AttendanceDetailCreatedOrUpdateListener | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
1056 | |
0.00% |
0 / 129 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| handle | |
0.00% |
0 / 1 |
992 | |
0.00% |
0 / 128 |
|||
| <?php | |
| namespace App\Listeners; | |
| use App\Http\Controllers\FirebaseController\FirebaseCloudMessaging; | |
| use App\Events\AttendanceDetailCreatedOrUpdated; | |
| use Illuminate\Queue\InteractsWithQueue; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| use App\Models\Admin\UserDetail; | |
| use App\Notifications\AttendanceDetailNotifications; | |
| use App\Notifications\SlackNotifications; | |
| use App\Models\NotificationModel\NotificationList; | |
| use App\Models\Auth\User; | |
| use App\Notifications\AppNotifications; | |
| use Carbon\Carbon; | |
| use GetShiftDetailHelper; | |
| class AttendanceDetailCreatedOrUpdateListener | |
| { | |
| /** | |
| * Create the event listener. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| // | |
| } | |
| /** | |
| * Handle the event. | |
| * | |
| * @param AttendanceDetailCreatedOrUpdated $event | |
| * @return void | |
| */ | |
| public function handle(AttendanceDetailCreatedOrUpdated $event) | |
| { | |
| $user_id = $event->attendanceDetail->user_id; //user_id | |
| $userDetail = UserDetail::where('user_id','=',$user_id)->first(); | |
| //primary reporting manager | |
| $primary_reporting_manager_id = $userDetail->primary_reporting_manager_id; | |
| //secondary reporting manager | |
| $secondaryManagers = $userDetail->secondaryManager; | |
| if($event->attendanceDetail->mode != 'On Request'){ | |
| $todayDate = Carbon::now(config('settings.time_zone'))->format(config('settings.php_date_format')); | |
| $startDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $endDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->addDay()->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| if($event->attendanceDetail->wasRecentlyCreated) //created | |
| { | |
| if(isset($userDetail->user_id)) | |
| { | |
| $userAttendance= User::find($userDetail->user_id); // checked - in user $user->name; | |
| // $userAttendanceTeam =$userAttendance->userdetail->teams; | |
| // foreach($userAttendanceTeam as $userTeam) | |
| // { | |
| // $userInTeam = $userTeam->team; | |
| // } | |
| //primary team name | |
| $userInTeam = $userDetail->primaryTeamName->team; | |
| //get shift details | |
| $shift = GetShiftDetailHelper::getShiftDetail($user_id,$startDate,$endDate); | |
| if($shift != null){ | |
| $shiftStartTime = $shift['shiftStartTime']; | |
| $shiftEndTime = $shift['shiftEndTime']; | |
| $bufferCheckinTime = $shift['bufferCheckinTime']; | |
| $bufferCheckinSecs = strtotime($bufferCheckinTime) - strtotime("00:00:00"); | |
| $timeWithBuffer = date("H:i:s", strtotime($shiftStartTime) + $bufferCheckinSecs); | |
| if(isset($event->attendanceDetail->check_in)) | |
| { | |
| $checkindate = Carbon::parse($event->attendanceDetail->check_in); | |
| $checkindate = ($checkindate)->setTimezone(config('settings.time_zone')); | |
| $checkInTime = Carbon::parse($checkindate)->toTimeString(); | |
| //user checkin time | |
| $checkInSlack = Carbon::parse($checkindate)->isoFormat('H:mm'); | |
| } | |
| else | |
| { | |
| $checkInTime = null; | |
| } | |
| // $notificationList = NotificationList::firstOrFail(); | |
| // $usersToNotifyList = $notificationList->users; | |
| if($checkInTime > $timeWithBuffer) | |
| { | |
| // $notificationList = NotificationList::firstOrFail(); | |
| // $usersToNotifyList = $notificationList->users; | |
| $checkTime = 'Check In Time - ' .$checkInTime; | |
| $subjectLine = $userAttendance->name.' '. $userAttendance->lastName.' has Checked In late for '.$todayDate; | |
| // foreach($usersToNotifyList as $notification_user) | |
| // { | |
| // $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine]; | |
| // $notification_user->notify(new AttendanceDetailNotifications($arr)); //username or checked in time | |
| // } | |
| $title = "Late Checkin"; | |
| if ($primary_reporting_manager_id != 0) { | |
| //primary reporting manager | |
| $primary_reporting_manager = User::find($primary_reporting_manager_id); | |
| //check if user is exists | |
| if(!empty($primary_reporting_manager)){ | |
| $arr = null; | |
| $message=$userAttendance->name . ' ' . $userAttendance->lastName . ' has sent Late Checkin request today.'; | |
| $arr = [$title,$message , null]; | |
| $check_data = [ | |
| 'notification' => $arr[0], | |
| 'message' => $arr[1], | |
| 'actionUrl' => $arr[2] | |
| ]; | |
| $primary_reporting_manager->notify(new AppNotifications($arr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$primary_reporting_manager_id); | |
| } | |
| } | |
| //secondary reporting managers | |
| if (count($secondaryManagers) > 0) { | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primary_reporting_manager_id != $secondaryManager->id){ | |
| $arr = null; | |
| $arr = [$title, $userAttendance->name . ' ' . $userAttendance->lastName . ' has Checked In late today.', null]; | |
| $check_data = [ | |
| 'notification' => $arr[0], | |
| 'message' => $arr[1], | |
| 'actionUrl' => $arr[2] | |
| ]; | |
| $secondaryManager->notify(new AppNotifications($arr)); | |
| } | |
| } | |
| } | |
| //Notification to the user | |
| $subjectLine = 'You checked in late for '.$todayDate; | |
| $arr = null; | |
| $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| $userAttendance->notify(new AttendanceDetailNotifications($arr)); | |
| $slackMessage = 'Checked in late.'. $slackMessage = ' Team: '.$userInTeam.', '. | |
| 'Name: '.$userAttendance->name.' '.$userAttendance->lastName.', '. 'Check In Time: ' . $checkInSlack; | |
| $userAttendance->notify(new SlackNotifications($slackMessage)); | |
| } | |
| else | |
| { | |
| if(isset($checkInSlack)) | |
| { | |
| $slackMessage = 'Team:'.' '.$userInTeam.', '. 'Name:'.' '.$userAttendance->name.' '. | |
| $userAttendance->lastName.', '. 'Check In Time: ' . $checkInSlack; | |
| $userAttendance->notify(new SlackNotifications($slackMessage)); //username or checked in time | |
| } | |
| } | |
| } | |
| if(!isset($event->attendanceDetail->check_in)) | |
| { | |
| $subjectLine = $userAttendance->name.' '. $userAttendance->lastName.' did not Check In for '.$todayDate; | |
| $title = 'Did not Checkin'; | |
| $checkTime = 'Check-In-Time - ' .''; | |
| if ($primary_reporting_manager_id != 0) { | |
| //primary reporting manager | |
| $primary_reporting_manager = User::find($primary_reporting_manager_id); | |
| //check if user is exists | |
| if(!empty($primary_reporting_manager)){ | |
| $arr = null; | |
| $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| $primary_reporting_manager->notify(new AttendanceDetailNotifications($arr)); | |
| } | |
| } | |
| //secondary reporting managers | |
| if (count($secondaryManagers) > 0) { | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primary_reporting_manager_id != $secondaryManager->id){ | |
| $arr = null; | |
| $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| $secondaryManager->notify(new AttendanceDetailNotifications($arr)); | |
| } | |
| } | |
| } | |
| $subjectLine = 'You did not Check In for '.$todayDate; | |
| $arr = null; | |
| $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| $userAttendance->notify(new AttendanceDetailNotifications($arr)); | |
| $slackMessage = 'Did not check in. Team:'.' '.$userInTeam.', '. 'Name:'.' '.$userAttendance->name.' '. | |
| $userAttendance->lastName ; | |
| $userAttendance->notify(new SlackNotifications($slackMessage)); //username or checked in time | |
| } | |
| } | |
| else | |
| { | |
| return; | |
| } | |
| } | |
| else //updated | |
| { | |
| $user_id = $event->attendanceDetail->user_id; //user_id | |
| $userDetail = UserDetail::where('user_id','=',$user_id)->first(); | |
| /*print_r($event->attendanceDetail); | |
| exit;*/ | |
| if(isset($userDetail->user_id)) | |
| { | |
| $userAttendance = User::find($userDetail->user_id); // checked - in user $user->name; | |
| if(!empty($userAttendance)){ | |
| if(!empty($userAttendance->userdetail) && !empty($userDetail->primaryTeamName->team)){ | |
| // $userAttendanceTeam = $userAttendance->userdetail->teams; | |
| // foreach($userAttendanceTeam as $userTeam) | |
| // { | |
| // $userInTeam = $userTeam->team; | |
| // } | |
| //primary team name | |
| $userInTeam = $userDetail->primaryTeamName->team; | |
| //get shift details | |
| $shift = GetShiftDetailHelper::getShiftDetail($user_id,$startDate,$endDate); | |
| if($shift != null){ | |
| $shiftStartTime = $shift['shiftStartTime']; | |
| $shiftEndTime = $shift['shiftEndTime']; | |
| // $bufferTime = $shift['bufferTime']; | |
| // $secs = strtotime($bufferTime)-strtotime("00:00:00"); | |
| $timeWithBuffer = date("H:i:s",strtotime($shiftEndTime)); | |
| if(isset($event->attendanceDetail->check_out)) | |
| { | |
| $checkoutdate = Carbon::parse($event->attendanceDetail->check_out); | |
| $checkoutdate = ($checkoutdate)->setTimezone(config('settings.time_zone')); | |
| $checkOutTime = Carbon::parse($checkoutdate)->toTimeString(); //user checkin time | |
| $checkOutSlack = Carbon::parse($checkoutdate)->isoFormat('H:mm'); | |
| } | |
| else | |
| { | |
| $checkOutSlack = $checkOutTime = null; | |
| } | |
| if($checkOutTime < $timeWithBuffer) //early checkout | |
| { | |
| // $notificationList = NotificationList::firstOrFail(); | |
| // $usersToNotifyList = $notificationList->users; | |
| // $subjectLine = 'AMS Notification : Early Check Out'; | |
| $subjectLine = $userAttendance->name.' '. $userAttendance->lastName.' has sent Early checkout request today.'; | |
| $checkTime = 'Check-Out-Time - ' .$checkOutTime; | |
| $title = "Early Checkout"; | |
| // foreach($usersToNotifyList as $notification_user) | |
| // { | |
| // $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine]; | |
| // $notification_user->notify(new AttendanceDetailNotifications($arr)); //username or checked in time | |
| // } | |
| $earlyCheckoutarr = null; | |
| $earlyCheckoutarr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| if ($primary_reporting_manager_id != 0) { | |
| //primary reporting manager | |
| $primary_reporting_manager = User::find($primary_reporting_manager_id); | |
| //check if user is exists | |
| if(!empty($primary_reporting_manager)){ | |
| // $primary_reporting_manager->notify(new AttendanceDetailNotifications($earlyCheckoutarr)); | |
| } | |
| } | |
| //secondary reporting managers | |
| if (count($secondaryManagers) > 0) { | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primary_reporting_manager_id != $secondaryManager->id){ | |
| // $secondaryManager->notify(new AttendanceDetailNotifications($earlyCheckoutarr)); | |
| } | |
| } | |
| } | |
| $subjectLine = 'You have checked out early.'; | |
| $arr = null; | |
| $arr = [$userInTeam, $userAttendance->name.' '. $userAttendance->lastName, $checkTime, $subjectLine,$title]; | |
| $userAttendance->notify(new AttendanceDetailNotifications($arr)); | |
| $slackMessage = 'Checked out early.'. ' Team: '.$userInTeam.', '. | |
| 'Name: '.$userAttendance->name.' '.$userAttendance->lastName.', '. 'Check Out Time: ' . $checkOutSlack; | |
| $userAttendance->notify(new SlackNotifications($slackMessage)); | |
| } | |
| else | |
| { | |
| $slackMessage = 'Team:'.' '.$userInTeam.', '. 'Name:'.' '.$userAttendance->name.' '. | |
| $userAttendance->lastName.', '. 'Check Out Time: ' . $checkOutSlack; | |
| $userAttendance->notify(new SlackNotifications($slackMessage)); //username or checked in time | |
| } | |
| } | |
| } | |
| }else | |
| { | |
| return; | |
| } | |
| } | |
| else | |
| { | |
| return; | |
| } | |
| } | |
| } | |
| } | |
| } |