Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 433 |
| RequestAttendanceController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
7310 | |
0.00% |
0 / 433 |
| index | |
0.00% |
0 / 1 |
600 | |
0.00% |
0 / 87 |
|||
| create | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| store | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| show | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| edit | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| update | |
0.00% |
0 / 1 |
870 | |
0.00% |
0 / 150 |
|||
| destroy | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| requestAttendanceDetail | |
0.00% |
0 / 1 |
756 | |
0.00% |
0 / 191 |
|||
| <?php | |
| namespace App\Http\Controllers\Attendance; | |
| use App\Http\Controllers\FirebaseController\FirebaseCloudMessaging; | |
| use Illuminate\Http\Request; | |
| use App\Http\Controllers\Controller; | |
| use App\Models\Auth\User; | |
| use App\Models\Admin\UserDetail; | |
| use App\Models\Dashboard\AttendanceDetail; | |
| use Illuminate\Support\Facades\Validator; | |
| use Carbon\Carbon; | |
| use Session; | |
| use Illuminate\Support\Facades\Auth; | |
| use App\Models\AttendanceRequests\UserAttendanceRequests; | |
| use App\Models\Admin\Setting; | |
| use App\Notifications\RequestAttendanceNotifications; | |
| use App\Notifications\AppNotifications; | |
| //use App\Models\NotificationModel\NotificationList; | |
| use App\Models\Admin\Team; | |
| use App\Models\Auth\UserShift; | |
| use App\Models\Admin\UserDetailShift; | |
| use App\Models\Schedule\Schedule; | |
| use App\Models\Schedule\ScheduleDetail; | |
| use App\Models\Admin\TeamShiftDetail; | |
| use App\Models\Admin\Shift; | |
| use App\Models\Leave\ApplyLeave; | |
| use App\Helpers\AttendanceHelper; | |
| class RequestAttendanceController extends Controller | |
| { | |
| /** | |
| * Display a listing of the resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function index(Request $request) | |
| { | |
| //for getting shift end time | |
| $user =User::find(Auth::user()->id); | |
| if($user->userdetail){ | |
| $teamId = $user->userdetail->primary_team_id; | |
| $team = Team::find($teamId); | |
| } | |
| else{ | |
| $team = null; | |
| } | |
| if($team !== null && $team->has_schedule == 1) { | |
| $check = ScheduleDetail::where('user_id', Auth::user()->id)->exists(); | |
| if($check){ | |
| $schedule_shift_id = ScheduleDetail::where('user_id', Auth::user()->id) | |
| ->where('is_active', 1) | |
| ->where(function ($query) { | |
| $query->where('mon', 1) | |
| ->orWhere('tue', 1) | |
| ->orWhere('wed', 1) | |
| ->orWhere('thu', 1) | |
| ->orWhere('fri', 1) | |
| ->orWhere('sat', 1) | |
| ->orWhere('sun', 1); | |
| }) | |
| ->latest()->first(); | |
| $schedule_shift_id = $schedule_shift_id->getAttribute('shift_id'); | |
| $schedule_shift = Shift::where('id', $schedule_shift_id)->exists(); | |
| if($schedule_shift){ | |
| $shift_End_Time = Shift::where('id', $schedule_shift_id) | |
| ->value('end_time'); | |
| } | |
| } | |
| else{ | |
| $shift_End_Time = null; | |
| } | |
| } | |
| else{ | |
| $check = UserDetail::where('user_id', Auth::user()->id)->exists(); | |
| if($check){ | |
| $usr_detail_id = UserDetail::where('user_id', Auth::user()->id) | |
| ->value('id'); | |
| $checkshift = UserDetailShift::where('user_detail_id', $usr_detail_id)->exists(); | |
| if($checkshift){ | |
| $shift_id = UserDetailShift::where('user_detail_id', $usr_detail_id) | |
| ->value('shift_id'); | |
| } | |
| $check_endtime = Shift::where('id', $shift_id)->exists(); | |
| if($check_endtime){ | |
| $shift_End_Time = Shift::where('id', $shift_id) | |
| ->value('end_time'); | |
| } | |
| } | |
| else{ | |
| $shift_End_Time = null; | |
| } | |
| } | |
| //to get attendance period value from table to provide the time | |
| $existAttPeriod = Setting::where("name",'=', "attendance_period")->exists(); | |
| if($existAttPeriod){ | |
| $attendance_period=Setting::where("name",'=', "attendance_period")->get(); | |
| $attendance_array = []; | |
| foreach($attendance_period as $attendance_data){ | |
| $attendance_array = $attendance_data['value']; | |
| } | |
| } | |
| else{ | |
| $attendance_array = null; | |
| } | |
| //to get attendance freq value from table | |
| $existAttFreq = Setting::where("name",'=', "attendance_freq")->exists(); | |
| if($existAttFreq){ | |
| $attendance_freq = Setting::where("name", '=', "attendance_freq")->get(); | |
| $attendance_frequency = []; | |
| foreach($attendance_freq as $attendance_data){ | |
| $attendance_frequency = $attendance_data['value']; | |
| } | |
| } | |
| else{ | |
| $attendance_frequency = null; | |
| } | |
| $title = '/Attendance/Request Attendance'; | |
| $backurl = route('home'); | |
| $id = Auth::user()->id; | |
| $user = User::find($id); | |
| $atdnce = 0; | |
| $attendances = AttendanceDetail::where('user_id',$id)->get(); | |
| if(!empty($attendances)){ | |
| foreach($attendances as $attendance) | |
| { | |
| $atdnce = $attendance->id; | |
| } | |
| } | |
| $absentDate = base64_decode(urldecode(request()->getQueryString())); | |
| // For returning attendance marking | |
| $val = AttendanceHelper::getAttendanceMarkValue(); | |
| $valTeam = AttendanceHelper::getAttendanceTeam($request); | |
| $valUser = AttendanceHelper::getAttendanceUser($request); | |
| //for deleting late checkin request if he request for both for the same day | |
| $currUser = Auth::user()->id; | |
| $currentDates = []; | |
| $attendanceDetail = UserAttendanceRequests::where('user_id', '=', $currUser) | |
| ->where('status', '=', 'pending') | |
| ->get(); | |
| for ($i = 0; $i < count($attendanceDetail); $i++) { | |
| $current_date[$i] = $attendanceDetail[$i]->getAttribute('created_at'); | |
| $currentDates[$i] = Carbon::parse($current_date[$i])->format('d-m-Y'); | |
| } | |
| // Compare the current dates to find pairs with "pending" status | |
| $pendingDates = []; | |
| for ($i = 0; $i < count($currentDates); $i++) { | |
| for ($j = $i + 1; $j < count($currentDates); $j++) { | |
| if ($attendanceDetail[$i]->status == 'pending' && $attendanceDetail[$j]->status == 'pending') { | |
| $pendingDates[] = [$currentDates[$i], $currentDates[$j]]; | |
| } | |
| } | |
| } | |
| // Delete UserAttendanceRequests where requested_for = "late check in" and "both" request is present | |
| foreach ($attendanceDetail as $attendance) { | |
| if ($attendance->requested_for == 'Late Checkin' && in_array(Carbon::parse($attendance->created_at)->format('d-m-Y'), $currentDates)) { | |
| // Check if a "both" request exists for the same date | |
| $bothRequestExists = UserAttendanceRequests::where('user_id', '=', $attendance->user_id) | |
| ->where('requested_for', '=', 'both') | |
| ->whereDate('check_in', Carbon::parse($attendance->created_at)->format('Y-m-d')) | |
| ->exists(); | |
| if ($bothRequestExists) { | |
| $attendance->delete(); | |
| } | |
| } | |
| } | |
| return view('attendance.RequestAttendanceForm',compact('title','user','attendances','atdnce','absentDate','backurl','val','valTeam','valUser','attendance_array','shift_End_Time')); | |
| } | |
| /** | |
| * Show the form for creating a new resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function create() | |
| { | |
| } | |
| /** | |
| * Store a newly created resource in storage. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function store(Request $request) | |
| { | |
| // | |
| } | |
| /** | |
| * Display the specified resource. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function show($id) | |
| { | |
| } | |
| /** | |
| * Show the form for editing the specified resource. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function edit($id) | |
| { | |
| } | |
| /** | |
| * Update the specified resource in storage. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function update(Request $request, $id) | |
| { | |
| $requestType = $request->requestType; | |
| $user = User::find($request->user); | |
| $date = Carbon::parse( Carbon::createFromFormat( config('settings.php_date_format') , $request->date ) )->format('Y-m-d'); | |
| // $date = date('Y-m-d', strtotime($request->date)); | |
| $checkout_null = $request->check_out; | |
| $checkIn = date("H:i:s", strtotime($request->check_in)); | |
| $checkOut = date("H:i:s", strtotime($request->check_out)); | |
| $request['check_in']= $checkIn; | |
| $request['check_out']= $checkOut; | |
| if($request->check_in > $request->check_out) //If check in dates greater than check out time means two day shift | |
| { | |
| $addDayToRequestDate = Carbon::parse(Carbon::createFromFormat( config('settings.php_date_format') , $request->date ),Config('app.timezone'))->addDay()->toDateString(); | |
| $timeStampCheckIn = $date.' '.$request->check_in; | |
| $timeStampCheckOut = $addDayToRequestDate.' '.$request->check_out; | |
| } | |
| else | |
| { | |
| $timeStampCheckIn = $date.' '.$request->check_in; | |
| $timeStampCheckOut = $date.' '.$request->check_out; | |
| } | |
| $timeStampCheckIn = Carbon::parse($timeStampCheckIn,config('settings.time_zone')); | |
| $timeStampCheckIn = ($timeStampCheckIn)->setTimezone(config('app.timezone')); | |
| $timeStampCheckOut = Carbon::parse($timeStampCheckOut,config('settings.time_zone')); | |
| $timeStampCheckOut = ($timeStampCheckOut)->setTimezone(config('app.timezone')); | |
| $timeStampCheckIn = Carbon::parse($timeStampCheckIn)->format('Y-m-d H:i:s'); | |
| $timeStampCheckOut = Carbon::parse($timeStampCheckOut)->format('Y-m-d H:i:s'); | |
| if($request->has('shift')){ | |
| $shift = Shift::where('id',$request->shift)->first(); | |
| $shiftStartTime = $shift->start_time; | |
| $shiftEndTime = $shift->end_time; | |
| }else{ | |
| $shiftDetails = $user->userdetail->shifts; | |
| if($shiftDetails->count() == 1) //if there is only one applicable shift for a user | |
| { | |
| foreach ($shiftDetails as $shift) | |
| { | |
| $shiftStartTime = $shift->start_time; | |
| $shiftEndTime = $shift->end_time; | |
| } | |
| } | |
| else | |
| { | |
| return; | |
| } | |
| } | |
| $today = Carbon::now(config('settings.time_zone')); | |
| $dcheck_in = Carbon::parse($date); | |
| $dcheck_in->setTimezone(config('settings.time_zone')); | |
| $dcheck_in = $dcheck_in->format(config('settings.php_date_format')); | |
| //if user is on leave | |
| $userOnLeave = ApplyLeave::where('from_date', '<=', $dcheck_in) | |
| ->where('to_date', '>=', $dcheck_in) | |
| ->where('status', 'Approve') | |
| ->where('user_id', Auth::user()->id) | |
| ->first(); | |
| $comment = $request->comment; | |
| if(!empty($userOnLeave) ){ | |
| if($userOnLeave->leave_type == 'full_day'){ | |
| $comment = $request->comment.' (System : Approving attendance will cancel the leave for the day)'; | |
| } | |
| } | |
| $startDate = Carbon::parse($date . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $endDate = Carbon::parse(Carbon::parse($date)->addDay()->toDateString() . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $attendanceDetail = AttendanceDetail::where('user_id', '=', Auth::user()->id) | |
| ->whereBetween('check_in', [$startDate, $endDate]) | |
| ->first(); | |
| $checkout_time = $timeStampCheckOut; | |
| if(empty($attendanceDetail)){ | |
| if($checkout_null == null){ | |
| $requested_for = 'Checkin'; | |
| $checkout_time = null; | |
| }else{ | |
| $requested_for = 'Both'; | |
| } | |
| }else if(!empty($attendanceDetail)){ | |
| if($checkout_null == null){ | |
| $requested_for = 'Checkin'; | |
| $checkout_time = null; | |
| }else if($attendanceDetail->checkout == null){ | |
| $requested_for = 'Checkout'; | |
| } | |
| } | |
| // $atdnce = UserAttendanceRequests::create( | |
| // [ | |
| // 'user_id' => $user->id, | |
| // 'check_in' => $timeStampCheckIn, | |
| // 'check_out' => $checkout_time, | |
| // 'requested_for' => $requested_for, | |
| // 'comments'=> $comment, | |
| // 'status' => "pending", | |
| // 'start_time' => $shiftStartTime, | |
| // 'end_time' => $shiftEndTime | |
| // ]); | |
| // $userAttendanceTeam =$user->userdetail->teams; | |
| // foreach($userAttendanceTeam as $userTeam) | |
| // { | |
| // $userInTeam = $userTeam->team; | |
| // } | |
| //primary team name | |
| $userInTeam = $user->userdetail->primaryTeamName->team; | |
| $date = $request->date; | |
| //app notification and mail notification | |
| //mail notifications to notification list | |
| $subjectLine = 'AMS Notification : '.$user->name . ' ' . $user->lastName . ' has requested attendance for '.$date; | |
| $mailArr = null; | |
| $checkinForMail = date(Config('settings.time_format'), strtotime($request->check_in)); | |
| $checkoutForMail = date(Config('settings.time_format'), strtotime($request->check_out)); | |
| //for app notification | |
| $title = "Attendance Request"; | |
| $message = $user->name . ' ' . $user->lastName . ' has requested attendance for '.$date; | |
| $actionUrl = 'approve-attendance.index'; | |
| $mailArr = [$subjectLine, $userInTeam, $user->name.' '. $user->lastName, | |
| $date ,$checkinForMail, $checkoutForMail,$title, | |
| $message,$actionUrl]; | |
| //primary reporting manager | |
| $primary_reporting_manager_id = $user->userdetail->primary_reporting_manager_id; | |
| //secondary reporting manager | |
| $secondaryManagers = $user->userdetail->secondaryManager; | |
| if ($primary_reporting_manager_id != 0) { | |
| //primary reporting manager | |
| $primary_reporting_manager = User::find($primary_reporting_manager_id); | |
| $primary_reporting_manager->notify(new AppNotifications( | |
| [ | |
| $title, | |
| $message, | |
| $actionUrl | |
| ] | |
| )); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$primary_reporting_manager); // removed id here efore the last commit but nitification were not working before removing the id so fingers crossed. | |
| //mail notification | |
| $primary_reporting_manager->notify(new RequestAttendanceNotifications($mailArr)); | |
| } | |
| //secondary reporting managers | |
| if (count($secondaryManagers) > 0) { | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primary_reporting_manager_id != $secondaryManager->id){ | |
| $arr = null; | |
| $arr = [$title, $message, $actionUrl]; | |
| // $secondaryManager->notify(new AppNotifications($arr)); | |
| //mail notification | |
| $secondaryManager->notify(new RequestAttendanceNotifications($mailArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$secondaryManager->id); | |
| } | |
| } | |
| } | |
| // else { | |
| // //notification users | |
| // $notificationList = NotificationList::firstOrFail(); | |
| // $usersToNotifyList = $notificationList->users; | |
| // foreach ($usersToNotifyList as $notification_user) { | |
| // $arr = null; | |
| // $arr = [$title, $message, $actionUrl]; | |
| // // $notification_user->notify(new AppNotifications($arr)); | |
| // $notification_user->notify(new RequestAttendanceNotifications($mailArr)); | |
| // } | |
| // } | |
| //user should not request twice in a day | |
| $userDetail = UserAttendanceRequests::where('user_id','=', Auth::user()->id) | |
| ->select('check_in') | |
| ->get(); | |
| $userDetails = array(); | |
| $selectedDate = $request->input('date'); | |
| $userSelectedDate = str_replace("/", "-", $selectedDate); | |
| $currentDate = date('d-m-Y'); | |
| $RequestedDates = []; | |
| for($i=0; $i<count($userDetail); $i++){ | |
| $userDetails[$i] = $userDetail[$i]->getAttribute('check_in'); | |
| $istDateTime = Carbon::parse($userDetails[$i])->setTimezone('Asia/Kolkata'); | |
| $RequestedDates[$i] = $istDateTime->format('d-m-Y'); | |
| } | |
| $selectedDateCount = 0; | |
| foreach ($RequestedDates as $requestedDate) { | |
| if ($requestedDate === $userSelectedDate) { | |
| $selectedDateCount++; | |
| } | |
| } | |
| if ($selectedDateCount >= 2) { | |
| if($requestType=="api"){ | |
| return json_encode([ | |
| "message" => "You can request only twice for a date" | |
| ]); | |
| } | |
| return redirect()->back()->with('warning', 'You can request only twice for a date'); | |
| } | |
| //checking if the user have crossed the attendance frequency in a month | |
| $existAttFreq = Setting::where("name", '=', "attendance_freq")->exists(); | |
| if($existAttFreq){ | |
| $attendance_freq = Setting::where("name", '=', "attendance_freq")->get(); | |
| $attendance_frequency = []; | |
| foreach($attendance_freq as $attendance_data){ | |
| $attendance_frequency = $attendance_data['value']; | |
| } | |
| }else{ | |
| $attendance_frequency = null; | |
| } | |
| $checkExist = UserAttendanceRequests::where('user_id',Auth::user()->id)->exists(); | |
| if($checkExist == true){ | |
| $attendance_freq=Auth::user()->id; | |
| $thisvalue=UserAttendanceRequests::where('user_id',$attendance_freq)->pluck('attendance_freq'); | |
| $previousvalue=$thisvalue[0]; | |
| } | |
| else{ | |
| $previousvalue = 0; | |
| } | |
| if($attendance_frequency!= null && $previousvalue>($attendance_frequency-1)){ | |
| $currentSelectedTime = Setting::where('name','=','attendance_freq')->first(); | |
| if($currentSelectedTime){ | |
| $currentSelectedTimes = $currentSelectedTime->value; | |
| } | |
| if($requestType=="api"){ | |
| return json_encode([ | |
| "message" => "You can request only ".$currentSelectedTimes." times" | |
| ]); | |
| } | |
| return redirect()->back()->with('warning','You can request only '. $currentSelectedTimes .' times'); | |
| }else{ | |
| $atdnce = UserAttendanceRequests::create( | |
| [ | |
| 'user_id' => $user->id, | |
| 'check_in' => $timeStampCheckIn, | |
| 'check_out' => $checkout_time, | |
| 'requested_for' => $requested_for, | |
| 'comments'=> $comment, | |
| 'status' => "pending", | |
| 'start_time' => $shiftStartTime, | |
| 'end_time' => $shiftEndTime | |
| ]); | |
| Session::flash('success', 'Request Attendance Successfully Sent!'); | |
| //to increase the attendance freq in the table | |
| $thisvalue=UserAttendanceRequests::where('user_id',Auth::user()->id)->pluck('attendance_freq'); | |
| $previousvalue=$thisvalue[0]; | |
| UserAttendanceRequests::where('user_id', Auth::user()->id)->update(['attendance_freq'=> $previousvalue+1]); | |
| if($requestType=="api"){ | |
| return json_encode([ | |
| "message" => "Attendance Request Sent Successfully" | |
| ]); | |
| } | |
| return redirect()->route('request-attendance.index'); | |
| } | |
| } | |
| /** | |
| * Remove the specified resource from storage. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function destroy($id) | |
| { | |
| // | |
| } | |
| // added the dropdown | |
| public function requestAttendanceDetail(Request $request) | |
| { | |
| $requestType = $request->requestType; | |
| $canRequest = true; | |
| $currentShift = ""; | |
| $currentShiftId = ""; | |
| $attendanceGraceSetting = Setting::where('name', 'applied_before')->first(); | |
| // dd($attendanceGraceSetting); | |
| $attendanceGrace = 0; | |
| if ($attendanceGraceSetting && isset($attendanceGraceSetting->value)) { | |
| $attendanceGrace = (int)$attendanceGraceSetting->value; | |
| } | |
| $requestedDate = Carbon::parse(Carbon::createFromFormat( config('settings.php_date_format') , $request->date ),Config('app.timezone')); | |
| $currentDateTime = $requestedDate->format('Y-m-d'); | |
| $serverDate = Carbon::now(); | |
| $serverMonth = $serverDate->format('m'); | |
| $requestedForMonth = $requestedDate->format('m'); | |
| $serverDay = $serverDate->format('d'); | |
| if($requestedForMonth<$serverMonth){ | |
| if($attendanceGrace<$serverDay){ | |
| $canRequest = false; | |
| return response()->json([ | |
| 'canRequest' => $canRequest | |
| ]); | |
| } | |
| } | |
| $user =User::find($request->userId); | |
| $timezone = Config('app.timezone'); | |
| //$currentDateTime =$request->date; | |
| $currentDateTime = Carbon::parse(Carbon::createFromFormat( config('settings.php_date_format') , $request->date ),Config('app.timezone'))->format('Y-m-d'); | |
| $presentDatesArray = []; | |
| $startDate = Carbon::parse($currentDateTime.'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $endDate = Carbon::parse(Carbon::parse($currentDateTime)->addDay()->toDateString().'00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $currentDate = $request->date; | |
| $user_shift_name = ''; | |
| $shiftTimes = []; | |
| $shiftNames = []; | |
| $val = AttendanceHelper::getAttendanceMarkValue(); | |
| $valTeam = AttendanceHelper::getAttendanceTeam($request); | |
| $valUser = AttendanceHelper::getAttendanceUser($request); | |
| if($valUser != null){ | |
| $attendanceValue = $valUser; | |
| }else if($valTeam != null){ | |
| $attendanceValue = $valTeam; | |
| }else{ | |
| $attendanceValue = $val; | |
| } | |
| if($attendanceValue == 3 || $attendanceValue == null){ | |
| $presentDates = AttendanceDetail::where('user_id', $user->id) | |
| ->whereNotNull('check_in') | |
| ->whereNotNull('check_out') | |
| ->get(); | |
| } else if ($attendanceValue == 2){ | |
| $presentDates = AttendanceDetail::where('user_id', $user->id) | |
| ->whereNotNull('check_in') | |
| ->get(); | |
| }else if ($attendanceValue == 1){ | |
| $presentDates = AttendanceDetail::where('user_id', $user->id) | |
| ->get(); | |
| } | |
| if ($presentDates->isNotEmpty()) { | |
| $presentDatesArray = []; | |
| foreach ($presentDates as $value) { | |
| $date = $value->check_in; | |
| $carbonDate = Carbon::parse($date)->setTimezone('Asia/Kolkata'); | |
| $singleDate = Carbon::parse($carbonDate)->format('d/m/Y'); | |
| $presentDatesArray[] = $singleDate; | |
| } | |
| } | |
| if(in_array($currentDate, $presentDatesArray)){ | |
| return response()->json([ | |
| 'status'=> '100', | |
| 'canRequest' => $canRequest, | |
| 'message' => 'Already Present On '.$currentDate, | |
| 'user_shift_name' => $user_shift_name, | |
| 'id'=> null, | |
| 'check_in' => null , | |
| 'check_out' => null, | |
| 'attendance_requet' => null, | |
| ]); | |
| } | |
| else{ | |
| if(isset($user)) | |
| { | |
| $currentDateTime1 = Carbon::parse($currentDateTime,config('settings.time_zone')); | |
| //check user is already requested atttendance on date | |
| // $user_attendance_requets = UserAttendanceRequests::where('user_id','=',$user->userdetail->biometric_id) | |
| // ->whereDate('check_in', '=',$currentDateTime1)->first(); | |
| $user_attendance_requets = UserAttendanceRequests::where('user_id','=',$user->id) | |
| ->whereBetween('check_in', [$startDate, $endDate]) | |
| ->first(); | |
| // if(!empty($user_attendance_requets)){ | |
| // $attendance_requet = 'You already requested attendance on '.$request->date; | |
| // }else{ | |
| $attendance_requet = null; | |
| //user teams | |
| // $userAttendanceTeam = $user->userdetail->teams; | |
| // foreach ($userAttendanceTeam as $userTeam) { | |
| // $teamId = $userTeam->id; | |
| // } | |
| //primary team id | |
| $teamId = $user->userdetail->primary_team_id; | |
| $team = Team::find($teamId); | |
| if($team->has_schedule == 1) { | |
| //$schedules = Schedule::where('team_id', $teamId)->get(); | |
| $schedule = Schedule::where('from_date', '<=', $currentDateTime1) | |
| ->where('to_date', '>=', $currentDateTime1)->where('status', '0') | |
| ->where('team_id', $teamId) | |
| ->first(); | |
| $teamSchedule = $schedule; | |
| if(!isset($teamSchedule)) { | |
| $teamSchedule = Schedule::where('is_default', 1) | |
| ->where('status', '0') | |
| ->where('team_id', $teamId) | |
| ->first(); | |
| } | |
| if(isset($teamSchedule)) { | |
| $shift = UserShift::where('date', $currentDateTime)->where('user_id',$request->userId)->first(); | |
| $teamShifts = TeamShiftDetail::where('team_id', $teamSchedule->team_id)->with('shift')->get(); | |
| // $user_shift_name .= '<select id = "shift" name="shift" class="form-control required">'; | |
| // $user_shift_name .= '<option disabled="" selected="" value="">Please Select Shift</option>'; | |
| // foreach($teamShifts as $teamShift){ | |
| // $user_shift_name .= '<option value="'.$teamShift->shift->id.'">'.$teamShift->shift->shift.'</option>'; | |
| // } | |
| // $user_shift_name .= '</select>'; | |
| if ($shift) { | |
| $user_shift_name = $shift->shift_name; | |
| $currentShift = $shift->shift_name; | |
| $currentShiftId = $shift->shift_id; | |
| $user_shift_name .= '<input type="hidden" name="shift" value="'.$shift->shift_id.'">'; | |
| $dataQuery = AttendanceDetail::where('user_id', $user->id) | |
| ->whereDate('check_in', '=', $currentDateTime) | |
| ->exists(); | |
| if ($dataQuery == false){ | |
| $utcStartTime = Carbon::parse($shift->shift_start, 'UTC'); | |
| $istStartTime = $utcStartTime->setTimezone(Config('settings.time_zone')); | |
| $formattedIstStartTime = $istStartTime->format('h:i A'); | |
| // For converting end_time of shift from UTC into IST | |
| $utcEndTime = Carbon::parse($shift->shift_end, 'UTC'); | |
| $istEndTime = $utcEndTime->setTimezone(Config('settings.time_zone')); | |
| $formattedIstEndTime = $istEndTime->format('h:i A'); | |
| return response()->json([ | |
| 'check_in' => $formattedIstStartTime, | |
| 'canRequest' => $canRequest, | |
| 'check_out' =>$formattedIstEndTime , | |
| 'currentShift' => $currentShift, | |
| 'currentShiftId' => $currentShiftId, | |
| 'id'=> null, | |
| 'attendance_requet' => $attendance_requet, | |
| 'user_shift_name' => $user_shift_name | |
| ]); | |
| } | |
| } else { | |
| $user_shift_name = 'Shift was not assigned for that day'; | |
| return response()->json([ | |
| 'check_in' => null, | |
| 'check_out' =>null, | |
| 'currentShift' => $currentShift, | |
| 'currentShiftId' => $currentShiftId, | |
| 'canRequest' => $canRequest, | |
| 'id'=> null, | |
| 'attendance_requet' => $attendance_requet, | |
| 'user_shift_name' => $user_shift_name | |
| ]); | |
| } | |
| } | |
| }else{ | |
| $teamShifts = Shift::all(); | |
| $shiftDetails = $user->userdetail->shifts; | |
| $user_shift_name .= '<table><tr>'; | |
| $columnOne = '<th>'; | |
| $columnTwo = '<th>'; | |
| if ($shiftDetails->count() == 1) //if there is only one applicable shift for a user | |
| { | |
| $shift = $shiftDetails[0]; | |
| $columnOne .= '<div id="shiftNameDiv">'.$shift->shift.'</div>'; | |
| $columnOne .= '<input type="hidden" name="shift" value="'.$shift->id.'">'; | |
| $columnOne .= '</th>'; | |
| $currentShift = $shift->shift; | |
| $currentShiftId = $shift->id; | |
| } | |
| $columnTwo .= '<select id="shift" name="shift" class="form-control">'; | |
| $columnTwo .= '<option disabled="" selected="" value="">Please Select Shift</option>'; | |
| foreach ($teamShifts as $teamShift) { | |
| $time["start_time"] = Carbon::parse($teamShift->start_time, 'UTC')->setTimezone('Asia/Kolkata')->format('h:i A'); | |
| $time["end_time"] = Carbon::parse($teamShift->end_time, 'UTC')->setTimezone('Asia/Kolkata')->format('h:i A'); | |
| $shiftTimes[$teamShift->id] = $time; | |
| $shiftNames[$teamShift->id] = $teamShift->shift; | |
| // Add an option for each shift | |
| $columnTwo .= '<option value="' . $teamShift->id . '">' . $teamShift->shift . '</option>'; | |
| } | |
| $columnTwo .= '</select>'; | |
| $columnTwo .= '</th>'; | |
| $user_shift_name.= $columnOne; | |
| $user_shift_name .= '<th>  </th>'; | |
| $user_shift_name .= $columnTwo; | |
| $user_shift_name .= '</tr>'; | |
| $user_shift_name .= '</table>'; | |
| } | |
| //} | |
| $query = AttendanceDetail::query(); | |
| $query->where('user_id','=',$user->id); | |
| $query->whereBetween('check_in', [$startDate, $endDate]); | |
| $attendanceDetail = $query->get(); | |
| $count = $attendanceDetail->count(); | |
| if($count != 0) | |
| { | |
| foreach ($attendanceDetail as $value) | |
| { | |
| if(isset($value->user_id)) | |
| { | |
| $attendanceDetailId = $value->id; | |
| if(isset($value->check_in)) | |
| { | |
| $check_in = Carbon::parse($value->check_in,config('app.timezone')); | |
| $check_in->setTimezone(Config('settings.time_zone')); | |
| $check_in = Carbon::parse($check_in)->toTimeString(); | |
| $check_in = date(Config('settings.time_format'), strtotime($check_in)); | |
| } | |
| else | |
| { | |
| $check_in = null; | |
| } | |
| if(isset($value->check_out)) | |
| { | |
| $check_out = Carbon::parse($value->check_out,config('app.timezone')); | |
| $check_out->setTimezone(Config('settings.time_zone')); | |
| $check_out = Carbon::parse($check_out)->toTimeString(); | |
| $check_out = date(Config('settings.time_format'), strtotime($check_out)); | |
| } | |
| else | |
| { | |
| $check_out = null; | |
| } | |
| } | |
| } | |
| } | |
| else | |
| { | |
| $utcStartTime = Carbon::parse($shift->start_time, 'UTC'); | |
| $istStartTime = $utcStartTime->setTimezone(Config('settings.time_zone')); | |
| $formattedIstStartTime = $istStartTime->format('h:i A'); | |
| // For converting end_time of shift from UTC into IST | |
| $utcEndTime = Carbon::parse($shift->end_time, 'UTC'); | |
| $istEndTime = $utcEndTime->setTimezone(Config('settings.time_zone')); | |
| $formattedIstEndTime = $istEndTime->format('h:i A'); | |
| return response()->json([ | |
| 'check_in' => $formattedIstStartTime, | |
| 'check_out' => $formattedIstEndTime, | |
| 'canRequest' => $canRequest, | |
| 'id'=> null, | |
| 'currentShift' => $currentShift, | |
| 'currentShiftId' => $currentShiftId, | |
| 'attendance_requet' => $attendance_requet, | |
| 'user_shift_name' => $user_shift_name, | |
| 'shiftTimings' => $shiftTimes, | |
| 'shiftNames' => $shiftNames | |
| ]); | |
| } | |
| return response()->json([ | |
| 'id'=> $attendanceDetailId, | |
| 'check_in' => $check_in , | |
| 'check_out' => $check_out, | |
| 'currentShift' => $currentShift, | |
| 'currentShiftId' => $currentShiftId, | |
| 'canRequest' => $canRequest, | |
| 'attendance_requet' => $attendance_requet, | |
| 'user_shift_name' => $user_shift_name, | |
| 'shiftTimings' => $shiftTimes, | |
| 'shiftNames' => $shiftNames | |
| ]); | |
| } | |
| } | |
| } | |
| } |