Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 179 |
| LeaveReportController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
2756 | |
0.00% |
0 / 179 |
| index | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 13 |
|||
| 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 |
506 | |
0.00% |
0 / 81 |
|||
| updateFromHomePage | |
0.00% |
0 / 1 |
506 | |
0.00% |
0 / 80 |
|||
| destroy | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| <?php | |
| namespace App\Http\Controllers\Leave; | |
| use App\Http\Controllers\FirebaseController\FirebaseCloudMessaging; | |
| use Illuminate\Http\Request; | |
| use App\Http\Controllers\Controller; | |
| use App\Models\Leave\ApplyLeave; | |
| use App\Models\Leave\RejectedLeave; | |
| use App\Models\Leave\LeaveEntitlement; | |
| use App\Models\Auth\User; | |
| use Illuminate\Support\Facades\Session; | |
| use Illuminate\Support\Facades\Auth; | |
| use DB; | |
| use Carbon\Carbon; | |
| use App\Notifications\AppNotifications; | |
| use App\Models\Auth\userRequestCollaborator; | |
| use App\Models\Auth\UserRequestDiscuss; | |
| use App\Helpers\LeaveHelper; | |
| use App\Helpers\GetShiftDetailHelper; | |
| use Illuminate\Support\Facades\Gate; | |
| use App\Helpers\AttendanceHelper; | |
| use App\Helpers\LeaveLogHelper; | |
| class LeaveReportController extends Controller | |
| { | |
| /** | |
| * Display a listing of the resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function index(Request $request) | |
| { | |
| $title = '/Leave/Approve Leave'; | |
| $Appliedleave = []; | |
| if (auth()->user()->can('View All leave requests')) { | |
| $Appliedleave = ApplyLeave::where('status', 'Pending')->orderBy('created_at', 'DESC')->get(); | |
| } else if (auth()->user()->can('View All leave requests of team')) { | |
| $userIds = GetShiftDetailHelper::getMyteamMembers(); | |
| $Appliedleave = ApplyLeave::where('status', 'Pending')->whereIn('user_id', $userIds)->orderBy('created_at', 'DESC')->get(); | |
| } else { | |
| //View All leave requests of assigned employees | |
| $userIds = GetShiftDetailHelper::getMyReportingEmployees(); | |
| $Appliedleave = ApplyLeave::where('status', 'Pending')->whereIn('user_id', $userIds)->orderBy('created_at', 'DESC')->get(); | |
| } | |
| //$Appliedleave = ApplyLeave::where('status', 'pending')->orderBy('created_at','DESC')->get(); | |
| // $currentYear = Carbon::now()->year; | |
| // $data = DB::table("apply_leaves") | |
| // ->join("leaves","leaves.id","=","apply_leaves.leave_id") | |
| // ->whereRaw('year(from_date) = ?',[$currentYear]) | |
| // ->orwhereRaw('year(to_date) = ?',[$currentYear]) | |
| // ->get(); | |
| // For Attendance Marking | |
| $val = AttendanceHelper::getAttendanceMarkValue(); | |
| $valTeam = AttendanceHelper::getAttendanceTeam($request); | |
| $valUser = AttendanceHelper::getAttendanceUser($request); | |
| return view('administrationForms.LeavePolicy.LeaveReportTable', compact('title', 'Appliedleave','val','valTeam','valUser')); | |
| } | |
| /** | |
| * 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) | |
| { | |
| if ($request->has('approver_comments')) { | |
| $status = $request->status_reject; | |
| $approver_comments = $request->approver_comments; | |
| } else { | |
| $approver_comments = null; | |
| // $status = $request->status; | |
| if ($request->status == 'Approve') { | |
| $status = 'Approve'; | |
| } else { | |
| return redirect()->back()->with('warning', 'Please select status and update'); | |
| } | |
| } | |
| $leaves = ApplyLeave::find($id); | |
| $leaves->is_process = 'true'; | |
| $leaves->update(['status' => $status,'updated_by_user' => Auth::user()->id]); | |
| LeaveHelper::updateLeaveBalanceOnRejecting($leaves->user_id, $status, $leaves->leave_id, $leaves->leave_length); | |
| //notification to user | |
| $user = User::find($leaves->user_id); | |
| if ($leaves->from_date == $leaves->to_date) { | |
| $leave_days = Carbon::parse($leaves->from_date)->format('jS M Y'); | |
| } else { | |
| $leave_days = Carbon::parse($leaves->from_date)->format('jS M Y') . ' to ' . Carbon::parse($leaves->to_date)->format('jS M Y'); | |
| } | |
| if ($status == 'Approve') { | |
| $leave_status = 'approved'; | |
| } else if ($status == 'Reject') { | |
| $leave_status = 'rejected'; | |
| } | |
| $title = "Leave request"; | |
| if ($request->has('approver_comments') && $status == 'Reject') { | |
| $leaveLog = new LeaveLogHelper; | |
| $leaveLog->updateLeaveLog($leaves->leave_length,"ADDED",$leaves->user_id,$leaves->leave_id,$approver_comments,Auth::user()->id); | |
| $rejected_leave = new RejectedLeave(); | |
| $rejected_leave->from_date = $leaves->from_date; | |
| $rejected_leave->to_date = $leaves->to_date; | |
| $rejected_leave->reason = $leaves->reason; | |
| $rejected_leave->is_process = 'true'; | |
| $rejected_leave->user_id = $leaves->user_id; | |
| $rejected_leave->leave_id = $leaves->leave_id; | |
| $rejected_leave->leave_length = $leaves->leave_length; | |
| $rejected_leave->leave_type = $leaves->leave_type; | |
| $rejected_leave->half_day_type = $leaves->half_day_type; | |
| $rejected_leave->approver_comments = $approver_comments; | |
| $rejected_leave->save(); | |
| $leaves->delete(); | |
| $message = 'Your leave request for ' . $leave_days . ' has been ' . $leave_status . ' due to ' . $approver_comments; | |
| $collaboratorMessage = $user->name . ' ' . $user->lastName . "'s leave request for " . $leave_days . ' has been ' . $leave_status . ' due to ' . $approver_comments . ' by ' . auth()->user()->name . ' ' . auth()->user()->lastName; | |
| } else { | |
| $message = 'Your leave request for ' . $leave_days . ' has been ' . $leave_status; | |
| $collaboratorMessage = $user->name . ' ' . $user->lastName . "'s leave request for " . $leave_days . ' has been ' . $leave_status . ' by ' . auth()->user()->name . ' ' . auth()->user()->lastName; | |
| } | |
| $actionUrl = 'apply-leave.index'; | |
| $arr = null; | |
| $arr = [$title, $message, $actionUrl]; | |
| $user->notify(new AppNotifications($arr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$user->id); | |
| //get collaborators | |
| $collaborators = userRequestCollaborator::where('module', 'leave') | |
| ->where('context_id', $id) | |
| ->pluck('user_id')->toArray(); | |
| //primary reporting manager | |
| $primaryReportingManagerId = $user->userdetail->primary_reporting_manager_id; | |
| $collArr = [$title, $collaboratorMessage, null]; | |
| if (!empty($collaborators)) { | |
| if (in_array(auth()->user()->id, $collaborators)) { | |
| foreach ($collaborators as $collaborator) { | |
| $collaboratorUser = User::where('id', $collaborator)->first(); | |
| //send notification to all collaborators | |
| if(auth()->user()->id != $collaborator){ | |
| $collaboratorUser->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$collaborator); | |
| } | |
| } | |
| }else{ | |
| foreach ($collaborators as $collaborator) { | |
| $collaboratorUser = User::where('id', $collaborator)->first(); | |
| //send notification to all collaborators | |
| $collaboratorUser->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$collaborator); | |
| } | |
| } | |
| } | |
| //check if user has primary reporting manager | |
| if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) { | |
| $primary_manger = User::where('id', $primaryReportingManagerId)->first(); | |
| if($primaryReportingManagerId != Auth::user()->id){ | |
| $primary_manger->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$primaryReportingManagerId); | |
| } | |
| } | |
| //secondary reporting managers | |
| $secondaryManagers = $user->userdetail->secondaryManager; | |
| if(!empty($secondaryManagers)){ | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primaryReportingManagerId != $secondaryManager->id && $secondaryManager->id != Auth::user()->id && !in_array($secondaryManager->id, $collaborators)) { | |
| $secondaryManager->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$secondaryManager->id); | |
| } | |
| } | |
| } | |
| if ($status == 'Approve') { | |
| Session::flash('success', 'Leave Approved Successfully!'); | |
| } else { | |
| Session::flash('warning', 'Leave Rejected Successfully!'); | |
| } | |
| //return redirect()->route('leave-report.index')->withErrors($this); | |
| return redirect()->back()->withErrors($this); | |
| } | |
| public function updateFromHomePage(Request $request, $id) | |
| { | |
| if ($request->has('approver_comments')) { | |
| $status = $request->status_reject; | |
| $approver_comments = $request->approver_comments; | |
| } else { | |
| $approver_comments = null; | |
| $status = $request->status; | |
| if ($status == '') { | |
| return response()->json(['status' => 422, 'message' => 'Please select status and update']); | |
| } | |
| } | |
| $leaves = ApplyLeave::find($id); | |
| $leaves->is_process = 'true'; | |
| $leaves->update(['status' => $status,'updated_by_user' => Auth::user()->id]); | |
| LeaveHelper::updateLeaveBalanceOnRejecting($leaves->user_id, $status, $leaves->leave_id, $leaves->leave_length); | |
| //notification to user | |
| $user = User::find($leaves->user_id); | |
| if ($leaves->from_date == $leaves->to_date) { | |
| $leave_days = Carbon::parse($leaves->from_date)->format('jS M Y'); | |
| } else { | |
| $leave_days = Carbon::parse($leaves->from_date)->format('jS M Y') . ' to ' . Carbon::parse($leaves->to_date)->format('jS M Y'); | |
| } | |
| if ($status == 'Approve') { | |
| $leave_status = 'approved'; | |
| } else if ($status == 'Reject') { | |
| $leave_status = 'rejected'; | |
| } | |
| $title = "Leave request"; | |
| if ($request->has('approver_comments') && $status == 'Reject') { | |
| $leaveLog = new LeaveLogHelper; | |
| $leaveLog->updateLeaveLog($leaves->leave_length,"ADDED",$leaves->user_id,$leaves->leave_id, $approver_comments,Auth::user()->id); | |
| $rejected_leave = new RejectedLeave(); | |
| $rejected_leave->from_date = $leaves->from_date; | |
| $rejected_leave->to_date = $leaves->to_date; | |
| $rejected_leave->reason = $leaves->reason; | |
| $rejected_leave->is_process = 'true'; | |
| $rejected_leave->user_id = $leaves->user_id; | |
| $rejected_leave->leave_id = $leaves->leave_id; | |
| $rejected_leave->leave_length = $leaves->leave_length; | |
| $rejected_leave->leave_type = $leaves->leave_type; | |
| $rejected_leave->half_day_type = $leaves->half_day_type; | |
| $rejected_leave->approver_comments = $approver_comments; | |
| $rejected_leave->save(); | |
| $leaves->delete(); | |
| $message = 'Your leave request for ' . $leave_days . ' has been ' . $leave_status . ' due to ' . $approver_comments; | |
| $collaboratorMessage = $user->name . ' ' . $user->lastName . "'s leave request for " . $leave_days . ' has been ' . $leave_status . ' due to ' . $approver_comments . ' by ' . auth()->user()->name . ' ' . auth()->user()->lastName; | |
| } else { | |
| $message = 'Your leave request for ' . $leave_days . ' has been ' . $leave_status; | |
| $collaboratorMessage = $user->name . ' ' . $user->lastName . "'s leave request for " . $leave_days . ' has been ' . $leave_status . ' by ' . auth()->user()->name . ' ' . auth()->user()->lastName; | |
| } | |
| $actionUrl = 'apply-leave.index'; | |
| $arr = null; | |
| $arr = [$title, $message, $actionUrl]; | |
| $user->notify(new AppNotifications($arr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$user->id); | |
| //get collaborators | |
| $collaborators = userRequestCollaborator::where('module', 'leave') | |
| ->where('context_id', $id) | |
| ->pluck('user_id')->toArray(); | |
| //primary reporting manager | |
| $primaryReportingManagerId = $user->userdetail->primary_reporting_manager_id; | |
| $collArr = [$title, $collaboratorMessage, null]; | |
| if (!empty($collaborators)) { | |
| if (in_array(auth()->user()->id, $collaborators)) { | |
| foreach ($collaborators as $collaborator) { | |
| $collaboratorUser = User::where('id', $collaborator)->first(); | |
| //send notification to all collaborators | |
| if(auth()->user()->id != $collaborator){ | |
| $collaboratorUser->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$collaborator); | |
| } | |
| } | |
| }else{ | |
| foreach ($collaborators as $collaborator) { | |
| $collaboratorUser = User::where('id', $collaborator)->first(); | |
| //send notification to all collaborators | |
| $collaboratorUser->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$collaborator); | |
| } | |
| } | |
| } | |
| //check if user has primary reporting manager | |
| if ($primaryReportingManagerId != 0 || $primaryReportingManagerId != NULL) { | |
| $primary_manger = User::where('id', $primaryReportingManagerId)->first(); | |
| if($primaryReportingManagerId != Auth::user()->id){ | |
| $primary_manger->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$primaryReportingManagerId); | |
| } | |
| } | |
| //secondary reporting managers | |
| $secondaryManagers = $user->userdetail->secondaryManager; | |
| if(!empty($secondaryManagers)){ | |
| foreach ($secondaryManagers as $secondaryManager) { | |
| if ($primaryReportingManagerId != $secondaryManager->id && $secondaryManager->id != Auth::user()->id && !in_array($secondaryManager->id, $collaborators)) { | |
| $secondaryManager->notify(new AppNotifications($collArr)); | |
| $firebaseObject = new FirebaseCloudMessaging(); | |
| $firebaseObject->sendFirebaseNotification($title,$message,$secondaryManager->id); | |
| } | |
| } | |
| } | |
| if ($status == 'Approve') { | |
| //Session::flash('success', 'Approve Leave Successful!'); | |
| return response()->json(['status' => 200, 'message' => 'Leave Approved Successfully!']); | |
| } else { | |
| //Session::flash('warning', 'Reject Leave Successful!'); | |
| return response()->json(['status' => 200, 'message' => 'Leave Rejected Successfully!']); | |
| } | |
| return response()->json(['status' => 200, 'message' => '']); | |
| } | |
| /** | |
| * Remove the specified resource from storage. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function destroy($id) | |
| { | |
| // | |
| } | |
| } |