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 approve(Request $request)
 {
     $create_notification = false;
     $pending_count = count($request->all());
     for ($i = 0; $i < count($request->all()); $i++) {
         if ($request->input($i . '.include')) {
             $pending_count--;
             $this->validate($request, [$i . '.approval_id' => 'required|numeric', $i . '.performance_approval_id' => 'required|numeric', $i . '.performance_id' => 'required|numeric']);
             if (!$create_notification) {
                 $admin = User::where('email', '*****@*****.**')->first();
                 $report = Report::where('id', $request->input($i . '.report_id'))->first();
                 $notification = new Notification();
                 $notification->receiver_user_id = $report->user_id;
                 $notification->sender_user_id = $admin->id;
                 $notification->subscriber = 'team-leader';
                 $notification->message = 'approved changes on';
                 $notification->state = 'main.approvals';
                 // $notification->event_id = $request->input($i.'.approval_id');
                 // $notification->event_id_type = 'approval_id';
                 $notification->event_id = $report->id;
                 $notification->event_id_type = 'report_id';
                 $notification->seen = false;
                 $notification->save();
                 $notify = DB::table('notifications')->join('approvals', 'approvals.report_id', '=', 'notifications.event_id')->join('reports', 'reports.id', '=', 'approvals.report_id')->join('projects', 'projects.id', '=', 'reports.project_id')->join('users', 'users.id', '=', 'notifications.sender_user_id')->select('*', DB::raw('LEFT(users.first_name, 1) as first_letter'))->where('notifications.id', $notification->id)->first();
                 event(new ApprovalNotificationBroadCast($notify));
                 // report
                 $create_notification = true;
             }
             $old_performance = Performance::where('id', $request->input($i . '.performance_id'))->first();
             // record history of the performance
             $performance_history = new PerformanceHistory();
             $performance_history->performance_id = $old_performance->id;
             $performance_history->report_id = $old_performance->report_id;
             $performance_history->member_id = $old_performance->member_id;
             $performance_history->position_id = $old_performance->position_id;
             $performance_history->department_id = $old_performance->department_id;
             $performance_history->project_id = $old_performance->project_id;
             $performance_history->target_id = $old_performance->target_id;
             $performance_history->date_start = $old_performance->date_start;
             $performance_history->date_end = $old_performance->date_end;
             $performance_history->daily_work_hours = $old_performance->daily_work_hours;
             $performance_history->output = $old_performance->output;
             $performance_history->hours_worked = $old_performance->hours_worked;
             $performance_history->output_error = $old_performance->output_error;
             $performance_history->average_output = $old_performance->average_output;
             $performance_history->productivity = $old_performance->productivity;
             $performance_history->quality = $old_performance->quality;
             $performance_history->quadrant = $old_performance->quadrant;
             $performance_history->save();
             $this->performance_approval_approved = PerformanceApproval::where('id', $request->input($i . '.performance_approval_id'))->first();
             $this->performance_approval_approved->status = 'approved';
             $this->performance_approval_approved->save();
             // fetch the changes
             // $performance_approval = DB::table('performance_approvals')
             //     ->join('members', 'members.id', '=', 'performance_approvals.member_id')
             //     ->where('performance_approvals.id', $request->input($i.'.performance_approval_id'))
             //     ->first();
             $performance_approval = PerformanceApproval::with(['member' => function ($query) {
                 $query->with(['experiences' => function ($query) {
                     $query->where('project_id', $this->performance_approval_approved->project_id);
                 }]);
             }])->where('id', $request->input($i . '.performance_approval_id'))->first();
             $performance = Performance::where('id', $request->input($i . '.performance_id'))->first();
             // apply changes
             $performance->position_id = $performance_approval->position_id;
             $performance->hours_worked = $performance_approval->hours_worked;
             $performance->output = $performance_approval->output;
             $performance->output_error = $performance_approval->output_error;
             // Round((Output / Hours Worked) * Daily Work Hours)
             $performance->average_output = round($performance_approval->output / $performance_approval->hours_worked * $performance_approval->daily_work_hours, 2);
             // fetch target
             $target = Target::withTrashed()->where('id', $performance_approval->target_id)->first();
             // recompute results
             // $result = Result::where('id', $performance_approval->result_id)->first();
             // average output / target output * 100 to convert to percentage
             $performance->productivity = round($performance_approval->average_output / $target->productivity * 100, 1);
             // 1 - output w/error / output * 100 to convert to percentage
             $performance->quality = round((1 - $performance_approval->output_error / $performance_approval->output) * 100, 1);
             $performance->save();
         }
     }
     if (!$pending_count) {
         $approval = Approval::where('id', $request->input('0.approval_id'))->first();
         $approval->status = 'done';
         $approval->save();
     }
 }