public function declinedDetails($id)
 {
     $performance_approval = PerformanceApproval::where('id', $id)->first();
     $details = DB::table('approvals')->join('reports', 'reports.id', '=', 'approvals.report_id')->join('projects', 'projects.id', '=', 'reports.project_id')->join('users', 'users.id', '=', 'reports.user_id')->select('*', 'approvals.id as approval_id', 'projects.name as project', DB::raw('UPPER(LEFT(projects.name, 1)) as first_letter'), DB::raw('DATE_FORMAT(reports.date_start, "%b. %d, %Y") as date_start'), DB::raw('DATE_FORMAT(reports.date_end, "%b. %d, %Y") as date_end'), DB::raw('DATE_FORMAT(approvals.created_at, "%h:%i %p %b. %d, %Y") as created_at'))->where('approvals.id', $performance_approval->approval_id)->first();
     $details->current = DB::table('performances')->join('members', 'members.id', '=', 'performances.member_id')->join('positions', 'positions.id', '=', 'performances.position_id')->select('performances.*', 'members.full_name', 'positions.name as position')->where('performances.id', $performance_approval->performance_id)->first();
     $details->request = DB::table('performance_approvals')->join('positions', 'positions.id', '=', 'performance_approvals.position_id')->select('performance_approvals.*', 'positions.name as position')->where('performance_approvals.id', $performance_approval->id)->orderBy('performance_approvals.created_at', 'desc')->first();
     return response()->json($details);
 }
 public function performanceEdit(Request $request, $reportID)
 {
     $create_notification = false;
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $this->validate($request, [$i . '.id' => 'required|numeric', $i . '.position_id' => 'required|numeric', $i . '.department_id' => 'required|numeric', $i . '.project_id' => 'required|numeric', $i . '.output' => 'required|numeric', $i . '.target_id' => 'required', $i . '.date_start' => 'required|date', $i . '.date_end' => 'required|date', $i . '.hours_worked' => 'required|numeric', $i . '.daily_work_hours' => 'required|numeric', $i . '.output_error' => 'required|numeric', $i . '.message' => 'required']);
             if (!$create_notification) {
                 $admin = User::where('email', '*****@*****.**')->first();
                 $approval = new Approval();
                 $approval->action = 'update';
                 $approval->report_id = $reportID;
                 $approval->status = 'pending';
                 $approval->save();
                 $report = Report::where('id', $request->input($i . '.report_id'))->first();
                 $report->user_id = $request->user()->id;
                 $report->save();
                 // create a notification
                 $notification = new Notification();
                 $notification->message = 'updates ';
                 $notification->receiver_user_id = $admin->id;
                 $notification->sender_user_id = $request->user()->id;
                 $notification->subscriber = 'admin';
                 $notification->state = 'main.approvals';
                 $notification->event_id = $report->id;
                 $notification->event_id_type = 'report_id';
                 $notification->seen = false;
                 $notification->save();
                 $notify = DB::table('reports')->join('users', 'users.id', '=', 'reports.user_id')->join('projects', 'projects.id', '=', 'reports.project_id')->join('notifications', 'notifications.event_id', '=', 'reports.id')->select('reports.*', 'users.*', DB::raw('LEFT(users.first_name, 1) as first_letter'), 'projects.*', 'notifications.*')->where('notifications.id', $notification->id)->first();
                 event(new ReportSubmittedBroadCast($notify));
                 // report
                 $create_notification = true;
             }
             $target = Target::withTrashed()->where('id', $request->input($i . '.target_id'))->first();
             $performance_approval = new PerformanceApproval();
             $performance_approval->approval_id = $approval->id;
             $performance_approval->report_id = $reportID;
             $performance_approval->message = $request->input($i . '.message');
             // $performance_approval->result_id = $request->input($i.'.result_id');
             $performance_approval->performance_id = $request->input($i . '.id');
             $performance_approval->member_id = $request->input($i . '.member_id');
             $performance_approval->position_id = $request->input($i . '.position_id');
             $performance_approval->department_id = $request->input($i . '.department_id');
             $performance_approval->project_id = $request->input($i . '.project_id');
             $performance_approval->target_id = $target->id;
             $performance_approval->date_start = Carbon::parse($request->input($i . '.date_start'));
             $performance_approval->date_end = Carbon::parse($request->input($i . '.date_end'));
             $performance_approval->daily_work_hours = $request->input($i . '.daily_work_hours');
             $performance_approval->output = $request->input($i . '.output');
             $performance_approval->hours_worked = $request->input($i . '.hours_worked');
             $performance_approval->output_error = $request->input($i . '.output_error');
             // Round((Output / Hours Worked) * Daily Work Hours)
             // store the rounded value
             $performance_approval->average_output = round($request->input($i . '.output') / $request->input($i . '.hours_worked') * $request->input($i . '.daily_work_hours'), 2);
             // average output / target output * 100 to convert to percentage
             $performance_approval->productivity = round($performance_approval->average_output / $target->productivity * 100, 1);
             // 1 - output w/error / output * 100 to convert to percentage
             $performance_approval->quality = round((1 - $performance_approval->output_error / $performance_approval->output) * 100, 1);
             // Quadrant
             if ($performance_approval->productivity < 100 && $performance_approval->quality >= $target->quality) {
                 $performance_approval->quadrant = 'Quadrant 1';
             } else {
                 if ($performance_approval->productivity >= 100 && $performance_approval->quality >= $target->quality) {
                     $performance_approval->quadrant = 'Quadrant 2';
                 } else {
                     if ($performance_approval->productivity >= 100 && $performance_approval->quality < $target->quality) {
                         $performance_approval->quadrant = 'Quadrant 3';
                     } else {
                         if ($performance_approval->productivity < 100 && $performance_approval->quality < $target->quality) {
                             $performance_approval->quadrant = 'Quadrant 4';
                         }
                     }
                 }
             }
             // save performance_approval request to database
             $performance_approval->save();
         }
     }
 }